Sign in to follow this  
metal_kid43

Maze type game C++

Recommended Posts

Hello one and everyone, im making this maze game with c++ where the user inputs a row and column and the program places an X in the location of the matrix corresponding to the input. My problem is this, i've been working on it for like an hour now and i cant figure out how to force the player to place an X the first row and first column. Help would be appreciated. Heres da source. #include <iostream.h> #include <stdlib.h> #include <conio.h> char matrix[20][20]; void map(void); int main() { int i,j; cout<<"Your starting position is X."<<endl; cout<<"Navigate to the @ symbol, one square at a time!"<<endl; while('X' != matrix[19][19]) { int totalmoves = 0; map(); while(totalmoves=0) { cout<<"Row : "; cin>> i; if(i != 1) //forces the user to input 1 as the starting row cout<<"Start at 1,1!"<<endl; else cout<<"Column : "; cin>> j; if(j != 1) //forces the user to input 1 as the starting column cout<<"Start at 1,1!"<<endl; else matrix[i-1][j-1]='X'; totalmoves++; } while(totalmoves>0) { cout<<"Row : "; cin>> i; cout<<"Column : "; cin>> j; matrix[i-1][j-1]='X'; } } if('X' == matrix[19][19]) { cout<<"All done."; } } void map(void) { cout<<" 1 2 3 4 5 6 7 8 9 1011121314151617181920"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 1 |"<<matrix[0][0]<<"|"<<"*"<<"|"<<matrix[0][2]<<"|"<<matrix[0][3]<<"|"<<matrix[0][4]<<"|"<<matrix[0][5]<<"|"<<matrix[0][6]<<"|"<<matrix[0][7]<<"|"<<matrix[0][8]<<"|"<<matrix[0][9]<<"|"<<matrix[0][10]<<"|"<<matrix[0][11]<<"|"<<matrix[0][12]<<"|"<<matrix[0][13]<<"|"<<matrix[0][14]<<"|"<<matrix[0][15]<<"|"<<matrix[0][16]<<"|"<<matrix[0][17]<<"|"<<matrix[0][18]<<"|"<<matrix[0][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 2 |"<<matrix[1][0]<<"|"<<"*"<<"|"<<matrix[1][2]<<"|"<<matrix[1][3]<<"|"<<matrix[1][4]<<"|"<<matrix[1][5]<<"|"<<matrix[1][6]<<"|"<<matrix[1][7]<<"|"<<matrix[1][8]<<"|"<<matrix[1][9]<<"|"<<matrix[1][10]<<"|"<<matrix[1][11]<<"|"<<matrix[1][12]<<"|"<<matrix[1][13]<<"|"<<matrix[1][14]<<"|"<<matrix[1][15]<<"|"<<matrix[1][16]<<"|"<<matrix[1][17]<<"|"<<matrix[1][18]<<"|"<<matrix[1][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 3 |"<<matrix[2][0]<<"|"<<"*"<<"|"<<matrix[2][2]<<"|"<<matrix[2][3]<<"|"<<matrix[2][4]<<"|"<<matrix[2][5]<<"|"<<matrix[2][6]<<"|"<<matrix[2][7]<<"|"<<matrix[2][8]<<"|"<<matrix[2][9]<<"|"<<matrix[2][10]<<"|"<<matrix[2][11]<<"|"<<matrix[2][12]<<"|"<<matrix[2][13]<<"|"<<matrix[2][14]<<"|"<<matrix[2][15]<<"|"<<matrix[2][16]<<"|"<<matrix[2][17]<<"|"<<matrix[2][18]<<"|"<<matrix[2][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 4 |"<<matrix[3][0]<<"|"<<"*"<<"|"<<matrix[3][2]<<"|"<<matrix[3][3]<<"|"<<matrix[3][4]<<"|"<<matrix[3][5]<<"|"<<matrix[3][6]<<"|"<<matrix[3][7]<<"|"<<matrix[3][8]<<"|"<<matrix[3][9]<<"|"<<matrix[3][10]<<"|"<<matrix[3][11]<<"|"<<matrix[3][12]<<"|"<<matrix[3][13]<<"|"<<matrix[3][14]<<"|"<<matrix[3][15]<<"|"<<matrix[3][16]<<"|"<<matrix[3][17]<<"|"<<matrix[3][18]<<"|"<<matrix[3][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 5 |"<<matrix[4][0]<<"|"<<"*"<<"|"<<matrix[4][2]<<"|"<<matrix[4][3]<<"|"<<matrix[4][4]<<"|"<<matrix[4][5]<<"|"<<matrix[4][6]<<"|"<<matrix[4][7]<<"|"<<matrix[4][8]<<"|"<<matrix[4][9]<<"|"<<matrix[4][10]<<"|"<<matrix[4][11]<<"|"<<matrix[4][12]<<"|"<<matrix[4][13]<<"|"<<matrix[4][14]<<"|"<<matrix[4][15]<<"|"<<matrix[4][16]<<"|"<<matrix[4][17]<<"|"<<matrix[4][18]<<"|"<<matrix[4][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 6 |"<<matrix[5][0]<<"|"<<"*"<<"|"<<matrix[5][2]<<"|"<<matrix[5][3]<<"|"<<matrix[5][4]<<"|"<<matrix[5][5]<<"|"<<matrix[5][6]<<"|"<<matrix[5][7]<<"|"<<matrix[5][8]<<"|"<<matrix[5][9]<<"|"<<matrix[5][10]<<"|"<<matrix[5][11]<<"|"<<matrix[5][12]<<"|"<<matrix[5][13]<<"|"<<matrix[5][14]<<"|"<<matrix[5][15]<<"|"<<matrix[5][16]<<"|"<<matrix[5][17]<<"|"<<matrix[5][18]<<"|"<<matrix[5][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 7 |"<<matrix[6][0]<<"|"<<"*"<<"|"<<matrix[6][2]<<"|"<<matrix[6][3]<<"|"<<matrix[6][4]<<"|"<<matrix[6][5]<<"|"<<matrix[6][6]<<"|"<<matrix[6][7]<<"|"<<matrix[6][8]<<"|"<<matrix[6][9]<<"|"<<matrix[6][10]<<"|"<<matrix[6][11]<<"|"<<matrix[6][12]<<"|"<<matrix[6][13]<<"|"<<matrix[6][14]<<"|"<<matrix[6][15]<<"|"<<matrix[6][16]<<"|"<<matrix[6][17]<<"|"<<matrix[6][18]<<"|"<<matrix[6][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 8 |"<<matrix[7][0]<<"|"<<"*"<<"|"<<matrix[7][2]<<"|"<<matrix[7][3]<<"|"<<matrix[7][4]<<"|"<<matrix[7][5]<<"|"<<matrix[7][6]<<"|"<<matrix[7][7]<<"|"<<matrix[7][8]<<"|"<<matrix[7][9]<<"|"<<matrix[7][10]<<"|"<<matrix[7][11]<<"|"<<matrix[7][12]<<"|"<<matrix[7][13]<<"|"<<matrix[7][14]<<"|"<<matrix[7][15]<<"|"<<matrix[7][16]<<"|"<<matrix[7][17]<<"|"<<matrix[7][18]<<"|"<<matrix[7][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 9 |"<<matrix[8][0]<<"|"<<"*"<<"|"<<matrix[8][2]<<"|"<<matrix[8][3]<<"|"<<matrix[8][4]<<"|"<<matrix[8][5]<<"|"<<matrix[8][6]<<"|"<<matrix[8][7]<<"|"<<matrix[8][8]<<"|"<<matrix[8][9]<<"|"<<matrix[8][10]<<"|"<<matrix[8][11]<<"|"<<matrix[8][12]<<"|"<<matrix[8][13]<<"|"<<matrix[8][14]<<"|"<<matrix[8][15]<<"|"<<matrix[8][16]<<"|"<<matrix[8][17]<<"|"<<matrix[8][18]<<"|"<<matrix[8][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 10|"<<matrix[9][0]<<"|"<<"*"<<"|"<<matrix[9][2]<<"|"<<matrix[9][3]<<"|"<<matrix[9][4]<<"|"<<matrix[9][5]<<"|"<<matrix[9][6]<<"|"<<matrix[9][7]<<"|"<<matrix[9][8]<<"|"<<matrix[9][9]<<"|"<<matrix[9][10]<<"|"<<matrix[9][11]<<"|"<<matrix[9][12]<<"|"<<matrix[9][13]<<"|"<<matrix[9][14]<<"|"<<matrix[9][15]<<"|"<<matrix[9][16]<<"|"<<matrix[9][17]<<"|"<<matrix[9][18]<<"|"<<matrix[9][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 11|"<<matrix[10][0]<<"|"<<"*"<<"|"<<matrix[10][2]<<"|"<<matrix[10][3]<<"|"<<matrix[10][4]<<"|"<<matrix[10][5]<<"|"<<matrix[10][6]<<"|"<<matrix[10][7]<<"|"<<matrix[10][8]<<"|"<<matrix[10][9]<<"|"<<matrix[10][10]<<"|"<<matrix[10][11]<<"|"<<matrix[10][12]<<"|"<<matrix[10][13]<<"|"<<matrix[10][14]<<"|"<<matrix[10][15]<<"|"<<matrix[10][16]<<"|"<<matrix[10][17]<<"|"<<matrix[10][18]<<"|"<<matrix[10][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 12|"<<matrix[11][0]<<"|"<<"*"<<"|"<<matrix[11][2]<<"|"<<matrix[11][3]<<"|"<<matrix[11][4]<<"|"<<matrix[11][5]<<"|"<<matrix[11][6]<<"|"<<matrix[11][7]<<"|"<<matrix[11][8]<<"|"<<matrix[11][9]<<"|"<<matrix[11][10]<<"|"<<matrix[11][11]<<"|"<<matrix[11][12]<<"|"<<matrix[11][13]<<"|"<<matrix[11][14]<<"|"<<matrix[11][15]<<"|"<<matrix[11][16]<<"|"<<matrix[11][17]<<"|"<<matrix[11][18]<<"|"<<matrix[11][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 13|"<<matrix[12][0]<<"|"<<"*"<<"|"<<matrix[12][2]<<"|"<<matrix[12][3]<<"|"<<matrix[12][4]<<"|"<<matrix[12][5]<<"|"<<matrix[12][6]<<"|"<<matrix[12][7]<<"|"<<matrix[12][8]<<"|"<<matrix[12][9]<<"|"<<matrix[12][10]<<"|"<<matrix[12][11]<<"|"<<matrix[12][12]<<"|"<<matrix[12][13]<<"|"<<matrix[12][14]<<"|"<<matrix[12][15]<<"|"<<matrix[12][16]<<"|"<<matrix[12][17]<<"|"<<matrix[12][18]<<"|"<<matrix[12][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 14|"<<matrix[13][0]<<"|"<<"*"<<"|"<<matrix[13][2]<<"|"<<matrix[13][3]<<"|"<<matrix[13][4]<<"|"<<matrix[13][5]<<"|"<<matrix[13][6]<<"|"<<matrix[13][7]<<"|"<<matrix[13][8]<<"|"<<matrix[13][9]<<"|"<<matrix[13][10]<<"|"<<matrix[13][11]<<"|"<<matrix[13][12]<<"|"<<matrix[13][13]<<"|"<<matrix[13][14]<<"|"<<matrix[13][15]<<"|"<<matrix[13][16]<<"|"<<matrix[13][17]<<"|"<<matrix[13][18]<<"|"<<matrix[13][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 15|"<<matrix[14][0]<<"|"<<"*"<<"|"<<matrix[14][2]<<"|"<<matrix[14][3]<<"|"<<matrix[14][4]<<"|"<<matrix[14][5]<<"|"<<matrix[14][6]<<"|"<<matrix[14][7]<<"|"<<matrix[14][8]<<"|"<<matrix[14][9]<<"|"<<matrix[14][10]<<"|"<<matrix[14][11]<<"|"<<matrix[14][12]<<"|"<<matrix[14][13]<<"|"<<matrix[14][14]<<"|"<<matrix[14][15]<<"|"<<matrix[14][16]<<"|"<<matrix[14][17]<<"|"<<matrix[14][18]<<"|"<<matrix[14][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 16|"<<matrix[15][0]<<"|"<<"*"<<"|"<<matrix[15][2]<<"|"<<matrix[15][3]<<"|"<<matrix[15][4]<<"|"<<matrix[15][5]<<"|"<<matrix[15][6]<<"|"<<matrix[15][7]<<"|"<<matrix[15][8]<<"|"<<matrix[15][9]<<"|"<<matrix[15][10]<<"|"<<matrix[15][11]<<"|"<<matrix[15][12]<<"|"<<matrix[15][13]<<"|"<<matrix[15][14]<<"|"<<matrix[15][15]<<"|"<<matrix[15][16]<<"|"<<matrix[15][17]<<"|"<<matrix[15][18]<<"|"<<matrix[15][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 17|"<<matrix[16][0]<<"|"<<"*"<<"|"<<matrix[16][2]<<"|"<<matrix[16][3]<<"|"<<matrix[16][4]<<"|"<<matrix[16][5]<<"|"<<matrix[16][6]<<"|"<<matrix[16][7]<<"|"<<matrix[16][8]<<"|"<<matrix[16][9]<<"|"<<matrix[16][10]<<"|"<<matrix[16][11]<<"|"<<matrix[16][12]<<"|"<<matrix[16][13]<<"|"<<matrix[16][14]<<"|"<<matrix[16][15]<<"|"<<matrix[16][16]<<"|"<<matrix[16][17]<<"|"<<matrix[16][18]<<"|"<<matrix[16][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 18|"<<matrix[17][0]<<"|"<<"*"<<"|"<<matrix[17][2]<<"|"<<matrix[17][3]<<"|"<<matrix[17][4]<<"|"<<matrix[17][5]<<"|"<<matrix[17][6]<<"|"<<matrix[17][7]<<"|"<<matrix[17][8]<<"|"<<matrix[17][9]<<"|"<<matrix[17][10]<<"|"<<matrix[17][11]<<"|"<<matrix[17][12]<<"|"<<matrix[17][13]<<"|"<<matrix[17][14]<<"|"<<matrix[17][15]<<"|"<<matrix[17][16]<<"|"<<matrix[17][17]<<"|"<<matrix[17][18]<<"|"<<matrix[17][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 19|"<<matrix[18][0]<<"|"<<"*"<<"|"<<matrix[18][2]<<"|"<<matrix[18][3]<<"|"<<matrix[18][4]<<"|"<<matrix[18][5]<<"|"<<matrix[18][6]<<"|"<<matrix[18][7]<<"|"<<matrix[18][8]<<"|"<<matrix[18][9]<<"|"<<matrix[18][10]<<"|"<<matrix[18][11]<<"|"<<matrix[18][12]<<"|"<<matrix[18][13]<<"|"<<matrix[18][14]<<"|"<<matrix[18][15]<<"|"<<matrix[18][16]<<"|"<<matrix[18][17]<<"|"<<matrix[18][18]<<"|"<<matrix[18][19]<<"|"<<endl; cout<<" -------------------------------------------"<<endl; cout<<" 20|"<<matrix[19][0]<<"|"<<matrix[19][1]<<"|"<<matrix[19][2]<<"|"<<matrix[19][3]<<"|"<<matrix[19][4]<<"|"<<matrix[19][5]<<"|"<<matrix[19][6]<<"|"<<matrix[19][7]<<"|"<<matrix[19][8]<<"|"<<matrix[19][9]<<"|"<<matrix[19][10]<<"|"<<matrix[19][11]<<"|"<<matrix[19][12]<<"|"<<matrix[19][13]<<"|"<<matrix[19][14]<<"|"<<matrix[19][15]<<"|"<<matrix[19][16]<<"|"<<matrix[19][17]<<"|"<<matrix[19][18]<<"|@"<<"|"<<endl; }

Share this post


Link to post
Share on other sites
iostream.h is depriciated, conio.h is non-standard, and you might want to learn about the wonders of for loops. And while(totalmoves=0) is most definiatly not doing what you expect (hint: = is for assignment, == is for testing equality)

Share this post


Link to post
Share on other sites
You can use tags around your code to make it look better in posts (see the main FAQ - lightbulb link at top right above forums)
[ code ] and [ /code ] OR [ source ] and [ /source ] (remove the spaces to use)

If I understand correctly, what you want to do is keep asking the user to enter the row and column until he enters it right? A better design would probably be to just start him at 1,1 and not force him to enter 1,1. But, if that what you want to do, then you need another while loop (instead of the two if statements).
Something like this:

while(totalmoves=0)
{
cout<<"Row : ";
cin>> i;
while (i != 1) //forces the user to input 1 as the starting row
{
cout<<"Start at 1,1!"<<endl;
cout<<"Row : ";
cin>> i;
}
cout<<"Column : ";
cin>> j;
while(j != 1) //forces the user to input 1 as the starting column
{
cout<<"Start at 1,1!"<<endl;
cout<<"Column : ";
cin>> j;
}

matrix[i-1][j-1]='X';
totalmoves++;
}



The reason your way wasn't working was that the if statements only check the conditions once. Then totalmoves is incremented, so you never go back in that section again.
Hmm, I just noticed that you have while(totalmoves=0)
that should be ==0. So that might be part of your problem in figuring out what's going wrong. But that while loop isn't really doing anything since you always set it to 0 before the loop and always increment it in the loop. That means it always just occurs once. So you probably have some more things to work out, but maybe things I pointed out will get you unstuck again.

Do you use a debugger? They can be very powerful, and for something like this you can step through your program and see what's actually happening.

Share this post


Link to post
Share on other sites
Cool that helps. Thanks peoples!
My next problem i know, is going to be forcing the user to move only one space at a time. Any hints on how to solve this would be appreciated to! THanks again.
(where would i get a debugger? as mentioned above.)

Share this post


Link to post
Share on other sites
For checking whether user moves too far, just check if (abs(current_position - new_position) <= 1). Do that for x and y. If you can only move up, down, right , and left then you'll have to check for preventing diagonal moves too (if both the x and y change by 1). Another option would be to change the design such that the user enters a direction (NSEW or UDRL or WSDA or numpad 8264).

What compiler are you using and what platform? Some compilers come with debuggers (like MS Visual Studio). There are probably some free debuggers out there too (I know there are for Linux). Others here might have some suggestions too. If you can't get a debugger, then you can use cout statements to check your variables. Just place cout's where you need to know things. Add counters to let you know how many times you are going through a loop, etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this