dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: C++ Tic Tac Toe (Smart Computer Player Issue)

  1. #1
    Join Date
    Oct 2011
    Posts
    6

    C++ Tic Tac Toe (Smart Computer Player Issue)

    I am making a tic tac toe game program using a 1D array and I am having problems making the smart computer player. This computer player has to block the human player and if he can either block or win he has to choose to win. However, I wrote the code and thought that it would work but it isn't. The problem is that when it is the computer's turn it makes multiple moves in a single turn, also X is printed in one of the grids (X = human player) what is wrong with my code? Can anyone help me?

    This is the function that makes the move for the computer
    Code:
    void goO3(char z[], const bool endMove)
    {
      bool endTurn = endMove;
      cout << "\nComputer(HARD) turn to move: \n" << endl;
      if (endTurn == false)
      {
        for(int i = 0; i < 9 && !endTurn ; i++)
        {
          if(endTurn == true) break;
          if ((z[0] == 'O') && (z[1] == 'O') && (z[2] == ' ')) //1
          {
            z[2] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == ' ') && (z[1] == 'O') && (z[2] ='O')) //2
          { 
            z[0] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == 'O') && (z[1] == ' ') && (z[2] ='O')) //3
          { 
            z[1] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[3] == 'O') && (z[4] == 'O') && (z[5] =' ')) //4
          { 
            z[5] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[3] == ' ') && (z[4] == 'O') && (z[5] ='O')) //5
          { 
            z[3] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[3] == 'O') && (z[4] == ' ') && (z[5] ='O')) //6
          { 
            z[4] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == 'O') && (z[7] == 'O') && (z[8] =' ')) //7
          { 
            z[8] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == ' ') && (z[7] == 'O') && (z[8] ='O')) //8
          { 
            z[6] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == 'O') && (z[7] == ' ') && (z[8] ='O')) //9
          { 
            z[7] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == ' ') && (z[3] == 'O') && (z[6] == 'O')) //10
          {
            z[0] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == 'O') && (z[3] == ' ') && (z[6] == 'O')) //11
          {
            z[3] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == 'O') && (z[3] == 'O') && (z[6] == ' ')) //12
          {
            z[6] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[1] == ' ') && (z[4] == 'O') && (z[7] == 'O')) //13
          {
            z[1] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[1] == 'O') && (z[4] == ' ') && (z[7] == 'O')) //14
          {
            z[4] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[1] == 'O') && (z[4] == 'O') && (z[7] == ' ')) //15
          {
            z[7] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[2] == ' ') && (z[5] == 'O') && (z[8] == 'O')) //16
          {
            z[2] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[2] == 'O') && (z[5] == ' ') && (z[8] == 'O')) //17
          {
            z[5] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[2] == 'O') && (z[5] == 'O') && (z[8] == ' ')) //18
           {
            z[8] = 'O';
            endTurn = true;        
          }
          if(endTurn == true) break;
          if ((z[0] == ' ') && (z[4] == 'O') && (z[8] == 'O')) //19
          {
            z[0] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == 'O') && (z[4] == ' ') && (z[8] == 'O')) //20
          {
            z[4] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == 'O') && (z[4] == 'O') && (z[8] == ' ')) //21
          {
            z[8] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == ' ') && (z[4] == 'O') && (z[2] == 'O')) //22
          {
            z[6] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == 'O') && (z[4] == ' ') && (z[2] == 'O')) //23
          {
            z[4] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == 'O') && (z[4] == 'O') && (z[2] == ' ')) //24
          {
            z[2] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
    
    
          //24 Combinations to Block
          if ((z[0] == 'X') && (z[1] == 'X') && (z[2] == ' ')) //1
          {
            z[2] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == ' ') && (z[1] == 'X') && (z[2] ='X')) //2
          { 
            z[0] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == 'X') && (z[1] == ' ') && (z[2] ='X')) //3
          { 
            z[1] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[3] == 'X') && (z[4] == 'X') && (z[5] =' ')) //4
          { 
            z[5] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[3] == ' ') && (z[4] == 'X') && (z[5] ='X')) //5
          { 
            z[3] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[3] == 'X') && (z[4] == ' ') && (z[5] ='X')) //6
          { 
            z[4] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == 'X') && (z[7] == 'X') && (z[8] =' ')) //7
          { 
            z[8] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == ' ') && (z[7] == 'X') && (z[8] ='X')) //8
          { 
            z[6] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == 'X') && (z[7] == ' ') && (z[8] ='X')) //9
          { 
            z[7] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == ' ') && (z[3] == 'X') && (z[6] == 'X')) //10
          {
            z[0] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] = 'X') && (z[3] == ' ') && (z[6] == 'X')) //11
          {
            z[3] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] = 'X') && (z[3] == 'X') && (z[6] == ' ')) //12
          {
            z[6] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[1] == ' ') && (z[4] == 'X') && (z[7] == 'X')) //13
          {
            z[1] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[1] == 'X') && (z[4] == ' ') && (z[7] == 'X')) //14
          {
            z[4] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[1] == 'X') && (z[4] == 'X') && (z[7] == ' ')) //15
          {
            z[7] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[2] == ' ') && (z[5] == 'X') && (z[8] == 'X')) //16
          {
            z[2] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[2] == 'X') && (z[5] == ' ') && (z[8] == 'X')) //17
          {
            z[5] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[2] == 'X') && (z[5] == 'X') && (z[8] == ' ')) //18
          {
            z[8] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == ' ') && (z[4] == 'X') && (z[8] == 'X')) //19
          {
            z[0] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == 'X') && (z[4] == ' ') && (z[8] == 'X')) //20
          {
            z[4] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[0] == 'X') && (z[4] == 'X') && (z[8] == ' ')) //21
          {
            z[8] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == ' ') && (z[4] == 'X') && (z[2] == 'X')) //22
          {
            z[6] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == 'X') && (z[4] == ' ') && (z[2] == 'X')) //23
          {
            z[4] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
          if ((z[6] == 'X') && (z[4] == 'X') && (z[2] == ' ')) //24
          {
            z[2] = 'O';
            endTurn = true;
          }
          if(endTurn == true) break;
    //      if(i == 8)
    //        endTurn = true;
    //      if(endTurn == true) break;
        }
      }
      if (endTurn == false)
      {
        int choice = rand()%9; // returns a number between 0 and 8
        cout << "Computer (EASY) turn: " << endl;
        while(z[choice]!= ' ')
          choice = rand()%9;
        if(choice == 0)
          z[0] = 'O';
        if(choice == 1)
          z[1] = 'O';
        if(choice == 2)
          z[2] = 'O';
        if(choice == 3)
          z[3] = 'O';
        if(choice == 4)
          z[4] = 'O';
        if(choice == 5)
          z[5] = 'O';
        if(choice == 6)
          z[6] = 'O';
        if(choice == 7)
          z[7] = 'O';
        if(choice == 8)
          z[8] = 'O'; 
      }
      printGrid(z);
    }

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: C++ Tic Tac Toe (Smart Computer Player Issue)

    What you really should do is to break down this into functions. All those if statements make it very likely that you have some kind of cut&paste bug. What if you decide to make the grid bigger? Breaking down the code into functions/classes make it a lot easier to debug.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Oct 2011
    Posts
    6

    Re: C++ Tic Tac Toe (Smart Computer Player Issue)

    Ok, I just checked my code again and it seems that in many of the else if states I was assigning a letter in an empty grid. My program is working as it should but I have encountered a new problem. I am supposed to keep the track of the scores and store it as .ini file and when I play again use that .ini to keep on adding the number of times x player or o players win or tie. I have no idea how to start this.

  4. #4
    Join Date
    Oct 2011
    Posts
    6

    Re: C++ Tic Tac Toe (Smart Computer Player Issue)

    How can I read a text file with words and number and store the number in int variables?

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: C++ Tic Tac Toe (Smart Computer Player Issue)

    If you want a regular ini file use Get/WritePrivateProfileInt otherwise use the registry. See this http://msdn.microsoft.com/en-us/libr...75(VS.85).aspx
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)