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

  1. /*
  2.   "Not so Dizzy" is a middleweight player who evaluates the scores around
  3.   him in the manner of "Round the CLock" and "Keep 'em Flying", but with a 
  4.   more sophisticated evaluation algorithm. Notice that, for speed, the arena
  5.   memory is searched directly instead of on a cell-by-cell basis via the
  6.   Inquire() function: a local copy of the board is made so that it may be
  7.   searched and modified withoug affecting the actual arena. The
  8.   strategy_init and strategy_finish functions allocate and free the
  9.   required memory.
  10.  */
  11. #include "cycles.h"
  12.  
  13. char player_name[] = "Not so Dizzy";
  14.  
  15. long xdir[] = {0,1,0,-1};
  16. long ydir[] = {-1,0,1,0};
  17.  
  18. static IMAGE myTable;
  19. static nbytes;
  20. static IMAGE *table = 0;
  21. static x_bytes;
  22.  
  23. void mySet(),myClr(),*malloc();
  24. IMAGE *GetScreenMem();
  25.  
  26. strategy_init()
  27. {
  28.   table       = GetScreenMem();
  29.   myTable.x   = table->x;
  30.   myTable.y   = table->y;
  31.   x_bytes     = (myTable.x>>3)+(!!(myTable.x&7));
  32.   nbytes      = myTable.y * x_bytes;
  33.   myTable.buf = (BYTE *)malloc(nbytes);
  34.   if (!myTable.buf) {
  35.     printf("Error, can't get memory for my copy of the board\n");
  36.     return(-1);
  37.   }
  38.   return(0);
  39. }
  40. strategy_finish()
  41. {
  42.   free(myTable.buf);
  43. }
  44.  
  45. strategy()
  46. {
  47.   long x,y,dir;
  48.   long score,best;
  49.   long i,j,d;
  50.  
  51.   GetInfo(&x,&y,&dir);
  52.   best = 0;
  53.   copyBoard();
  54.   for(d=TURN_RIGHT(dir), i=0; i<4; i++,d=TURN_RIGHT(d)) {
  55.     score = value(x + xdir[d],y + ydir[d],2);
  56.     if (score > best) {
  57.       dir  = d;
  58.       best = score;
  59.     }
  60.   }
  61.   return(dir);
  62. }
  63. value(x,y,d)
  64. {
  65.   long i,best,v;
  66.  
  67.   if (myInquire(x,y)) return(0);
  68.   if (d > 0) {
  69.     mySet(x,y);
  70.     best = 0;
  71.     for (i=0; i<4; i++) {
  72.       v = value(x + xdir[i], y+ ydir[i], d-1);
  73.       if (v>best) best = v;
  74.     }
  75.     myClr(x,y);
  76.     return(1 + best);
  77.   }
  78.   return(1);
  79. }
  80. copyBoard()
  81. {
  82.   register i;
  83.   register char *p,*q;
  84.  
  85.   q = myTable.buf;
  86.   p = table->buf;
  87.   for (i = nbytes; --i>=0; )
  88.     *q++ = *p++;
  89. }
  90. static power[] = {1,2,4,8,16,32,64,128};
  91. /* return the sttaus of the pixel @ x,y */
  92. myInquire(x,y)
  93. {
  94.   if (x<0 || y<0 || x>=myTable.x || y>=myTable.y) return(1);
  95.   return((myTable.buf[(x>>3)+y*x_bytes]&power[x&7]) != 0);
  96. }
  97. /* return the status of the pixel @ x,y */
  98. void mySet(x,y)
  99. {
  100.   if (x<0 || y<0 || x>=myTable.x || y>=myTable.y) return;
  101.   myTable.buf[(x>>3)+y*x_bytes] |= power[x&7];
  102. }
  103. /* return the status of the pixel @ x,y */
  104. void myClr(x,y)
  105. {
  106.   if (x<0 || y<0 || x>=myTable.x || y>=myTable.y) return;
  107.   myTable.buf[(x>>3)+y*x_bytes] &= ~power[x&7];
  108. }
  109.