i need the 'O'(AKA player) to move based on user input, but nothing happens! i also need to add a function that records the time from the beginning until the player reaches the finish line, in order to calculate their score.
#include <iostream>
#include <cstdlib> //rand + srand
#include <ctime> //time
#include <cmath> //math problems
#include <conio.h> //keys
#include <string>
using namespace std;
const char HEIGHT = 7, WIDTH = 7;
unsigned char maze[WIDTH][HEIGHT] = {
{'#','#','#','#','#','#','#'},
{'#',' ','#','#','#','#','#'},
{'#',' ',' ','#',' ',' ','#'},
{'#','#',' ',' ',' ',' ','#'},
{'#',' ','#',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ','#'},
{'#','#','#','#','#','#','#'},
};
struct barrier { int x;
int y;
};
void riddle(int x, int y) { srand(time(0)); int res = rand() % 3 + 1; int ans;
switch(res) { case 1 :
cout << "what's (13*3) ?\n";
cin >> ans;
if(ans == 39) maze[x][y] = ' '; break;
case 2 :
cout << "what's 2^7?\n";
cin >> ans;
if(ans == 128) maze[x][y] = ' '; break;
case 3 :
cout << "what's 2*24?\n";
cin >> ans;
if(ans == 48) maze[x][y] = ' '; break;
}
}
bool on = true;
const int FRAME_RATE = (int) 1000 / 30;
void slp(unsigned int t) { clock_t GoalTime = clock() + t; while(clock() < GoalTime);
}
void printer() { for(int x = 0; x < WIDTH; x++) {
cout << endl;
for(int y = 0; y < HEIGHT; y++) {
cout << maze[x][y];
}
}
}
void game() {
char move; int Xplayer = 1, Yplayer = 1; int Xfinish = 5, Yfinish = 5;
barrier b1, b2;
b1.x = 2; b1.y = 2;
b2.x = 3; b2.y = 4;
char player = 'O'; char bar1 = 'X', bar2 = 'X';
char finish = '*';
while(on) {
maze[Xplayer][Yplayer] = player; maze[Xfinish][Yfinish] = finish; maze[b1.x][b1.y] = bar1; maze[b2.x][b2.y] = bar2;
printer(); cout << endl;
cout << "move : " << endl;
for(; ;) {
move = getchar(); switch(move) { case 'u' : if(maze[Xplayer][Yplayer-1] != '#' && maze[Xplayer][Yplayer-1] == ' ') {
maze[Xplayer][Yplayer] = ' ';
Yplayer--;
}
break;
case 'd' : if(maze[Xplayer][Yplayer+1] != '#' && maze[Xplayer][Yplayer+1] == ' ') {
maze[Xplayer][Yplayer] = ' ';
Yplayer++;
}
break;
case 'l' : if(maze[Xplayer-1][Yplayer] != '#' && maze[Xplayer-1][Yplayer] == ' ') {
maze[Xplayer][Yplayer] = ' ';
Xplayer--;
}
break;
case 'r' : if(maze[Xplayer+1][Yplayer] != '#' && maze[Xplayer+1][Yplayer] == ' ') {
maze[Xplayer][Yplayer] = ' ';
Xplayer++;
}
break;
case 'q' : exit(0);
break;
}
if(Xplayer == b1.x && Yplayer == b1.y) { cout << "barrier! solve riddle to continue!...\n"; riddle(Xplayer, Yplayer); }
else if(Xplayer == b2.x && Yplayer == b2.y) { cout << "barrier! solve riddle to continue!...\n";
riddle(Xplayer, Yplayer); }
if(Xplayer == 5 && Yplayer == 5) { cout << "success! your score is : " << "press q to quit\n";
}
}
slp(FRAME_RATE); system("CLS"); }
}
int main() {
game();
return 0;
}
What I have tried:
i changed the input function from cin to getch and getchar but i guess that's not the problem. i tried changing the while and for loops as well, didn't work. as for the time recorder, i have no idea!