/*
  "Look B4 U Leap" uses a similar staregy to "Not So Dizzy"
  except that he prefers the current direction, and looks one cell further
  from his current position, looking ahead a total of four cells. This player
  consistently wins in games played in the 20 millisecond class.
 */
#include "cycles.h"

char player_name[] = "Look B4 U Leap";

long xdir[] = {0,1,0,-1};
long ydir[] = {-1,0,1,0};

static IMAGE myTable;
static nbytes;
static IMAGE *table = 0;
static x_bytes;

void mySet(),myClr(),*malloc();
IMAGE *GetScreenMem();

strategy_init()
{
  table       = GetScreenMem();
  myTable.x   = table->x;
  myTable.y   = table->y;
  x_bytes     = (myTable.x>>3)+(!!(myTable.x&7));
  nbytes      = myTable.y * x_bytes;
  myTable.buf = (BYTE *)malloc(nbytes);
  if (!myTable.buf) {
    printf("Error, can't get memory for my copy of the board\n");
    return(-1);
  }
  return(0);
}
strategy_finish()
{
  free(myTable.buf);
}

strategy()
{
  long x,y,dir;
  long score,best;
  long i,j,d;

  GetInfo(&x,&y,&dir);
  best = 0;
  copyBoard();
  for(d=dir,i=0; i<4; i++,d=TURN_RIGHT(d)) {
    score = value(x + xdir[d],y + ydir[d],3);
    if (score > best) {
      dir  = d;
      best = score;
    }
  }
  return(dir);
}
value(x,y,d)
{
  long i,best,v;

  if (myInquire(x,y)) return(0);
  if (d > 0) {
    mySet(x,y);
    best = 0;
    for (i=0; i<4; i++) {
      v = value(x + xdir[i], y + ydir[i], d-1);
      if (v>best) best = v;
    }
    myClr(x,y);
    return(1 + best);
  }
  return(1);
}
copyBoard()
{
  register i;
  register char *p,*q;

  q = myTable.buf;
  p = table->buf;
  for (i = nbytes; --i>=0; )
    *q++ = *p++;
}
static power[] = {1,2,4,8,16,32,64,128};
/* return the sttaus of the pixel @ x,y */
myInquire(x,y)
{
  if (x<0 || y<0 || x>=myTable.x || y>=myTable.y) return(1);
  return((myTable.buf[(x>>3)+y*x_bytes]&power[x&7]) != 0);
}
/* return the status of the pixel @ x,y */
void mySet(x,y)
{
  if (x<0 || y<0 || x>=myTable.x || y>=myTable.y) return;
  myTable.buf[(x>>3)+y*x_bytes] |= power[x&7];
}
/* return the status of the pixel @ x,y */
void myClr(x,y)
{
  if (x<0 || y<0 || x>=myTable.x || y>=myTable.y) return;
  myTable.buf[(x>>3)+y*x_bytes] &= ~power[x&7];
}
