Last active
July 18, 2023 21:32
-
-
Save brunofunnie/9883f1c5d7d5d91cc41b3ed4020a1365 to your computer and use it in GitHub Desktop.
Tic Tac Toe - Grid Flexible
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 <stdbool.h> | |
void printBoard(int size, char board[size][size]) | |
{ | |
printf("\n"); | |
for (int i = 0; i < size; i++) { | |
// Print row | |
for (int j = 0; j < size; j++) { | |
printf(" %c ", board[i][j]); | |
if (j != size - 1) { | |
printf("|"); | |
} | |
} | |
printf("\n"); | |
// Print separator | |
if (i != size - 1) { | |
for (int j = 0; j < size; j++) { | |
printf("___"); | |
if (j != size - 1) { | |
printf("|"); | |
} | |
} | |
printf("\n"); | |
} | |
} | |
printf("\n"); | |
} | |
bool isBoardFull(int size, char board[size][size]) | |
{ | |
for (int i = 0; i < size; i++) { | |
for (int j = 0; j < size; j++) { | |
if (board[i][j] == ' ') | |
return false; | |
} | |
} | |
return true; | |
} | |
bool checkWin(int size, char board[size][size], char symbol, int lastRow, int lastCol) | |
{ | |
// Check row | |
bool win = true; | |
for (int j = 0; j < size; j++) { | |
if (board[lastRow][j] != symbol) { | |
win = false; | |
break; | |
} | |
} | |
if (win) | |
return true; | |
// Check column | |
win = true; | |
for (int i = 0; i < size; i++) { | |
if (board[i][lastCol] != symbol) { | |
win = false; | |
break; | |
} | |
} | |
if (win) | |
return true; | |
// Check main diagonal | |
if (lastRow == lastCol) { | |
win = true; | |
for (int i = 0; i < size; i++) { | |
if (board[i][i] != symbol) { | |
win = false; | |
break; | |
} | |
} | |
if (win) | |
return true; | |
} | |
// Check anti-diagonal | |
if (lastRow + lastCol == size - 1) { | |
win = true; | |
for (int i = 0; i < size; i++) { | |
if (board[i][size - i - 1] != symbol) { | |
win = false; | |
break; | |
} | |
} | |
if (win) | |
return true; | |
} | |
return false; | |
} | |
int main() | |
{ | |
int size = 3; // Change the size of the Tic Tac Toe board here | |
char board[size][size]; | |
for (int i = 0; i < size; i++) { | |
for (int j = 0; j < size; j++) { | |
board[i][j] = ' '; | |
} | |
} | |
char currentPlayer = 'X'; | |
bool gameFinished = false; | |
while (!gameFinished) { | |
printf("Player %c's turn.\n", currentPlayer); | |
int row, col; | |
printf("Enter the row (1-%d): ", size); | |
scanf("%d", &row); | |
printf("Enter the column (1-%d): ", size); | |
scanf("%d", &col); | |
// Adjust row and col to 0-based indices | |
row--; | |
col--; | |
if (row < 0 || row >= size || col < 0 || col >= size) { | |
printf("Invalid move. Please try again.\n"); | |
continue; | |
} | |
if (board[row][col] != ' ') { | |
printf("Invalid move. Please try again.\n"); | |
continue; | |
} | |
board[row][col] = currentPlayer; | |
printBoard(size, board); | |
if (checkWin(size, board, currentPlayer, row, col)) { | |
printf("Player %c wins!\n", currentPlayer); | |
gameFinished = true; | |
} else if (isBoardFull(size, board)) { | |
printf("It's a draw!\n"); | |
gameFinished = true; | |
} else { | |
// Switch player | |
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment