Created
April 26, 2024 23:09
-
-
Save agmezr/030f77370ab6a0740f7d25034ba6d0f5 to your computer and use it in GitHub Desktop.
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 <time.h> | |
#include <stdlib.h> | |
#define X_SYMBOL 'X' | |
#define O_SYMBOL 'O' | |
#define STALE_SYMBOL 'S' | |
struct TicTacToe { | |
char board[3][3]; | |
char symbol; | |
}; | |
int emptyCell(char board[3][3], int i, int j){ | |
return board[i][j] == X_SYMBOL || board[i][j] == O_SYMBOL || board[i][j] == STALE_SYMBOL; | |
} | |
void printBoard(char board[3][3]) { | |
for(int i = 0; i < 3; i++){ | |
for(int j = 0; j < 3; j++){ | |
printf("%c |", board[i][j]); | |
} | |
printf("\n-------\n"); | |
} | |
} | |
void printAllBoard(struct TicTacToe boards[3][3]) { | |
// also should be a for, but meetings | |
printf("++++++++++++++++++++++++++++++++++++\n"); | |
printf("+ %c | %c | %c + %c | %c |%c + %c | %c | %c + \n", boards[0][0].board[0][0], boards[0][0].board[0][1], boards[0][0].board[0][2], | |
boards[0][1].board[0][0], boards[0][1].board[0][1], boards[0][1].board[0][2], | |
boards[0][2].board[0][0], boards[0][2].board[0][1], boards[0][2].board[0][2] | |
); | |
printf("+ %c | %c | %c + %c | %c |%c + %c | %c | %c + \n", boards[0][0].board[1][0], boards[0][0].board[1][1], boards[0][0].board[1][2], | |
boards[0][1].board[1][0], boards[0][1].board[1][1], boards[0][1].board[1][2], | |
boards[0][2].board[1][0], boards[0][2].board[1][1], boards[0][2].board[1][2] | |
); | |
printf("+ %c | %c | %c + %c | %c |%c + %c | %c | %c + \n", boards[0][0].board[2][0], boards[0][0].board[2][1], boards[0][0].board[2][2], | |
boards[0][1].board[2][0], boards[0][1].board[2][1], boards[0][1].board[2][2], | |
boards[0][2].board[2][0], boards[0][2].board[2][1], boards[0][2].board[2][2] | |
); | |
printf("++++++++++++++++++++++++++++++++++++\n"); | |
printf("+ %c | %c | %c + %c | %c |%c + %c | %c | %c + \n", boards[1][0].board[0][0], boards[1][0].board[0][1], boards[1][0].board[0][2], | |
boards[1][1].board[0][0], boards[1][1].board[0][1], boards[1][1].board[0][2], | |
boards[1][2].board[0][0], boards[1][2].board[0][1], boards[1][2].board[0][2] | |
); | |
printf("+ %c | %c | %c + %c | %c |%c + %c | %c | %c + \n", boards[1][0].board[1][0], boards[1][0].board[1][1], boards[1][0].board[1][2], | |
boards[1][1].board[1][0], boards[1][1].board[1][1], boards[1][1].board[1][2], | |
boards[1][2].board[1][0], boards[1][2].board[1][1], boards[1][2].board[1][2] | |
); | |
printf("+ %c | %c | %c + %c | %c |%c + %c | %c | %c + \n", boards[1][0].board[2][0], boards[1][0].board[2][1], boards[1][0].board[2][2], | |
boards[1][1].board[2][0], boards[1][1].board[2][1], boards[1][1].board[2][2], | |
boards[1][2].board[2][0], boards[1][2].board[2][1], boards[1][2].board[2][2] | |
); | |
printf("++++++++++++++++++++++++++++++++++++\n"); | |
printf("+ %c | %c | %c + %c | %c |%c + %c | %c | %c + \n", boards[2][0].board[0][0], boards[2][0].board[0][1], boards[2][0].board[0][2], | |
boards[2][1].board[0][0], boards[2][1].board[0][1], boards[2][1].board[0][2], | |
boards[2][2].board[0][0], boards[2][2].board[0][1], boards[2][2].board[0][2] | |
); | |
printf("+ %c | %c | %c + %c | %c |%c + %c | %c | %c + \n", boards[2][0].board[1][0], boards[2][0].board[1][1], boards[2][0].board[1][2], | |
boards[2][1].board[1][0], boards[2][1].board[1][1], boards[2][1].board[1][2], | |
boards[2][2].board[1][0], boards[2][2].board[1][1], boards[2][2].board[1][2] | |
); | |
printf("+ %c | %c | %c + %c | %c |%c + %c | %c | %c + \n", boards[2][0].board[2][0], boards[2][0].board[2][1], boards[2][0].board[2][2], | |
boards[2][1].board[2][0], boards[2][1].board[2][1], boards[2][1].board[2][2], | |
boards[2][2].board[2][0], boards[2][2].board[2][1], boards[2][2].board[2][2] | |
); | |
printf("++++++++++++++++++++++++++++++++++++\n"); | |
} | |
int addMove(char board[3][3], char symbol, int x, int y) { | |
if (x < 0 || x > 2 || y < 0 || y > 2){ | |
printf("Movimiento invalido \n"); | |
return 0; | |
} | |
if (emptyCell(board,x, y) != 0) { | |
printf("Casilla ya usada \n"); | |
return 0; | |
} | |
board[x][y] = symbol; | |
return 1; | |
} | |
int winnerBoard(char board[3][3], char symbol) { | |
// this should be a for but I have a meeting | |
int a = 0; | |
a = a || board[0][0] == symbol && board[0][1] == symbol && board[0][2] == symbol; | |
a = a || board[1][0] == symbol && board[1][1] == symbol && board[1][2] == symbol; | |
a = a || board[2][0] == symbol && board[2][1] == symbol && board[2][2] == symbol; | |
a = a || board[0][0] == symbol && board[1][0] == symbol && board[2][0] == symbol; | |
a = a || board[0][1] == symbol && board[1][1] == symbol && board[2][1] == symbol; | |
a = a || board[0][2] == symbol && board[1][2] == symbol && board[2][2] == symbol; | |
a = a || board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol; | |
a = a || board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol; | |
return a; | |
} | |
int staleBoard(char board[3][3]) { | |
int total = 0; | |
for(int i = 0; i < 3; i++){ | |
for(int j = 0; j < 3; j++){ | |
total += emptyCell(board, i, j); | |
} | |
} | |
return total == 9; | |
} | |
int main () { | |
struct TicTacToe boards[3][3]; | |
for(int k = 0; k < 3; k++){ | |
for(int l = 0; l < 3; l++){ | |
boards[k][l].symbol = ' '; | |
for(int i = 0; i < 3; i++){ | |
for(int j = 0; j < 3; j++){ | |
boards[k][l].board[i][j] = ' '; | |
} | |
} | |
} | |
} | |
// this is the outer board. | |
char mainBoard[3][3]; | |
char player1[20] = "Alex"; | |
char player2[20] = "Dafnee"; | |
int playing = 1; | |
char symbols[2] = {X_SYMBOL, O_SYMBOL}; | |
char* players[2] = {player1, player2}; | |
// init random | |
srand(time(NULL)); | |
// to check which player starts, turn 0 player 1, turn 1 player 2; | |
int turn = rand() % 2; | |
int boardX = -1; | |
int boardY = -1; | |
int miniX = 0; | |
int miniY = 0; | |
while (playing){ | |
char currentSymbol = symbols[turn % 2]; | |
char* currentPlayer = players[turn % 2]; | |
printf("Turno del jugador %s \n", currentPlayer); | |
printf("Tablero \n"); | |
printAllBoard(boards); | |
if (boardX < 0 || boardY < 0){ | |
printf("Elige un mini tablero.\n" ); | |
printf("Coordenada X:"); | |
scanf("%d", &boardX); | |
printf("Coordenada Y:"); | |
scanf("%d", &boardY); | |
} | |
if(emptyCell(mainBoard, boardX, boardY)!= 0){ | |
printf("Mini tablero ya ganado, elige otro\n"); | |
boardX = -1; | |
boardY = -1; | |
continue; | |
} | |
printf("Elegiste %d, %d \n", boardX, boardY); | |
printf("Elige una coordenada en el mini tablero\n"); | |
printf("Coordenada X:"); | |
scanf("%d", &miniX); | |
printf("Coordenada Y:"); | |
scanf("%d", &miniY); | |
if(!addMove(boards[boardX][boardY].board, currentSymbol, miniX, miniY)) { | |
continue; | |
} | |
if(winnerBoard(boards[boardX][boardY].board, currentSymbol)){ | |
printf("Mini board ganado por %s \n", currentPlayer); | |
boards[boardX][boardY].symbol = currentSymbol; | |
mainBoard[boardX][boardY] = currentSymbol; | |
boardX = -1; | |
boardY = -1; | |
printBoard(mainBoard); | |
if(winnerBoard(mainBoard, currentSymbol)){ | |
printf("Juego ganado por %s \n", currentPlayer); | |
break; | |
} | |
}else if(staleBoard(boards[boardX][boardY].board)) { | |
boardX = -1; | |
boardY = -1; | |
boards[boardX][boardY].symbol = STALE_SYMBOL; | |
}else { | |
if (boards[boardX][boardY].symbol != ' '){ | |
boardX = -1; | |
boardY = -1; | |
}else { | |
boardX = miniX; | |
boardY = miniY; | |
} | |
} | |
turn++; | |
} | |
printf("Fin del juego"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment