-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticTacToe.c
More file actions
145 lines (127 loc) · 3.59 KB
/
ticTacToe.c
File metadata and controls
145 lines (127 loc) · 3.59 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
//
// C program that runs a tic tac toe game with 2 players
//
#include <stdio.h>
#include <stdbool.h>
// Prints the game board
void printBoard(int board[], int playerA, int playerB){
for(int i = 0; i < 9; i++){
// If the value at index i is 10, that means it is player A's spot
if(board[i] == playerA){
printf(" A");
} else if (board[i] == playerB){
// If the value at index i is 11, that means it is player B's spot
printf(" B");
} else {
printf(" %d", i + 1);
}
// New line of board
if(i % 3 == 2){
printf("\n");
}
}
}
// Ensures that each position is not claimed twice
int requestValidInput(int board[], int playerA, int playerB){
int index;
scanf("%d", &index);
while(true){
if(index > 0 && index < 10){
// User input is one more than array index so we need to subtract it
index = index - 1;
// If index is not 0, that means it is claimed
if(board[index] != 0){
printf("That position has already been played, please try again.\n");
scanf("%d", &index);
} else {
return index;
}
} else {
// User entered invalid position
printf("Invalid input, please try again.\n");
scanf("%d", &index);
}
}
}
// Functions checks every possibly win (3 in a row) for either player and returns the winner
int checkForWinner(int board[], int playerA, int playerB){
for(int i = 0; i < 3; i++){
if(playerA == board[i + 0] && playerA == board[i + 3] && playerA == board[i + 6]){
return playerA;
}
if(playerB == board[i + 0] && playerB == board[i + 3] && playerB == board[i + 6]){
return playerB;
}
if(playerA == board[i * 3 + 0] && playerA == board[i * 3 + 1] && playerA == board[i * 3 + 2]){
return playerA;
}
if(playerB == board[i * 3 + 0] && playerB == board[i * 3 + 1] && playerB == board[i * 3 + 2]){
return playerB;
}
}
if(playerA == board[0] && playerA == board[4] && playerA == board[8]){
return playerA;
}
if(playerB == board[0] && playerB == board[4] && playerB == board[8]){
return playerB;
}
if(playerA == board[2] && playerA == board[4] && playerA == board[6]){
return playerA;
}
if (playerB == board[2] && playerB == board[4] && playerB == board[6]){
return playerB;
}
return 0;
}
// Checks if every position has been filled
bool checkforStalemate(int board[]){
for(int index = 0; index < 9; index++){
// 0 means no one has claimed that position
if(board[index] == 0){
return false;
}
}
return true;
}
int main(void){
int board[9], i, validIndex, winner;
const int playerA = 10;
const int playerB = 11;
char turn = 'A';
bool gameOver = false;
// Initial entire board to 0
for(i = 0; i < 9; i++){
board[i] = 0;
}
while(gameOver == false){
printf("The current state of the Tic-tac-toe Board:\n\n");
printBoard(board, playerA, playerB);
printf("\nIt is Player %c’s turn.\nPlease enter a valid position to play.\n", turn);
validIndex = requestValidInput(board, playerA, playerB);
if(turn == 'A'){
board[validIndex] = playerA;
turn = 'B';
} else {
board[validIndex] = playerB;
turn = 'A';
}
winner = checkForWinner(board, playerA, playerB);
if(winner != 0){
gameOver = true;
// Checks if every position has been filled, if it has that means someone has won
} else if(checkforStalemate(board))
gameOver = true;
}
// Checks for winner and displays the final board
if(playerA == winner){
printf("Player A wins!\n\n");
printBoard(board, playerA, playerB);
} else if(playerB == winner){
printf("Player B wins!\n\n");
printBoard(board, playerA, playerB);
} else {
printf("It’s a draw!\n\n");
printBoard(board, playerA, playerB);
}
return 0;
}