-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic Tac Toe.c
180 lines (105 loc) · 3.71 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
#include <stdio.h>
#include <stdlib.h>
/*
Name: Jordan Chen
Date: 28/04/2023
UBC E-Mail: [email protected]
When run, the program simulates a classic game of a Two player Tic Tac Toe game on a 3 x 3 dimensioned board.
*/
char gameBoard[10] = {'o','1','2','3','4','5','6','7','8','9'}; // The game board
int checkWin();
void playerTurn(int, int, char);
void boardVisual();
// Main function
int main(){
int player = 1, winIndicator = -1 , choice;
char mark; // Either X or O
while (winIndicator == -1) {
boardVisual();
if (player % 2 != 0) {
player = 1;
mark = 'X';
}
else {
player = 2;
mark = 'O';
}
printf("Player %d, Please enter your choice : ",player);
scanf("%d",&choice);
playerTurn(choice, player, mark);
winIndicator = checkWin();
player++;
}
boardVisual();
if (winIndicator==1) {
printf("Congratulations! Player %d won",--player);
}
else {
printf("The game was a draw!");
}
getch();
return 0;
}
// Function which after each turn, checks whether or not a player has won
int checkWin(){
if(gameBoard[1] == gameBoard[2] && gameBoard[2] == gameBoard[3])
return 1;
else if (gameBoard[4] == gameBoard[5] && gameBoard[5] == gameBoard[6])
return 1;
else if(gameBoard[7] == gameBoard[8] && gameBoard[8] == gameBoard[9])
return 1;
else if(gameBoard[1] == gameBoard[4] && gameBoard[4] == gameBoard[7])
return 1;
else if(gameBoard[2] == gameBoard[5] && gameBoard[5] == gameBoard[8])
return 1;
else if(gameBoard[3] == gameBoard[6] && gameBoard[6] == gameBoard[9])
return 1;
else if(gameBoard[1] == gameBoard[5] && gameBoard[5] == gameBoard[9])
return 1;
else if(gameBoard[3] == gameBoard[5] && gameBoard[5] == gameBoard[7])
return 1;
else if(gameBoard[1] != '1' && gameBoard[2] != '2' && gameBoard[3] != '3' && gameBoard[4] !='4' && gameBoard[5] != '5' && gameBoard[6] != '6' && gameBoard[7] != '7' && gameBoard[8] != '8' && gameBoard[9] != '9')
return 0;
else
return -1;
}
// Function which makes the visual changes on the board corresponding to the players choice
void playerTurn(int choice, int player, char mark){
if(choice == 1 && gameBoard[1] == '1')
gameBoard[1] = mark;
else if(choice == 2 && gameBoard[2] == '2')
gameBoard[2] = mark;
else if(choice == 3 && gameBoard[3] == '3')
gameBoard[3] = mark;
else if(choice == 4 && gameBoard[4] == '4')
gameBoard[4] = mark;
else if(choice == 5 && gameBoard[5] == '5')
gameBoard[5] = mark;
else if(choice == 6 && gameBoard[6] == '6')
gameBoard[6] = mark;
else if(choice == 7 && gameBoard[7] == '7')
gameBoard[7] = mark;
else if(choice == 8 && gameBoard[8] == '8')
gameBoard[8] = mark;
else if(choice == 9 && gameBoard[9] == '9')
gameBoard[9] = mark;
else {
printf("That option was Invalid! Your turn is skipped.");
player--;
getch();
}
}
// Function which draws the visual board itself
void boardVisual(){
printf("\n\n\t Welcome to Tic Tac Toe! \n\n");
printf("Player1 is (X) - Player2 is (O) \n\n\n");
printf(" | | \n");
printf(" %c | %c | %c \n",gameBoard[1],gameBoard[2],gameBoard[3]);
printf("____|_____|____\n");
printf(" | | \n");
printf(" %c | %c | %c \n",gameBoard[4],gameBoard[5],gameBoard[6]);
printf("____|_____|____\n");
printf(" | | \n");
printf(" %c | %c | %c \n",gameBoard[7],gameBoard[8],gameBoard[9]);
printf(" | | \n");
}