Help

This is a discussion on Help within the C++ Programming forums, part of the General Programming Boards category; I was wondering if anyone can help me with my game. Im makeing a tic tac toe game and I ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    73

    Exclamation Help

    I was wondering if anyone can help me with my game. Im makeing a tic tac toe game and I at the point in witch Im getting the players move. Iv got everything except how to let the user now the move was illegal (either moveing in a taken spot or a spot not on the board). Here is my code.

    Code:
    	int rungame()
    	{
    		int move, x, open;
    
    		for(x=1; x<=9; x++)
    		{
    			cout << "Please make a move between 1 and 9. Followed by enter." << endl;
    
    			cin >> move;
    
    			cout << "Move " << x << endl;
    
    			if ((move==1)&&(board[0]!='x')&&(board[0]!='o'))
    			{
    				board[0]='x';
    				cout << board[0] << " takes square 1" << endl;
    			}else
    			if ((move==2)&&(board[1]!='x')&&(board[1]!='o'))
    			{
    				board[1]='x';
    				cout << board[1] << " takes square 2" << endl;
    			}else
    			if ((move==3)&&(board[2]!='x')&&(board[2]!='o'))
    			{
    				board[2]='x';
    				cout << board[2] << " takes square 3" << endl;
    			}else
    			if ((move==4)&&(board[3]!='x')&&(board[3]!='o'))
    			{
    				board[3]='x';
    				cout << board[3] << " takes square 4" << endl;
    			}else
    			if ((move==5)&&(board[4]!='x')&&(board[4]!='o'))
    			{
    				board[4]='x';
    				cout << board[4] << " takes square 5" << endl;
    			}else
    			if ((move==6)&&(board[5]!='x')&&(board[5]!='o'))
    			{
    				board[5]='x';
    				cout << board[5] << " takes square 6" << endl;
    			}else
    			if ((move==7)&&(board[6]!='x')&&(board[6]!='o'))
    			{
    				board[6]='x';
    				cout << board[6] << " takes square 7" << endl;
    			}else
    			if ((move==8)&&(board[7]!='x')&&(board[7]!='o'))
    			{
    				board[7]='x';
    				cout << board[7] << " takes square 8" << endl;
    			}else
    			if ((move==9)&&(board[8]!='x')&&(board[8]!='o'))
    			{
    				board[8]='x';
    				cout << board[8] << " takes square 9" << endl;
    			}
    		}
    		cout << "CAT" << endl;
    		return 0;
    	}
    How can I get the error message and redo the turn?

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    I think it would make it a lot easier to have the printing of the board in one method, and the checking the validity of the move in another method, that way, if they input something that is incorrect, you can just call yourself again.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    A straightforward method would be to use a two dimensional array to hold the nine positions of the board. Place a zero in a position for empty, one for x, and two for o. Then you can just check the correct position to see if it is taken, you can also use it for determining a win.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    MethodMan Im not quit understanding what you mean by method. Is method supposed to be an array or function?

  5. #5
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    When I referred to mehtod, I meant function, sorry its my Java background.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Ok.......I was wondering how the function would work? If I use an array to store the board in and then I give the blocks (numbers of the array) an value of x or o (the array is a char). So if the board stores a x or o in any givin array location how can I call that later on, or check the board, to see if there is a value in the block?

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by MethodMan
    When I referred to mehtod, I meant function, sorry its my Java background.
    Well class functions are also called class methods (or at least in "Teach Yourself C++ in 21 Days"), so you're still right.

    But thats besides the point.

  8. #8
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Well you make the array static, so the values entered will remain there, so you just have a normal print() function that will print the board.

    Another function will be used to check to see if the array position is already taken.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    I take it that static means global, or where you juz giving me an example?

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    O and juz to check. If I make a global array and then say something like this

    Code:
     array[0]='x';
    That means that the first number in the array is givin the value of X. Well will that value stay there until changed? Even if I give array[1] a differant value? Im juz checking because it does'nt seem like it.

  11. #11
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Yes, or you can just pass the array into the fucntion, so you can alter its contents.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool endgame=false;
    
    int rungame();
    int board[9]={0,1,2,3,4,5,6,7,8};
    
    void checkboard();
    void printboard();
    
    
    	int main()
    	{
    		char x;
    
    		while (!endgame)
    		{
    
    			cout << "Welcome. Would you like to play a game? " << endl;
    			cout << "Press Y for yes and N for no. Followed by enter" << endl;
    			cin >> x;
    
    			if (x=='y')
    				rungame();
    			else
    			if (x=='n')
    				endgame=true;
    		}
    
    		return 0;
    	}
    
    	int rungame()
    	{
    		int move, x, open;
    
    		for(x=1; x<=9; x++)
    		{
    			cout << "Please make a move between 1 and 9. Followed by enter." << endl;
    
    			if (x==1)
    			{
    				printboard();
    			}
    
    			cin >> move;
    
    			if ((move==1)&&(board[0]!='x')&&(board[0]!='o'))
    			{
    				board[0]='x';
    				cout << board[0] << " takes square 1" << endl;
    			}else
    			if ((move==2)&&(board[1]!='x')&&(board[1]!='o'))
    			{
    				board[1]='x';
    				cout << board[1] << " takes square 2" << endl;
    			}else
    			if ((move==3)&&(board[2]!='x')&&(board[2]!='o'))
    			{
    				board[2]='x';
    				cout << board[2] << " takes square 3" << endl;
    			}else
    			if ((move==4)&&(board[3]!='x')&&(board[3]!='o'))
    			{
    				board[3]='x';
    				cout << board[3] << " takes square 4" << endl;
    			}else
    			if ((move==5)&&(board[4]!='x')&&(board[4]!='o'))
    			{
    				board[4]='x';
    				cout << board[4] << " takes square 5" << endl;
    			}else
    			if ((move==6)&&(board[5]!='x')&&(board[5]!='o'))
    			{
    				board[5]='x';
    				cout << board[5] << " takes square 6" << endl;
    			}else
    			if ((move==7)&&(board[6]!='x')&&(board[6]!='o'))
    			{
    				board[6]='x';
    				cout << board[6] << " takes square 7" << endl;
    			}else
    			if ((move==8)&&(board[7]!='x')&&(board[7]!='o'))
    			{
    				board[7]='x';
    				cout << board[7] << " takes square 8" << endl;
    			}else
    			if ((move==9)&&(board[8]!='x')&&(board[8]!='o'))
    			{
    				board[8]='x';
    				cout << board[8] << " takes square 9" << endl;
    			}
    
    			if (move>=9)
    				cout << "Illegal move!" << endl;
    		}
    		cout << "CAT" << endl;
    		return 0;
    	}
    
    	void checkboard()
    	{
    
    	}
    
    	void printboard()
    	{
    		cout << board[0] << "|" << board[1] << "|" << board[2] << endl;
    		cout << board[3] << "|" << board[4] << "|" << board[5] << endl;
    		cout << board[6] << "|" << board[7] << "|" << board[8] << endl;
    	}
    Does anyone know why this code produces the number 120 when ever the player moves?

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    never mind guys.....didn't see that the board array wasn't a char. But why when ever my board is printed does the computer make a beep?

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Welp once again you can disregaurd that last question. Ill try to save any responsis for when I don't know how to answer the question my self.

Popular pages Recent additions subscribe to a feed

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