Thread: array/grid qustion

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

    array/grid qustion

    Hello

    im trying to make a c++ program and If for example i have aarray/grids that prints this out a startgrid and a goal grid like this

    startgrid
    1 2 3
    4 5 6
    7 8 X

    goalgrid
    2 1 3
    7 5 6
    4 8 X

    During the program you are supposed to move X around to make the startgrid become exactly the same as the goalgrid, and here comes my question.

    How can i make the program check the array/grid after each move of X to se if the startgrid is the same as the goalgrid and then halt the game if they are the same or continue the game like normal if they are still different?

  2. #2
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Couldn't you make an if every time X changes? Like, you should have a way to move X around, add an if there to check if those grids are the same.
    Sorry if I ain't correct. Well, I tried

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And you should break that out into a function, called something like isSolved(board, goal_board), because you are going to have to check each position.

  4. #4
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Woops, didn't think clearly >.< Ignore this

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Quote Originally Posted by tabstop View Post
    And you should break that out into a function, called something like isSolved(board, goal_board), because you are going to have to check each position.
    ye, each time a move has been done each position in the startgrid needs to compared with the goalgrid and that's what im wondering how i can do? i have no clue at all :/ could you give me an example?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by razzaz View Post
    ye, each time a move has been done each position in the startgrid needs to compared with the goalgrid and that's what im wondering how i can do? i have no clue at all :/ could you give me an example?
    Since I have no idea what you don't understand, no. Are you saying you have never seen == before? Are you saying you don't know what a function is? Are you saying you don't know how to loop over nine spots in the board?

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Quote Originally Posted by tabstop View Post
    Since I have no idea what you don't understand, no. Are you saying you have never seen == before? Are you saying you don't know what a function is? Are you saying you don't know how to loop over nine spots in the board?
    Here is my code, icant get the game to end when the grids are the same :/

    Code:
    // heero.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <iostream>  
    #include <fstream>
    #include <string>
    
    using namespace std; 
    //Prototyper
    void print(char start[][3]);
    void jump3(char start[][3],string filnamn);
    void jump(char start[][3]);
    
    int main() 
    {
    
    int yes,abryt,count; 
    
    char start[3][3], goal[3][3]; 
    	string filnamn1, filnamn2, filnamn3;
    	filnamn1 = "start.txt",
    	filnamn2 = "goal.txt";
    	filnamn3 = "score.txt";
    	jump3(start,filnamn1);
    	jump3(goal,filnamn2);
    /*  jump3(score,filnamn3); */
    	count = 0;
    
    	 int loop = 0,score;
    	 while (loop == 0){
         print(start);
    	 print(goal);
    	 jump(start);
    	 
    	 if (start == goal){
    	 cout << "lol" << endl;
    	 }
    
    // lägger till +1 på count varje gång och skriver sedan ut hur många gåner man har rört på sej.
    	 count++;
    	 cout << "You have done: " << count << " moves" << endl << endl;
    	 /* system ("cls");*/ 
    
    /* print(score); 
     if (count > score)
     cout << "your in the highscore, please enter your name" << endl;
     cin >> */ 
    
    }
    
    return 0;
    }
    
    void jump(char start[][3]){
    	string jump2;
    	cout << "You move by using W A S D on you keyboard like in Counter-Strike ";
    	cin >> jump2;
    
    int nx, ny, temp, x, y,jump;
    //tar reda på vart X är
    for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
    
    if (start[i][j] == 'X'){
    	y=i;
    	x=j;
    	//Visar position av X för spealren
    	cout << "You are on square: " << "Y: " << y << " X: " << x << endl << endl;
                
    			}
    		}
    	}
    
    //funktion för att avbryta spelet
    if(jump2 == "quit"){
    	string quit;
        cout << "Want to quit the game eh? Yes/no" ;
        cin >> quit;
    
    if(quit == "yes"){
    	system ("cls");
    	cout << "Even Uther was more brave then you are, he never gave up." << endl;
    	
    }
    else cout << "Hail to the king baby!";
    //gör så spelet forsätter
    }
    
    //if satser för att röra på sej
    	if(jump2 == "w"){
    	ny = y-1;
        nx = x;
    }
    	else if (jump2 == "a"){
    	nx = x-1;
        ny = y;
    	
    }
    	else if (jump2 == "s"){
    	ny = y+1;
        nx = x;
    }
    	else if (jump2 == "d"){
    	nx = x+1;
        ny = y;
    }
    	else{
    	cout << "YOU CANT GO HERE!!!!!!!!" << endl;
    	nx = x;
    		ny = y;
    }
    
    //temporär variable för att lagra  position av X och flytta på X
    if (nx > -1 && nx < 3 && ny > -1 && ny < 3)
    {
    	temp = start[ny][nx];
    	start[ny][nx] = start[y][x];
    	start[y][x] = temp;
    	}
    }
    
    
    void jump3(char start[][3],string filnamn){
    	ifstream infil(filnamn.c_str());
    
    //array som 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];
    
    //array somskriver ut filen som blivit vald
    }
    void print(char start[][3]){
    	 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;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You still have to check each position.

    Remember, array names, by themselves, decay to pointers to the first element; so when you type "start" you just get a memory address where start lives, and same for "goal". Since these memory addresses are never the same, the check is always false.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    15
    Quote Originally Posted by tabstop View Post
    You still have to check each position.

    Remember, array names, by themselves, decay to pointers to the first element; so when you type "start" you just get a memory address where start lives, and same for "goal". Since these memory addresses are never the same, the check is always false.
    i still dont understand how to do it i just started c++ not long ago and im getting lost in my own coding :/

  10. #10
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Code:
    bool compare() {
    	int count = 0;
    	for(int x = 0; x < 2; x++) {
    		for(int y = 0; y < 2; y++) {
    			if(startgrid[x][y] == goalgrid[x][y])
    				count++;
    		}
    	}
    	if(count == 9)
    		return true;
    
    return false;
    }
    I did not test that and I ain't no C++ pro, but I like helping, so try that out
    Again, sorry if it's not right. Well, I tried :P
    And, could someone check if that's right, if not him. 'Cause I really wanna learn

    Edit: Ofc :P
    Last edited by Akkernight; 10-19-2008 at 04:49 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    if (count == 9)

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    I'm gonna throw 2 ideas out there for you Akkernight, you can take them or leave them.

    1) If you ever see a pattern like this:
    Code:
    if (some boolean expression)
       return true;
    else
       return false;
    this can always be shortened to:
    Code:
    return (some boolean expression);
    If I saw one of my friends write code like that, I would laugh at them (and I have laughed at friends for this). Call me cruel, but this is a great way to help your friends break bad habits / learn better coding strategies. When other people point and laugh at something in my code, it tends to leave a lasting impression with me...

    2) You can "short circuit" that loop easily. As soon as you know that one of the grid locations doesn't match the goal, you can stop checking the rest. The result would look like this:
    Code:
    bool compare() {
       for (int x = 0; x < 2; x++)
          for (int y = 0; y < 2; y++)
             if (startgrid[x][y] != goalgrid[x][y])
                return false;
    
       // We only reach here if all of the grid locations matched the goal
       return true;
    }

  13. #13
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ah... Thanks for the advice ^^
    Currently research OpenGL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct qustion..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 11-18-2008, 10:00 AM
  2. qustion game
    By Gardul in forum Game Programming
    Replies: 11
    Last Post: 11-05-2005, 11:48 PM
  3. Qustion problem
    By Gardul in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2005, 01:06 AM
  4. quick qustion
    By PanzTec in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2004, 09:12 AM
  5. quick qustion
    By Oluf in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2004, 07:30 AM