-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.c
More file actions
191 lines (148 loc) · 5.99 KB
/
Copy pathTicTacToe.c
File metadata and controls
191 lines (148 loc) · 5.99 KB
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
/* includes */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
/* global variables */
char board[3][3]; //tic-tac-toe board
const char PLAYER = 'X'; //player's symbol
const char COMPUTER = 'O'; //computer's symbol
/* functions */
void resetBoard();
void printBoard();
int checkFreeSpaces();
void playerMove();
void computerMove();
char checkWinner();
void printWinner(char);
int main(int argc, char** argv) {
/* holds the winner ('X' for player, 'O' for computer, ' ' for tie or ongoing game) */
char winner = ' ';
char response; //response for replaying the game
do
{
winner = ' '; //resets winner for each new game
resetBoard(); //resets the game board at the start of each game
/* Main game loop: continues until there's a winner or no free spaces left */
while (winner == ' ' && checkFreeSpaces() != 0) {
printBoard(); //displays the current game board
playerMove(); //lets the player make a move
winner = checkWinner(); //checks if the player won
if (winner != ' ' || checkFreeSpaces() == 0) {
break; //exits loop if there's a winner or no free spaces left
}
computerMove(); //lets the computer make a move
winner = checkWinner(); //checks if the computer won
if (winner != ' ' || checkFreeSpaces() == 0) {
break; //exits loop if there's a winner or no free spaces left
}
}
printBoard(); //prints the final board after the game ends
printWinner(winner); //prints the winner of the game
/* asks the player if they want to play again */
printf("Would you like to play again? (Y/N): ");
/* reads the user's response, with leading space to clear any leftover newline character */
scanf(" %c", &response);
response = toupper(response); //converts the response to uppercase for consistency
} while (response == 'Y'); //repeats the game loop if the user enters 'Y' (yes)
printf("Thanks for playing!\n"); //prints thank you message when exiting
return 0; //end of program
}
/* Function to reset the board to its initial empty state */
void resetBoard() {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
board[i][j] = ' '; //initializes all positions to empty space
}
}
}
/* Function to display the current state of the game board */
void printBoard() {
printf(" %c | %c | %c ", board[0][0], board[0][1], board[0][2]);
printf("\n---|---|---\n");
printf(" %c | %c | %c ", board[1][0], board[1][1], board[1][2]);
printf("\n---|---|---\n");
printf(" %c | %c | %c ", board[2][0], board[2][1], board[2][2]);
printf("\n");
}
/* Function to check the number of free spaces left on the board */
int checkFreeSpaces() {
int freeSpaces = 9; //total spaces on the board (3x3)
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(board[i][j] != ' ') { //if the space is already filled
freeSpaces--; //decreases the count of free spaces
}
}
}
return freeSpaces; //returns the number of free spaces
}
/* Function to handle the player's move */
void playerMove() {
int x, y; //row and column input for the player's move
/* Loop until the player makes a valid move */
do {
printf("Enter row #(1-3): ");
scanf("%d", &x); //reads row number (1-3)
x--; //adjusts for zero-indexed array
printf("Enter column #(1-3): ");
scanf("%d", &y); //reads row number (1-3)
y--; //adjusts for zero-indexed array
/* checks if the selected spot is free */
if (board[x][y] != ' ') {
printf("Invalid move!\n"); //informs the player
} else {
board[x][y] = PLAYER; //places the player's mark on the board
break; //exits the loop after a valid move
}
} while (board[x][y] != ' '); //repeats until a valid move is made
}
/* Function to handle the computer's move */
void computerMove() {
/* random number generator seed with the current time */
srand(time(NULL));
int x, y; //row and column for the computer's move
/* makes a move only if there are free spaces available */
if (checkFreeSpaces() > 0) {
do {
x = rand() % 3; //random row (0-2)
y = rand() % 3; //random column (0-2)
} while(board[x][y] != ' '); //ensures the selected spot is free
board[x][y] = COMPUTER; //places the computer's mark on the board
} else {
printWinner(' '); //if no spaces are left, it's a tie
}
}
/* Function to check if there's a winner */
char checkWinner() {
/* checks rows for a winner */
for (int i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][0] == board[i][2]) {
return board[i][0]; //returns the winner ('X' or 'O')
}
}
/* checks columns for a winner */
for (int i = 0; i < 3; i++) {
if (board[0][i] == board[1][i] && board[0][i] == board[2][i]) {
return board[0][i]; //returns the winner ('X' or 'O')
}
}
/* checks diagonals for a winner */
if (board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
return board[0][0]; //returns the winner ('X' or 'O')
}
if (board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
return board[0][2]; //returns the winner ('X' or 'O')
}
return ' '; //returns ' ' if there is no winner yet
}
/* Function to print the result of the game */
void printWinner(char winner) {
if (winner == PLAYER) {
printf("YOU WIN!\n"); //player wins
} else if (winner == COMPUTER) {
printf("YOU LOSE!\n"); //computer wins
} else {
printf("IT'S A TIE!\n"); //tie
}
}