#include "plugin.h"
static struct plugin_api* rb;

int maze[11][11];
int been[11][11];
int check = 0;
int exrow, excol;

const unsigned char logo[] =
{
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xf8, 0xf8, 0xf0,
    0xb0, 0xe0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x80, 0x80, 0xc0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf0, 0xf8, 0xf8, 0xf9, 0xff, 0xde,
    0xff, 0xff, 0xff, 0xfc, 0xf8, 0x58, 0xf8, 0xf8, 0xd8, 0xf8, 0x70, 0xb0, 0xf0,
    0x70, 0x20, 0xe0, 0xe0, 0xc0, 0x80, 0xc0, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
    0xc0, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x02, 0x03, 0x03, 0x01, 0x03,
    0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0xc1, 0xe1, 0xb3, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xf7, 0xef, 0xff, 0xd3,
    0xff, 0x9f, 0x7d, 0xfd, 0xff, 0xcf, 0x7d, 0xee, 0xfd, 0xe7, 0xbf, 0xff, 0x7e,
    0xfc, 0xac, 0xe0, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xf0, 0xf0, 0xe0,
    0xe0, 0xc0, 0xe0, 0x40, 0xc0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0f, 0xff, 0xf9, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0x7f, 0xfe, 0xbf, 0xff, 0xdf, 0xee, 0xf7, 0xfb, 0xff, 0xfc, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf7, 0x07,
    0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x3f, 0x3f, 0x7f, 0xff, 0xff, 0xdf, 0xff,
    0xbf, 0xff, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfc, 0xfc,
    0xf8, 0xf0, 0xf0, 0x40, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe5, 0xff, 0xef, 0xff, 0xff, 0xdf,
    0xcf, 0x07, 0x07, 0x03, 0x01, 0x00, 0xc1, 0xe1, 0xf7, 0xff, 0xfb, 0xfb, 0xff,
    0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
    0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
    0x01, 0x01, 0xe1, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x1f,
    0x0f, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x80, 0xc0, 0xc0, 0xe1, 0xb3, 0xf3, 0xfb, 0xef, 0xff,
    0xfd, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xfe, 0xf0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xe0,
    0xfc, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x3f, 0x0f, 0x07, 0x01, 0x01, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x06, 0x06, 0x0e, 0x3f, 0x3f, 0x7f, 0x3f, 0x7f, 0x3f, 0x7f, 0x3f, 0x3f, 0x3f,
    0x1f, 0x1f, 0x0f, 0x0f, 0x0f, 0x07, 0x03, 0x03, 0x03, 0x01, 0x00, 0x01, 0x01,
    0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xf4, 0xc0, 0xc0, 0xe0, 0xf8, 0xfc, 0xff, 0x7f, 0x1f,
    0x0f, 0x0f, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x07,
    0x0f, 0x1f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc3, 0xc1, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80,
    0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe0, 0xe0, 0xe0, 0xf0, 0xe0, 0xf0, 0xf0, 0xf0,
    0xf0, 0xf0, 0xe0, 0xf0, 0xe0, 0xf0, 0xe0, 0xe0, 0xe0, 0xc0, 0xc0, 0xc0, 0xc0,
    0xc0, 0xc0, 0x80, 0x80, 0xc1, 0xf7, 0xf7, 0xfb, 0xfd, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xf8,
    0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x54, 0xfc, 0xfe, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xfb, 0x7f, 0xbf, 0xe7, 0xff,
    0xfb, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f,
    0x0f, 0x18, 0x30, 0xf8, 0xe0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x01, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x9f, 0x1f, 0x3f, 0x3b, 0x7f, 0xff, 0xf7, 0xff, 0xef, 0xdf, 0xff, 0xff,
    0xff, 0xff, 0xbf, 0xff, 0x7f, 0x7f, 0xbf, 0xff, 0xfa, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x0f, 0x07, 0x03, 0x01, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x0e, 0x3f, 0x3f, 0x7e, 0x7c, 0x78, 0x78, 0x3c, 0x3e, 0x1f,
    0x0f, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0xff, 0xff, 0xf6, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0x30, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x03, 0x01,
    0x03, 0x03, 0x03, 0x03, 0xf3, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0x7f, 0x3f, 0x0f, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x1f, 0xfe, 0xfb, 0xff, 0xff,
    0xff, 0xff, 0xf1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x80, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff,
    0xff, 0xff, 0xdf, 0xff, 0xff, 0x7b, 0x3f, 0xbf, 0x8f, 0x8f, 0x83, 0x81, 0x00,
    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x1f, 0x3f,
    0xff, 0xff, 0xff, 0xff, 0xf8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf7, 0xff,
    0xff, 0xff, 0xfd, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfc, 0xfc, 0xfc,
    0xfc, 0xfc, 0xf8, 0x7c, 0x7c, 0x7c, 0x7e, 0xfc, 0xfe, 0xf6, 0xfe, 0xfe, 0xfc,
    0x7c, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x81, 0xff, 0xff, 0xff, 0xbf, 0xff, 0xfc, 0xf0, 0xc0, 0x00, 0x00, 0x00, 0x00,
    0x03, 0x03, 0x07, 0x03, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x03, 0x07, 0x07,
    0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    0x07, 0x07, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x01, 0x00, 0x01,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xe1, 0xf3, 0xff, 0xff, 0xff, 0x7f,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x04, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x1e, 0x0e, 0x0e, 0x0f,
    0x0f, 0x07, 0x07, 0x07, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};

const unsigned char guy[] =
{
    0x08, 0x08, 0xEA, 0x1D, 0x1D, 0xEA, 0x08, 0x08,
};

const unsigned char minotaur[] =
{
    0x03, 0x3E, 0x68, 0xC4, 0xC4, 0x68, 0x3E, 0x03,
};

void draw_bitmap(const unsigned char *bitmap, int x, int y)
{
    rb->lcd_bitmap(bitmap, 9 * x + 1, 9 * y + 1, 8, 8, true);
}

void clear_bitmap(int x, int y)
{
    rb->lcd_clearrect(9 * x + 1, 9 * y + 1, 8, 8);
}

// Initializes board.
void create(void)
{
    int row, col;
    
    for (row = 0; row <= 10; row++)
    {
        for (col = 0; col <= 10; col++)
        {
                maze[row][col] = 0;
                maze[row][col] = rb->rand() % 4;
                been[row][col] = 0;
        }
    }
}

// Check for possible route.
void pos(int row, int col)
{
    if (been[row][col] == 0)
    {
        been[row][col] = 1;
        
        if (row == exrow && col == excol)
        {
            check++;
        }
        else
        {
            if (row + 1 <= 10)
            {
                if (maze[row][col] != 3 && maze[row + 1][col] != 0)
                {
                    pos(row + 1,col);
                }
            }
            
            if (row - 1 >= 0)
            {
                if (maze[row][col] != 0 && maze[row - 1][col] != 3)
                {
                    pos(row - 1,col);
                }
            }

            if (col + 1 <= 10)
            {
                if (maze[row][col] != 2 && maze[row][col + 1] != 1)
                {
                    pos(row, col + 1);
                }
            }

            if (col - 1 >= 0)
            {
                if (maze[row][col] != 1 && maze[row][col - 1] != 2)
                {
                    pos(row, col - 1);
                }
            }
        }
    }
}

// Introduction menu.

const char *entries[] =
{
    "Play",
    "Controls",
    "Exit",
    NULL,
};

int intro(void)
{
    int menu = 0;
    int key;
    int w, h, mw, mh;
    int y;
    
    rb->lcd_clear_display();
    
    //98,128
    rb->lcd_bitmap(logo, LCD_WIDTH - 98, (LCD_HEIGHT / 2) - (128 / 2), 98, 128, true);
    
    mw = mh = -1;
    for (menu = 0; entries[menu] != NULL; menu++)
    {
        w = h = 0;
        rb->lcd_getstringsize(entries[menu], &w, &h);
        h += 2;
        
        if (w > mw)
        {
            mw = w;
        }
        
        if (h > mh)
        {
            mh = h;
        }
    }
    
    y = (LCD_HEIGHT / 2) - (mh / 2);
    
    rb->lcd_drawrect(0, y - 2, 2 + mw + 4, (mh * menu) + 2);
    
    for (menu = 0; entries[menu] != NULL; menu++)
    {
        rb->lcd_putsxy(2, y + (menu * mh), entries[menu]);
    }
    
    menu = 0;
    
    rb->lcd_invertrect(1, y + (menu * mh) - 1, mw + 4, mh);
    
    rb->lcd_update();
    
    while (true)
    {
        rb->button_clear_queue();
        key = rb->button_get(true);
        
        if (!(key & BUTTON_REL))
        {
            if (key & BUTTON_SELECT)
            {
                return menu;
            }
            else if (key & (BUTTON_UP | BUTTON_DOWN))
            {
                rb->lcd_invertrect(1, y + (menu * mh) - 1, mw + 4, mh);
                rb->lcd_update_rect(1, y + (menu * mh) - 1, mw + 4, mh);
                if (key & BUTTON_UP)
                {
                    menu--;
                    
                    if (menu < 0)
                    {
                        menu = 2;
                    }
                }
                else if (key & BUTTON_DOWN)
                {
                    menu++;
                    
                    if (menu > 2)
                    {
                        menu = 0;
                    }
                }
                rb->lcd_invertrect(1, y + (menu * mh) - 1, mw + 4, mh);
                rb->lcd_update_rect(1, y + (menu * mh) - 1, mw + 4, mh);
            }
        }
    }
}

void wait(void)
{
    bool rez;
    
    do
    {
        rb->button_clear_queue();
        rez = !(rb->button_get(true) & BUTTON_REL);
    }
    while (rez == false);
}

void game(void)
{
    int key = 0;
    int make = 0;
    int a, b, row, col;
    int exit = 0;
    int cont = 0;
    int move = 0;
    int aicount = 0;
    
    int hurow;
    int hucol;
    int mirow;
    int micol;
    
    int side = rb->rand() % 4;
    int location = rb->rand() % 11;
    
    do
    {
        hurow = rb->rand() % 11;
        hucol = rb->rand() % 11;
        mirow = rb->rand() % 11;
        micol = rb->rand() % 11;
    }
    while (hurow != mirow && hucol != micol);
    
    switch (side)
    {
        case 0:
        {
            exrow = 0;
            excol = location;
            break;
        }
        
        case 1:
        {
            exrow = location;
            excol = 0;
            break;
        }
        
        case 2:
        {
            exrow = location;
            excol = 10;
            break;
        }
        
        case 3:
        {
            exrow = 10;
            excol = location;
            break;
        }
        
        default:
        {
            exrow = excol = 0;
            break;
        }
    }
    
    rb->lcd_clear_display();
    
    while (make == 0)
    {
        check = 0;
        make = 1;
        
        create();
        
        pos(hurow, hucol);
        
        for (a = 0; a < 3000; a++)
        {
            b = rb->rand() % 500;
        }
        
        if (check == 0)
        {
            make = 0;
        }
        
        check = 0;
        
        if (make == 1)
        {
            check = 0;
            
            for (row = 0; row <= 10; row++)
            {
                for (col = 0; col <= 10; col++)
                {
                    been[row][col] = 0;
                }
            }
            
            pos(mirow, micol);
            
            for (a = 0; a < 3000; a++)
            {
                b = rb->rand() % 500;
            }
            
            if (check != 0)
            {   
                make = 1;
            }
            
            if (check == 0)
            {   
                make = 0;
            }
        }
    } 
    
    rb->lcd_clear_display();
    rb->lcd_drawrect(0, 0, 100, 100);
    
    for (row = 0; row <= 10; row++)
    {
        for (col = 0; col <= 10; col++)
        {
            switch (maze[row][col])
            {
                case 0:
                {
                    rb->lcd_drawline(9 * col, 9 * row, 9 * col + 9, 9 * row);
                    break;
                }
                
                case 1:
                {
                    rb->lcd_drawline(9 * col, 9 * row, 9 * col, 9 * row + 9);
                    break;
                }
                
                case 2:
                {
                    rb->lcd_drawline(9 * col + 9, 9 * row + 9, 9 * col + 9, 9 * row);
                    break;
                }
                
                case 3:
                {
                    rb->lcd_drawline(9 * col + 9, 9 * row + 9, 9 * col, 9 * row + 9);
                    break;
                }
            }
        }
    }
    
    /*
    rb->lcd_bitmap(guy, 9 * hucol + 1, 9 * hurow + 1, 8, 8, true);
    rb->lcd_bitmap(minotaur, 9 * micol + 1, 9 * mirow + 1, 8, 8, true);
    */
    
    draw_bitmap(guy, hucol, hurow);
    draw_bitmap(minotaur, micol, mirow);
    
    switch (side)
    {
        case 0:
        {
            rb->lcd_clearline(9 * excol + 1, 9 * exrow, 9 * excol + 8, 9 * exrow);
            break;
        }
        
        case 1:
        {
            rb->lcd_clearline(9 * excol, 9 * exrow+ 1 , 9 * excol, 9 * exrow + 8);
            break;
        }
        
        case 2:
        {
            rb->lcd_clearline(9 * excol + 9, 9 * exrow + 8, 9 * excol+ 9, 9 *exrow + 1);
            break;
        }
        
        case 3:
        {
            rb->lcd_clearline(9 * excol + 8, 9 * exrow + 9, 9 * excol + 1, 9 * exrow + 9);
            break;
        }
    }
    
    rb->lcd_update();
    
    while (exit == 0)
    {
        cont = 0;
        
        while (cont == 0)
        {
            rb->button_clear_queue();
            key = rb->button_get(true);
            
            if (!(key & BUTTON_REL))
            {
                if (key & (BUTTON_UP | BUTTON_DOWN | BUTTON_LEFT | BUTTON_RIGHT))
                {
                    clear_bitmap(hucol, hurow);
                    
                    if (key & BUTTON_UP)
                    {
                        if (hurow != 0 && maze[hurow][hucol] != 0)
                        {
                            if (maze[hurow-1][hucol] != 3)
                            {
                                hurow--;
                                cont++;
                            }
                        }
                    }
                    else if (key & BUTTON_DOWN)
                    {
                        if (hurow != 10 && maze[hurow][hucol] != 3)
                        {
                            if (maze[hurow+1][hucol] != 0)  
                            {
                                hurow++;
                                cont++;
                            }
                        }
                    }
                    else if (key & BUTTON_LEFT)
                    {
                        if (hucol != 0 && maze[hurow][hucol] != 1)
                        {
                            if (maze[hurow][hucol-1] != 2)
                            {
                                hucol--;
                                cont++;
                            }
                        }
                    }
                    else if (key & BUTTON_RIGHT)
                    {
                        if (hucol !=10 && maze[hurow][hucol] != 2)
                        {
                            if (maze[hurow][hucol+1] != 1)
                            {
                                hucol++;
                                cont++;
                            }
                        }
                    }
                    
                    draw_bitmap(guy, hucol, hurow);
                }
                else if (key & (BUTTON_SELECT | BUTTON_REC | BUTTON_ON))
                {
                    cont++;
                }
                else if (key & BUTTON_OFF)
                {
                    cont++;
                    exit=1;
                }
            }
        }
        
        rb->lcd_update();
        
        //ai segment loops through twice, for each of the 2 ai moves
        for (aicount = 0; aicount < 2; aicount++)
        {
            move = aicount;
            
            // first check horizontal moves, they are before vertical ones (i think, but dont quote me on this)
            if (micol > hucol && move != aicount + 1)
            {
                if (maze[mirow][micol] != 1 && maze[mirow][micol-1] != 2)
                {
                    clear_bitmap(micol, mirow);
                    micol--;
                    draw_bitmap(minotaur, micol, mirow);
                    move++;
                }
            }
            
            if (micol < hucol && move != aicount + 1)
            {
                if (maze[mirow][micol] != 2 && maze[mirow][micol+1] != 1)
                {
                    clear_bitmap(micol, mirow);
                    micol++;
                    draw_bitmap(minotaur, micol, mirow);
                    move++;
                }
            }
            
            if (mirow > hurow && move != aicount + 1) //check vertical moves
            {
                if (maze[mirow][micol] != 0 && maze[mirow-1][micol] != 3)
                {
                    clear_bitmap(micol, mirow);
                    mirow--;
                    draw_bitmap(minotaur, micol, mirow);
                    move++;
                }
            }
            
            if (mirow < hurow && move != aicount + 1)
            {
                if (maze[mirow][micol] != 3 && maze[mirow+1][micol] != 0)
                {
                    clear_bitmap(micol, mirow);
                    mirow++;
                    draw_bitmap(minotaur, micol, mirow);
                    move++;
                }
            }
        }
        
        rb->lcd_update();
        
        if (hurow == exrow && hucol == excol) //if you got out
            exit=2;
        
        if (mirow == hurow && micol == hucol) //if you got eaten
            exit=3;
    }
    
    if (exit == 2) // different exits depending on how you quit
    {
        rb->lcd_clear_display();
        rb->lcd_putsxy(5, 45, "Congratulations! you won");
        rb->lcd_update();
        wait();
    }
    
    if (exit == 3)
    {
        rb->lcd_clear_display();
        rb->lcd_putsxy(5, 45, "The Minotaur got you :-(");
        rb->lcd_update();
        wait();
    }
}

const char *help[] =
{
    "Controls",
    "Arrow Keys:",
    "Move Character",
    "",
    "Play / Select / Rec:",
    "Wait",
    "",
    "Stop / Off:",
    "Quit to menu",
    NULL,
};

enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
    int menu, w, h, y;
    
    TEST_PLUGIN_API(api);
    (void)parameter;
    rb = api;
    
    rb->srand(*rb->current_tick);
    
    while (true)
    {
        menu = intro();
        
        switch (menu)
        {
            case 0:
            {
                game();
                break;
            }
            
            case 1:
            {
                rb->lcd_clear_display();
                y = 0;
                for (menu = 0; help[menu] != NULL; menu++)
                {
                    if (rb->strcmp(help[menu], "") == 0)
                    {
                        y += 6;
                        continue;
                    }
                    
                    w = h = 0;
                    rb->lcd_getstringsize(help[menu], &w, &h);
                    rb->lcd_putsxy((LCD_WIDTH / 2) - (w / 2), y, help[menu]);
                    
                    if (menu == 0)
                    {
                        rb->lcd_invertrect(0, 0, LCD_WIDTH, h);
                        y += 4;
                    }
                    
                    y += h;
                }
                rb->lcd_update();
                wait();
                break;
            }
            
            case 2:
            {
                return PLUGIN_OK;
            }
        }
    }
}
