Velocity Reviews > Newbie Question about my Code

# Newbie Question about my Code

JB Cardenas
Guest
Posts: n/a

 11-12-2003
Hello all, attached to this message is my code for a Tic Tac Toe game,
this game plays with the strategy Minimax and Minimax Alpha Beta
Prunnig. Somebody can review my code to find errors and to test my
algorithms.

Thanks,

Jairo Cárdenas

*************************My Code Begin Here***********************
//************************************************** ************************//
//
//
// Author: Jairo A. Cardenas
//
// Date: 11/10/2003
//
// Version: 2.0
//
//
//
//************************************************** ************************//
// Author: Jairo A. Cardenas
// Student # F01082069
// Compiler: Turbo C++ 3.0

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <ctype.h>

#define ancho_tablero 3
int cierto= 1;
int falso= 0;

/* in each square of the board there is always an element that it can
be O,0, X*/
enum simbolo {vacio = 0, X, O};

/*The States found after the recursive search are lost, win, draw*/

/* structure that models the board. */
/*This is a vector with as many positions as will be the value of
};
/* type of data of the board*/

/* this variable keeps the letter with the human player palys*/
char letra_para_jugar;

/* this variable keeps a 'C' if the first that plays is the computer
of the contrary keeps a 'H'*/
char primero_en_jugar;

/*this variable keeps a '1' if play tic tac toe using the strategy
minimax, and '2' if play using minimax with alphabeta pruning*/
char minimax_o_alfabeta;

/*Store the name of the player*/

/*this function puts a zero(0) on each one of the positions of the
board*/
void iniciar_tablero (tipo_tablero * tablero);

/*draw the board in the screen*/
void mostrar_tablero1 (tipo_tablero tablero,int valcol,int valfila);

/* draw the lines of the board*/

/*read from the console the content of the variable etra_para_jugar*/

/*stop the operating of the program by a moment*/
void pulsar_tecla_para_continuar (void);

/*read the name of the player and who plays first*/
void otros_datos();

/*maintain the cycle of the game*/
void jugar();

/*read a character from the console or screen*/
char capturar_caracter (char* letrero);

/*puts a X or O on the indicated position of the board*/
void marcar_el_tablero (tipo_tablero * tablero, int posicion,

/*requests to the human that it writes the coordinates of the position
that is going to play*/
int capturar_jugada (tipo_tablero tablero, int linea);

/* determine if the board is full*/
int esta_lleno_el_tablero (tipo_tablero tablero);

/*determine if there is a winner*/

/*feature that returns the position in the one which plays the
computer*/

/*function for the search using minimax with alpha beta pruning */

/*function for the search using minimax*/
figura_activa,

figura_activa,

/* puts on zero the board, clear the board*/
void borrar_figura (tipo_tablero * tablero, int lugar);

void main(void)
{

char seguir_jugando='Y';
clrscr();

gotoxy(5,10);
Player*/

clrscr();
while (minimax_o_alfabeta!='3')
{
randomize();
while(seguir_jugando=='Y')
{
otros_datos();
jugar( );
seguir_jugando=capturar_caracter("\nDo you want to play one more
time? YES[Y], NO[Any Key]");
}

seguir_jugando='Y';

}

}
void iniciar_tablero (tipo_tablero * tablero)

{
int i;

for (i = 0; i < numero_de_cuadros; i++)
{
}
}

/* end iniciar_tablero */

/* Function Show Board*/
void mostrar_tablero1 (tipo_tablero tablero, int valcol,int valfila)
{
int i,j,col,fila;
col=valcol;
fila=valfila;
/*clrscr();*/ /* si no se require se quita*/
for(i=0;i<ancho_tablero;i++)
{
for(j=0;j<ancho_tablero;j++)
{
gotoxy(col,fila);

{
case X:
printf (" X ");
break;
case O:
printf (" O ");
break;
case vacio:
printf (" ");
break;
default:
printf (" ");
}

col=col+5;
}

fila=fila+2;
col=valcol;

}
}

/* end mostrar_tablero */

{
int i,j,fila,col;

fila=valfila;
col=valcol;
for(i=1;i<=ancho_tablero+1;i++)
{
gotoxy(col-1,fila+1);
if (i<ancho_tablero+1) printf("%d",i);
gotoxy(col,fila);

printf("----------------");
fila=fila+2;
}
col=valcol;
fila=valfila;
for(i=1;i<=ancho_tablero+1;i++)
{
gotoxy(col+2,fila-1);
if (i<ancho_tablero+1) printf("%d",i);
for (j=1;j<=2*ancho_tablero+1;j++)
{
gotoxy(col,fila);printf("|");
fila=fila+1;
}
col=col+5;
fila=valfila;

}

}

{
char op;
do
{

clrscr ();
printf (" TIC - TAC - TOE\n");
printf (" ===============\n");
printf ("\n");
printf (" Author : Jairo Alberto Cardenas\n");
printf (" 1. Minimax Strategy \n");
printf (" 2. Minimax with Alpha Beta Prunning Strategy\n");
printf (" 3. Exit.\n");

printf ("\n");
op=capturar_caracter("Select the Option: ");

}
while(op!='1' && op!='2' && op!='3');
return op;

}

/*Define other data as name of the player*/
void otros_datos()
{

clrscr();
gotoxy(1,1);
printf("%s%s",jugador," WELCOME TO THE GAME ");
if( minimax_o_alfabeta =='1')
printf("PLAYING TIC TAC TOE USING THE MINIMAX STRATEGY\n");
else
printf("PLAYING TIC TAC TOE USING THE MINIMAX WITH ALPHA BETA
PRUNNING\n");

do
{
gotoxy(1,3);
primero_en_jugar=capturar_caracter("Who Plays First? The Computer[C]
or Human[H] ");
primero_en_jugar=toupper(primero_en_jugar);
}
while(primero_en_jugar!='H'&& primero_en_jugar!='C');

do
{
gotoxy(1,4);
letra_para_jugar=capturar_caracter("With which letter wants to Play
'X' or 'O'? ");
letra_para_jugar=toupper(letra_para_jugar);
}
while(letra_para_jugar!='X'&& letra_para_jugar!='O');

}
/*end otros_datos*/

char capturar_caracter (char* letrero)
{
char aux;

printf ("%s", letrero);
scanf ("%c", &aux);
aux = toupper (aux);
fflush (stdin);

return aux;
}

/* end capturar_caracter */

/*function Play*/
void jugar()
{
tipo_tablero tablero;
int se_acabo;
char turno;
int juega_en_la_posicion,indice, fila,x,y;
int j;
j=0;
se_acabo=falso;

if (primero_en_jugar=='C')
turno='C';
else
{
turno='H';
}
clrscr();
fila=4;
iniciar_tablero (&tablero);
mostrar_tablero1 (tablero,5,fila);
j++;

while(se_acabo==falso)

{
if (turno=='C')
{

/*turn for the computer*/
figura_activa= (letra_para_jugar=='X')? O:X;

figura_activa);

x=(juega_en_la_posicion % ancho_tablero)+1;
y=(juega_en_la_posicion / ancho_tablero)+1;

marcar_el_tablero (&tablero,juega_en_la_posicion,figura_activa);
if (j==2)
{
pulsar_tecla_para_continuar();
j=0;
fila=5;
clrscr();
}
else
{
fila=fila+10;

}
printf("\nThe Computer plays in:");printf ("(%d, %d)\n", x, y);
mostrar_tablero1 (tablero,5,fila);
j++;

}
else

/*turn for the human*/
{
figura_activa= (letra_para_jugar=='X')? X:O;
{

juega_en_la_posicion = 0;
juega_en_la_posicion++;
}
else
{
}
marcar_el_tablero
(&tablero,juega_en_la_posicion,figura_activa);

}

if (j==2)
{
pulsar_tecla_para_continuar();
j=0;
fila=5;
clrscr();
}
else
{
fila=fila+10;

}

mostrar_tablero1 (tablero,5,fila);
j++;

{
printf ("\nThe WINNER IS ... ");
if(turno =='C')
printf( "!!!The Computer!!!");
else
printf("!!!The Human!!!!");
se_acabo = cierto;
}
else
{
if (esta_lleno_el_tablero (tablero)==cierto)
{
printf ("\nGame is Daw");
se_acabo = cierto;
} /* end if esta_lleno_el_tablero */
else
{
/* chage the turn */
turno = ((turno =='C') ?'H' : 'C');
}
}

}/*fin of while*/

}/*fin of play()*/

void marcar_el_tablero (tipo_tablero * tablero, int posicion,
{

} /* end of marcar el tablero */

/* function to read play */
int capturar_jugada (tipo_tablero tablero, int linea)
{
int columna, fila, posicion;
int ok;

do {

gotoxy(1,linea);
printf ("Write the position in which is going to
scanf ("%d,%d", &columna, &fila);
fflush (stdin);
/* go from matrix position to vector position*/
posicion = (columna - 1) + ancho_tablero * (fila - 1);

if ((posicion < 0) || (posicion >= numero_de_cuadros) ||
gotoxy(1,linea);printf ("
");
gotoxy(1,linea); printf ("This position is occupied or it is
not enables, press enter");getch();
gotoxy(1,linea);printf ("
");
ok = falso;
}
else {
ok = cierto;
}
} while ( ok==falso);

return posicion;
} /*end of function to read play*/

/* Function the board is full*/
int esta_lleno_el_tablero (tipo_tablero tablero)
{
{
return cierto;
}
else
{
return falso;
}
}

/* end of esta_lleno_el_tablero*/

/*function find the best movement*/

{
int esquinas[4];

esquinas[0]=0;
esquinas[1]= ancho_tablero - 1 ;

randomize();
pos_inicial=0;

{
/* There is no full squares. then play in the left upper
corner*/
}
else
{
{
{
}
else
{
/* if the left upper corner is occupied, search other corner */
do
{
/* choose a corner */
esquina = esquinas[random (4)];
else
} while (ocupado==cierto || esquina==(ancho_tablero - 1));

} /* end if else - primer cuadro lleno*/
}
else
{
/* 2 or more squares are already marked */
/* call minimax or minimax with alpha-beta pruning */
if (minimax_o_alfabeta=='1')
{

}
if (minimax_o_alfabeta=='2')
{

-10, 10);
}

}
}

} /* end find the best movement */

/*Function that indicates if there is a winner*/
{
int i, posicion;

/* check rows */
posicion = 0;
{

/* verifica que en cada fila haya ocurrencias de la misma figura
'X' o 'O' */
for (i = (posicion + 1); i < (posicion + ancho_tablero); i++)

posicion += ancho_tablero;
} /* fin while - que revisa filas */

/* check columns */
posicion = 0;
while ((ganador==falso) && (posicion < ancho_tablero)) {

/*check that in each column there are occurrences of the same figure
'X' or 'O' */

for (i = (posicion + ancho_tablero); i < numero_de_cuadros; i +=
ancho_tablero)

posicion++;
} /* end while - check columns */

/* check diagonals */
/* diagonal from left upper corner */
for (i = (ancho_tablero + 1); i < numero_de_cuadros; i +=
(ancho_tablero + 1))

}
/* diagonal from right upper corner */
for (i = (2 * ancho_tablero - 2); i < (numero_de_cuadros - 1);
i += (ancho_tablero - 1))

} /* end if */

}

/* Function Alpha Beta for Computer Player*/
figura_activa,

{

{
/* The human win with the last play */
}
else
{
if (esta_lleno_el_tablero(tablero)==cierto)
{
/* the game is over */
}
else
{
/* here it begins the recursive search */
for (i = 0; (i < numero_de_cuadros) && (*estadox < beta); i++)
{
{
marcar_el_tablero (&tablero, i, figura_activa);
alfabeta_humano (tablero, ((figura_activa == X) ? O : X),
borrar_figura (&tablero, i);
{
/* found a better move for a player */
}
}
}

}
}

}

/* Function Alpha Beta Human*/
void alfabeta_humano (tipo_tablero tablero,

{
int i, auxiliar;

/* goal state amd terminal case, player has won by last move */
}
else {
if (esta_lleno_el_tablero (tablero)==cierto) {
/* terminal case */
}
else {
/* recursive search */

for (i = 0; (i < numero_de_cuadros) && (*estadox > alfa) ; i++) {
marcar_el_tablero (&tablero, i,figura_activa);

auxiliar = alfabeta_computador (tablero, ((figura_activa == X)
? O : X),
borrar_figura (&tablero, i);

}
}
}

}
}

}

/* fin alfabeta_humano */

/*Function Clear Figure*/
void borrar_figura (tipo_tablero * tablero, int lugar)
{

} /* end if */
}
/* end of borrar figura*/

void pulsar_tecla_para_continuar(void)
{

printf("\nPress any key to continue\n");
getch();
}

/* Fuction Minimax for Computer Player*/
figura_activa,

{

}
else {

if (esta_lleno_el_tablero(tablero)==cierto) {

}
else {

for (i = 0; i< numero_de_cuadros ; i++) {
marcar_el_tablero (&tablero, i, figura_activa);
minimax_humano (tablero, ((figura_activa == X) ? O : X),
borrar_figura (&tablero, i);

}
}
}
}
}

}

/*Function Minimax for Human Player*/
void minimax_humano (tipo_tablero tablero,

{
int i, auxiliar;

/* goal state amd terminal case, player has won by last move */
}
else {
if (esta_lleno_el_tablero (tablero)==cierto) {
/* terminal case */
}
else {
/*
recursive search
*/

for (i = 0; i < numero_de_cuadros ; i++)
{
{
marcar_el_tablero (&tablero, i,figura_activa);

auxiliar = minimax_computador (tablero, ((figura_activa == X)
? O : X),
borrar_figura (&tablero, i);
{

}
}
}

}
}

}
/* end of minimax_humano */

**********************My Code End Here**************************

Matt Gessner
Guest
Posts: n/a

 11-12-2003
JB Cardenas <(E-Mail Removed)> wrote:
> Hello all, attached to this message is my code for a Tic Tac Toe game,
> this game plays with the strategy Minimax and Minimax Alpha Beta
> Prunnig. Somebody can review my code to find errors and to test my
> algorithms.

Good luck getting anyone to read over 800 lines of your code, especially
when it's written in Spanish.

I'd recommend you go find a Net Etiquette book or article and then

This isn't the place to post your code for others to review.
This is the place to ask question about problems one has regarding
the C language.

> Thanks,

[snip 800+ lines of C code that should have never made it here]

Simon Biber
Guest
Posts: n/a

 11-12-2003
"JB Cardenas" <(E-Mail Removed)> wrote:
> Hello all, attached to this message is my code for a Tic Tac Toe game,
> this game plays with the strategy Minimax and Minimax Alpha Beta
> Prunnig. Somebody can review my code to find errors and to test my
> algorithms.

I'm not reading through 879 lines of code, so I'll rely on my compiler
to pick out the good bits:

> C:\docs\prog\c>gcc -std=c99 -pedantic -Wall -W -O2 cardenas.c -o cardenas
> cardenas.c:21:19: conio.h: No such file or directory

Whoops -- conio is not standard C. It is an unofficial compiler
extension on some MS-DOS compilers. It makes your code very
unportable. I recommend you get rid of it and make do with
plain stdin/stdout.

> cardenas.c:133: warning: return type of `main' is not `int'

The main function should always return int. Make it:
int main(void)

> cardenas.c: In function `main':
> cardenas.c:136: warning: implicit declaration of function `clrscr'
> cardenas.c:138: warning: implicit declaration of function `gotoxy'
> cardenas.c:147: warning: implicit declaration of function `randomize'

These three functions are not part of standard C.

> cardenas.c: In function `jugar':
> cardenas.c:341: warning: unused variable `indice'

Remove the indice variable.

> cardenas.c:485: warning: too many arguments for format

> cardenas.c:494: warning: implicit declaration of function `getch'

This function is not part of standard C.

> cardenas.c:563: warning: implicit declaration of function `random'

This function is not part of standard C.

> cardenas.c:529: warning: unused variable `i'

Remove the i variable.

> cardenas.c:529: warning: `mejor_jugada' might be used uninitialized in this function
> cardenas.c:667: warning: `mejor_jugada' might be used uninitialized in this function
> cardenas.c:783: warning: `mejor_jugada' might be used uninitialized in this function

Come back once you've fixed all those errors.

--
Simon.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post New_kid@nowhere.new VOIP 0 08-11-2007 01:40 PM Lee UK VOIP 4 05-17-2005 04:10 PM No Spam Cisco 3 06-07-2004 10:02 AM =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM Jerry C. Perl Misc 8 11-23-2003 04:11 AM