Here is my latest C++ masterpiece after the jump. Move your guy (U) to get the treasure (T) and avoid the monsters (Q and M).
This is some seriously hi-tech shizzle.
(Oh and I stole the clear screen function from the interweb ^_^. I couldn’t come up with that on my own!)
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
#define HEIGHT 8
#define WIDTH 8
struct coord {
int x;
int y;
};
void clear_screen ( void )
{
DWORD n; /* Number of characters written */
DWORD size; /* number of visible characters */
COORD coord = {0}; /* Top left screen position */
CONSOLE_SCREEN_BUFFER_INFO csbi;
/* Get a handle to the console */
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo ( h, &csbi );
/* Find the number of characters to overwrite */
size = csbi.dwSize.X * csbi.dwSize.Y;
/* Overwrite the screen buffer with whitespace */
FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
GetConsoleScreenBufferInfo ( h, &csbi );
FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
/* Reset the cursor to the top left position */
SetConsoleCursorPosition ( h, coord );
}
void initialize_grid(char grid[][HEIGHT], coord u, coord t, coord q, coord m) {
for (int H=0; H
grid[u.x][u.y]='U';
grid[t.x][t.y]='T';
grid[q.x][q.y]='Q';
grid[m.x][m.y]='M';
}
void print_grid(char grid[][HEIGHT]) {
for (int H=0; H
for (int W=0; W
cout << endl;
}
}
void check_input (char &input) {
switch (input) {
case 'd':
input='D';
break;
case 'u':
input='U';
break;
case 'l':
input='L';
break;
case 'r':
input='R';
break;
case 'q':
input='Q';
break;
case 'D':
case 'U':
case 'L':
case 'R':
case 'Q':
break;
default:
cout << "Invalid input. Try again. " <
cout << "Do loop input: ";
cin >> input;
check_input(input);
break;
}
}
void move_q(char grid[][HEIGHT], coord& q){
int random_integer1(0);
int random_integer2(0);
int lowest=-2, highest=2;
int range=(highest-lowest)+1;
if (grid[q.x][q.y]=='M')
grid[q.x][q.y]='M';
else if (grid[q.x][q.y]=='U')
grid[q.x][q.y]='U';
else
grid[q.x][q.y]='-';
random_integer1 = lowest+int(range*rand()/(RAND_MAX + 1.0));
q.x=q.x+random_integer1;
random_integer2 = lowest+int(range*rand()/(RAND_MAX + 1.0));
q.y=q.y+random_integer2;
if ((q.x<=-1) || (q.x>=WIDTH) || (q.y<=-1) || (q.y>=HEIGHT))
{
q.x=q.x-random_integer1;
q.y=q.y-random_integer2;
grid[q.x][q.y]='Q';}
else
grid[q.x][q.y]='Q';
}
void move_m(char grid[][HEIGHT], coord& m){
int random_integer3(0);
int random_integer4(0);
int lowest=-2, highest=2;
int range=(highest-lowest)+1;
if (grid[m.x][m.y]=='Q')
grid[m.x][m.y]='Q';
else if (grid[m.x][m.y]=='U')
grid[m.x][m.y]='U';
else
grid[m.x][m.y]='-';
random_integer3 = lowest+int(range*rand()/(RAND_MAX + 1.0));
m.x=m.x+random_integer3;
random_integer4 = lowest+int(range*rand()/(RAND_MAX + 1.0));
m.y=m.y+random_integer4;
if ((m.x<=-1) || (m.x>=WIDTH) || (m.y<=-1) || (m.y>=HEIGHT))
{
m.x=m.x-random_integer3;
m.y=m.y-random_integer4;
grid[m.x][m.y]='M';}
else
grid[m.x][m.y]='M';
}
void move_right(char grid[][HEIGHT], coord& u)
{
if (u.x != WIDTH-1)
{
if (grid[u.x][u.y]=='Q')
grid[u.x][u.y]='Q';
else if (grid[u.x][u.y]=='M')
grid[u.x][u.y]='M';
else
grid[u.x][u.y]='-';
grid[u.x+1][u.y]='U';
u.x++;
}
}
void move_left(char grid[][HEIGHT], coord& u)
{
if (u.x != 0)
{
grid[u.x][u.y]='-';
grid[u.x-1][u.y]='U';
u.x--;
}
}
void move_up(char grid[][HEIGHT], coord& u)
{
if (u.y != 0)
{
grid[u.x][u.y]='-';
grid[u.x][u.y-1]='U';
u.y--;
}
}
void move_down (char grid[][HEIGHT], coord& u)
{
if (u.y != HEIGHT-1)
{
grid[u.x][u.y]='-';
grid[u.x][u.y+1]='U';
u.y++;
}
}
void check_status(char grid[][HEIGHT], coord u, coord t, coord q, coord m, char& input){
if ((u.x==q.x)&&(u.y==q.y))
{
clear_screen();
print_grid(grid);
cout << "The Q monster ate you. YOU LOSE!!"<
else if ((u.x==t.x)&&(u.y==t.y))
{
clear_screen();
print_grid(grid);
cout << "You got the treasure. YOU WIN!!"<
else if ((m.x==u.x)&&(m.y==u.y))
{
clear_screen();
print_grid(grid);
cout << "The M monster at you. YOU LOSE!!"<
}
}
void your_move(char grid[][HEIGHT], coord u, coord t, coord q, coord m)
{
char input='m';
clear_screen();
print_grid(grid);
if (input != 'Q')
do
{
cout << "Press U, D, L, or R (Up, Down, Left, or Right) or Q to quit: ";
cin >> input;
check_input(input);
switch (input){
case 'R':
move_right(grid, u);
break;
case 'U':
move_up(grid, u);
break;
case 'L':
move_left(grid, u);
break;
case 'D':
move_down(grid, u);
break;
}
move_q(grid, q);
move_m(grid,m);
clear_screen();
print_grid(grid);
check_status(grid,u, t, q, m, input);
} while (input != 'Q');
}
int main (){
coord U={0,0};
coord T={WIDTH-1, HEIGHT-1};
coord Q={WIDTH/2, HEIGHT/2};
coord M={WIDTH/2+1, HEIGHT/2+1};
char grid[WIDTH][HEIGHT];
initialize_grid(grid, U, T, Q, M);
print_grid(grid);
your_move(grid, U, T, Q, M);
return (0);
}
sneaky








How does one play it?