commit 42c857f2243f7c6d623fb47461220ed7b6740915 Author: mnjx Date: Thu Mar 2 09:36:48 2023 -1000 Initial commit with a WIP C version and a completed Python version diff --git a/piskvorky b/piskvorky new file mode 100755 index 0000000..701adb4 Binary files /dev/null and b/piskvorky differ 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