Need Help With Tic Tac Toe AI

May 19, 2015 at 4:05am
The project is to build on the tic tac toe program that we made earlier in the year. I made the first program (human vs human) no problem but now I've been asked to make an "AI" that will never lose against a human player. The computer must go first and start in the top left hand corner. It must play until it has won or there is a tie, losing is not an option. The program ran fine until I tried to make an "AI" function. Every time the program gets to this function instead of the computer placing an X in the appropriate spot it just forfeits it's turn and the human player is allowed to go again. Please help me figure out how to fix the "AI" portion of the code. I know the rest of the code is every inefficient but it works how I need it to. This is my first ever programming class I have taken and for some reason I choose to take it online, so I have every little knowledge when it comes to coding.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
void AITurn()
{
    for(int y = 0, x = 0; y < 3; y++)
        if((gameboard[x][y] == 'O' || gameboard[x][y] == 'X') && (gameboard[x+1][y] == gameboard[x][y]) && (gameboard[x+2][y] != 'O' && gameboard[x+2][y] != 'O')){
             gameboard[x+2][y] = 'X'; return;
        }
    for(int y = 0, x = 1; y < 3; y++)
        if((gameboard[x][y] == 'O' || gameboard[x][y] == 'X') && (gameboard[x+1][y] == gameboard[x][y]) && (gameboard[x-1][y] != 'O' && gameboard[x-1][y] != 'O')){
             gameboard[x-1][y] = 'X'; return;
        }
    for(int y = 0, x = 0; y < 3; y++)
        if((gameboard[x][y] == 'O' || gameboard[x][y] == 'X') && (gameboard[x+2][y] == gameboard[x][y]) && (gameboard[x+1][y] != 'O' && gameboard[x+1][y] != 'O')){
             gameboard[x+1][y] = 'X'; return;
        }
    for(int x = 0, y = 0; x < 3; x++)
        if((gameboard[x][y] == 'O' || gameboard[x][y] == 'X') && (gameboard[x][y+1] == gameboard[x][y]) && (gameboard[x][y+2] != 'O' && gameboard[x][y+2] != 'O')){
             gameboard[x][y+2] = 'X'; return;
        }
    for(int x = 0, y = 1; x < 3; x++)
        if((gameboard[x][y] == 'O' || gameboard[x][y] == 'X') && (gameboard[x][y+1] == gameboard[x][y]) && (gameboard[x][y-1] != 'O' && gameboard[x][y-1] != 'O')){
             gameboard[x][y-1] = 'X'; return;
        }
    for(int x = 0, y = 0; x < 3; x++)
        if((gameboard[x][y] == 'O' || gameboard[x][y] == 'X') && (gameboard[x][y+2] == gameboard[x][y]) && (gameboard[x][y+1] != 'O' && gameboard[x][y+1] != 'O')){
             gameboard[x][y+1] = 'X'; return;
        }
    if((gameboard[0][0] == gameboard[1][1]) && (gameboard[2][2] != 'O' || gameboard[2][2] != 'X')){
        gameboard[2][2] = 'X'; return;
    }
    if((gameboard[2][0] == gameboard[1][1]) && (gameboard[0][2] != 'O' || gameboard[0][2] != 'X')){
        gameboard[0][2] = 'X'; return;
    }
    if((gameboard[0][2] == gameboard[1][1]) && (gameboard[2][0] != 'O' || gameboard[2][0] != 'X')){
        gameboard[2][0] = 'X'; return;
    }
    if((gameboard[2][2] == gameboard[1][1]) && (gameboard[0][0] != 'O' && gameboard[0][0] != 'X')){
        gameboard[0][0] = 'X'; return;
    }
    if((gameboard[0][0] == gameboard[2][2]) && (gameboard[1][1] != 'O' && gameboard[1][1] != 'X')){
        gameboard[1][1] = 'X'; return;
    }
    if((gameboard[0][2] == gameboard[2][0]) && (gameboard[1][1] != 'O' && gameboard[1][1] != 'X')){
        gameboard[1][1] = 'X'; return;
    }

}
// Change players
char changeplayers()
    {
        if (player == 'X')
            {
                player = 'O';
                cout << "Player O, row and column: ";
            }    
        else if (player == 'O')
            {
            player = 'X';
            cout << "Player X, row and column: # #";
            cout << endl;
            }    
    }
int main()
{
   cout << "Tic Tac Toe" << endl;
   gameboard[0][0] = 'X';
   showboard();
   cout << "Player X, row and column: 11" << endl;
   cout << "Player " << player << ", row and column: ";
   while (gameend() == 'A' || 'B' || 'C')
        {
            if (player == 'O')
                {
                    selection();
                }
                
            else if (player == 'X')
                {
                    void AITurn();
                }
        showboard();
            if (gameend() == 'A')
                {
                    cout << "Player X wins." << endl;
                    break;
                }
            else if (gameend() == 'B') 
                {
                    cout << "Player O wins." << endl;
                    break;
                }
            else if (gameend()== 'C')
                {
                    cout << "The cat wins." << endl;
                    break;
                }    
            changeplayers();
            
            
        }    
    
   return 0;
}
May 19, 2015 at 5:58am
I didn't check every for loop for a logic error but...

1
2
3
4
  else if (player == 'X')
{
        void AITurn();
} 


This isn't how you call a function, remove the void. You do not include the return type when calling something.
http://www.cplusplus.com/doc/tutorial/functions/


May 19, 2015 at 7:08am
May 19, 2015 at 12:01pm
Tic tac toe is such a small game that you can compute every possibility for every move for the whole game each move and you pick the sequence of moves that has the less loses and the most wins and draws at the end of the game and you do that after each move
Last edited on May 19, 2015 at 12:04pm
Topic archived. No new replies allowed.