-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.cpp
215 lines (180 loc) · 4.39 KB
/
TicTacToe.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// TicTacToe.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
// by 3n3a
#include <cstdlib>
#include <cstdio>
#include <windows.h>
#define DEBUG false
// 1 = 'O'
// 2 = 'X'
//TODO: overwriting still possible
void display_winner(int winner, int arr[3][3]);
char num_to_tic(int num)
{
switch (num)
{
case 0:
return ' ';
case 1:
return 'O';
case 2:
return 'X';
default:
return ' ';
}
}
void print_playing_field(int arr[3][3])
{
for (int i = 0; i < 3; ++i)
{
const char one = num_to_tic(arr[i][0]);
const char two = num_to_tic(arr[i][1]);
const char three = num_to_tic(arr[i][2]);
printf(" %c | %c | %c \n", one, two, three);
if (i != 2) printf("---|---|---\n");
}
}
void print_debug_array(int arr[3][3])
{
printf("\nDisplaying values:\n");
for (int i = 0; i < 3; ++i)
{
printf("[%i][%i][%i]\n", arr[i][0], arr[i][1], arr[i][2]);
}
}
int set_state_for_pos(int arr[3][3], const int position, const int state)
{
const int row = (position - 1) / 3;
const int column = (position - 1) % 3;
if (arr[row][column] == 0) {
arr[row][column] = state;
return 0;
}
return 1;
}
void init()
{
printf("\n\n\n\t\t---- TIC TAC TOE ----\n\t\t\tby 3n3a\n\n\n");
Sleep(2000);
}
void count_x_o(int number, int sum_arr[3][2], int elem_sum_arr)
{
if (number> 0)
{
switch (number)
{
case 1:
sum_arr[elem_sum_arr][0]++;
break;
case 2:
sum_arr[elem_sum_arr][1]++;
break;
}
}
}
int who_wins(int arr[3][3])
{
//Calculation
// [no 1/o][no 2/x]
int sum_rows[3][2] = { {0, 0}, {0, 0}, {0, 0} };
int sum_columns[3][2] = { {0, 0}, {0, 0}, {0, 0} };
int sum_diags[2][2] = { {0, 0}, {0, 0} };
for (int j = 0; j < 3; ++j)
{
for (int i = 0; i < 3; ++i)
{
// rows
count_x_o(arr[j][i], sum_rows, j);
// columns
count_x_o(arr[i][j], sum_columns, j);
}
// diagonals
count_x_o(arr[j][j], sum_diags, 0);
count_x_o(arr[j][2 - j], sum_diags, 1);
}
// Checking
if (DEBUG)
{
printf("\nDisplaying values:\n");
for (int i = 0; i < 3; ++i)
{
printf("C%i\tSums: [o: %i | x: %i]\n", i, sum_columns[i][0], sum_columns[i][1]);
}
for (int i = 0; i < 3; ++i)
{
printf("R%i\tSums: [o: %i | x: %i]\n", i, sum_rows[i][0], sum_rows[i][1]);
}
for (int i = 0; i < 2; ++i)
{
printf("D%i\tSums: [o: %i | x: %i]\n", i, sum_diags[i][0], sum_diags[i][1]);
}
}
if (sum_rows[0][0] == 3) return 1;
if (sum_rows[0][1] == 3) return 2;
if (sum_columns[0][0] == 3) return 1;
if (sum_columns[0][1] == 3) return 2;
if (sum_diags[0][0] == 3) return 1;
if (sum_diags[0][1] == 3) return 2;
return 0;
}
int main() {
int tic_tac_array[3][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
int player = 1;
int positions_filled = 0;
init();
// Game Loop
do
{
if (!DEBUG) system("cls");
if (DEBUG) print_debug_array(tic_tac_array);
printf("PLAYER %i - %c\n", player, player == 1 ? 'O' : 'X');
print_playing_field(tic_tac_array);
int chosen_position;
printf("Enter position [1-9]: ");
scanf_s("%i", &chosen_position);
printf("\n");
set_state_for_pos(tic_tac_array, chosen_position, player);
//if () == 1) goto continuation;
positions_filled++;
// Swap Player after every round
if (player == 1)
{
player = 2;
}
else if (player == 2)
{
player = 1;
}
const int winner = who_wins(tic_tac_array);
if (positions_filled == 9)
{
switch (winner)
{
case 0:
printf("No one wins. Draw\n");
exit(EXIT_SUCCESS);
case 2:
display_winner(winner, tic_tac_array);
case 1:
display_winner(winner, tic_tac_array);
default:
exit(EXIT_FAILURE);
}
}
switch(winner)
{
case 1:
display_winner(winner, tic_tac_array);
case 2:
display_winner(winner, tic_tac_array);
}
//continuation:;
} while (true);
return 0;
}
void display_winner(int winner, int arr[3][3])
{
system("cls");
print_playing_field(arr);
printf("Player %i Wins!!\n", winner);
exit(EXIT_SUCCESS);
}