need help with a number game

This is a discussion on need help with a number game within the C++ Programming forums, part of the General Programming Boards category; Hello there Im trying to make a numbers game and i have gotten so far as to display a 3*3 ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    15

    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;
    	}
    }
    	}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Quote Originally Posted by dwks View Post
    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 :/

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Quote Originally Posted by master5001 View Post
    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 :/

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Quote Originally Posted by master5001 View Post
    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;
    	}
    }
    	}

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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;
    	}
    }
    Last edited by master5001; 10-15-2008 at 05:25 PM.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    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?

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,669
    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.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What OS are you writing this for? And do you care if you implement non-standard functions?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help!!! Number guessing game
    By JesterJoker89 in forum C++ Programming
    Replies: 16
    Last Post: 10-05-2007, 12:47 PM
  2. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  3. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  4. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  5. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21