Tag Archive for lol

Maze builder for Arduino

Recently I bought the LOL shield for Arduino. This is for a learning platform I’m building for a very intelligent kid, age 8. He is borring himself to death at school. So I thought lets build him a platform to teach electronics and programming. The LOL shield is perfect for this and the library has several games. Now I’m building this new game, Maze.

It makes a maze in a boolean array and displays only a small part of it on the screen. The user can only see the part he would see if he is physically there, and it is dark.

At this moment the maze making routine is ready. Next I will continue with building the game and from that moment on it will become hardware dependent. So here is the maze routine and later I will post the rest.

/* maze builder
 create a maze in a bolean array 
 Display the maze on the LOL shield
 use the keys defined to move around the maze
 Cheat sheet is displayed on serial port (x,y reversed)
 TODO something nice when won
 coordinate display

 */
#include 

#define xgrid 20  
#define ygrid 20
#define xMazeDisplay 10
#define yMazeDisplay 5
#define dim1 7
#define dim2 7
#define dim3 7

#define upKey 36
#define rightKey 38
#define downKey 40
#define leftKey 42

bool maze [xgrid][ygrid];
unsigned char mazeEndX,mazeEndY;

void setup()
{
  LedSign::Init(GRAYSCALE);            //Initilizes the LoL Shield

  pinMode(upKey, INPUT); //set key inputs
  pinMode(rightKey, INPUT);
  pinMode(downKey, INPUT);
  pinMode(leftKey, INPUT);
  digitalWrite(upKey,HIGH); //make pullup
  digitalWrite(rightKey,HIGH);
  digitalWrite(downKey,HIGH);
  digitalWrite(leftKey,HIGH);
  Serial.begin(9600);
  randomSeed(analogRead(0)+analogRead(3));
  for(int x=0; x< xgrid; x++) //empty maze
  {
    for(int y=0; y< ygrid; y++)
      maze[x][y]=0;
  }

  Serial.println("strart run");
}

void loop()
{
  int x,y; 
  buildMaze();
  //testMaze();
  for( x=0; x< xgrid; x++) //print maze
  {
    for( y=0; y< ygrid; y++)
    {
      if(maze[x][y]==0)
        Serial.print('X');
      else
        Serial.print(' ');
    }
    Serial.println('|');

  }

  displayMaze();
  delay(5000); 
  LedSign::Clear();
  x=0;
  y=0;
  displayMazeSpot(x,y);
  //  delay(5000);

  // eraseMazeSpot();
  // delay(5000);

  Serial.print("Maze End =");
  Serial.print(mazeEndX,DEC);
  Serial.print(',');
  Serial.println(mazeEndY,DEC);

  while(1)
  {
    if(moveMaze(x,y, 500)==1)
    { //if won do something nice, restart game
       break;
    }  
    LedSign::Set(xMazeDisplay,yMazeDisplay,7);
    delay(100);
    LedSign::Set(xMazeDisplay,yMazeDisplay,0);
    delay(100);
  };
}
/*
void testMaze()
{
  maze[0][0]=1;
  for( int x=0;xmillis())
  {
    if(digitalRead(upKey)==LOW)
    {//do move up
      if(y>0)
      {
        if(maze[x][y-1]==1)
          y--;

        Serial.print("x=");
        Serial.print(x,DEC);
        Serial.print(" y=");
        Serial.println(y,DEC);
        break;
      }
    }
    if(digitalRead(rightKey)==LOW)
    {//do move right
      if(x0)
      {
        if(maze[x-1][y]==1)
          x--;
        Serial.print("x=");
        Serial.print(x,DEC);
        Serial.print(" y=");
        Serial.println(y,DEC);
        break;
      }
    }

  };
  //eraseMazeSpot();
  LedSign::Clear();
  displayMazeSpot(x,y);
  if(x==mazeEndX && y==mazeEndY)
    return 1;
  else 
    return 0;

}

void eraseMazeSpot()
{
  int xe,xf,ye,yf;
  xe=xMazeDisplay-3;
  xf=xMazeDisplay+3;
  ye=yMazeDisplay-3;
  yf=yMazeDisplay+3;
  if (xexgrid)
    xf=xgrid;
  if (yeygrid)
    yf=ygrid;
  for(int x=xe;x=0 && x=0 && y=0 && x=0 && y14)
  //  x=14;
  y=ygrid;
  //if(ygrid>9)
  //  y=9;
  for(int xx=0; xx< x; xx++) //print maze
  {
    for(int yy=0; yy< y; yy++)
    {
      LedSign::Set(xx,yy,(1-maze[xx][yy])*7); //maze held at 90 degrees turned
    }
  }
}

void buildMaze()
{
  /*
in what direction to go?
   1=5= right x+1
   2=6= down Y+1
   3=7= left x-1
   4= up y-1
   direction = old direction + rnd(3)
   Is the new direction correct?
   coordinate assignments for up move see table below.
   the E & F fields could be ommited but the maze would then seem to have many diagonal moves.
   0 E D F 0
   0 A B C 0
   0 0 Q 0 0
   0 0 0 0 0
   0 0 0 0 0
   From Q A is not used or the limit of the board, I not used K not used. then A is next Q 
   Same goes for the others. 
   All directions are filled? 
   Test all locations until a spot is found that is used Q where A is not used or the limit of the board, I not used K not used. then A is next Q 
   Same goes for the others.
   Relative positions to check from Q in relation to grid:
   UP      Right      Down      Left
   A(-1,-1)  (1,-1)    (-1,1)    (-1,-1)
   B(0 ,-1)  (1, 0)    ( 0,1)    (-1, 0)
   C(1 ,-1)  (1, 1)    ( 1,1)    (-1, 1)
   D(0 ,-2)  (2, 0)    ( 0,2)    (-2, 1)
   E(-1,-2)  (2,-1)    ( 1,2)    (-2, 1)
   F( 1,-2)  (2, 1)    (-1,2)    (-2,-1)

   First choose rnd(direction) then test if direcrion is ok
   Any used spot is 1 so 1 is road 0 is wall

   */
  char dir, olddir=1;
  int x=0,y=0;
  //char runs=0;
  char tries=0;
  // for(int runs=0;runs= xgrid)
  {
    dx=cx; 
    ex=cx; 
    fx=cx;
  }
  if(maze[ax][ay]==0 && maze[bx][by]==0 && maze[cx][cy]==0 && maze[dx][dy]==0 && maze[ex][ey]==0 && maze[fx][fy]==0)
  {  
    x++;
    maze[x][y]=1;
    return 1;
  } 
  return 0;

}

bool downMove(int x, int y)
{
  char ax, bx, cx, dx, ex, fx, ay, by, cy, dy, ey ,fy;
  ax=x-1;
  bx=x;
  cx=x+1;
  dx=x;
  ex=x+1;
  fx=x-1;
  ay=y+1;
  by=y+1;
  cy=y+1;
  dy=y+2;
  ey=y+2;
  fy=y+2;
  if(ay == ygrid) //test grid limits
    return 0;  //down is not possible
  if (ax= ygrid)
  {
    dy=cy; 
    ey=cy; 
    fy=cy;
  }
  if(maze[ax][ay]==0 && maze[bx][by]==0 && maze[cx][cy]==0 && maze[dx][dy]==0 && maze[ex][ey]==0 && maze[fx][fy]==0)
  {
    y++;
    maze[x][y]=1;
    return 1;
  }
  return 0;
}

bool leftMove(int x, int y)
{
  char ax, bx, cx, dx, ex, fx, ay, by, cy, dy, ey ,fy;

  ax=x-1;
  bx=x-1;
  cx=x-1;
  dx=x-2;
  ex=x-2;
  fx=x-2;
  ay=y-1;
  by=y;
  cy=y+1;
  dy=y;
  ey=y+1;
  fy=y-1;
  if(ax(ygrid-4); y--)
  {
    for(int x=xgrid-1; x>(xgrid-4); x--)
    {
      if(maze[x][y]==1)
      {
        mazeEndX=x;
        mazeEndY=y;
        return;
      }
    }
  }
}