Tic-tac-toe AI problems

Can any1 give anysuggestion on how to complete my ai?
here's my code
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>

using namespace std;

char board[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9'};
void display();
bool WinningCondition();
void GetPlayerMove(bool);
void GetAIMove(bool);
bool CorrectDrop(int);
int choice;

void main()
        {
        bool player = false;            
        int i= 0;

        display();

        while(!WinningCondition())
                if ( i==0 || i==2  || i==4 || i==6 || i==8 )
                {
                        i++;
                        {
                        if(player == true)
                                player = false;
                        else
                                player = true;
                                GetPlayerMove(player);
                        }
                }
                else if ( i==1 || i == 3  || i==5 || i==7 )
                {
                        i++;
                        {
                        if(player == true)
                                player = false;
                        else
                                player = true;
                                GetAIMove(player);
                        }
                }
                else
                cout << " It's a TIE !!! " << endl;


        
        if(player == true)
                cout << "Player one WINS !!!" << endl;
        else
                cout << "The computer WINS !!!" << endl;
        }
        

void display()
        {
			system("cls");
	cout << "\n " << board[0] << " | " << board[1] << " | " << board[2] << endl
		 << " ---------" << endl
		 << " " << board[3] << " | " << board[4] << " | " << board[5] << endl
		 << " ---------" << endl
		 << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
        }

bool WinningCondition()
        {
          if(board[0] == board[1] && board[2] == board[0])//1st horizontal 
		return true;
	else if(board[3] == board[4] && board[5] == board[3])//2nd horizontal 
		return true;
	else if(board[6] == board[7] && board[8] == board[6])//3rd horizontal
		return true;
	else if(board[0] == board[3] && board[6] == board[0])//1st vertical
		return true;
	else if(board[1] == board[4] && board[7] == board[1])//2nd vertical
		return true;
	else if(board[2] == board[5] && board[8] == board[2])//3rd vertical
		return true;
	else if(board[0] == board[4] && board[8] == board[0])//1st diagonal
		return true;
	else if(board[6] == board[4] && board[2] == board[6])//2nd diagonal
		return true;
	else
		return false;
        }

void GetPlayerMove(bool whichPlayer)
        {
                int choice;
                cout << "Enter Your Move: ";
                cin >> choice;

				
	while(choice > 9)
	{
		cout << "Invaild Move!" << endl;
		cout << "Enter number between 1 to 9 ONLY!" << endl;
		cout << "Enter your move again!" << endl;

		cin >> choice;
	}
                if(CorrectDrop(choice))
                        {
                                if(whichPlayer == true)
                                board[choice-1] = 'X';
                                else
                                board[choice] = 'O';
                        }
                else
                        GetPlayerMove(whichPlayer);
                        display();
        }

  // This is the ai part 
void GetAIMove(bool whichPlayer)
        {
			int choice;
			cout << "Computer's Move: " << endl;
			cin >> choice;

							if(board[0] == 'O' && board[1] == 'O' && board[0] != ' ') {
								board[2] = 'O';}

							else if(board[3] == 'O' && board[4] == 'O' && board[3] != ' ') {
								board[5] = 'O';}

							else if(board[6] == 'O' && board[7] == 'O' && board[6] != ' ') {
								board[8] = 'O';}

							else if(board[0] == 'O' && board[3] == 'O' && board[0] != ' ') {
								board[6] = 'O';}

							else if(board[1] == 'O' && board[4] == 'O' && board[1] != ' ') {
								board[7] = 'O';}

							else if(board[2] == 'O' && board[5] == 'O' && board[2] != ' ') {
								board[8] = 'O';}

							else if(board[0] == 'O' && board[4] == 'O' && board[0] != ' ') {
								board[8] = 'O';}

							else if(board[8] == 'O' && board[4] == 'O' && board[8] != ' ') {
								board[0] = 'O';}

							else if(board[6] == 'O' && board[4] == 'O' && board[6] != ' ') {
								board[2] = 'O'; }


                if(CorrectDrop(choice))
                        {
                                if(whichPlayer == true)
                                board[choice] = 'X';
                                else
                                board[choice-1] = 'O';
                        }
                else
                        GetAIMove(whichPlayer);
                        display();
        }


bool CorrectDrop(int choice)
        {
                if(board[choice-1] == 'X' || board[choice-1] == 'O')
                return false;
                else
                return true;
        }


What should my choice = to?
Why are you having a cin for a move that is not made by a human?

Add the final else as choice2=rand()%9. Also, you need to make another for the fact that the computer should know to block winning situations, but only let it go to that whole rigmarole if it passes a rand()%100 greater than a certain number (otherwise it would be unbeatable, no fun.) If it fails to be greater than a certain number, then make it run the random.

Good recursion under the if(correctdrop()), but I would suggest calling the computer's choice choice2. Just a preference.

Next time, though, try making a program that can play Calvinball. O_o
Topic archived. No new replies allowed.