-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
92 lines (81 loc) · 2.49 KB
/
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
char board[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
int first;
int second;
bool check = false;
int turn = 0;
int userInput(int coordNum);
bool winCondition(char board[3][3]);
int main(int argc, char* argv[]) {
do {
//get coordinates
do {
first = userInput(1);
second = userInput(2);
} while (isalpha(board[second][first]));
if ((turn % 2) == 0) {
board[second][first] = 'X';
} else {
board[second][first] = 'O';
}
printf(" 0, 1, 2\n");
for (int i = 0; i < 3; i++){
printf("%i", i);
for (int j = 0; j < 3; j++) {
printf("[%c]", board[i][j]);
}
printf("\n");
}
if (winCondition(board) == true) {
turn = 9;
}
turn = turn + 1;
} while (turn < 9);
return EXIT_SUCCESS;
}
bool winCondition(char board[3][3]) {
char player = ' ';
for (int i = 0; i < 2; i++ ) {
player = (i == 0 ? 'X' : 'O');
//horizontal conditions
if ((board[0][0] == player) && (board[0][1] == player) && (board[0][2] == player)) {
printf("%c has won!\n", player);
return true;
} else if ((board[1][0] == player) && (board[1][1] == player) && (board[1][2] == player)){
printf("%c has won!\n", player);
return true;
} else if ((board[2][0] == player) && (board[2][1] == player) && (board[2][2] == player)){
printf("%c has won!\n", player);
return true;
//vertical conditions
} else if ((board[0][0] == player) && (board[1][0] == player) && (board[2][0] == player)){
printf("%c has won!\n", player);
return true;
} else if ((board[0][1] == player) && (board[1][1] == player) && (board[2][1] == player)){
printf("%c has won!\n", player);
return true;
} else if ((board[0][2] == player) && (board[1][2] == player) && (board[2][2] == player)){
printf("%c has won!\n", player);
return true;
//diagonal conditions
} else if ((board[0][0] == player) && (board[1][1] == player) && (board[2][2] == player)){
printf("%c has won!\n", player);
return true;
} else if ((board[0][2] == player) && (board[1][1] == player) && (board[2][0] == player)){
printf("%c has won!\n", player);
return true;
}
}
return false;
}
int userInput(int coordNum) {
int input = -1;
do {
printf("please enter coordinate %i: ", coordNum);
scanf("%i", &input);
} while ((input < 0) || (input > 2));
return input;
}