-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic Tac Toe game.c
161 lines (140 loc) · 3.64 KB
/
Tic Tac Toe game.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
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
char board[3][3];
const char PLAYER = 'X';
const char COMPUTER = 'O';
void resetBoard();
void printBoard();
int checkFreeSpaces();
void playerMove();
void computerMove();
char checkWinner();
void printWinner(char);
int main(){
printf("\n\n");
printf("****************************************************\n");
printf(" Welcome to the Tic Tac Toe game! \n");
printf("****************************************************\n");
printf("\n* Instructions \n\n");
printf("\tPlayer 1 sign = X\n");
printf("\tPlayer 2 sign = O\n\n");
char winner = ' ';
resetBoard();
while(winner == ' ' && checkFreeSpaces() != 0)
{
printBoard();
playerMove();
winner = checkWinner();
if(winner != ' ' || checkFreeSpaces() == 0){
break;
}
computerMove();
winner = checkWinner();
if(winner != ' ' || checkFreeSpaces() == 0){
break;
}
}
printBoard();
printWinner(winner);
return 0;
}
void resetBoard(){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
board[i][j] = ' ';
}
}
}
void printBoard(){
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]);
printf("\n\n");
}
int checkFreeSpaces(){
int freeSpaces = 9;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(board[i][j] != ' '){
freeSpaces--;
}
}
}
return freeSpaces;
}
void playerMove(){
int x;
int y;
do{
printf("Enter row #(1-3): ");
scanf("%d", &x);
x--;
printf("Enter column #(1-3): ");
scanf("%d", &y);
y--;
printf("\n");
if(board[x][y] !=' '){
printf("Invalid move! Try again!\n");
printf("\n");
}
else{
board[x][y] = PLAYER;
break;
}
} while(board[x][y] != ' ');
}
void computerMove(){
srand(time(NULL));
int x;
int y;
if(checkFreeSpaces() > 0){
do{
x = rand() % 3;
y = rand() % 3;
} while(board[x][y] != ' ');
board[x][y] = COMPUTER;
}
}
char checkWinner(){
//check rows
for(int i = 0; i < 3; i++){
if(board[i][0] == board[i][1] && board[i][0] == board[i][2]){
return board[i][0];
}
}
//check columns
for(int i = 0; i < 3; i++){
if(board[0][i] == board[1][i] && board[0][i] == board[2][i]){
return board[0][i];
}
}
//check diagonals
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[0][2] == board[2][0]){
return board[0][2];
}
return ' ';
}
void printWinner(char winner) {
if(winner == PLAYER){
printf("\n\t --- Game Over --- \n");
printf(" *** Congratulations, You won!! ***\n");
printf(":: Thanks for playing Tic Tac Toe game! :: \n");
}
else if(winner == COMPUTER){
printf("\n\t --- Game Over --- \n");
printf(" *** Sorry, you lost. Better luck next time! ***\n");
printf(":: Thanks for playing Tic Tac Toe game! :: \n");
}
else{
printf("\n\t --- Game Over --- \n");
printf(" *** It's a tie! ***\n");
printf(":: Thanks for playing Tic Tac Toe game! :: \n");
}
}