|
/***************************************************************************
* Copyright (C) 2007 by Lorenzo La Porta *
* lorelapo@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
char tris[3][3];
int nvuote(void);
int main(int, char**);
void print(void);
void move(void);
int valutamossa(char, int);
int ctrlwin(char);
int endgame(void);
void leggimossa(void);
int psble(int, int);
int nvuote(void)
{
int i,j,vuote=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(psble(i,j))vuote++;
return vuote;
}
int psble(int i, int j)
{
return tris[i][j]==' ';
}
int endgame(void)
{
register int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(psble(i,j))return 0;
return 1;
}
int ctrlwin(char wnr)
{
register int i;
if(tris[0][0]==wnr&&tris[1][1]==wnr&&tris[2][2]==wnr)return 1;
if(tris[2][0]==wnr&&tris[1][1]==wnr&&tris[0][2]==wnr)return 1;
for(i=0;i<3;i++)
{
if(tris[i][0]==wnr&&tris[i][1]==wnr&&tris[i][2]==wnr)return 1;
if(tris[0][i]==wnr&&tris[1][i]==wnr&&tris[2][i]==wnr)return 1;
}
return 0;
}
void leggimossa(void)
{
int i;
do
{
printf("\nInserire la mossa : ");
scanf("%d",&i);
if((i>=0)&&(i<=9))
{
if(i<=3)
tris[2][i-1]='X';
else if(i<=6)
tris[1][i-4]='X';
else
tris[0][i-7]='X';
}
}while(i<1||i>9);
}
void print(void)
{
register int i,j;
char alpha='A';
for(i=0;i<3;i++)
{
putchar(alpha++);
for(j=0;j<3;j++)
putchar('\n');
}
}
void move(void)
{
int i,j;
int max=INT_MIN,mi=1,mj=1,t;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(psble(i,j))
{
tris[i][j]='O';
t=valutamossa('X', 20);
if(t>max)
{
max=t;
mi=i;
mj=j;
}
tris[i][j]=' ';
}
tris[mi][mj]='O';
}
int valutamossa(char wnr, int deep)//Minimax
{
if(ctrlwin('O'))
return INT_MAX;
if(endgame())
return 0;
int i, j, res, tmp;
if(wnr=='X')
{
res=1;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(psble(i,j))
{
tris[i][j]='X';
if(ctrlwin('X'))
if(deep==20)
{
tris[i][j]=' ';
return INT_MIN;
}
else
res-=2;
else if((tmp=valutamossa('O', deep - 1))<res)
res=tmp;
tris[i][j]=' ';
}
}
}
else
{
res=-1;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(psble(i,j))
{
tris[i][j]='O';
if(ctrlwin('O'))
res+=2;
else if((tmp=valutamossa('X', deep - 1))>res)
res=tmp;
tris[i][j]=' ';
}
}
}
return res;
}
int main(int argc, char**args)
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
tris[i][j]=' ';
printf("Benvenuto in Tris: per indicare\n"
"la mossa usa il tastierino numerico\n\n");
for(;;)
{
print();
leggimossa();
if(ctrlwin('X'))
{
print();
printf("Il giocatore vince\n");
break;
}
move();
if(ctrlwin('O'))
{
print();
printf("Il computer vince\n");
break;
}
if(endgame())
{
print();
break;
}
}
}
|
|