-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.c
222 lines (201 loc) · 7.73 KB
/
tic_tac_toe.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
219
220
221
222
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
enum process{success = 0, failed = 1};
enum board_features{row_one = 0, row_two = 1, row_three = 2, column_one = 0, column_two = 1, column_three = 2};
const char COMPUTER = 'O';
const char PLAYER = 'X';
char board[3][3];
void reset_board(); // This function reset every value in board to ' '
void print_board(); // To track the game this function print the status of broad
int take_input(); // This function will take the input from the user in the row coloum format
void computer_algorithm(); // Thi function used to make computer decision to make the move
void update_board(int,int,char); // This function will update the data into the broad given by the user or computer
void main_loop(); // This will loop the take input print board computer algorthim etc
int free_space(); // This will calculate how much free spaces are left the game
void check_winner(); // This function will check if there is any winner after the user or the computer make a move.
void first_computer_move(); //This function will decide the first move the computer, also avoid the first move computer to over written in the assigned character
int main(){
reset_board();
printf("Let's start the game, I'm Brilliant than you");
main_loop();
return 0;
}
void reset_board(){
int i;
int j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
board[i][j] = ' ';
}
}
}
void print_board(){
printf("\n | | \n");
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
printf("---|-----|----\n");
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
printf("---|-----|----\n");
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
}
int take_input(){
int x;
int y;
printf("Enter the row(1-3): ");
scanf("%d", &x);
x--;
printf("Enter the column(1-3): ");
scanf("%d", &y);
y--;
if(x<3&&y<3){
update_board(x,y,PLAYER);
return success;
}else{
return failed;
}
}
void main_loop(){
int free_spaces = free_space();
printf("%d", free_spaces);
while(free_spaces>0){
printf("%d", free_spaces);
print_board();
int result = take_input();
if(result==success){
free_spaces--; // It will decrease the value of the free space whenever user or computer make a valid move.
computer_algorithm();
check_winner();
}
}
}
void update_board(int x, int y,char who){
board[x][y] = who;
}
int free_space(){
int free_space = 0;
for(int i=0;i<3;i++){
for(int j=0; j<3; j++){
if (board[i][j]==' '){
free_space++; // This loop will increment the free space until out of free space in the board
}
}
}
return free_space;
}
void first_computer_move(){
srand(time(0));
int random_x_cordinate;
int random_y_cordinate;
int temp_int = 20; // It is a temp integer which is used to loop to avoid over writtening the assigned character.
for (int i=0;i<temp_int;i++){
random_x_cordinate = rand()% 2; //Random no. b/w 0 to 2
if (random_x_cordinate==1){ // We don't want 1 to be the x cordinate it will lead to losing game
random_x_cordinate++; // It will make the 1 to 2
random_y_cordinate = rand() % 2; //Random no.b/w 1 to 2 for y cordinate
if (random_y_cordinate==1){ // We don't want y be also 1
random_y_cordinate++; // it make 1 to 0 if y = 1
if(board[random_x_cordinate][random_y_cordinate] == ' '){
update_board(random_x_cordinate,random_y_cordinate,COMPUTER); // y == 1, x == 1
break;
}else{
continue;
}
}else{
if(board[random_x_cordinate][random_y_cordinate] == ' '){
update_board(random_x_cordinate,random_y_cordinate,COMPUTER); //// y !=1, x==1
break;
}else{
continue;
}
}
}else{ // x !=1
random_y_cordinate = rand() % 2; // Random no b/w 0 to 2
if (random_y_cordinate==1){ // checking if y==1
random_y_cordinate++;
if(board[random_x_cordinate][random_y_cordinate] == ' '){
update_board(random_x_cordinate,random_y_cordinate,COMPUTER);
break;
}else{
continue;
}
}else{ // x != 1, y != 1
if(board[random_x_cordinate][random_y_cordinate] == ' '){
update_board(random_x_cordinate,random_y_cordinate,COMPUTER); //// y !=1, x==1
break;
}else{
continue;
}
}
}
}
}
void computer_algorithm(){
int temp_array[9] = {0}; // temp array is the array we store the value as same as the index no.
int array[6] = {}; // This is the main array which will remove the zeros from the
int temp_int = 0; // This is temp integer which is used to array for loop to assign the index correctly
for (int i=0;i<3;i++){
for (int j=0;j<3;j++){
if (board[i][j] != ' '){
if (i==0){
temp_array[i+j+1] = i+j+1;
printf("i+J+1 value given = %d\n", temp_array[i+j+1]);
}else if (i==1){
temp_array[i+j+3] = i+j+3;
}else if (i==2){
temp_array[i+j+5] = i+j+5;
}
}
}
}
for (int k=0;k<10;k++){
if (temp_array[k] != 0){
array[temp_int] = temp_array[k];
temp_int ++;
}
}
if (array[1] == 0){
first_computer_move();
}
}
void check_winner(){
for (int column=0;column<3;column++){ // This will check if there is a winner in the row section
if (board[row_one][column]== board[row_two][column] && board[row_one][column]== board[row_three][column]){
if (board[row_one][column]==PLAYER){
printf("OHH YOU WIN THE GAME\n");
printf("HOW IS IT POSSIBLE !");
}else if (board[row_one][column]==COMPUTER){
printf("I TOLD YOU IN THE BEIGNING\n");
printf("I AM THE BEST ALL TIME... go and sleep");
}
}
}
for (int row=0;row<3;row++){ // This will check if there is a winner in the row section
if (board[row][column_one]== board[row][column_two] && board[row][column_one]== board[row][column_three]){
if (board[row][column_one]==PLAYER){
printf("OHH YOU WIN THE GAME\n");
printf("HOW IS IT POSSIBLE !");
}else if (board[row][column_one]==COMPUTER){
printf("I TOLD YOU IN THE BEIGNING\n");
printf("I AM THE BEST ALL TIME... go and sleep");
}
}
}
if(board[0][0]==board[1][1] && board[0][0] == board[2][2]){
if (board[1][1]==PLAYER){
printf("OHH YOU WIN THE GAME\n");
printf("HOW IS IT POSSIBLE !");
}else if (board[1][1]==COMPUTER){
printf("I TOLD YOU IN THE BEIGNING\n");
printf("I AM THE BEST ALL TIME... go and sleep");
}
}else if(board[0][2]==board[1][1] && board[2][0] == board[1][1]){
if (board[1][1]==PLAYER){
printf("OHH YOU WIN THE GAME\n");
printf("HOW IS IT POSSIBLE !");
}else if (board[1][1]==COMPUTER){
printf("I TOLD YOU IN THE BEIGNING\n");
printf("I AM THE BEST ALL TIME... go and sleep");
}
}
}