forum.getinpulse.com
http://www.getinpulse.com/hack/forum/

Maze Generator
http://www.getinpulse.com/hack/forum/viewtopic.php?f=7&t=70
Page 1 of 1

Author:  Duane [ Wed Mar 02, 2011 4:51 pm ]
Post subject:  Maze Generator

Here's a basic maze generator. Note that this is *not* a game - all it does is paint the maze.

/**
* Maze Generator
*
* This program draws a maze on the screen.
*
* It uses a peculiar maze generation algorithm that builds the maze
* one row at a time without keeping a history of the parts it has already
* generated.  This also means that it does not currently retain enough
* information to implement a game.  That would be easy to remedy in itself,
* however this device doesn't have enough memory to do so except for a
* fairly coarse maze.  Perhaps someone can take this code and do more with it.
*
* You can change the density of the maze by setting CELL_SIZE (minimum 2)
*
* Copyright (C) 2011, Duane Maxwell [email protected]
*
* Permission to use, copy, modify, and/or distribute this software for 
* any purpose with or without fee is hereby granted, provided that the 
* above copyright notice and this permission notice appear in all copies. 

* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 
* BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES 
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
* SOFTWARE.
**/

#include <pulse_os.h>
#include <pulse_types.h>

#include <stdint.h>

// a simple random number generator
uint32_t m_w, m_z;

void srand() {
   struct pulse_time_tm tm;
   pulse_get_time_date(&tm);
   m_w = (((tm.tm_hour<<6)+tm.tm_min)<<6)+tm.tm_sec;
   m_z = (((tm.tm_year<<4)+tm.tm_mon)<<5)+tm.tm_mday;
}

uint32_t rand() {
   m_z = 36969L * (m_z & 65535L) + (m_z >> 16);
   m_w = 18000L * (m_w & 65535L) + (m_w >> 16);
   return ((m_w & 0x65535L) + (m_z << 16)) & 0x7fffffff;
}


#define CELL_SIZE 4
#define CELLS_PER_ROW ((SCREEN_WIDTH)/CELL_SIZE)
#define ROWS_PER_SCREEN ((SCREEN_HEIGHT-1)/CELL_SIZE)

int x,y;

void resetMaze() {
   pulse_blank_canvas();
   x = 0;
   y = -CELL_SIZE+1;
}

color24_t white = {0xff,0xff,0xff,0x00};

void output(char *s) {
   char c;
   int i;
   while ((c=(*s++))) {
      switch (c) {
         case '|':
            pulse_set_draw_window(x,y-1,x,y+CELL_SIZE-1);
            for (i=0;i<=CELL_SIZE;i++) {
               pulse_draw_point24(white);
            }
         case '.':
            break;
         case '_':
            pulse_set_draw_window(x,y+CELL_SIZE-1,x+CELL_SIZE,y+CELL_SIZE-1);
            for (i=0;i<=CELL_SIZE;i++) {
               pulse_draw_point24(white);
            }
         case ' ':
            x += CELL_SIZE;
            break;
         case '\n':
            x = 0; y += CELL_SIZE;
            break;
      }
   }
}

void drawMaze() {
   resetMaze();
   char out[3] = "XX";
   int c,e;
   int left[CELLS_PER_ROW],right[CELLS_PER_ROW];
   left[0] = 1;
   for (e=CELLS_PER_ROW; --e; left[e] = right[e] = e)
      output("._");
   output("\n|");
   int height = ROWS_PER_SCREEN;
   while (--height) {
      for (c=CELLS_PER_ROW; --c; output(out)) {
         if (c != (e=left[c-1]) && 6<<27<rand()) {
            right[e] = right[c];
            left[right[c]] = e;
            right[c] = c-1;
            left[c-1] = c;
            out[1] = '.';
         } else {
            out[1] = '|';
         }
         if (c != (e=left[c]) && 6<<27<rand()) {
            right[e] = right[c];
            left[right[c]] = e;
            left[c] = c;
            right[c] = c;
            out[0] = '_';
         } else {
            out[0] = ' ';
         }
      }
      output("\n|");
   }
   out[0] = '_';
   for (c=CELLS_PER_ROW; --c; output(out)) {
      if (c != (e=left[c-1]) && (c==right[c] || 6<<27<rand())) {
         left[right[e]=right[c]] = e;
         left[right[c]=c-1] = c;
         out[1] = '.';
      } else {
         out[1] = '|';
      }
      e = left[c];
      right[e] = right[c];
      left[right[c]] = e;
      left[c] = c;
      right[c] = c;
   }
   output("\n");
}

void handle_button_causing_wakeup();

void main_app_init() {
   srand();
   pulse_blank_canvas();
   pulse_oled_set_brightness(100);
    pulse_update_power_down_timer(15000);
    pulse_register_callback(ACTION_WOKE_FROM_BUTTON, &handle_button_causing_wakeup);
   drawMaze();
}

void handle_button_causing_wakeup() {
   pulse_oled_set_brightness(100);
   pulse_update_power_down_timer(15000);
   drawMaze();
}

void main_app_handle_button_down() {
    drawMaze();
   pulse_update_power_down_timer(20000);
}

void main_app_handle_button_up() {
}

void main_app_loop() {
}

void main_app_handle_doz() {
    for (int i = 100; i >= 0; i-=6) {
        pulse_oled_set_brightness(i);
        pulse_mdelay(60);
    }
}

void main_app_handle_hardware_update(enum PulseHardwareEvent event) {
}


Attachments:

maze.jpg [ 30.2 KiB | Viewed 201 times ]

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/