Created
June 3, 2022 19:06
-
-
Save JosiasAurel/f04a12f1cfe678e2ecca7b32be48b5c4 to your computer and use it in GitHub Desktop.
Just my implemetation of tic tac toe game
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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| static char grid[3][3]; | |
| static char player1 = 'O'; | |
| static char player2 = 'X'; | |
| int winner = 0; | |
| void printGameGrid(); | |
| void insertSymbol(int position, int whichPlayer); | |
| char *getPlayer(int whichPlayer); | |
| void printHelper(); | |
| void prefillGrid(); | |
| void checkHorizontals(); | |
| void checkVerticals(); | |
| void checkDiagonals(); | |
| int main(int agrc, char **argv) | |
| { | |
| int whichPlayer = 0; | |
| int position; | |
| prefillGrid(); | |
| while (!winner) | |
| { | |
| printHelper(); | |
| puts(""); | |
| printGameGrid(); | |
| if (whichPlayer) | |
| { | |
| puts("It's player 1's turn"); | |
| } | |
| else | |
| puts("It's player 2's turn"); | |
| printf("Enter the position to insert your symbol : "); | |
| scanf("%d", &position); | |
| insertSymbol(position, whichPlayer); | |
| checkHorizontals(); | |
| checkVerticals(); | |
| checkDiagonals(); | |
| whichPlayer = ~whichPlayer; | |
| } | |
| return 0; | |
| } | |
| void printGameGrid() | |
| { | |
| for (int i = 0; i < 3; i++) | |
| { | |
| for (int j = 0; j < 3; j++) | |
| { | |
| if (grid[i][j] != 'E') | |
| { | |
| printf("%c ", grid[i][j]); | |
| } | |
| else | |
| printf(" "); | |
| } | |
| puts(""); | |
| } | |
| } | |
| void insertSymbol(int position, int whichPlayer) | |
| { | |
| if (position == 1) | |
| { | |
| if (grid[0][0] == 'E') | |
| { | |
| grid[0][0] = getPlayer(whichPlayer); | |
| } | |
| else | |
| puts("Cannot insert here"); | |
| } | |
| else if (position == 2) | |
| { | |
| if (grid[0][1] == 'E') | |
| { | |
| grid[0][1] = getPlayer(whichPlayer); | |
| } | |
| else | |
| puts("Cannot insert here"); | |
| } | |
| else if (position == 3) | |
| { | |
| if (grid[0][2] == 'E') | |
| { | |
| grid[0][2] = getPlayer(whichPlayer); | |
| } | |
| else | |
| puts("Cannot insert here"); | |
| } | |
| else if (position == 4) | |
| { | |
| if (grid[1][0] == 'E') | |
| { | |
| grid[1][0] = getPlayer(whichPlayer); | |
| } | |
| else | |
| puts("Cannot insert here"); | |
| } | |
| else if (position == 5) | |
| { | |
| if (grid[1][1] == 'E') | |
| { | |
| grid[1][1] = getPlayer(whichPlayer); | |
| } | |
| else | |
| puts("Cannot insert here"); | |
| } | |
| else if (position == 6) | |
| { | |
| if (grid[1][2] == 'E') | |
| { | |
| grid[1][2] = getPlayer(whichPlayer); | |
| } | |
| else | |
| puts("Cannot insert here"); | |
| } | |
| else if (position == 7) | |
| { | |
| if (grid[2][0] == 'E') | |
| { | |
| grid[2][0] = getPlayer(whichPlayer); | |
| } | |
| else | |
| puts("Cannot insert here"); | |
| } | |
| else if (position == 8) | |
| { | |
| if (grid[2][1] == 'E') | |
| { | |
| grid[2][1] = getPlayer(whichPlayer); | |
| } | |
| else | |
| puts("Cannot insert here"); | |
| } | |
| else if (position == 9) | |
| { | |
| if (grid[2][2] == 'E') | |
| { | |
| grid[2][2] = getPlayer(whichPlayer); | |
| } | |
| else | |
| puts("Cannot insert here"); | |
| } | |
| } | |
| char *getPlayer(int whichPlayer) | |
| { | |
| if (!whichPlayer) | |
| return player1; | |
| else | |
| return player2; | |
| } | |
| void printHelper() | |
| { | |
| int count = 0; | |
| for (int i = 0; i < 3; i++) | |
| { | |
| for (int j = 0; j < 3; j++) | |
| { | |
| count += 1; | |
| printf("%d ", count); | |
| } | |
| puts(""); | |
| } | |
| } | |
| void prefillGrid() | |
| { | |
| for (int i = 0; i < 3; i++) | |
| { | |
| for (int j = 0; j < 3; j++) | |
| { | |
| grid[i][j] = 'E'; | |
| } | |
| } | |
| } | |
| void checkHorizontals() | |
| { | |
| for (int i = 0; i < 3; i++) | |
| { | |
| if ((grid[i][0] == grid[i][1]) && (grid[i][1] == grid[i][2]) && (grid[i][0] != 'E')) | |
| { | |
| winner = 1; | |
| if (grid[i][0] == player1) | |
| { | |
| puts("Player 1 won"); | |
| } | |
| else if (grid[i][0] == player2) | |
| { | |
| puts("Player 2 won"); | |
| } | |
| } | |
| } | |
| } | |
| void checkVerticals() | |
| { | |
| for (int i = 0; i < 3; i++) | |
| { | |
| if ((grid[0][i] == grid[1][i]) && (grid[1][i] == grid[2][i]) && (grid[0][i] != 'E')) | |
| { | |
| winner = 1; | |
| if (grid[0][i] == player1) | |
| { | |
| puts("Player 1 won"); | |
| } | |
| else if (grid[0][i] == player2) | |
| { | |
| puts("Player 2 won"); | |
| } | |
| } | |
| } | |
| } | |
| void checkDiagonals() | |
| { | |
| if ((grid[0][0] == grid[1][1]) && (grid[1][1] == grid[2][2]) && (grid[0][0] != 'E')) | |
| { | |
| winner = 1; | |
| if (grid[0][0] == player1) | |
| { | |
| puts("Player 1 won"); | |
| } | |
| else if (grid[0][0] == player2) | |
| { | |
| puts("Player 2 won"); | |
| } | |
| } | |
| else if ((grid[0][2] == grid[1][1]) && (grid[1][1] == grid[2][0]) && (grid[0][2] != 'E')) | |
| { | |
| winner = 1; | |
| if (grid[0][2] == player1) | |
| { | |
| puts("Player 1 won"); | |
| } | |
| else if (grid[0][2] == player2) | |
| { | |
| puts("Player 2 won"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment