Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
miniMAXTris - tris.c

tris.c

Caricato da:
Scarica il programma completo

  1. /***************************************************************************
  2.  *   Copyright (C) 2007 by Lorenzo La Porta   *
  3.  *   lorelapo@gmail.com   *
  4.  *                                                                         *
  5.  *   This program is free software; you can redistribute it and/or modify  *
  6.  *   it under the terms of the GNU General Public License as published by  *
  7.  *   the Free Software Foundation; either version 2 of the License, or     *
  8.  *   (at your option) any later version.                                   *
  9.  *                                                                         *
  10.  *   This program is distributed in the hope that it will be useful,       *
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13.  *   GNU General Public License for more details.                          *
  14.  *                                                                         *
  15.  *   You should have received a copy of the GNU General Public License     *
  16.  *   along with this program; if not, write to the                         *
  17.  *   Free Software Foundation, Inc.,                                       *
  18.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19.  ***************************************************************************/
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <limits.h>
  29.  
  30. char tris[3][3];
  31.  
  32. int nvuote(void);
  33. int main(int, char**);
  34. void print(void);
  35. void move(void);
  36. int valutamossa(char, int);
  37. int ctrlwin(char);
  38. int endgame(void);
  39. void leggimossa(void);
  40. int psble(int, int);
  41.  
  42. int nvuote(void)
  43. {
  44.         int i,j,vuote=0;
  45.         for(i=0;i<3;i++)
  46.                 for(j=0;j<3;j++)
  47.                         if(psble(i,j))vuote++;
  48.         return vuote;
  49. }
  50.  
  51. int psble(int i, int j)
  52. {
  53.         return tris[i][j]==' ';
  54. }
  55.  
  56. int endgame(void)
  57. {
  58.         register int i,j;
  59.         for(i=0;i<3;i++)
  60.                 for(j=0;j<3;j++)
  61.                         if(psble(i,j))return 0;
  62.         return 1;
  63. }
  64.  
  65. int ctrlwin(char wnr)
  66. {
  67.         register int i;
  68.         if(tris[0][0]==wnr&&tris[1][1]==wnr&&tris[2][2]==wnr)return 1;
  69.         if(tris[2][0]==wnr&&tris[1][1]==wnr&&tris[0][2]==wnr)return 1;
  70.         for(i=0;i<3;i++)
  71.         {
  72.                 if(tris[i][0]==wnr&&tris[i][1]==wnr&&tris[i][2]==wnr)return 1;
  73.                 if(tris[0][i]==wnr&&tris[1][i]==wnr&&tris[2][i]==wnr)return 1;
  74.         }
  75.         return 0;
  76. }
  77.  
  78. void leggimossa(void)
  79. {
  80.         int i;
  81.         do
  82.         {
  83.                 printf("\nInserire la mossa : ");
  84.                 scanf("%d",&i);
  85.                 if((i>=0)&&(i<=9))
  86.                 {
  87.                         if(i<=3)
  88.                                 tris[2][i-1]='X';
  89.                         else if(i<=6)
  90.                                 tris[1][i-4]='X';
  91.                         else
  92.                                 tris[0][i-7]='X';
  93.                 }
  94.         }while(i<1||i>9);
  95.        
  96. }
  97.  
  98. void print(void)
  99. {
  100.         register int i,j;
  101.         char alpha='A';
  102.         for(i=0;i<3;i++)
  103.         {
  104.                 putchar(alpha++);
  105.                 for(j=0;j<3;j++)
  106.                         printf("[%1c]",tris[i][j]);
  107.                 putchar('\n');
  108.         }
  109.         printf("  1  2  3\n");
  110. }
  111.  
  112. void move(void)
  113. {
  114.         int i,j;
  115.         int max=INT_MIN,mi=1,mj=1,t;
  116.         for(i=0;i<3;i++)
  117.                 for(j=0;j<3;j++)
  118.                         if(psble(i,j))
  119.                         {
  120.                                 tris[i][j]='O';
  121.                                 t=valutamossa('X', 20);
  122.                                 printf("%d\n",t);
  123.                                 if(t>max)
  124.                                 {
  125.                                         max=t;
  126.                                         mi=i;
  127.                                         mj=j;
  128.                                 }
  129.                                 tris[i][j]=' ';
  130.                         }
  131.                
  132.         tris[mi][mj]='O';
  133. }
  134.  
  135. int valutamossa(char wnr, int deep)//Minimax
  136. {
  137.         if(ctrlwin('O'))
  138.                 return INT_MAX;
  139.         if(endgame())
  140.                 return 0;
  141.         int i, j, res, tmp;
  142.         if(wnr=='X')
  143.         {
  144.                 res=1;
  145.                 for(i=0;i<3;i++)
  146.                         for(j=0;j<3;j++)
  147.                         {
  148.                                 if(psble(i,j))
  149.                                 {
  150.                                         tris[i][j]='X';
  151.                                         if(ctrlwin('X'))
  152.                                                 if(deep==20)
  153.                                                 {
  154.                                                         tris[i][j]=' ';
  155.                                                         return INT_MIN;
  156.                                                 }
  157.                                                 else
  158.                                                         res-=2;
  159.                                         else if((tmp=valutamossa('O', deep - 1))<res)
  160.                                                 res=tmp;
  161.                                         tris[i][j]=' ';
  162.                                 }
  163.                         }
  164.         }
  165.         else
  166.         {
  167.                 res=-1;
  168.                 for(i=0;i<3;i++)
  169.                         for(j=0;j<3;j++)
  170.                         {
  171.                                 if(psble(i,j))
  172.                                 {
  173.                                         tris[i][j]='O';
  174.                                         if(ctrlwin('O'))
  175.                                                 res+=2;
  176.                                         else if((tmp=valutamossa('X', deep - 1))>res)
  177.                                                 res=tmp;
  178.                                         tris[i][j]=' ';
  179.                                 }
  180.                         }
  181.         }
  182.         return res;
  183. }
  184.  
  185. int main(int argc, char**args)
  186. {
  187.         int i,j;
  188.         for(i=0;i<3;i++)
  189.                 for(j=0;j<3;j++)
  190.                         tris[i][j]=' ';
  191.         printf("Benvenuto in Tris: per indicare\n"
  192.                "la mossa usa il tastierino numerico\n\n");
  193.         for(;;)
  194.         {
  195.                 print();
  196.                 leggimossa();
  197.                 if(ctrlwin('X'))
  198.                 {
  199.                         print();
  200.                         printf("Il giocatore vince\n");
  201.                         break;
  202.                 }
  203.                 move();
  204.                 if(ctrlwin('O'))
  205.                 {
  206.                         print();
  207.                         printf("Il computer vince\n");
  208.                         break;
  209.                 }
  210.                 if(endgame())
  211.                 {
  212.                         print();
  213.                         printf("Patta\n");
  214.                         break;
  215.                 }
  216.         }
  217. }