-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
134 lines (110 loc) · 3.15 KB
/
main.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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
///////////////////////////////////////////////////////////////////////////////
// BOARD //
///////////////////////////////////////////////////////////////////////////////
char board[9] = {
'1', '2', '3',
'4', '5', '6',
'7', '8', '9'
};
bool is_board_full()
{
for (int i = 0; i < 9; i++)
if (board[i] == i + '1') return false;
return true;
}
void print_board()
{
printf("%c %c %c\n", board[6], board[7], board[8]);
printf("%c %c %c\n", board[3], board[4], board[5]);
printf("%c %c %c\n", board[0], board[1], board[2]);
}
char has_someone_won()
{
if (board[0] == board[3] && board[3] == board[6])
return board[0];
if (board[1] == board[4] && board[4] == board[7])
return board[1];
if (board[2] == board[5] && board[5] == board[8])
return board[2];
if (board[0] == board[1] && board[1] == board[2])
return board[0];
if (board[3] == board[4] && board[4] == board[5])
return board[3];
if (board[6] == board[7] && board[7] == board[8])
return board[6];
if (board[0] == board[4] && board[4] == board[8])
return board[0];
if (board[2] == board[4] && board[4] == board[6])
return board[2];
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// PLAYER STUFF //
///////////////////////////////////////////////////////////////////////////////
#define PLAYER_1 'X'
#define PLAYER_2 'O'
char winner = 0;
int play(char player)
{
char c = 0;
printf("Player %c, it is your turn: ", player);
scanf("%c", &c);
getchar();
if (c > '9' || c < '1')
return 0;
if (board[c - '1'] != c)
return 0;
board[c - '1'] = player;
return 1;
}
static inline
void clear_screen()
{
printf(
"\x1b[2J" // clear screen
"\x1b[H" // moves cursor to (0, 0)
);
fflush(stdout);
}
int main()
{
// setup terminal window //
#ifdef _WIN32
system(""); // We gotta do this to make escape codes work in windows
#endif
printf(
"\x1b[?1049h" // allow saving more information on current state of terminal
"\x1b[?47h" // save screen
"\x1b[s" // save cursor
);
clear_screen();
// execution //
bool running = !is_board_full(board) && !(winner = has_someone_won(board));
char player = PLAYER_1;
while (running)
{
clear_screen();
print_board(board);
while (running && !play(player));
running = !is_board_full(board) && !(winner = has_someone_won(board));
player = player == PLAYER_1 ? PLAYER_2 : PLAYER_1;
}
clear_screen();
print_board(board);
if (winner == 0)
printf("Well... Looks like we got no winner.\n");
else
printf("Congratulations, player %c. You won!\n", winner);
printf("Press any key to quit... ");
getchar(); // game doesn't disappear out of the blue
printf(
"\x1b[u" // restore cursor
"\x1b[?47l" // restore screen
"\x1b[?1049l" // we don't need to store extra information anymore
);
fflush(stdout);
return 0;
}