-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bea7e82
Showing
11 changed files
with
504 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "(gdb) Launch", | ||
"preLaunchTask": "TicTacToe", | ||
"type": "cppdbg", | ||
"request": "launch", | ||
"program": "${workspaceFolder}/TicTacToe.out", | ||
"args": [], | ||
"stopAtEntry": false, | ||
"cwd": "${workspaceFolder}", | ||
"environment": [], | ||
"externalConsole": true, | ||
"MIMode": "gdb", | ||
"setupCommands": [ | ||
{ | ||
"description": "Enable pretty-printing for gdb", | ||
"text": "-enable-pretty-printing", | ||
"ignoreFailures": true | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "TicTacToe", | ||
"type": "shell", | ||
"command": "gcc", | ||
"args": [ | ||
"-std=c99", | ||
"-fno-builtin", | ||
"-g", | ||
"interacao.c", | ||
"tabela.c", | ||
"ai.c", | ||
"TicTacToe.c", | ||
"-lm", | ||
"-o", | ||
"TicTacToe.out" | ||
], | ||
"problemMatcher": [ | ||
"$gcc" | ||
] | ||
} | ||
], | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
tictactoe: TicTacToe.c interacao.c tabela.c | ||
gcc -o TicTacToe interacao.c tabela.c ai.c TicTacToe.c | ||
clean: | ||
rm -rf TicTacToe |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include "tabela.h" | ||
#include "interacao.h" | ||
#include "ai.h" | ||
|
||
void iniciar_um_jogador(); | ||
void iniciar_dois_jogadores(); | ||
|
||
int main() | ||
{ | ||
printf(" _______ _ _______ _______ __ ___ \n"); | ||
printf(" |__ __(_) |__ __| |__ __| /_ | / _ \\ \n"); | ||
printf(" | | _ ___| | __ _ ___| | ___ ___ | || | | |\n"); | ||
printf(" | | | |/ __| |/ _` |/ __| |/ _ \\ / _ \\ | || | | |\n"); | ||
printf(" | | | | (__| | (_| | (__| | (_) | __/ | || |_| |\n"); | ||
printf(" |_| |_|\\___|_|\\__,_|\\___|_|\\___/ \\___| |_(_)___/ \n"); | ||
|
||
while (1) | ||
{ | ||
|
||
int modo = solicitar_modo_de_jogo(); | ||
|
||
if (modo == 2) | ||
{ | ||
iniciar_dois_jogadores(); | ||
} | ||
else | ||
{ | ||
iniciar_um_jogador(); | ||
} | ||
|
||
printf("\n\n\n\n--> Deseja jogar de novo? (1) Sim - (2) - Não\n"); | ||
int opcao; | ||
scanf("%d", &opcao); | ||
if (opcao != 1) | ||
{ | ||
printf("\nAté mais! Espero que tenha gostado do jogo!\n"); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void iniciar_um_jogador() | ||
{ | ||
printf("\nIniciando modo de um jogador!\n"); | ||
|
||
char *tabela = criar_tabela(); | ||
char player1[21]; | ||
|
||
solicitar_nome_do_usuario(player1); | ||
|
||
int proximo = 1; | ||
|
||
while (1) | ||
{ | ||
int posicao_escolhida; | ||
char proximo_nome[21]; | ||
|
||
imprimir_tabela(tabela); | ||
if (proximo) | ||
{ | ||
printf("É a sua vez, %s!\n", player1); | ||
strcpy(proximo_nome, player1); | ||
solicitar_posicao(&posicao_escolhida); | ||
} | ||
else | ||
{ | ||
printf("O computador já jogou!\n"); | ||
strcpy(proximo_nome, "computador"); | ||
|
||
computador_solicitar_posicao(&posicao_escolhida, tabela); | ||
} | ||
|
||
if (posicao_disponivel(posicao_escolhida, tabela)) | ||
{ | ||
marcar_posicao(posicao_escolhida, tabela, proximo); | ||
int fim_de_jogo = 0; | ||
if (houve_vitoria(tabela)) | ||
{ | ||
printf("\n\n\nVocê venceu, %s!\n", proximo_nome); | ||
fim_de_jogo = 1; | ||
} | ||
else if (houve_empate(tabela)) | ||
{ | ||
printf("\n\n\nJogo empatado!\n"); | ||
fim_de_jogo = 1; | ||
} | ||
if (fim_de_jogo) | ||
{ | ||
imprimir_tabela(tabela); | ||
break; | ||
} | ||
|
||
//proximo jogador | ||
proximo = !proximo; | ||
} | ||
else | ||
{ | ||
printf("A posição escolhida não está disponível ou não é válida!\n"); | ||
} | ||
} | ||
free(tabela); | ||
} | ||
|
||
void iniciar_dois_jogadores() | ||
{ | ||
printf("\nIniciando modo de dois jogadores!\n"); | ||
char *tabela = criar_tabela(); | ||
|
||
char player1[21]; | ||
char player2[21]; | ||
|
||
solicitar_nome_do_usuario(player1); | ||
solicitar_nome_do_usuario(player2); | ||
|
||
//variavel estática para que o próximo usuário alterne entre partidas | ||
static int proximo = 1; | ||
|
||
while (1) | ||
{ | ||
char proximo_nome[21]; | ||
|
||
imprimir_tabela(tabela); | ||
if (proximo) | ||
{ | ||
strcpy(proximo_nome, player1); | ||
} | ||
else | ||
{ | ||
strcpy(proximo_nome, player2); | ||
} | ||
printf("É sua vez, %s!\n", proximo_nome); | ||
|
||
int posicao_escolhida = 0; | ||
solicitar_posicao(&posicao_escolhida); | ||
|
||
if (posicao_disponivel(posicao_escolhida, tabela)) | ||
{ | ||
marcar_posicao(posicao_escolhida, tabela, proximo); | ||
int fim_de_jogo = 0; | ||
if (houve_vitoria(tabela)) | ||
{ | ||
printf("\n\n\nVocê venceu, %s!\n", proximo_nome); | ||
fim_de_jogo = 1; | ||
} | ||
else if (houve_empate(tabela)) | ||
{ | ||
printf("\n\n\nJogo empatado!\n"); | ||
fim_de_jogo = 1; | ||
} | ||
if (fim_de_jogo) | ||
{ | ||
imprimir_tabela(tabela); | ||
break; | ||
} | ||
//proximo jogador | ||
proximo = !proximo; | ||
} | ||
else | ||
{ | ||
printf("A posição escolhida não está disponível ou não é válida!\n"); | ||
} | ||
} | ||
free(tabela); | ||
} |
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include <math.h> | ||
#include "ai.h" | ||
#include "tabela.h" | ||
|
||
static void posicoes_disponiveis(char tabela[10], int posicoes_disponiveis[9]) { | ||
for (int i = 0; i < 9; i++) { | ||
if (tabela[i] == '_') { | ||
posicoes_disponiveis[i] = 1; | ||
} else { | ||
posicoes_disponiveis[i] = 0; | ||
} | ||
} | ||
} | ||
|
||
static int utilidade(char tabela[10], int maximizing_player) { | ||
if (houve_vitoria(tabela)) { | ||
if (maximizing_player) { | ||
return 1; | ||
} else { | ||
return -1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
static char * resultado(char tabela[10], int posicao, int maximizing_player) { | ||
char * tabela_simulada = malloc(10 * sizeof (char)); | ||
strncpy(tabela_simulada, tabela, 10); | ||
marcar_posicao(posicao, tabela_simulada, !maximizing_player); | ||
|
||
return tabela_simulada; | ||
} | ||
|
||
static int fim_de_jogo(char tabela[10]) { | ||
if (houve_vitoria(tabela)) { | ||
return 1; | ||
} | ||
if (houve_empate(tabela)) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static double minimax(char tabela[10], int maximizing_player) { | ||
if (fim_de_jogo(tabela)) { | ||
return utilidade(tabela, !maximizing_player); | ||
} | ||
|
||
int disponiveis[9]; | ||
posicoes_disponiveis(tabela, disponiveis); | ||
if (maximizing_player) { | ||
double valor = -INFINITY; | ||
for (int i = 0; i < 9; i++) { | ||
if (disponiveis[i]) { | ||
char * tabela_filha = resultado(tabela, i, maximizing_player); | ||
valor = fmaxf(valor, minimax(tabela_filha, !maximizing_player)); | ||
|
||
free(tabela_filha); | ||
} | ||
} | ||
return valor; | ||
} else { | ||
double valor = INFINITY; | ||
for (int i = 0; i < 9; i++) { | ||
if (disponiveis[i]) { | ||
char * tabela_filha = resultado(tabela, i, maximizing_player); | ||
valor = fminf(valor, minimax(tabela_filha, !maximizing_player)); | ||
|
||
free(tabela_filha); | ||
} | ||
} | ||
return valor; | ||
} | ||
} | ||
|
||
void computador_solicitar_posicao(int *posicao_escolhida, char tabela[10]) { | ||
double mm = -INFINITY; | ||
int posicao = -1; | ||
|
||
int disponiveis[9]; | ||
posicoes_disponiveis(tabela, disponiveis); | ||
for (int i = 0; i < 9; i++) { | ||
if (disponiveis[i]) { | ||
if (posicao == -1) { | ||
posicao = i; | ||
} | ||
|
||
char * tabela_i = resultado(tabela, i, 1); | ||
double temp_mm = minimax(tabela_i, 0); | ||
if (temp_mm > mm) { | ||
mm = temp_mm; | ||
posicao = i; | ||
} | ||
|
||
free(tabela_i); | ||
} | ||
} | ||
|
||
*posicao_escolhida = posicao; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void computador_solicitar_posicao(int * posicao_escolhida, char tabela[9]); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <stdio.h> | ||
#include "interacao.h" | ||
|
||
int solicitar_modo_de_jogo() { | ||
printf("\nModo de jogo: (1) Um jogador - (2) Dois jogadores\n"); | ||
printf("--> Escolha o modo de jogo: "); | ||
|
||
int modo; | ||
scanf("%d", &modo); | ||
if (modo != 1 && modo != 2) { | ||
modo = 1; | ||
} | ||
|
||
printf("\n"); | ||
|
||
return modo; | ||
} | ||
|
||
void solicitar_nome_do_usuario(char nome[21]) { | ||
printf("\n--> Informe seu nome de usuário (20 caracteres): "); | ||
scanf("%20s", nome); | ||
} | ||
|
||
void solicitar_posicao(int *posicao) { | ||
printf("\n--> Escolha onde deseja jogar (Ex.: B2, A0, etc): "); | ||
char input[3]; | ||
scanf(" %2s", input); | ||
int pos[2]; | ||
|
||
switch (input[0]) { | ||
case 'A': case 'a': | ||
pos[0] = 0; | ||
break; | ||
case 'B': case 'b': | ||
pos[0] = 1; | ||
break; | ||
case 'C': case 'c': | ||
pos[0] = 2; | ||
break; | ||
default: | ||
pos[0] = 0; | ||
} | ||
|
||
pos[1] = input[1] - '0'; | ||
|
||
*posicao = (pos[1] * 3) + pos[0]; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
int solicitar_modo_de_jogo(); | ||
|
||
void solicitar_nome_do_usuario(char * nome); | ||
|
||
void solicitar_posicao(int * posicao); |
Oops, something went wrong.