same ol' problem

This is a discussion on same ol' problem within the C++ Programming forums, part of the General Programming Boards category; but new info. I know that I need a for loop to save the moves the user makes (tic tac ...

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

    same ol' problem

    but new info. I know that I need a for loop to save the moves the user makes (tic tac toe for those of you who have no idea what im talking about). I have the entire program run out of a for loop that will end if it reaches 9 moves, but after each move the previous move is deleted from the memory. How do I make it stick in them memory? heres my source.

    Code:
     
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void xturn();
    void oturn();
    void checkwin();
    
    int board[9] = {0,1,2,3,4,5,6,7,8};
    int playa[9] = {0,1,2,3,4,5,6,7,8};
    
    	int main()
    	{
    
    		int x, move, xx;
    		
    		for (x=0; x<=8; x++)
    		{	
    				if (playa[x] == 0 || playa[x] == 2 || playa[x] == 4 || 
    						playa[x] == 6 || playa[x] == 8)
    				{
    					cin >> move;
    
    					cout << "turn " << playa[x] << endl;
    				}
    				else
    				if (playa[x] == 1 || playa[x] ==3 || playa[x] == 5 ||
    						playa[x] == 7)
    				{
    					cout << "computer turn " << playa[x] << endl;
    				}
    
    				if (playa[x]==5)
    				{
    					cout << playa[0] << endl;
    				}
    
    			if (board[x] == 1 && board[x] == 2 && board[x] == 3)
    			{
    				cout << "WINNER!" << endl;
    			}
    		}
    
    		cout << "DRAW!!" << endl;
    
    		return 0;
    	}
    
    	void xturn()
    	{
    
    		int x, xx;
    
    		cin >> xx;
    
    		cout << "you move to square " << x << endl;
    
    		cout << playa[x] << endl;
    	}
    
    	void oturn()
    	{
    
    		int y;
    
    		cout << "computer not ready yet" << endl;
    	}

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Well guys i think im a dumb ass. It was under my nose the entire time. All I had to do to save the input to the player array and board array was to write an if statement like this

    Code:
    cin >> move;
    
     if(move == 1)            //says if the user presses 1 then board[0]
       board[0]=player[0]// is taken by player[0]
    //and so on
    //same for the o's
    Im pritty sure thats all I have to do to save input to an array. Ok now on to a more interesting topic.......SPACE DETECTION!!!! Iv got a general idea of how to determine if a square is taken by x's or o's. Heres what I think....

    Code:
     if (board[0] == player[1] //o's )
         //then you may not move there.....
    Do yall think im on the right path?

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Code:
     if (board[0] == taken && board[1] == taken && board[2] == taken)
    cout << "winner" << endl;
    I waz wondering if anyone new why this piece of code wont work. It supposed to check for a winner. If there is someone in the 0 square and in the 1 square and in the 2 square it should say winner. But it only says winner if you input 0, 1, and 2 in that order. Any way I can fix this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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