Click here to Skip to main content
16,021,417 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.
C++
#include <iostream>
#include <cstdlib> //rand + srand
#include <ctime>   //time
#include <cmath>   //math problems
#include <conio.h>  //keys
#include <string>
using namespace std;

//5 by 5 maze
const char HEIGHT = 7, WIDTH = 7;
unsigned char maze[WIDTH][HEIGHT] = {
{'#','#','#','#','#','#','#'},
{'#',' ','#','#','#','#','#'},
{'#',' ',' ','#',' ',' ','#'},
{'#','#',' ',' ',' ',' ','#'},
{'#',' ','#',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ','#'},
{'#','#','#','#','#','#','#'},
};

struct barrier {    //use for barriers later
    int x;
    int y;
}; 

void riddle(int x, int y) { //function to generate riddles when met with barriers
    srand(time(0)); //seed srand with the current time
    int res = rand() % 3 + 1; //generate random number from 1-3
    int ans;    //recieve answer from player

    switch(res) {   //basic math problems!
        case 1 :
            cout << "what's (13*3) ?\n";
            cin >> ans;
            if(ans == 39)   //for correct answer
                maze[x][y] = ' ';   //clear the way
            break;

        case 2 : 
            cout << "what's 2^7?\n";
            cin >> ans;
            if(ans == 128)  //for correct answer
                maze[x][y] = ' ';   //clear the way
            break;

        case 3 : 
            cout << "what's 2*24?\n";
            cin >> ans;
            if(ans == 48)   //for correct answer
                maze[x][y] = ' ';   //clear the way
            break;
    }
}

bool on = true; //for the while loop

const int FRAME_RATE = (int) 1000 / 30; //frame rate of 30 fps

void slp(unsigned int t) { //wait for 't' milliseconds
    clock_t GoalTime = clock() + t; //current time plus t
    while(clock() < GoalTime);
}

void printer() { //print the maze
    for(int x = 0; x < WIDTH; x++) {
        cout << endl;
        for(int y = 0; y < HEIGHT; y++) {
            cout << maze[x][y];
        }
    }
}

void game() {
    //important variables :
    char move; //player choice of move
    int Xplayer = 1, Yplayer = 1; //player start position
    int Xfinish = 5, Yfinish = 5; //finish line position
    
    barrier b1, b2;
    b1.x = 2; //first barrier position
    b1.y = 2; 
    b2.x = 3; //second barrier position
    b2.y = 4;

    char player = 'O'; //character to represent the player
    char bar1 = 'X', //character rep. of barriers
         bar2 = 'X';
    char finish = '*'; //character rep. of finish line

    //loop for the game to  run
    while(on) {
        maze[Xplayer][Yplayer] = player;    //player placed in respective coordinates
        maze[Xfinish][Yfinish] = finish;    //finish line placed in respective coordinates
        maze[b1.x][b1.y] = bar1;    //first barrier placed in coordinates
        maze[b2.x][b2.y] = bar2;    //second barrier in coordinates

        printer();  //print maze
        cout << endl;
        cout << "move : " << endl;

        for(; ;) {
                move = getchar(); //get movement choice as input 
                switch(move) {  //different scenarios of moves chosen by player
                    case 'u' :  //player wants to go up
                        if(maze[Xplayer][Yplayer-1] != '#' && maze[Xplayer][Yplayer-1] == ' ') {
                            maze[Xplayer][Yplayer] = ' ';
                            Yplayer--;
                        }
                        break;

                    case 'd' :  //player wants to go down
                        if(maze[Xplayer][Yplayer+1] != '#' && maze[Xplayer][Yplayer+1] == ' ') {
                            maze[Xplayer][Yplayer] = ' ';
                            Yplayer++;
                        }
                        break;

                    case 'l' :  //player wants to go left
                        if(maze[Xplayer-1][Yplayer] != '#' && maze[Xplayer-1][Yplayer] == ' ') {
                            maze[Xplayer][Yplayer] = ' ';
                            Xplayer--;
                        }
                        break;

                    case 'r' :  //player wants to go right
                        if(maze[Xplayer+1][Yplayer] != '#' && maze[Xplayer+1][Yplayer] == ' ') {
                            maze[Xplayer][Yplayer] = ' ';
                            Xplayer++;
                        }
                        break;

                    case 'q' : //player chooses to quit
                        exit(0);
                        break;
                }

            if(Xplayer == b1.x && Yplayer == b1.y) {    //player meeting the first barrier 
                cout << "barrier! solve riddle to continue!...\n"; //message
                riddle(Xplayer, Yplayer);   //call function for current position [2,2]
            }

            else if(Xplayer == b2.x && Yplayer == b2.y) { //player meeting with the second barrier
                cout << "barrier! solve riddle to continue!...\n";
                riddle(Xplayer, Yplayer); //call function for current position [3,4]
            }

            if(Xplayer == 5 && Yplayer == 5) {  //player reaches finish line
                cout << "success! your score is : " << //"insert score"
                "press q to quit\n";
            }
        }

        slp(FRAME_RATE);    //stop 30 ms
        system("CLS");  //clear screen
    }
}

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!
Posted
Comments
RedDk 3-Jun-24 14:52pm    
Go online and search for "conio.h" ... see what anyone says in the returns. Also a suggestion: try to capitalize first person pronouns (always) and don't forget to start your sentences with words that are similarly capitalized.

You're guessing, not debugging. You have to set a breakpoint on the first line of your for( ; ; ) loop and step through the code in the debugger, line by line, watching the content of the variables and what's supposed to happen given their values. THIS IS THE MOST IMPORTANT TECHNIQUE YOU CAN LEARN when writing code.

The debugger is there to debug YOU and your understanding of how the code works.

I think you'll find you're missing at least one call to a certain function in your game loop. See if you can figure out what that call is.
 
Share this answer
 
Comments
merano99 3-Jun-24 21:27pm    
+5
Of course, Dave's advice to use the debugger to find errors is very useful.

Even without the debugger, however, it is already noticeable that the x and y coordinates are swapped, which of course makes the code run strangely.
C++
// if (maze[Xplayer][Yplayer + 1] != '#') { ... }
if (maze[Yplayer + 1][Xplayer] != '#') { ... }

If you then look in detail at what should happen, for example, if the player is to move downwards after d has been entered, the question arises as to why the same field is checked to see whether it is empty and also whether it is not an edge. It should be enough to check whether the field is walkable (empty). I would restore the old content in case the player is moved instead of always drawing an empty field.
C++
case 'd': //player wants to go down
    // if (maze[Yplayer + 1][Xplayer] != '#')
    if (maze[Yplayer + 1][Xplayer] == ' ') {
        maze[Yplayer][Xplayer] = savepos;
        Yplayer++;
        savepos = maze[Yplayer][Xplayer];
    }
  break;

As suggested by Dave, it should now be easy to find other errors that prevent the game from not behaving as it should.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900