forked from darlashahu/TicTacToe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
218 lines (205 loc) · 5.09 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char board[3][3];
void initBoard()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
}
void printBoard()
{
printf("-------------\n");
for (int i = 0; i < 3; i++)
{
printf("|");
for (int j = 0; j < 3; j++)
{
printf(" %c |", board[i][j]);
}
printf("\n-------------\n");
}
}
char win()
{
for (int i = 0; i < 3; i++)
{
if (board[i][0] == board[i][1] && board[i][0] == board[i][2])
return board[i][0];
if (board[0][i] == board[1][i] && board[0][i] == board[2][i])
return board[0][i];
}
if (board[0][0] == board[1][1] && board[0][0] == board[2][2])
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
return board[1][1];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] == ' ')
return ' ';
}
}
return 'T';
}
int tryMove(int row, int col, char playerSymbol)
{
if (row >= 0 && row <= 2 && col >= 0 && col <= 2)
{
if (board[row][col] == ' ')
{
board[row][col] = playerSymbol;
return 1;
}
}
printf("Invalid move! Try again!\n");
return 0;
}
void getHumanMove(char playerSymbol)
{
printf("%c's turn!\n", playerSymbol);
int row;
int col;
do
{
printf("Enter row (1-3): ");
scanf("%d", &row);
printf("Enter column (1-3): ");
scanf("%d", &col);
}
while (!tryMove(row - 1, col - 1, playerSymbol));
printBoard();
}
void getComputerMove(char playerSymbol)
{
printf("%c's turn!\n", playerSymbol);
int row, col;
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
if (board[row][col] == ' ')
{
board[row][col] = playerSymbol;
if (win() == playerSymbol)
{
printf("Computer chose (%d, %d)\n", row + 1, col + 1);
printBoard();
return;
}
board[row][col] = ' ';
}
}
}
char opponentSymbol = (playerSymbol == 'X') ? 'O' : 'X';
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
{
if (board[row][col] == ' ')
{
board[row][col] = opponentSymbol;
if (win() == opponentSymbol)
{
board[row][col] = playerSymbol;
printf("Computer chose (%d, %d)\n", row + 1, col + 1);
printBoard();
return;
}
board[row][col] = ' ';
}
}
}
if (board[1][1] == ' ')
{
board[1][1] = playerSymbol;
printf("Computer chose (2, 2)\n");
printBoard();
return;
}
int corners[4][2] = {{0, 0}, {0, 2}, {2, 0}, {2, 2}};
for (int i = 0; i < 4; i++)
{
row = corners[i][0];
col = corners[i][1];
if (board[row][col] == ' ')
{
board[row][col] = playerSymbol;
printf("Computer chose (%d, %d)\n", row + 1, col + 1);
printBoard();
return;
}
}
int edges[4][2] = {{0, 1}, {1, 0}, {1, 2}, {2, 1}};
for (int i = 0; i < 4; i++)
{
row = edges[i][0];
col = edges[i][1];
if (board[row][col] == ' ')
{
board[row][col] = playerSymbol;
printf("Computer chose (%d, %d)\n", row + 1, col + 1);
printBoard();
return;
}
}
}
int main(void)
{
printf("Welcome to TicTacToe!\n");
printf("Choose game mode:\n\t1 for human vs human\n\t2 for human vs "
"computer\n\t3 for computer vs computer\n");
char currentPlayer = 'X';
int gameMode;
scanf("%d", &gameMode);
char humanSymbol = 'X';
char computer1Symbol;
char computer2Symbol;
int isHumanVsHuman = 1;
int isComputerVsComputer = 0;
if (gameMode == 2)
{
printf("Do you want to play as X or O? ");
scanf(" %c", &humanSymbol);
humanSymbol = toupper(humanSymbol);
computer1Symbol = (humanSymbol == 'X') ? 'O' : 'X';
isHumanVsHuman = 0;
}
else if (gameMode == 3)
{
computer1Symbol = 'X';
computer2Symbol = 'O';
isHumanVsHuman = 0;
isComputerVsComputer = 1;
}
initBoard();
printBoard();
do
{
if ((isHumanVsHuman || currentPlayer == humanSymbol) &&
!isComputerVsComputer)
getHumanMove(currentPlayer);
else
getComputerMove(currentPlayer);
if (win() == 'T')
{
printf("It's a tie! ");
return 0;
}
if (win() != ' ')
{
printf("%c won!", win());
return 0;
}
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
while (1);
return 0;
}