Skip to content

Instantly share code, notes, and snippets.

@M3mbrillo
Created May 27, 2018 21:52
Show Gist options
  • Save M3mbrillo/61083481e02adc65832f9262faf23190 to your computer and use it in GitHub Desktop.
Save M3mbrillo/61083481e02adc65832f9262faf23190 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "windows.h"
#define false 0
#define FASLE 0
#define true 1
#define TRUE 1
int EsEmpate(int pInt[3][3]) {
int anyFree = false;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (pInt[i][j] == 0)
{
anyFree = true;
}
}
}
return !anyFree;
}
int Gano(int t[3][3], int i) {
//Recorro filas
for (int j = 0; j < 3; ++j) {
if (t[j][0] == i && t[j][1] == i&& t[j][2] == i )
{
return true;
}
}
//recorro columnas
for (int j = 0; j < 3; ++j) {
if (t[0][j] == i && t[1][j] == i && t[2][j] == i )
{
return true;
}
}
//Valido la diagonal principa
int diagonalCoin = 0;
for (int k = 0; k < 3; ++k) {
if (t[k][k] == i ){
diagonalCoin++;
}
}
if (diagonalCoin == 3)
{
return true;
} else{
diagonalCoin = 0;
}
//Diagonal espejo
int n = 0;
for (int m = 2; m >= 0 ; m--) {
if (t[m][n] == i)
{
diagonalCoin++;
}
n++;
}
if (diagonalCoin == 3)
{
return true;
}
return false;
}
int QuienGana(int t[3][3])
{
// < 0 : Gano X
// > 0 : Gano O
// = 0: no gano nadie aun
int rta = 0;
if (Gano(t, -1) == true){
rta = -1;
} else if (Gano(t, 1) == true)
{
rta = 1;
} else{
rta = 0;
}
return rta;
}
void InicializarTablero(int t[3][3])
{
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
t[i][j] = 0;
}
}
}
void PrintTablero(int t[3][3])
{
// < 0 : X
// > 0 : O
// = 0: _
int i1 = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
i1 =t[i][j];
printf(" | ");
if (i1 == 0)
{
printf("-");
}else if(i1 > 0)
{
printf("O");
}else if(i1 < 0)
{
printf("X");
}
}
printf(" |\n");
}
}
//Funcionas para testeo
void llenarFila(int t[3][3], int indice, int valor){
for (int i = 0; i < 3; ++i) {
t[indice][i] = valor;
}
}
void llenarColumna(int t[3][3], int indice, int valor)
{
for (int i = 0; i < 3; ++i) {
t[i][indice] = valor;
}
}
void llenarDiagonal(int t[3][3], int valor)
{
for (int i = 0; i < 3; ++i) {
t[i][i] = valor;
}
}
void llenarDiagonalInversa(int t[3][3], int valor)
{
int j=0;
for (int i = 2; i >= 0; --i) {
t[i][j] = valor;
j++;
}
}
void PrintGanador(int t[3][3] ){
int winner = QuienGana(t);
// < 0 : Gano X
// > 0 : Gano O
// = 0: no gano nadie aun
if (winner == 0)
{
printf("No gana nadie\n");
}else if (winner < 0)
{
printf("Gano X\n");
} else if (winner > 0)
{
printf("Gano O\n");
}
}
void Jugada(char simbol, int valor, int t[3][3])
{
int input_f, input_c;
printf( "Jugado - %c - Ingrese Col: ", simbol);
scanf("%d", &input_c);
printf("Jugado - %c - Ingrese Fil: ", simbol);
scanf("%d", &input_f);
t[input_f][input_c] = valor;
}
int main() {
printf("TA-TE-TI\n");
int tablero[3][3];
InicializarTablero(tablero);
while (QuienGana(tablero) == 0 || EsEmpate(tablero))
{
// < 0 : X
// > 0 : O
// = 0: no gano nadie aun
PrintTablero(tablero);
Jugada('X', -1, tablero);
printf("----------------------\n");
if (QuienGana(tablero) != -1)
{
PrintTablero(tablero);
Jugada('O', 1, tablero);
printf("----------------------\n");
}
}
PrintTablero(tablero);
PrintGanador(tablero);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment