-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic-tac.c
145 lines (104 loc) · 3.02 KB
/
tic-tac.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
#ifdef _WIN32
#include <conio.h>
#else
#include <stdio.h>
#define clrscr() printf("\e[1;1H\e[2J")
#endif
#include <stdlib.h>
void print_board (char board[3][3]);
int check_won (char board[3][3]);
int main (void)
{
int game_won, count, player, status = 0;
char input = '\0';
char symbol = 'X';
char reserved_nums[10] = {'\0'};
//configure the initial board
char board[3][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
print_board(board);
while (game_won == 0)
{
status = 0;
if(count % 2 == 0)
{
player = 1;
symbol = 'X';
}
else
{
player = 2;
symbol = 'O';
}
printf("player %d, enter a number\n", player);
scanf("%s", &input);
for (int i = 0; i < 10; i++)
if (input == reserved_nums[i])
{
printf("unallowed input, try again\n");
status = 1;
}
if (status > 0)
continue;
reserved_nums[count] = input;
for (int i = 0; i < 3; ++i) //assign the symbol to the chosen corresponding input in array
for (int j =0; j < 3; ++j)
if (input == board[i][j])
board[i][j] = symbol;
print_board(board); // print the modified board
if ((check_won(board)) > 0)
{
printf("we have a winner! congrats player %d!\n\n", player);
game_won = 1;
break;
}
else if ((check_won(board)) < 0)
{
printf("The game is a draw!\n\n");
game_won = 1;
break;
}
++count;
}
return 0;
}
void print_board(char board[3][3]) //prints the current board when invoked
{
clrscr(); //clear the screen
printf("\nplayer 1: X\nPlayer 2: O\n\n");
printf("\n");
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");
return;
}
int check_won(char board[3][3]) //checks the current board for wins when invoked, returns win status, 8 possible win scenarios
{
int status = 0;
if (board[0][0] == board[0][1] && board[0][0] == board[0][2]) //Checks for horizontal three in a row
status = 1;
else if (board[1][0] == board[1][1] && board[1][0] == board[1][2])
status = 1;
else if (board[0][0] == board[1][0] && board[0][0] == board[2][0])
status = 1;
else if (board[0][0] == board[1][0] && board[0][0] == board[2][0]) //checks for vertical three in a row
status = 1;
else if (board[0][1] == board[1][1] && board[0][1] == board[2][1])
status = 1;
else if (board[0][2] == board[1][2] && board[0][2] == board[2][2])
status = 1;
else if (board[0][0] == board[1][1] && board[0][0] == board[2][2]) //checks for diagonal three in a row
status = 1;
else if (board[0][2] == board[1][1] && board[0][2] == board[2][0])
status = 1;
else if (board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' && board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' &&
board[2][0] != '7' && board[2][1] != '8' && board[2][2] != '9')
status = -1;
return status;
}