home *** CD-ROM | disk | FTP | other *** search
/ Ultimate Game Collection / XULTI.ISO / 452 / game.cc next >
C/C++ Source or Header  |  1993-12-01  |  8KB  |  386 lines

  1. #include <stdlib.h>
  2.  
  3. /*============= STRUCTURES ================*/
  4.  
  5. struct GAME
  6. {
  7.     int nbrcoups;
  8.     int lastX,lastY;
  9.     int t[9][8];
  10.     int lastpos[8];
  11. } game;
  12.  
  13. /*============= DEFINES ====================*/
  14.  
  15. #define COMPUTER -1
  16. #define PLAYER       1
  17. #define NOBODY       0
  18. #define OTHER     2
  19.  
  20. #define COUP int                /* Generateur de coup */
  21.  
  22. #define MaxVal 128
  23. #define InitCoup(x) x=1
  24. #define CoupSvt(x) x++
  25. #define LastCoup(x) (x>7)
  26.                 /* Mouvements dans le jeu */
  27. #define put(i,colour) \
  28.     game.t[i][++game.lastpos[i]]=colour;\
  29.     game.nbrcoups++;\
  30.     game.lastY=game.lastpos[i];\
  31.     game.lastX=i;\
  32.  
  33. #define get(i) \
  34.     game.t[i][game.lastpos[i]--]=0;\
  35.     game.nbrcoups--;\
  36.  
  37. #define finish() (game.nbrcoups==46)            /* regles du jeu */
  38.  
  39. #define good(i) (game.lastpos[i]!=6)
  40.  
  41. /*============= PROTOTYPES ================*/
  42.  
  43. void InitGame(void);
  44. int win (int);
  45. int Heurist(int, int);
  46. int Max(int, int);
  47. int Min(int, int);
  48. int Min_Max(int);
  49.  
  50. /* ============ FUNCTIONS =================*/
  51.  
  52. void InitGame()
  53. {
  54.     short i,j;
  55.     game.nbrcoups=0;
  56.     for (i=0;i<=8;i++)
  57.     {
  58.         game.t[i][0]=OTHER;
  59.         game.t[i][7]=OTHER;
  60.     }
  61.     for(i=0;i<=7;i++)
  62.     {
  63.         game.t[0][i]=OTHER;
  64.         game.t[8][i]=OTHER;
  65.         game.lastpos[i]=0;
  66.     }
  67.     for (i=1;i<=7;i++)
  68.         for(j=1;j<=6;j++)
  69.             game.t[i][j]=NOBODY;
  70. }
  71.  
  72. int win(colour)
  73. int colour;
  74. {
  75.     int c;                    /* vertical */
  76.     c=0;
  77.     if (game.t[game.lastX][game.lastY-1]==colour)
  78.     {
  79.         c++;
  80.         if (game.t[game.lastX][game.lastY-2]==colour)
  81.         {
  82.             c++;
  83.             if (game.t[game.lastX][game.lastY-3]==colour)
  84.                 return (1);
  85.         }
  86.     }
  87.     c=0;                    /* horizontal */
  88.     if (game.t[game.lastX-1][game.lastY]==colour)
  89.     {
  90.         c++;
  91.         if (game.t[game.lastX-2][game.lastY]==colour)
  92.         {
  93.             c++;
  94.             if (game.t[game.lastX-3][game.lastY]==colour)
  95.                 return(1);
  96.         }
  97.     }
  98.     if (game.t[game.lastX+1][game.lastY]==colour)
  99.     {
  100.         c++;
  101.         if (game.t[game.lastX+2][game.lastY]==colour)
  102.         {
  103.             c++;
  104.             if (game.t[game.lastX+3][game.lastY]==colour)
  105.                 return(1);
  106.         }
  107.     }
  108.     if (c>=3) return(1);
  109.     c=0;                    /* diagonal gauche */
  110.     if (game.t[game.lastX+1][game.lastY-1]==colour)
  111.     {
  112.         c++;
  113.         if (game.t[game.lastX+2][game.lastY-2]==colour)
  114.         {
  115.             c++;
  116.             if (game.t[game.lastX+3][game.lastY-3]==colour)
  117.                 return (1);
  118.         }
  119.     }
  120.     if (game.t[game.lastX-1][game.lastY+1]==colour)
  121.     {
  122.         c++;
  123.         if (game.t[game.lastX-2][game.lastY+2]==colour)
  124.         {
  125.             c++;
  126.             if (game.t[game.lastX-3][game.lastY+3]==colour)
  127.                 return (1);
  128.         }
  129.     }
  130.     if (c>=3) return(1);
  131.     c=0;                    /* diagonal droite */
  132.     if (game.t[game.lastX+1][game.lastY+1]==colour)
  133.     {
  134.         c++;
  135.         if (game.t[game.lastX+2][game.lastY+2]==colour)
  136.         {
  137.             c++;
  138.             if (game.t[game.lastX+3][game.lastY+3]==colour)
  139.                 return(1);
  140.         }
  141.     }
  142.     if (game.t[game.lastX-1][game.lastY-1]==colour)
  143.     {
  144.         c++;
  145.         if (game.t[game.lastX-2][game.lastY-2]==colour)
  146.         {
  147.             c++;
  148.             if (game.t[game.lastX-3][game.lastY-3]==colour)
  149.                 return (1);
  150.         }
  151.     }
  152.     if (c>=3) return(1);
  153.     return(0);
  154. }
  155.  
  156. int Heurist(X,colour)
  157. int X; int colour;
  158. {
  159.     int c,Y,vertic,horiz,diagg,diagd;
  160.     /* vertical */
  161.     Y=game.lastpos[X];
  162.  
  163.     vertic=0;
  164.     c=0;
  165.     if ((game.t[X][Y-1]==colour)||(game.t[X][Y-1]==0))
  166.     {
  167.         vertic++;
  168.         if (game.t[X][Y-1]==colour) c++;
  169.         if ((game.t[X][Y-2]==colour)||(game.t[X][Y-2]==0))
  170.         {
  171.             vertic++;
  172.             if (game.t[X][Y-2]==colour) c++;
  173.             if (game.t[X][Y-3]==colour) c++;
  174.             else
  175.                 if (game.t[X][Y-3]==0) vertic++;
  176.         }
  177.     }
  178.  
  179.     if ((game.t[X][Y+1]==colour)||(game.t[X][Y+1]==0))
  180.     {
  181.         vertic++;
  182.         if (game.t[X][Y+1]==colour) c++;
  183.         if ((game.t[X][Y+2]==colour)||(game.t[X][Y+2]==0))
  184.         {
  185.             vertic++;
  186.             if (game.t[X][Y+2]==colour) c++;
  187.             if (game.t[X][Y+3]==colour) c++;
  188.             else
  189.                 if (game.t[X][Y+3]==0) vertic++;
  190.         }
  191.     }
  192.     if (vertic>=3) vertic=vertic-2+2*c;
  193.     else vertic=0;
  194.  
  195.     c=0;                                                    /* horizontal */
  196.     horiz=0;
  197.     if ((game.t[X-1][Y]==colour)||(game.t[X-1][Y]==0))
  198.     {
  199.         horiz++;
  200.         if (game.t[X-1][Y]==colour) c++;
  201.         if ((game.t[X-2][Y]==colour)||(game.t[X-2][Y]==0))
  202.         {
  203.             horiz++;
  204.             if (game.t[X-2][Y]==colour) c++;
  205.             if (game.t[X-3][Y]==colour) c++;
  206.             else
  207.                 if (game.t[X-3][Y]==0) horiz++;
  208.         }
  209.     }
  210.  
  211.     if ((game.t[X+1][Y]==colour)||(game.t[X+1][Y]==0))
  212.     {
  213.         horiz++;
  214.         if (game.t[X+1][Y]==colour) c++;
  215.         if ((game.t[X+2][Y]==colour)||(game.t[X+2][Y]==0))
  216.         {
  217.             horiz++;
  218.             if (game.t[X+2][Y]==colour) c++;
  219.             if (game.t[X+3][Y]==colour) c++;
  220.             else
  221.                 if (game.t[X+3][Y]==0) horiz++;
  222.         }
  223.     }
  224.     if (horiz>=3) horiz=horiz-2+2*c;
  225.     else horiz=0;
  226.  
  227.     c=0;                                            /* diag gauche */
  228.     diagg=0;
  229.     if ((game.t[X-1][Y-1]==colour)||(game.t[X-1][Y-1]==0))
  230.     {
  231.         diagg++;
  232.         if (game.t[X-1][Y-1]==colour) c++;
  233.         if ((game.t[X-2][Y-2]==colour)||(game.t[X-2][Y-2]==0))
  234.         {
  235.             diagg++;
  236.             if (game.t[X-2][Y-2]==colour) c++;
  237.             if (game.t[X-3][Y-3]==colour) c++;
  238.             else
  239.                 if (game.t[X-3][Y-3]==0) diagg++;
  240.         }
  241.     }
  242.  
  243.     if ((game.t[X+1][Y+1]==colour)||(game.t[X+1][Y+1]==0))
  244.     {
  245.         diagg++;
  246.         if (game.t[X+1][Y+1]==colour) c++;
  247.         if ((game.t[X+2][Y+2]==colour)||(game.t[X+2][Y+2]==0))
  248.         {
  249.             diagg++;
  250.             if (game.t[X+2][Y+2]==colour) c++;
  251.             if (game.t[X+3][Y+3]==colour) c++;
  252.             else
  253.                 if (game.t[X+3][Y+3]==0) diagg++;
  254.         }
  255.     }
  256.     if (diagg>=3) diagg=diagg-2+2*c;
  257.     else diagg=0;
  258.  
  259.     c=0;                                    /* diag droite */
  260.     diagd=0;
  261.     if ((game.t[X+1][Y-1]==colour)||(game.t[X+1][Y-1]==0))
  262.     {
  263.         diagd++;
  264.         if (game.t[X+1][Y-1]==colour) c++;
  265.         if ((game.t[X+2][Y-2]==colour)||(game.t[X+2][Y-2]==0))
  266.         {
  267.             diagd++;
  268.             if (game.t[X+2][Y-2]==colour) c++;
  269.             if (game.t[X+3][Y-3]==colour) c++;
  270.             else
  271.                 if (game.t[X+3][Y-3]==0) diagd++;
  272.         }
  273.     }
  274.     if ((game.t[X-1][Y+1]==colour)||(game.t[X-1][Y+1]==0))
  275.     {
  276.         diagd++;
  277.         if (game.t[X-1][Y+1]==colour) c++;
  278.         if ((game.t[X-2][Y+2]==colour)||(game.t[X-2][Y+2]==0))
  279.         {
  280.             diagd++;
  281.             if (game.t[X-2][Y+2]==colour) c++;
  282.             if (game.t[X-3][Y+3]==colour) c++;
  283.             else
  284.                 if (game.t[X-3][Y+3]==0) diagd++;
  285.         }
  286.     }
  287.     if (diagd>=3) diagd=diagd-2+2*c;
  288.     else diagd=0;
  289.  
  290.     return(vertic+horiz+diagg+diagd);
  291. }
  292.  
  293. /* ============================= Min Max ========================== */
  294.  
  295.  
  296. int Min(level, Seuil)
  297. int level; int Seuil;
  298. {
  299.     int BestVal,Val;
  300.     COUP Coup;
  301.     InitCoup(Coup);
  302.     BestVal=MaxVal;
  303.     do
  304.     {
  305.         if (good(Coup))
  306.         {
  307.             put(Coup,PLAYER);
  308.             if (win(PLAYER)) Val= -level;
  309.             else if ((level==0)||(finish())) Val=0;
  310.             else Val=Max(level-1,BestVal);
  311.             get(Coup);
  312.             if (Val<BestVal)
  313.                 {
  314.                 BestVal=Val;
  315.                 if (BestVal<Seuil) break;
  316.                 }
  317.         }
  318.         CoupSvt(Coup);
  319.     }
  320.     while (!LastCoup(Coup));
  321.     return(BestVal);
  322. }
  323.  
  324. int Max(level, Seuil)
  325. int level; int Seuil;
  326.     int BestVal,Val;
  327.     COUP Coup;
  328.     InitCoup(Coup);
  329.     BestVal= -MaxVal;
  330.     do
  331.     {
  332.         if (good(Coup))
  333.         {
  334.             put(Coup,COMPUTER);
  335.             if (win(COMPUTER)) Val=level;
  336.             else if ((level==0)||(finish())) Val=0;
  337.                 else Val=Min(level-1,BestVal);
  338.             get(Coup);
  339.             if (Val>BestVal)
  340.                 {
  341.                 BestVal=Val;
  342.                 if (BestVal>Seuil) break;
  343.                 }
  344.         }
  345.         CoupSvt(Coup);
  346.     }
  347.     while (!LastCoup(Coup));
  348.     return(BestVal);
  349. }
  350.  
  351. COUP Min_Max(level)
  352. int level;
  353. {
  354.     int Val,BestVal,BestHVal,HVal;
  355.     COUP Coup,BestCoup;
  356.     InitCoup(Coup);
  357.     BestHVal=0;
  358.     BestVal= -MaxVal;
  359.     while (!LastCoup(Coup))
  360.     {
  361.         if (good(Coup))
  362.         {
  363.             put(Coup,COMPUTER);
  364.             if (win(COMPUTER)) Val=level;
  365.             else if (finish()||(level==0)) Val=0;
  366.             else Val=Min(level,BestVal);
  367.             if (BestVal<=Val)
  368.             {
  369.                 HVal=Heurist(Coup,COMPUTER);
  370.                 if ((BestVal<Val)||((BestVal==Val)&&
  371.                     ((BestHVal<HVal)||
  372.                     ((BestHVal==HVal)&&(rand()<RAND_MAX/2)))))
  373.                 {
  374.                     BestVal=Val;
  375.                     BestCoup=Coup;
  376.                     BestHVal=HVal;
  377.                 }
  378.             }
  379.             get(Coup);
  380.         }
  381.         CoupSvt(Coup);
  382.     }
  383.     return(BestCoup);
  384. }
  385.