From 42c857f2243f7c6d623fb47461220ed7b6740915 Mon Sep 17 00:00:00 2001 From: mnjx Date: Thu, 2 Mar 2023 09:36:48 -1000 Subject: [PATCH] Initial commit with a WIP C version and a completed Python version --- piskvorky | Bin 0 -> 16256 bytes piskvorky.c | 116 ++++++++++++++++++++++++++++++++++++++++++++ tictactoe-v2.py | 124 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 240 insertions(+) create mode 100755 piskvorky create mode 100644 piskvorky.c create mode 100644 tictactoe-v2.py diff --git a/piskvorky b/piskvorky new file mode 100755 index 0000000000000000000000000000000000000000..701adb4b04561f9a90416e37f3e6577b0b6c53f7 GIT binary patch literal 16256 zcmeHOeQaCR6~A_xHifoMOAGy?d1Z98gSbgQ!$(*5!|e-eOIBJ|*vhM8JB_JhXZCY( z5M^OmC!(x~N!wZ`_JO7eX-E@dsA7X{p=B*pV@o$_>ZCD62)0CJTUZMUrPTYK`_6G* z-t4jsX@B`fe&?Q#-?`_Wd*Aha@w;CNZ`tJc`IM9@^=U<1cda7{v7&!9S3p9lNlnGI zPAyOq!B5aKM;~$sv`#tWA5UvoUIj|H*;FY(=R0C0R3aowxN>EX0zwh0xF3Y8CI$0e z`s5^1DkW6(#r6!XR?0)aMg}NC-8c$mpkAxhz?$)N59g(wso;nu+#ury8CRs--XK%S zC*_1bO`K0JHK3zn=&Mp*+%Cp>=|;8}N^MZN^DR5}zqHpPI77QpO&%)k2$l9e3mgxv z1N?}q4D)twuiQ8^aQ=mITGi5IqIK1ZrO8-*GLg#V>+?;k>Q}7@X4AoCx@C%s+MqeL zdD~9q?^gXJ6Fk*t;zMaAef!TdN8U1yJ^keu!t;N)>~&Z=xz){b{5%fe_yt*jl%*j7g*k%ArAg($yiIm+2 zX(F4B-h8u_jYd*!ia<1?tJLN#&FeQ<%Yv(mwPnGT#rpCU%4*)R)r!S4@x2MOnAx#) zLo%I;?})S}9d<%>g(UgW~}uXW@!&NK(4yyB9(=jDJ) z?#xREamFQ=Hb^t*l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-eB5}0 zaY9?%#;CKpNT2fHJJ~>}#4B!u7y66`-_3ZTS5x9$McNn9QB>UV%X6Gi+F!eicmBT! zCE;cKl)=_aS2HWbi+>pkH@@6?&T{zQdGOtaV<~hBId>Jwn~45|3f^Va_YleCJe-{O z^x6wAbO@M$3orZrGxg+cY`!_%7%%wqP|)1tWvjR!FP`7!_+=OcC*$}uuw){5IfvK3 z6hf564k=y*FZ4rb+<18|9%6h`5$RL}Cv+mfZoCn4?2HD7N}LWi)_>t?e8`u?FZ;;` z#!EY5@0~x>xqUVlGbQ-CqQt3CtW?~zICmYDzP3pF+~Bp4&cH|Fk@c7E4~bm^`Lb(; bQ`zisF71jfZU0N*zeiteD04jk53BwKWN^u) literal 0 HcmV?d00001 diff --git a/piskvorky.c b/piskvorky.c new file mode 100644 index 0000000..39e32ed --- /dev/null +++ b/piskvorky.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include + + +#if defined(_WIN32) + #define PLATFORM 0 +#elif defined(_WIN64) + #define PLATFORM 0 +#elif defined(__CYGWIN__) + #define PLATFORM 0 +#elif defined(__linux__) + #define PLATFORM 1 +#else + #define PLATFORM NULL +#endif + +int kdoHraje = 0; + +int vypisHru(char pole[][3], int vyherce) { + if (PLATFORM == 0) { + system("cls"); + } else if (PLATFORM == 1) { + system("clear"); + } + + if (vyherce == 0) printf("Vyhralo o!\n"); + else if (vyherce == 1) printf("Vyhralo x!\n"); + else if (vyherce == 2) printf("Remiza!\n"); + + for (int i = 0; i < 3; i++) { + printf("[%c] ", pole[i][0]); + printf("[%c] ", pole[i][1]); + printf("[%c]\n", pole[i][2]); + } + + if (vyherce == 0 || vyherce == 1 || vyherce == 2) return 0; +} + +int checkniVyhru(char pole[][3]) { + // diagonalni check + if (pole[0][0] == 'o' && pole[1][1] == 'o' && pole[2][2] == 'o' || pole[0][2] == 'o' && pole[1][1] == 'o' && pole[2][0] == 'o') { + return 0; + } + + if (pole[0][0] == 'x' && pole[1][1] == 'x' && pole[2][2] == 'x' || pole[0][2] == 'x' && pole[1][1] == 'x' && pole[2][0] == 'x') { + return 1; + } + + // horizontalni check + for (int i = 0; i < 3; i++) { + if (pole[i][0] == 'o' && pole[i][1] == 'o' && pole[i][2] == 'o') { + return 0; + } + + if (pole[i][0] == 'x' && pole[i][1] == 'x' && pole[i][2] == 'x') { + return 1; + } + } + + // vertikalni check + for (int i = 0; i < 3; i++) { + if (pole[0][i] == 'o' && pole[1][i] == 'o' && pole[2][i] == 'o') { + return 0; + } + + if (pole[0][i] == 'x' && pole[1][i] == 'x' && pole[2][i] == 'x') { + return 1; + } + } + + // check na remizu + int zabrane = 0; + for (int i = 0; i < 3; i++) { + if (pole[i][0] == 'o' || pole[i][0] == 'x') zabrane++; + if (pole[i][1] == 'o' || pole[i][1] == 'x') zabrane++; + if (pole[i][1] == 'o' || pole[i][2] == 'x') zabrane++; + if (zabrane == 9) return 2; + } +} + +int herniKolo(char pole[][3]) { + int rada, sloupec; + + vypisHru(pole, checkniVyhru(pole)); + + scanf("%d", &rada); + scanf("%d", &sloupec); + + if (kdoHraje == 0) { + if (pole[rada-1][sloupec-1] == ' ' && 1 <= rada <= 3 && 1 <= sloupec <= 3) { + pole[rada-1][sloupec-1] = 'o'; + kdoHraje = 1; + } + } else if (kdoHraje == 1) { + if (pole[rada-1][sloupec-1] == ' ' && 1 <= rada <= 3 && 1 <= sloupec <= 3) { + pole[rada-1][sloupec-1] = 'x'; + kdoHraje = 0; + } + } + + checkniVyhru(pole); + + if (vypisHru(pole, checkniVyhru(pole)) == 0) return 0; +} + +int main() { + char pole[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}}; + + for (int i = 0; i < 9; i++) { + if (herniKolo(pole) == 0) return 0; + herniKolo(pole); + } + return 0; +} diff --git a/tictactoe-v2.py b/tictactoe-v2.py new file mode 100644 index 0000000..efb3e86 --- /dev/null +++ b/tictactoe-v2.py @@ -0,0 +1,124 @@ +import os + +konec = 0 +kdoHraje = 0 +def init(): + global konec, kdoHraje + + pole = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] + + while konec == 0: + herniKolo(pole) + +def clearScreen(): + if os.name == "posix": + os.system("clear") + if os.name == "nt": + os.system("cls") + + +def vypisHru(pole): + for i in range(3): + print(f"[{pole[i][0]}] [{pole[i][1]}] [{pole[i][2]}]") + +def check(pole): + # horizontal checking + # 0 is for when 'o's win and 1 is for when 'x's win + for i in range(3): + if pole[i][0] == pole[i][1] == pole[i][2] == 'o': + return 0 + if pole[i][0] == pole[i][1] == pole[i][2] == 'x': + return 1 + + # vertical checking + for i in range(3): + if pole[0][i] == pole[1][i] == pole[2][i] == 'o': + return 0 + if pole[0][i] == pole[1][i] == pole[2][i] == 'x': + return 1 + + # diagonal checking + if pole[0][0] == pole[1][1] == pole[2][2] == 'o': + return 0 + if pole[0][0] == pole[1][1] == pole[2][2] == 'x': + return 1 + if pole[0][2] == pole[1][1] == pole[2][0] == 'o': + return 0 + if pole[0][2] == pole[1][1] == pole[2][0] == 'x': + return 1 + + # draw checking + # 2 is for when a draw occurs + pocet = 0 + for i in range(3): + for j in range(3): + if pole[i][j] == 'o' or pole[i][j] == 'x': + pocet += 1 + if pocet == 9: + return 2 + +def herniKolo(pole): + global konec, kdoHraje + + # algorithm steps + # + # 1 clear the screen + # 2 print the current state of the game + # 3 allow for one player or the other to insert coords + # 4 place an 'o' or an 'x' to the coresponding coords + # 5 check if the game should end (if someone won) + # 5.1 if yes -> end the game + # 5.2 if no -> jump to step 1 + + # step 1 -> clear the screen + # + # cross-platform (Windows, Unix-like OSs) + clearScreen() + + # step 2 -> print the current state of the game + vypisHru(pole) + + # step 3 -> allow for one player or the other to insert coords + rada, sloupec = map(int, input().split()) + + # step 4 -> place an 'o' or an 'x' to the coresponding coords + # + # only if there isn't another symbol present and the coords are valid + # the 'o' or 'x' is determined by the kdoHraje variable + # - if it is 0, then an 'o' is placed, if it is 1, an 'x' is placed instead + # the rows and columns are marked 1, 2 and 3, not 0, 1 and 2 as the indexes + # for better user experience + if pole[rada-1][sloupec-1] == ' ' and 1 <= rada <= 3 and 1 <= sloupec <= 3: + print(kdoHraje) + if kdoHraje == 0: + pole[rada-1][sloupec-1] = 'o' + kdoHraje = 1 + elif kdoHraje == 1: + pole[rada-1][sloupec-1] = 'x' + kdoHraje = 0 + + # step 5 -> check if the game should end (if someone won) + vyherce = check(pole) + + if vyherce == 0: + # step 5.1 -> if yes -> end the game + clearScreen() + print("Vyhralo o!") + vypisHru(pole) + konec = 1 + # step 5.2 -> if no -> jump to step 1 + + if vyherce == 1: + clearScreen() + print("Vyhralo x!") + vypisHru(pole) + konec = 1 + + if vyherce == 2: + clearScreen() + print("Remiza!") + vypisHru(pole) + konec = 1 + +if __name__ == "__main__": + init() \ No newline at end of file