Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//---------------------------------------------------------------------------
#pragma hdrstop
#include <tchar.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
//---------------------------------------------------------------------------
typedef short unsigned int SUINT;
#pragma argsused
#define HEIGHT 15
#define WIDTH 20
#define MAX_FUEL 40
using namespace std;
bool InGame = true;
enum ERROR { OK, OK_EXIT, BAD, FATAL };
ERROR err = OK;
SUINT current_fuel = MAX_FUEL;
//prototypes
inline void DrawMap(char map[HEIGHT][WIDTH]);
ERROR Movement(int&, int&, char map[HEIGHT][WIDTH]);
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_CTYPE, "Russian");
int x = 0, y = 0;
char map[HEIGHT][WIDTH];
//map initialize
for (short int i = 0; i < HEIGHT; i++) {
for (short int j = 0; j < WIDTH; j++) {
map[i][j] = ' ';
}
}
map [0][0] = 'T';
map [7][8] = 'C';
while (!err || err == BAD){
//map out
system("cls");
DrawMap(map);
cout << "x = " << x << " ;; y = " << y << " ;; fuel = " << current_fuel;
err = Movement(x, y, map);
//counter++;
}
system("pause");
return 0;
}
//---------------------------------------------------------------------------
inline void DrawMap(char map[HEIGHT][WIDTH]){
for (short int i = 0; i < HEIGHT; i++) {
for (short int j = 0; j < WIDTH; j++) {
cout << map[i][j];
}
cout << "\n";
}
map[7][8] = 'C';
}
ERROR Movement(int &x, int &y, char map[HEIGHT][WIDTH]){
char act;
int ch;
static int counter = 0;
act = _getch();
ch = static_cast<int>(act);
switch (ch) {
case 27 :
{
return OK_EXIT;
};
case 0 : return OK;
case 72 : //up
{
if (y > 0) {
map[y][x] = ' ';
y--;
current_fuel--;
if (map[y][x] == 'C') {
map[y][x] = 'T';
current_fuel = MAX_FUEL;
}
}
return OK;
};
case 75 : //left
{
if (x > 0) {
map[y][x] = ' ';
x--;
current_fuel--;
map[y][x] = 'T';
}
return OK;
} ;
case 77 : //right
{
if (x < WIDTH-1) {
map[y][x] = ' ';
x++;
current_fuel--;
map[y][x] = 'T';
}
return OK;
} ;
case 80 : //down
{
if (y < HEIGHT-1){
map[y][x] = ' ';
y++;
current_fuel--;
map[y][x] = 'T';
}
return OK;
} ;
default: return BAD;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment