-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.c
186 lines (168 loc) · 4.34 KB
/
core.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
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
#include "core.h"
#include "io.h"
#include "helpers.h"
#include "common.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#ifdef _WIN32
#include <windows.h>
#endif /* ifdef _WIN32 */
#ifdef __unix__
#include <unistd.h>
#endif /* ifdef __unix__ */
void clrscr(){
// system("clear");
printf("\e[1;1H\e[2J");
}
int row(int position){
int mod = position % 3;
int row = 2 - (position / 3 + (mod == 0 ? -1 : 0));
return row;
}
int col(int position){
int mod = position % 3;
int col = mod == 0 ? 2 : mod - 1;
return col;
}
int position(int row, int col){
return (2 - row) * 3 + 1 + col;
}
void init(char table[3][3]){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
table[i][j] = PLACEHOLDER;
}
}
}
void print(const char table[3][3]) {
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
printf("%c", table[i][j]);
if (j < 2) {
printf("|");
}
}
printf("\n");
}
}
void highlight(const char table[3][3], size_t len, int positions[]){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (contains(len, positions, position(i, j))){
printf(BOLD""BLUE"%c"EXIT, table[i][j]);
} else {
printf("%c", table[i][j]);
}
if (j < 2){
printf("|");
}
}
printf("\n");
}
}
void setsqr(char table[3][3], int position, char player){
table[row(position)][col(position)] = player;
}
bool valid(char table[3][3], int position){
if (position <= 9 && position >= 1 && table[row(position)][col(position)] == PLACEHOLDER){
return true;
}
printf(RED""BOLD"invalid position: %d\n"EXIT, position);
sleep(1);
return false;
}
bool hasFilledRow(char table[3][3], char player){
bool found;
int positions[3];
for (int i = 0; i < 3; i++){
found = true;
for (int j = 0; j < 3; j++){
if (table[i][j] != player){
found = false; break;
}
positions[j] = position(i, j);
}
if (found){
clrscr(); highlight(table, 3, positions);
break;
}
}
return found;
}
bool hasFilledCol(char table[3][3], char player){
bool found;
int positions[3];
for (int j = 0; j < 3; j++){
found = true;
for (int i = 0; i < 3; i++){
if (table[i][j] != player){
found = false; break;;
}
positions[i] = position(i, j);
}
if (found){
clrscr(); highlight(table, 3, positions);
break;
}
}
return found;
}
bool hasFilledDiag(char table[3][3], char player){
bool found = true;
int positions[3];
// diagonal 7-5-3
for (int i = 0; i < 3; i++){
if (table[i][i] != player){
found = false; break;
}
positions[i] = position(i, i);
}
if (found){
clrscr(); highlight(table, 3, positions);
return true;
}
// diagonal 1-5-9
for (int i = 0; i < 3; i++){
if (table[i][2 - i] != player){
return false;
}
positions[i] = position(i, 2 - i);
}
clrscr(); highlight(table, 3, positions);
return true;
}
bool wins(char table[3][3], char player){
return hasFilledRow(table, player) || hasFilledCol(table, player) || hasFilledDiag(table, player);
}
bool tie(char table[3][3]){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (table[i][j] == PLACEHOLDER){
return false;
}
}
}
return true;
}
void startPvP(char table[3][3]){
init(table);
char player = 'x';
int position = -1;
while (true){
clrscr();
print(table);
printf("%s%c"EXIT" chooses: ", player == 'x' ? LIGHT_RED: GREEN, player); position = readint("");
if (!valid(table, position)){
continue;
}
setsqr(table, position, player);
if (wins(table, player)){
printf(BOLD"player ""%s%c"EXIT""BOLD" wins.\n"EXIT, player == 'x' ? LIGHT_RED : GREEN, player); break;
}
if (tie(table)){
clrscr(); printf(BOLD""GRAY"tie.\n"EXIT); break;
}
player = player == 'x' ? 'o': 'x';
}
}