# need help with a number game

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-15-2008
razzaz
need help with a number game
Hello there

Im trying to make a numbers game and i have gotten so far as to display a 3*3 grid, fil it with the numbers 1-8 and and X in the 9'th grid, and now what i need some help with is getting X to move around, your supposed to move X with W A S D as seen below. When X moves around the grid it's supposed to change position with the current number of the position it moves to, and it's this that i have problem with. Getting it to move, and getting it to switch position wich i hope someone could be very kind to help me with/solve? so far i managed to make it show it's current position on the grid.

*note: inside goal.txt and start.txt is currently this*

1 2 3
4 5 6
7 8 X

Code:

```// heero.cpp : Defines the entry point for the console application. // /* string map1,map2 cout "map" "(start.txt/start2.txt): ";*/ #include "stdafx.h" #include <iostream>  #include <fstream> #include <string> using namespace std; void jump(char start[][3],int x, int y); int main() { int yes,abryt,x,y; //deklarerade variable char start[3][3], goal[3][3]; //börjar på mitt grid ifstream infil("start.txt"); //läser in filen som blivit vald infil >> start[0][0] >> start[0][1] >> start[0][2]       >> start[1][0] >> start[1][1] >> start[1][2]           >> start[2][0] >> start[2][1] >> start[2][2]; //tar reda på vart X är på gridet                   for(int i=0;i<3;i++){                 for(int j=0;j<3;j++){                         if (start[i][j] == 'X'){                         y=i;                         x=j;                         cout << "du är på ruta: " << "Y: " << y << " X: " << x << endl << endl;                                   }                 }         } //skriver ut mitt start grid cout << start[0][0] << " " << start[0][1] << " " << start[0][2]     << endl << start[1][0] << " " << start[1][1] << " " << start[1][2]         << endl << start[2][0] << " " << start[2][1] << " " << start[2][2]         << endl << endl; ifstream infil2("goal.txt"); //läser in mitt målgrid från vald fil infil2 >> goal[0][0] >> goal[0][1] >> goal[0][2]       >> goal[1][0] >> goal[1][1] >> goal[1][2]           >> goal[2][0] >> goal[2][1] >> goal[2][2]; //skriver ut mitt mål grid cout << goal[0][0] << " " << goal[0][1] << " " << goal[0][2]     << endl << goal[1][0] << " " << goal[1][1] << " " << goal[1][2] << endl     << goal[2][0] << " " << goal[2][1] << " " << goal[2][2] << endl << endl;         int loop = 0;         while (loop == 0){                 jump(start, x, y);           }         /* system ("cls"); */         return 0; } void jump(char start[][3], int x, int y){ string jump2; cout << "You move by using W A S D on you keyboard: "; cin >> jump2; //funktion för att avbryta spelet if(jump2 == "avbryt"){         string quit;     cout << "Want to quit the game eh? Yes/no" ;     cin >> quit; if(quit == "yes"){         cout << "bye message" << endl; } else cout << "stay message"; } //if satser för att röra på sej if(jump2 == "w"){         y--;         if (y < 0 ){                 cout << "lol, you cant move here" << endl;         } } if(jump2 == "a"){         x--;         if (x < 0 ){                 cout << "lol, you cant move here" << endl;         } } if(jump2 == "s"){         y++;         if (y > 2 ){                 cout << "lol, you cant move here" << endl;         } } if(jump2 == "d"){         x++;         if (x > 2 ){                 cout << "lol, you cant move here" << endl;         } }         }```
• 10-15-2008
tabstop
I guess x and y is the current position of your 'X'? If so, you just need to swap the appropriate values in start. (So start[x][y] and start[x][y-1] might switch, in your w case.)
• 10-15-2008
dwks
Code:

```cout << start[0][0] << " " << start[0][1] << " " << start[0][2]     << endl << start[1][0] << " " << start[1][1] << " " << start[1][2]         << endl << start[2][0] << " " << start[2][1] << " " << start[2][2]         << endl << endl;```
That would be much easier to do in a for loop.

Code:

```if(jump2 == "s"){         y++;         if (y > 2 ){                 cout << "lol, you cant move here" << endl;         } }```
Perhaps you can't move there, but you just did. In other words, if y>=2, don't even increment y in the first place.
• 10-15-2008
razzaz
Quote:

Originally Posted by dwks
Code:

```if(jump2 == "s"){         y++;         if (y > 2 ){                 cout << "lol, you cant move here" << endl;         } }```
Perhaps you can't move there, but you just did. In other words, if y>=2, don't even increment y in the first place.

what should i do then? this is the part i cant understand :/
• 10-15-2008
master5001
Ok, lets say I hit "s" twice. What is y's value? Is it not 2? Ok, lets say I hit "s" twice more. Now what is my value? 4, right? I mean it told me that I cannot move there and what not. But my y is continuing to exist out of bounds.

Maybe inside the if statement you could set y = 2?
• 10-15-2008
razzaz
Quote:

Originally Posted by master5001
Ok, lets say I hit "s" twice. What is y's value? Is it not 2? Ok, lets say I hit "s" twice more. Now what is my value? 4, right? I mean it told me that I cannot move there and what not. But my y is continuing to exist out of bounds.

Maybe inside the if statement you could set y = 2?

ah i se, thank you! :) now the thing i still cant figure out is moving X and how to swap positions with X and the numbers :/
• 10-15-2008
master5001
Can you post your fixed code, por favor. Make sure to fix all of those if statements. Not just the "s" key. All of your directions have the same lack of constraint.
• 10-15-2008
razzaz
Quote:

Originally Posted by master5001
Can you post your fixed code, por favor. Make sure to fix all of those if statements. Not just the "s" key. All of your directions have the same lack of constraint.

i think this is correct? :/
Code:

```#include "stdafx.h" #include <iostream>  #include <fstream> #include <string> using namespace std; void jump(char start[][3],int x, int y); int main() { int yes,abryt,x,y; //deklarerade variabl char start[3][3], goal[3][3]; //börjar på mitt grid ifstream infil("start.txt"); //läser in filen som blivit vald infil >> start[0][0] >> start[0][1] >> start[0][2]       >> start[1][0] >> start[1][1] >> start[1][2]           >> start[2][0] >> start[2][1] >> start[2][2]; //tar reda på vart X är på gridet                   for(int i=0;i<3;i++){                 for(int j=0;j<3;j++){                         if (start[i][j] == 'X'){                         y=i;                         x=j;                         cout << "your on Square: " << "Y: " << y << " X: " << x << endl << endl;                                   }                 }         } //skriver ut mitt start grid cout << start[0][0] << " " << start[0][1] << " " << start[0][2]     << endl << start[1][0] << " " << start[1][1] << " " << start[1][2]         << endl << start[2][0] << " " << start[2][1] << " " << start[2][2]         << endl << endl; ifstream infil2("goal.txt"); //läser in mitt målgrid från vald fil infil2 >> goal[0][0] >> goal[0][1] >> goal[0][2]       >> goal[1][0] >> goal[1][1] >> goal[1][2]           >> goal[2][0] >> goal[2][1] >> goal[2][2]; //skriver ut mitt mål grid cout << goal[0][0] << " " << goal[0][1] << " " << goal[0][2]     << endl << goal[1][0] << " " << goal[1][1] << " " << goal[1][2] << endl     << goal[2][0] << " " << goal[2][1] << " " << goal[2][2] << endl << endl;         int loop = 0;         while (loop == 0){                 jump(start, x, y);           }         /* system ("cls"); */         return 0; } void jump(char start[][3], int x, int y){ string jump2; cout << "You move by using W A S D on you keyboard: "; cin >> jump2; //funktion för att avbryta spelet if(jump2 == "avbryt"){         string quit;     cout << "Want to quit the game eh? Yes/no" ;     cin >> quit; if(quit == "yes"){         cout << "bye message" << endl; } else cout << "stay message"; } //if satser för att röra på sej if(jump2 == "w"){         y--;         if (y = 0 ){                 cout << "lol, you cant move here" << endl;         }         } if(jump2 == "a"){         x--;         if (x = 0 ){                 cout << "lol, you cant move here" << endl;         } } if(jump2 == "s"){         y++;         if (y = 2 ){                 cout << "lol, you cant move here" << endl;         } } if(jump2 == "d"){         x++;         if (x = 2 ){                 cout << "lol, you cant move here" << endl;         } }         }```
• 10-15-2008
tabstop
Did you notice how your compiler said "suggest parentheses around assignment used as truth value"? Do you think maybe you don't want to do an assignment inside the condition of an if statement?

You still have to swap values. (Hint: It involves "=". Three times.)
• 10-15-2008
dwks
Quote:

Maybe inside the if statement you could set y = 2?
I have a (I believe) better solution. It goes something like this.
Code:

```if(jump2 == "s"){         if (y > 2 ){                 cout << "lol, you cant move here" << endl;         }         else {                 y++;         } }```
Why increment y and then set it back down again if you don't have to? (Unless you can move more than one unit at a time, in which case your solution might be a good idea.)

If I was actually implementing this myself, I'd probably use something like this, but I'm thinking it's a little too complicated for the OP at the moment.
Code:

```bool is_valid_position(int xp, int yp) {     return xp >= 0 && yp >= 0 && xp <= X_MAX && yp <= Y_MAX; } bool move_if_possible(int &xp, int &yp, int x_add, int y_add) {     if(is_valid_position(xp + x_add, yp + y_add)) {         xp += x_add;         yp += y_add;     }     else {         // warn the user about an invalid move or whatever     } } if(command == MOVE_UP) {     move_if_possible(current_xp, current_yp, 0, -1); } else if(command == MOVE_RIGHT) {     move_if_possible(current_xp, current_yp, 1, 0); } // and so on```
• 10-15-2008
master5001
Code:

```#include "stdafx.h" #include <iostream>  #include <fstream> #include <string> using namespace std; void jump(char start[][3],int x, int y); int main() { int yes,abryt,x,y; //deklarerade variabl char start[3][3], goal[3][3]; //b&#246;rjar p&#229; mitt grid ifstream infil("start.txt"); //l&#228;ser in filen som blivit vald infil >> start[0][0] >> start[0][1] >> start[0][2]       >> start[1][0] >> start[1][1] >> start[1][2]           >> start[2][0] >> start[2][1] >> start[2][2]; //tar reda p&#229; vart X &#228;r p&#229; gridet                   for(int i=0;i<3;i++){                 for(int j=0;j<3;j++){                         if (start[i][j] == 'X'){                         y=i;                         x=j;                         cout << "your on Square: " << "Y: " << y << " X: " << x << endl << endl;                                   }                 }         } //skriver ut mitt start grid cout << start[0][0] << " " << start[0][1] << " " << start[0][2]     << endl << start[1][0] << " " << start[1][1] << " " << start[1][2]         << endl << start[2][0] << " " << start[2][1] << " " << start[2][2]         << endl << endl; ifstream infil2("goal.txt"); //l&#228;ser in mitt m&#229;lgrid fr&#229;n vald fil infil2 >> goal[0][0] >> goal[0][1] >> goal[0][2]       >> goal[1][0] >> goal[1][1] >> goal[1][2]           >> goal[2][0] >> goal[2][1] >> goal[2][2]; //skriver ut mitt m&#229;l grid cout << goal[0][0] << " " << goal[0][1] << " " << goal[0][2]     << endl << goal[1][0] << " " << goal[1][1] << " " << goal[1][2] << endl     << goal[2][0] << " " << goal[2][1] << " " << goal[2][2] << endl << endl;         int loop = 0;         while (loop == 0){                 jump(start, x, y);           }         /* system ("cls"); */         return 0; } void jump(char start[][3], int x, int y) {         string jump2;         out << "You move by using W A S D on you keyboard: ";         in >> jump2;         //funktion f&#246;r att avbryta spelet         if(jump2 == "avbryt")         {                 string quit;                 cout << "Want to quit the game eh? Yes/no" ;                 cin >> quit;                 if(quit == "yes")                 {                         cout << "bye message" << endl;                 } else                         t << "stay message";         } else         //if satser f&#246;r att r&#246;ra p&#229; sej         switch(jump2[0])         {                 case 'w':                         y--;                         if (y < 0 ){                                 y = 0;                                 cout << "lol, you cant move here" << endl;                         }                         break;                 case 'a':                         x--;                         if (x < 0 ){                                 x = 0;                                 cout << "lol, you cant move here" << endl;                         }                         break;                 case 's':                         y++;                         if (y > 2 ){                                 y = 2;                                 cout << "lol, you cant move here" << endl;                         }                         break;                 case 'd':                         x++;                         if (x > 2 ) {                                 x = 2;                                 cout << "lol, you cant move here" << endl;                         }                         break;         } }```
• 10-15-2008
razzaz
thank you very very much for the guidence guys! :) i dont know how to thank you enough, i though have one last question if i may. :/

When the game starts you have the starting grid and the goal grid showing and under it this line appears "You move by using W A S D on you keyboard:" and you get to punch in someting. now, each time someting is enterd, a while loop goes on and shows "You move by using W A S D on you keyboard:" again and you get to punch in someting new. What i cant get working is that so each time you punch in someting and the loop goes on, the START grid is supposed to also reload also, ive been breaking it each time ive been trying to get the grid to loop also :/ what should i do?
• 10-15-2008
master5001
Why not start off by changing your main() function to write the grids just using a loop or two, first of all. Perhaps it couldn't hurt to write a function that draws grides. I mean the code to draw a grid is fundamentally the same each time.
• 10-15-2008
whiteflags
Redraw the grid for each move.

Unless you do something like clear the screen older grids will scoot up the screen, with the most recent at the bottom.
• 10-15-2008
master5001
What OS are you writing this for? And do you care if you implement non-standard functions?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last