Last active
April 6, 2021 14:15
-
-
Save yogggoy/d34c5b1eb6a8054ec05895ab3f282819 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// just X_O game, writed for fun | |
// g++ -std=gnu++11 just_play.cpp; ./a.out | |
#include <iostream> | |
#include <string> | |
#define BB 4 | |
// 0 - " " | |
// 1 - "X" | |
// 2 - "0" | |
struct Table { | |
int b = BB; | |
int a[BB][BB]; | |
int symbhol = 1; | |
}; | |
std::string w(int a) { | |
return (a == 0 ? " " : (a==1) ? "X" : "0"); | |
} | |
void draw(const Table& t) { | |
for (int i=0; i<t.b; i++) { | |
for (int j=0; j<t.b; j++) | |
std::cout << "|" << w(t.a[i][j]); | |
std::cout << "|" << "\n"; | |
} | |
for (int i=0; i<t.b; i++) | |
std::cout << "--"; | |
std::cout << "-\n"; | |
} | |
void clear(Table& t) { | |
for (int i=0; i<t.b; i++) | |
for (int j=0; j<t.b; j++) | |
t.a[i][j] = 0; | |
} | |
int step(Table& t) { | |
// 0 - ok | |
// 1 - out of range | |
// 2 - not empty | |
int x,y; | |
std::cin >> x >> y; | |
if ((x < 1 or x > t.b) or (y < 1 or y > t.b)) | |
return 1; | |
x--; | |
y--; | |
if (t.a[x][y] != 0) | |
return 2; | |
t.a[x][y] = t.symbhol; | |
t.symbhol ^= 3; | |
return 0; | |
} | |
int diagonally_lr(const Table& t) { | |
// diagonale test up_left -> down_right | |
int result = t.a[0][0]; | |
if (result) | |
for (int i=0; i<t.b; i++) | |
if (t.a[i][i] != result) { | |
result = 0; | |
break; | |
} | |
return result; | |
} | |
int diagonally_rl(const Table& t) { | |
// diagonale test up_right -> down_left | |
int z = t.b-1; | |
int result = t.a[0][z]; // last elem | |
if (result) | |
for (int i=0; i<t.b;i++) | |
if (t.a[i][z-i] != result) { | |
result = 0; | |
break; | |
} | |
return result; | |
} | |
int vertical(const Table& t) { | |
int result; | |
for (int j=0; j<t.b; j++) { | |
result = t.a[0][j]; | |
if (result) | |
for (int i=0; i<t.b; i++) | |
if (t.a[i][j] != result) { | |
result = 0; | |
break; | |
} | |
if (result) | |
return result; | |
} | |
return result; | |
} | |
int horizontal(const Table& t) { | |
int result; | |
for (int j=0; j<t.b; j++) { | |
result = t.a[j][0]; | |
if (result) | |
for (int i=0; i<t.b; i++) | |
if (t.a[j][i] != result) { | |
result = 0; | |
break; | |
} | |
if (result) | |
return result; | |
} | |
return result; | |
} | |
int full_field(const Table& t) { | |
// field is full but no winner | |
for (int i=0; i<t.b;i++) | |
for (int j=0; j<t.b; j++) | |
if (t.a[i][j] == 0) { | |
return 0; | |
} | |
return 3; | |
} | |
int check(const Table& t) { | |
// 0 - next | |
// 1 - "X" | |
// 2 - "0" | |
// 3 - end game. field is full but no one win | |
int result; | |
if (result = diagonally_lr(t)) | |
return result; | |
if (result = diagonally_rl(t)) | |
return result; | |
if (result = vertical(t)) | |
return result; | |
if (result = horizontal(t)) | |
return result; | |
if (result = full_field(t)) | |
return result; | |
return 0; | |
} | |
int main() { | |
Table t; | |
clear(t); | |
std::cout << "first step [X]" << "\n"; | |
draw(t); | |
int step_res = 0; | |
int check_res = 0; | |
int in_game = 1; | |
while (in_game) { | |
if (step_res = step(t)) { | |
std::cout << "Wrong numbers ["; | |
std::cout << ((step_res == 1) ? "out of range" : "not empty") << "]\n"; | |
} | |
draw(t); | |
check_res = check(t); | |
switch (check_res) { | |
case 0: | |
break; | |
case 3: | |
std::cout << "try again " << "\n"; | |
clear(t); | |
draw(t); | |
break; | |
default: | |
std::cout << "WIN " << ((check_res==1) ? "X" : "0") << "\n"; | |
in_game = 0; | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment