Thread: Help with Tic Tac Toe game

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    1

    Help with Tic Tac Toe game

    Hi, I am writing a tic tac toe game and cant get it to print out the board with an X after the player has made a move. Any help would be greatly appreciated.

    Thanks

    Code:
      bool playerturn=false; //false = my turn, true = computer turn   
       bool gameover=false;
       int x; //X in Tic Tac Toe
       int o; //O in Tic Tac toe
       int playermove[4];
       int computermove[4];
       int board[3][3]= {1,2,3,4,5,6,7,8,'x'};
       int totalmoves[9];
       int move;
       
       move = 0;
       
       
     //  while (gameover == false)
      // {   
    
          for(int row = 0; row < 3; row ++)
          {
         	 for(int col = 0; col < 3; col ++)
             {
    			cout << board[row][col] << " ";
    	     }
    
             cout << endl;
          }
    
          cout<<"Enter a move [" << move << "]    ";
          cin>>playermove[move];
    
     switch (playermove[move])
       
      {
       
    case 1:
    
    board[0][0] =='x';
    playermove[0]=='x';
    break;     
    
    case 2:
    board[0][1] =='x';
    playermove[0]=='x';
    break;     
          
    case 3:
    board[0][2] =='x';
    playermove[0]=='x';
    break;     
    
    case 4:
    board[1][0] =='x';
    playermove[0]=='x';
    break;     
    
    case 5:
    board[1][1] =='x';
    playermove[0]=='x';
    break;     
    
    case 6:
    board[1][2] ='x';
    playermove[0]=='x';
    break;     
    
    case 7:
    board[2][0] =='x';
    playermove[0]=='x';
    break;     
    
    case 8:
    board[2][1] =='x';
    playermove[0]=='x';
    break;     
    
    case 9:
    board[2][2] =='x';
    playermove[0]=='x';
    break; 
    
    default:
    cout<<"Enter a valid statement"<<endl;
    
    }
    move ++;
    
    
    
     for(int row = 0; row < 3; row ++)
          {
         	 for(int col = 0; col < 3; col ++)
             {
    			cout << board[row][col] << " ";
    	     }
    
             cout << endl;
          }

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    OK...playerMove[] is only set with 4 elements...yet the board has 9 places. you could do something like this:
    Code:
    //if the board positions are like the following:
    //    1 | 2 | 3
    //    4 | 5 | 6
    //    7 | 8 | 9
    
    int position = 0;
    
    cout << "please enter move position (1-9): ";
    cin >> position;
    
    switch (position){
         case 1: boardPosition[0][0] = 'x'; break; //declare boardPosition as a char array
    
         //continue with cases....
    }
    //...display board
    //...check if done
    //...change player
    OR BETTER YET...use search!!!

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe game
    By nafix in forum C Programming
    Replies: 6
    Last Post: 11-10-2007, 01:45 PM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. 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
  4. tic tac toe AI roadblock >:-(|)
    By dark_rocket in forum Game Programming
    Replies: 5
    Last Post: 06-12-2006, 05:13 AM
  5. not 3x3 tic tac toe game AI
    By Unregistered in forum Game Programming
    Replies: 9
    Last Post: 08-29-2001, 04:02 AM