home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 138.lha / Cycle / LookB4.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  2KB  |  105 lines

  1. /*
  2.   "Look B4 U Leap" uses a similar staregy to "Not So Dizzy"
  3.   except that he prefers the current direction, and looks one cell further
  4.   from his current position, looking ahead a total of four cells. This player
  5.   consistently wins in games played in the 20 millisecond class.
  6.  */
  7. #include "cycles.h"
  8.  
  9. char player_name[] = "Look B4 U Leap";
  10.  
  11. long xdir[] = {0,1,0,-1};
  12. long ydir[] = {-1,0,1,0};
  13.  
  14. static IMAGE myTable;
  15. static nbytes;
  16. static IMAGE *table = 0;
  17. static x_bytes;
  18.  
  19. void mySet(),myClr(),*malloc();
  20. IMAGE *GetScreenMem();
  21.  
  22. strategy_init()
  23. {
  24.   table       = GetScreenMem();
  25.   myTable.x   = table->x;
  26.   myTable.y   = table->y;
  27.   x_bytes     = (myTable.x>>3)+(!!(myTable.x&7));
  28.   nbytes      = myTable.y * x_bytes;
  29.   myTable.buf = (BYTE *)malloc(nbytes);
  30.   if (!myTable.buf) {
  31.     printf("Error, can't get memory for my copy of the board\n");
  32.     return(-1);
  33.   }
  34.   return(0);
  35. }
  36. strategy_finish()
  37. {
  38.   free(myTable.buf);
  39. }
  40.  
  41. strategy()
  42. {
  43.   long x,y,dir;
  44.   long score,best;
  45.   long i,j,d;
  46.  
  47.   GetInfo(&x,&y,&dir);
  48.   best = 0;
  49.   copyBoard();
  50.   for(d=dir,i=0; i<4; i++,d=TURN_RIGHT(d)) {
  51.     score = value(x + xdir[d],y + ydir[d],3);
  52.     if (score > best) {
  53.       dir  = d;
  54.       best = score;
  55.     }
  56.   }
  57.   return(dir);
  58. }
  59. value(x,y,d)
  60. {
  61.   long i,best,v;
  62.  
  63.   if (myInquire(x,y)) return(0);
  64.   if (d > 0) {
  65.     mySet(x,y);
  66.     best = 0;
  67.     for (i=0; i<4; i++) {
  68.       v = value(x + xdir[i], y + ydir[i], d-1);
  69.       if (v>best) best = v;
  70.     }
  71.     myClr(x,y);
  72.     return(1 + best);
  73.   }
  74.   return(1);
  75. }
  76. copyBoard()
  77. {
  78.   register i;
  79.   register char *p,*q;
  80.  
  81.   q = myTable.buf;
  82.   p = table->buf;
  83.   for (i = nbytes; --i>=0; )
  84.     *q++ = *p++;
  85. }
  86. static power[] = {1,2,4,8,16,32,64,128};
  87. /* return the sttaus of the pixel @ x,y */
  88. myInquire(x,y)
  89. {
  90.   if (x<0 || y<0 || x>=myTable.x || y>=myTable.y) return(1);
  91.   return((myTable.buf[(x>>3)+y*x_bytes]&power[x&7]) != 0);
  92. }
  93. /* return the status of the pixel @ x,y */
  94. void mySet(x,y)
  95. {
  96.   if (x<0 || y<0 || x>=myTable.x || y>=myTable.y) return;
  97.   myTable.buf[(x>>3)+y*x_bytes] |= power[x&7];
  98. }
  99. /* return the status of the pixel @ x,y */
  100. void myClr(x,y)
  101. {
  102.   if (x<0 || y<0 || x>=myTable.x || y>=myTable.y) return;
  103.   myTable.buf[(x>>3)+y*x_bytes] &= ~power[x&7];
  104. }
  105.