Tic Tac Toe

This is a discussion on Tic Tac Toe within the C++ Programming forums, part of the General Programming Boards category; The assignment is to make a two player tic-tac-toe game. Here's what I have so far: Code: char board[9] = ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Tic Tac Toe

    The assignment is to make a two player tic-tac-toe game. Here's what I have so far:


    Code:
    char board[9] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}, choice;
         int index=0, player = 1, result;
         bool found = false;
         
         do{
              
         if (player == 1){
         do{
         
         cout << "Player one make your move.\n";
         cin >> choice;
         cout << "\n";
         
         index = 0;
         
         while ((!found) && (index < 9)){
            if (choice == board[index]){
              found = true;
              board[index] = 'X';
              }
            else
            index++;
          }
         if(!found)
         cout << "Illegal move.\n\n";
         }while(!found);
         
         index = 0;
             
         if (board[index]==board[index+1] && board[index+1]==board[index+2])
         result = 1;
         else if(board[index+3]==board[index+4] && board[index+4]==board[index+5])
         result = 1;
         else if(board[index+6]==board[index+7] && board[index+7]==board[index+8])
         result = 1;
         else if(board[index]==board[index+3] && board[index+3]==board[index+6])
         result = 1;
         else if(board[index+1]==board[index+4] && board[index+4]==board[index+7])
         result = 1;
         else if(board[index+2]==board[index+5] && board[index+5]==board[index+8])
         result = 1;
         else if(board[index]==board[index+4] && board[index+4]==board[index+8])
         result = 1;
         else if(board[index+6]==board[index+4] && board[index+4]==board[index+2])
         result = 1;
         else
         result = -1;
         
         if (result == 1)
         cout << "Player one wins!\n";
         
         for (index = 0; index < 9; index++){     
         cout << board[index] << ' ';
         if (index == 2 || index == 5 || index == 8)
         cout << "\n";
         }
         cout << "\n\n";
         player++;
         }
         
         else if (player == 2){
         do{
         
         cout << "Player two make your move.\n";
         cin >> choice;
         cout << "\n";
         
         index = 0;
         
         while ((!found) && (index < 9)){
            if (choice == board[index]){
              found = true;
              board[index] = 'O';
              }
            else
            index++;
          }
         if(!found)
         cout << "Illegal move.\n\n";
         }while(!found);
         
         index = 0;
    
         if (board[index]==board[index+1] && board[index+1]==board[index+2])
         result = 1;
         else if(board[index+3]==board[index+4] && board[index+4]==board[index+5])
         result = 1;
         else if(board[index+6]==board[index+7] && board[index+7]==board[index+8])
         result = 1;
         else if(board[index]==board[index+3] && board[index+3]==board[index+6])
         result = 1;
         else if(board[index+1]==board[index+4] && board[index+4]==board[index+7])
         result = 1;
         else if(board[index+2]==board[index+5] && board[index+5]==board[index+8])
         result = 1;
         else if(board[index]==board[index+4] && board[index+4]==board[index+8])
         result = 1;
         else if(board[index+6]==board[index+4] && board[index+4]==board[index+2])
         result = 1;
         else
         result = -1;
         
         if (result == 1)
         cout << "Player two wins!\n";
         
         for (index = 0; index < 9; index++){
         cout << board[index] << ' ';
         if (index == 2 || index == 5 || index == 8)
         cout << "\n";
         }
         cout << "\n\n";
         player--;
         }
         
         }while (result != 1);
    When I test it, it works the first time. But after that it doesn't. But I can't figure out why.

    Here's the out put:
    Code:
    Welcome to Tic-Tac-Toe!
    Player 1 is 'X' and Player 2 is 'O'.
    a b c
    d e f
    g h i
    Choose a spot by entering the corresponding letter.
    
    Player one make your move.
    e
    
    a b c
    d X f
    g h i
    
    
    Player two make your move.
    a
    
    a b c
    d X f
    g h i
    
    
    Player one make your move.
    i
    
    a b c
    d X f
    g h i
    
    
    Player two make your move.
    e
    
    a b c
    d X f
    g h i
    
    
    Player one make your move.
    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    First of all, you should indent better. Makes it easier to read and easier for us to solve your problem. Also, you should really use loops (or a function) for the two parts that are pretty much identical, except for the player name and number.

    Now, as for your problem. If I haven't missed anything (I didn't test it), the problem is that you forget to reset found to false.

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    are you seriously doing that all in one function?
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    106
    this is funny I tried this last night... though I tried using switches... I worked till I got to about the 3rd input the it had x on certain blocks.... I tink that a switch could make your if statements easier to understand.... yes im a noob... I also think that instead of using letters to have the grid for tic tac toe that numbers would be better(1-9) its pretty hard being able to sit here and pick out an x x in a bunch of numbers for me...
    Last edited by jamort; 06-03-2009 at 05:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 11:52 AM

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