-
Notifications
You must be signed in to change notification settings - Fork 0
/
tttai.c
173 lines (151 loc) · 3.94 KB
/
tttai.c
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
169
170
171
172
173
#include<stdio.h>
#include<limits.h>
#define AI 'O'
#define Player 'X'
void printboard(char board[3][3]){
for (int i = 0; i < 3; i++){
for (int j =0; j < 3; j++){
printf(" %c ",board[i][j]);
if(j<2)printf("|");
}printf("\n");
if(i<2)printf("---+---+---+\n");
}
}
int boardfull(char board[3][3]){
for (int i = 0; i < 3; i++){
for (int j =0; j < 3; j++){
if(board[i][j]== ' ')return 0;
}
}
return 1;
}
int checkwin(char board[3][3], char player){
for (int i = 0; i < 3; i++)
{
if((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player)){
return 1;
}
}
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
( board[0][2] == player && board[1][1] == player && board[2][0] == player))
{
return 1;
}
return 0;
}
int minimax(char board[3][3],int ismaximizing){
if (checkwin(board,Player))
{return -1;
}
if (checkwin(board,AI))
{return 1;
}
if (boardfull(board))
{
return 0;
}
if (ismaximizing)
{
int bestscore = INT_MIN;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if(board[i][j]==' '){
board[i][j]=AI;
int score = minimax(board,0);
board[i][j]=' ';
if(score > bestscore){
bestscore=score;
}
}
}
}
return bestscore;
}
else{
int bestscore = INT_MAX;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if(board[i][j]==' '){
board[i][j]=Player;
int score = minimax(board,1);
board[i][j]=' ';
if(score < bestscore){
bestscore=score;
}
}
}
}
return bestscore;
}
}
void bestmove(char board[3][3],int *bestrow,int *bestcol){
int bestscore = INT_MIN;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if(board[i][j]==' '){
board[i][j]=AI;
int score = minimax(board,0);
board[i][j]=' ';
if(score > bestscore){
bestscore=score;
*bestrow= i;
*bestcol= j;
}
}
}
}
}
int main(){
char board[3][3]={
{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '}
};
int currentplayer = 1;
while (1)
{
printboard(board);
if (currentplayer)
{
int row,col;
printf("enter row and col between 1-3: " );
scanf("%d %d", &row,&col);
row--;
col--;
if ((board[row][col]!=' ') || (row < 0 || row >= 3 || col < 0 || col >= 3))
{
printf("invalid input\n");
continue;
}
board[row][col]= Player;
}
else
{
int bestrow,bestcol;
bestmove(board,&bestrow,&bestcol);
board[bestrow][bestcol]= AI;
printf("Ai chose postion: (%d,%d)\n",bestrow+1,bestcol+1);
}
if (checkwin(board,Player))
{
printboard(board);
printf("you win\n");
break;
}
if (checkwin(board,AI))
{
printboard(board);
printf("Ai won\n");
break;
}
if (boardfull(board))
{
printboard(board);
printf("draw\n");
break;
}
currentplayer = !currentplayer;
}
return 0;
}