diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6caf68a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +output \ No newline at end of file diff --git a/TicTacToe.c b/TicTacToe.c index 4c86c76..03c466e 100644 --- a/TicTacToe.c +++ b/TicTacToe.c @@ -1,26 +1,47 @@ #include - +#include char cPlayfield[3][3] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}}; char *ptrPlayfield; +char currentUser; + +int running = 1; // TODO: find bool and convert to it +int counter = 1; void renderPlayfield(); +void getUserInput(); +void checkForWin(); int main() { - renderPlayfield(); + while (running == 1) + { + renderPlayfield(); + currentUser = counter++ % 2 == 1 ? 'X' : 'O'; + getUserInput(); + checkForWin(); + } return 0; } -void renderPlayfield() { +void renderPlayfield() +{ +#ifdef WIN32 + system("cls"); +#endif + +#ifdef linux + system("clear"); +#endif + printf(" | 1 | 2 | 3 |\n"); printf("---------------\n"); for (int i = 0; i < 3; i++) { - printf("%d | ", i+1); + printf("%d | ", i + 1); for (int j = 0; j < 3; j++) { ptrPlayfield = &cPlayfield[i][j]; @@ -28,5 +49,52 @@ void renderPlayfield() { } printf("\n---------------\n"); } - +} + +void getUserInput() +{ + int currentCol = 0; + int currentRow = 0; + printf("\nCurrent Player: %c\n", currentUser); + printf("Row and Colum: "); + scanf("%d %d", ¤tRow, ¤tCol); + + if (currentRow < 1 || currentRow > 3 || currentCol < 1 || currentCol > 3) + { + printf("Your Input is out of Range. Use only numbers 1, 2 or 3. \n"); + return getUserInput(); + } + else + { + cPlayfield[currentRow - 1][currentCol - 1] = currentUser; + } + return; +} + +void checkForWin() { + for (int i = 0; i < 3; i++) { + if (cPlayfield[i][0] == cPlayfield[i][1] && cPlayfield[i][1] == cPlayfield[i][2] && cPlayfield[i][2] != ' ') { + running = 0; + printf("The winner is: %c\n", cPlayfield[i][0]); + return; + } + + if (cPlayfield[0][i] == cPlayfield[1][i] && cPlayfield[1][i] == cPlayfield[2][i] && cPlayfield[2][i] != ' ') { + running = 0; + printf("The winner is: %c\n", cPlayfield[0][i]); + return; + } + + } + if (cPlayfield[0][0] == cPlayfield[1][1] && cPlayfield[1][1] == cPlayfield[2][2] && cPlayfield[2][2] != ' ') { + running = 0; + printf("The winner is %c\n", cPlayfield[0][0]); + return; + } + + if (cPlayfield[0][2] == cPlayfield[1][1] && cPlayfield[1][1] == cPlayfield[2][0] && cPlayfield[2][0] != ' ') { + running = 0; + printf("The winner is %d\n", cPlayfield[0][2]); + return; + } } \ No newline at end of file diff --git a/main b/main deleted file mode 100755 index c9a86bd..0000000 Binary files a/main and /dev/null differ