From 7826fc39f2ea85effb4dea94d215788cfb823848 Mon Sep 17 00:00:00 2001 From: Björn Stenberg Date: Thu, 20 Jun 2002 10:49:15 +0000 Subject: Changed level array to more friendly format. Made all global variables static. Replaced nested ifs with switches. Added button exit in "you win" display. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@1123 a1c6a512-1295-4272-9138-f99709370657 --- apps/recorder/sokoban.c | 1203 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 818 insertions(+), 385 deletions(-) (limited to 'apps/recorder/sokoban.c') diff --git a/apps/recorder/sokoban.c b/apps/recorder/sokoban.c index a15b8eedd1..2e98774676 100644 --- a/apps/recorder/sokoban.c +++ b/apps/recorder/sokoban.c @@ -5,7 +5,7 @@ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ - * $Id: sokoban.c,v 0.01 2002/06/15 + * $Id$ * * Copyright (C) 2002 Eric Linenberg * @@ -31,21 +31,445 @@ #define SOKOBAN_TITLE "Sokoban" #define SOKOBAN_TITLE_FONT 2 -#define NUM_LEVELS 25 +#define NUM_LEVELS sizeof(levels)/320 -int board[16][20]; -int current_level=0; -int moves=0; -int row=0; -int col=0; -int boxes_to_go=0; -int current_spot=1; +static char board[16][20]; +static int current_level=0; +static int moves=0; +static int row=0; +static int col=0; +static int boxes_to_go=0; +static int current_spot=1; /* 320 boxes per level */ -char levels[320*NUM_LEVELS] = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000002220000000000000000023200000000000000000212222000000000000222414320000000000002314522200000000000022224200000000000000000232000000000000000002220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022222000000000000000211120000000000000002544202220000000000021412023200000000000222122232000000000000221111320000000000002111211200000000000021112222000000000000222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000222222200000000000002111112220000000000224222111200000000002151411412000000000021332141220000000000223321112000000000000222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002222200000000000022221132000000000000211141120000000000002144143200000000000022522332000000000000022222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000222200000000000000002512220000000000000021411200000000000002221212200000000000023212112000000000000234112120000000000002311141200000000000022222222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000222222200000000000022112152000000000000211121120000000000002414141200000000000021422112000000000022214121220000000000233333112000000000002222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022222200000000000022211112000000000002231422122000000000023341411520000000000233141412200000000002222221120000000000000000222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022222200000000000000211112000000000000222444120000000000002514331200000000000021433322000000000000222211200000000000000002222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000222200000000000000002332000000000000000221322000000000000002114320000000000000221411220000000000002112441200000000000021151112000000000000222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022222000000000000022211520000000000000211431220000000000002113431200000000000022211412000000000000002111220000000000000022222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002222222200000000000021121112000000000000214334120000000000002543112200000000000021433412000000000000211211120000000000002222222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002222220000000000000021111222000000000000214111120000000000222141221200000000002333141112000000000023334241220000000000222212141200000000000002115112000000000000022222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000222222000000000000002111120000000000000021444220000000000000211233222000000000002211334120000000000002151111200000000000022222222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000222222220000000000002111231200000000000221143332000000000002114121320000000000221224212200000000002111411412000000000021112111120000000000222222251200000000000000002222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022222220000000000000233331200000000000022233342220000000000211424141200000000002144112412000000000021111211120000000000222215122200000000000002222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000222222220000000000002331111200000000000023341452000000000000242444220000000000002334141200000000000023311112000000000000222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022222222000000000000211111120000000000002124411200000000000021333212000000000000223334122000000000000212214120000000000002411411200000000000021121152000000000000222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002222200000000000002221112222000000000021114141120000000000214111415200000000002224422222000000000000211332000000000000002333320000000000000022222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002222222220000000000021111111200000000000214343152000000000002134343120000000000021434341200000000000211111112000000000002222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022222222200000000002211111112000000000021112424120000000000214411343200000000002152223332000000000022220222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002222220000000000000023133200000000000000231432000000000000022211422000000000000214114120000000000002124221200000000000021115112000000000000222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002222200000000000000021112222000000000002212411120000000000021411441200000000000212423132000000000002111533320000000000022222222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002222200000000000022221112200000000000214141112000000000002523132120000000000021231321200000000000211141412000000000002211122220000000000002222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000222220000000000002222311220000000000021434311200000000000254212412000000000002143131120000000000022224241200000000000002313112000000000000022222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002222222200000000022221111312000000000211414143120000000002113222232200000000021434141520000000000211311112200000000002222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - - -void load_level(int level_to_load) { +static const char levels[][320] = { + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000222000000000" + "00000000232000000000" + "00000000212222000000" + "00000022241432000000" + "00000023145222000000" + "00000022224200000000" + "00000000023200000000" + "00000000022200000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000222220000000000" + "00000211120000000000" + "00000254420222000000" + "00000214120232000000" + "00000222122232000000" + "00000022111132000000" + "00000021112112000000" + "00000021112222000000" + "00000022222000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000022222220000000" + "00000021111122200000" + "00000224222111200000" + "00000215141141200000" + "00000213321412200000" + "00000223321112000000" + "00000022222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000022222000000" + "00000022221132000000" + "00000021114112000000" + "00000021441432000000" + "00000022522332000000" + "00000002222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000002222000000000" + "00000002512220000000" + "00000002141120000000" + "00000022212122000000" + "00000023212112000000" + "00000023411212000000" + "00000023111412000000" + "00000022222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000222222200000" + "00000002211215200000" + "00000002111211200000" + "00000002414141200000" + "00000002142211200000" + "00000222141212200000" + "00000233333112000000" + "00000222222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000222222000000" + "00000022211112000000" + "00000223142212200000" + "00000233414115200000" + "00000233141412200000" + "00000222222112000000" + "00000000002222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000222222000000" + "00000000211112000000" + "00000022244412000000" + "00000025143312000000" + "00000021433322000000" + "00000022221120000000" + "00000000022220000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000222200000000" + "00000000233200000000" + "00000002213220000000" + "00000002114320000000" + "00000022141122000000" + "00000021124412000000" + "00000021151112000000" + "00000022222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000222220000000" + "00000022211520000000" + "00000021143122000000" + "00000021134312000000" + "00000022211412000000" + "00000000211122000000" + "00000000222220000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000022222222000000" + "00000021121112000000" + "00000021433412000000" + "00000025431122000000" + "00000021433412000000" + "00000021121112000000" + "00000022222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000002222220000000" + "00000002111122200000" + "00000002141111200000" + "00000222141221200000" + "00000233314111200000" + "00000233342412200000" + "00000222212141200000" + "00000000211511200000" + "00000000222222200000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000022222200000000" + "00000021111200000000" + "00000021444220000000" + "00000021123322200000" + "00000022113341200000" + "00000002151111200000" + "00000002222222200000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000002222222200000" + "00000002111231200000" + "00000022114333200000" + "00000021141213200000" + "00000221224212200000" + "00000211141141200000" + "00000211121111200000" + "00000222222251200000" + "00000000000222200000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000022222220000000" + "00000023333120000000" + "00000222333422200000" + "00000211424141200000" + "00000214411241200000" + "00000211112111200000" + "00000222215122200000" + "00000000222220000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000022222222000000" + "00000023311112000000" + "00000023341452000000" + "00000024244422000000" + "00000023341412000000" + "00000023311112000000" + "00000022222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000222222220000000" + "00000211111120000000" + "00000212441120000000" + "00000213332120000000" + "00000223334122000000" + "00000021221412000000" + "00000024114112000000" + "00000021121152000000" + "00000022222222000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000002222200000000" + "00000222111222200000" + "00000211141411200000" + "00000214111415200000" + "00000222442222200000" + "00000002113320000000" + "00000002333320000000" + "00000002222220000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000222222222000000" + "00000211111112000000" + "00000214343152000000" + "00000213434312000000" + "00000214343412000000" + "00000211111112000000" + "00000222222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000022222222200000" + "00000221111111200000" + "00000211124241200000" + "00000214411343200000" + "00000215222333200000" + "00000222202222200000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000002222220000000" + "00000002313320000000" + "00000002314320000000" + "00000022211422000000" + "00000021411412000000" + "00000021242212000000" + "00000021115112000000" + "00000022222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000022222000000000" + "00000021112222000000" + "00000221241112000000" + "00000214114412000000" + "00000212423132000000" + "00000211153332000000" + "00000222222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000022222000000" + "00000022221112200000" + "00000021414111200000" + "00000025231321200000" + "00000021231321200000" + "00000021114141200000" + "00000022111222200000" + "00000002222200000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000222220000000" + "00000222231122000000" + "00000214343112000000" + "00000254212412000000" + "00000214313112000000" + "00000222242412000000" + "00000002313112000000" + "00000002222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000", + + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000222222220000" + "00000222211113120000" + "00000211414143120000" + "00000211322223220000" + "00000214341415200000" + "00000211311112200000" + "00000222222222000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" + "00000000000000000000" +}; + + +void load_level (int level_to_load) { int a = 0; int b = 0; int c = 0; @@ -53,22 +477,10 @@ void load_level(int level_to_load) { boxes_to_go = 0; /* load level into board */ /* get to the current level in the level array */ - a = level_to_load*320; for(b=0 ; b<16 ; b++) { for (c=0 ; c<20 ; c++) { - if (levels[a] == '0') - board[b][c]=0; - if (levels[a] == '1') - board[b][c]=1; - if (levels[a] == '2') - board[b][c]=2; - if (levels[a] == '3') - board[b][c]=3; - if (levels[a] == '4') - board[b][c]=4; - if (levels[a] == '5') - board[b][c]=5; + board[b][c] = levels[level_to_load][a] - '0'; a++; if (board[b][c]==5) { row = b; @@ -89,44 +501,44 @@ void update_screen(void) { /* load the board to the screen */ for(b=0 ; b<16 ; b++) { for (c=0 ; c<20 ; c++) { - - /* this is a black space */ - if (board[b][c]==0) { - lcd_drawrect (c*4, b*4, c*4+3, b*4+3); - lcd_drawrect (c*4+1, b*4+1, c*4+2, b*4+2); - } - /* this is a wall */ - if (board[b][c]==2) { - lcd_drawpixel (c*4, b*4); - lcd_drawpixel (c*4+2, b*4); - lcd_drawpixel (c*4+1, b*4+1); - lcd_drawpixel (c*4+3, b*4+1); - lcd_drawpixel (c*4, b*4+2); - lcd_drawpixel (c*4+2, b*4+2); - lcd_drawpixel (c*4+1, b*4+3); - lcd_drawpixel (c*4+3, b*4+3); - } - /* this is a box */ - if (board[b][c]==4) { - lcd_drawrect (c*4, b*4, c*4+3, b*4+3); - } - /* this is a home location */ - if (board[b][c]==3) { - lcd_drawrect (c*4+1, b*4+1, c*4+2, b*4+2); - } - /* this is you */ - if (board[b][c]==5) { - lcd_drawline (c*4+1, b*4, c*4+2, b*4); - lcd_drawline (c*4, b*4+1, c*4+3, b*4+1); - lcd_drawline (c*4+1, b*4+2, c*4+2, b*4+2); - lcd_drawpixel (c*4, b*4+3); - lcd_drawpixel (c*4+3, b*4+3); + switch ( board[b][c] ) { + case 0: /* this is a black space */ + lcd_drawrect (c*4, b*4, c*4+3, b*4+3); + lcd_drawrect (c*4+1, b*4+1, c*4+2, b*4+2); + break; + + case 2: /* this is a wall */ + lcd_drawpixel (c*4, b*4); + lcd_drawpixel (c*4+2, b*4); + lcd_drawpixel (c*4+1, b*4+1); + lcd_drawpixel (c*4+3, b*4+1); + lcd_drawpixel (c*4, b*4+2); + lcd_drawpixel (c*4+2, b*4+2); + lcd_drawpixel (c*4+1, b*4+3); + lcd_drawpixel (c*4+3, b*4+3); + break; + + case 3: /* this is a home location */ + lcd_drawrect (c*4+1, b*4+1, c*4+2, b*4+2); + break; + + case 4: /* this is a box */ + lcd_drawrect (c*4, b*4, c*4+3, b*4+3); + break; + + case 5: /* this is you */ + lcd_drawline (c*4+1, b*4, c*4+2, b*4); + lcd_drawline (c*4, b*4+1, c*4+3, b*4+1); + lcd_drawline (c*4+1, b*4+2, c*4+2, b*4+2); + lcd_drawpixel (c*4, b*4+3); + lcd_drawpixel (c*4+3, b*4+3); + break; + + case 7: /* this is a box on a home spot */ + lcd_drawrect (c*4, b*4, c*4+3, b*4+3); + lcd_drawrect (c*4+1, b*4+1, c*4+2, b*4+2); + break; } - /* this is a box on a home spot */ - if (board[b][c]==7) { - lcd_drawrect (c*4, b*4, c*4+3, b*4+3); - lcd_drawrect (c*4+1, b*4+1, c*4+2, b*4+2); - } } } @@ -148,336 +560,355 @@ void update_screen(void) { void sokoban_loop(void) { int ii = 0; - int b = 0; moves = 0; current_level = 0; load_level(current_level); update_screen(); - while(1) { - b = button_get(true); - - if ( b & BUTTON_OFF ) { - /* get out of here */ - return; - } - - - if ( b & BUTTON_F3 ) { - /* increase level */ - boxes_to_go=0; - } - - if ( b & BUTTON_F2 ) { - /* same level */ - load_level(current_level); - lcd_clear_display(); - update_screen(); - moves=0; + switch ( button_get(true) ) { + + case BUTTON_OFF: + /* get out of here */ + return; + + case BUTTON_F3: + /* increase level */ + boxes_to_go=0; + break; + + case BUTTON_F2: + /* same level */ + load_level(current_level); + lcd_clear_display(); + update_screen(); + moves=0; + break; + case BUTTON_F1: + /* previous level */ + if (current_level==0) { + current_level=0; + } + else { + current_level--; + } + load_level(current_level); + lcd_clear_display(); + update_screen(); + moves=0; + break; + + case BUTTON_LEFT: + switch ( board[row][col-1] ) { + case 1: /* if it is a blank spot */ + board[row][col-1]=5; + board[row][col]=current_spot; + current_spot=1; + col--; + moves++; + break; + + case 3: /* if it is a home spot */ + board[row][col-1]=5; + board[row][col]=current_spot; + current_spot=3; + col--; + moves++; + break; + + case 4: + switch ( board[row][col-2] ) { + case 1: /* if we are going from blank to blank */ + board[row][col-2]=board[row][col-1]; + board[row][col-1]=board[row][col]; + board[row][col]=current_spot; + current_spot=1; + col--; + moves++; + break; + + /* if there is a wall then do not move the box */ + case 2: + /* do nothing */ + break; + + case 3: /* if we are going from a blank to home */ + board[row][col-2]=7; + board[row][col-1]=board[row][col]; + board[row][col]=current_spot; + current_spot=1; + col--; + boxes_to_go--; + moves++; + break; + } + break; + + case 7: + switch ( board[row][col-2] ) { + case 1: /* we are going from a home to a blank */ + board[row][col-2]=4; + board[row][col-1]=board[row][col]; + board[row][col]=current_spot; + current_spot=3; + col--; + boxes_to_go++; + moves++; + break; + + /* if there is a wall then do not move the box */ + case 2: + /* do nothing */ + break; + + case 3: /* if we are going from a home to home */ + board[row][col-2]=7; + board[row][col-1]=board[row][col]; + board[row][col]=current_spot; + current_spot=3; + col--; + moves++; + break; + } + } + lcd_clear_display(); + update_screen(); + break; + + case BUTTON_RIGHT: /* if it is a blank spot */ + switch ( board[row][col+1] ) { + case 1: + board[row][col+1]=5; + board[row][col]=current_spot; + current_spot=1; + col++; + moves++; + break; + + case 3: /* if it is a home spot */ + board[row][col+1]=5; + board[row][col]=current_spot; + current_spot=3; + col++; + moves++; + break; + + case 4: + switch ( board[row][col+2] ) { + case 1: /* if we are going from blank to blank */ + board[row][col+2]=board[row][col+1]; + board[row][col+1]=board[row][col]; + board[row][col]=current_spot; + current_spot=1; + col++; + moves++; + break; + + /* if there is a wall then do not move the box */ + case 2: + /* do nothing */ + break; + + case 3: /* if we are going from a blank to home */ + board[row][col+2]=7; + board[row][col+1]=board[row][col]; + board[row][col]=current_spot; + current_spot=1; + col++; + boxes_to_go--; + moves++; + break; + } + break; + + case 7: + switch ( board[row][col+2] ) { + case 1: /* we are going from a home to a blank */ + board[row][col+2]=4; + board[row][col+1]=board[row][col]; + board[row][col]=current_spot; + current_spot=3; + col++; + boxes_to_go++; + moves++; + break; + + /* if there is a wall then do not move the box */ + case 2: + /* do nothing */ + break; + + case 3: + board[row][col+2]=7; + board[row][col+1]=board[row][col]; + board[row][col]=current_spot; + current_spot=3; + col++; + moves++; + break; + } + break; + } + lcd_clear_display(); + update_screen(); + break; + + case BUTTON_UP: + switch ( board[row-1][col] ) { + case 1: /* if it is a blank spot */ + board[row-1][col]=5; + board[row][col]=current_spot; + current_spot=1; + row--; + moves++; + break; + + case 3: /* if it is a home spot */ + board[row-1][col]=5; + board[row][col]=current_spot; + current_spot=3; + row--; + moves++; + break; + + case 4: + switch ( board[row-2][col] ) { + case 1: /* if we are going from blank to blank */ + board[row-2][col]=board[row-1][col]; + board[row-1][col]=board[row][col]; + board[row][col]=current_spot; + current_spot=1; + row--; + moves++; + break; + + /* if there is a wall then do not move the box */ + case 2: + /* do nothing */ + break; + + case 3: /* if we are going from a blank to home */ + board[row-2][col]=7; + board[row-1][col]=board[row][col]; + board[row][col]=current_spot; + current_spot=1; + row--; + boxes_to_go--; + moves++; + break; + } + break; + + case 7: + switch ( board[row-2][col] ) { + case 1: /* we are going from a home to a blank */ + board[row-2][col]=4; + board[row-1][col]=board[row][col]; + board[row][col]=current_spot; + current_spot=3; + row--; + boxes_to_go++; + moves++; + break; + + /* if there is a wall then do not move the box */ + case 2: + /* do nothing */ + break; + + case 3: /* if we are going from a home to home */ + board[row-2][col]=7; + board[row-1][col]=board[row][col]; + board[row][col]=current_spot; + current_spot=3; + row--; + moves++; + break; + } + break; + } + lcd_clear_display(); + update_screen(); + break; + + case BUTTON_DOWN: + switch ( board[row+1][col] ) { + case 1: /* if it is a blank spot */ + board[row+1][col]=5; + board[row][col]=current_spot; + current_spot=1; + row++; + moves++; + break; + + case 3: /* if it is a home spot */ + board[row+1][col]=5; + board[row][col]=current_spot; + current_spot=3; + row++; + moves++; + break; + + case 4: + switch ( board[row+2][col] ) { + case 1: /* if we are going from blank to blank */ + board[row+2][col]=board[row+1][col]; + board[row+1][col]=board[row][col]; + board[row][col]=current_spot; + current_spot=1; + row++; + moves++; + break; + + /* if there is a wall then do not move the box */ + case 2: + /* do nothing */ + break; + + case 3: /* if we are going from a blank to home */ + board[row+2][col]=7; + board[row+1][col]=board[row][col]; + board[row][col]=current_spot; + current_spot=1; + row++; + boxes_to_go--; + moves++; + break; + } + break; + + case 7: + switch ( board[row+2][col] ) { + case 1: /* we are going from a home to a blank */ + board[row+2][col]=4; + board[row+1][col]=board[row][col]; + board[row][col]=current_spot; + current_spot=3; + row++; + boxes_to_go++; + moves++; + break; + + /* if there is a wall then do not move the box */ + case 2: + /* do nothing */ + break; + + case 3: /* if we are going from a home to home */ + board[row+2][col]=7; + board[row+1][col]=board[row][col]; + board[row][col]=current_spot; + current_spot=3; + row++; + moves++; + break; + } + } + lcd_clear_display(); + update_screen(); + break; } - - if ( b & BUTTON_F1 ) { - /* previous level */ - if (current_level==0) { - current_level=0; - } - else { - current_level--; - } - load_level(current_level); - lcd_clear_display(); - update_screen(); - moves=0; - } - - if ( b & BUTTON_LEFT ) { - /* if it is a blank spot */ - if (board[row][col-1]==1) { - board[row][col-1]=5; - board[row][col]=current_spot; - current_spot=1; - col--; - moves++; - } - /* if it is a home spot */ - else if (board[row][col-1]==3) { - board[row][col-1]=5; - board[row][col]=current_spot; - current_spot=3; - col--; - moves++; - } - else if (board[row][col-1]==4) { - /* if there is a wall then do not move the box */ - if(board[row][col-2]==2) { - /* do nothing */ - } - /* if we are going from blank to blank */ - else if(board[row][col-2]==1) { - board[row][col-2]=board[row][col-1]; - board[row][col-1]=board[row][col]; - board[row][col]=current_spot; - current_spot=1; - col--; - moves++; - } - /* if we are going from a blank to home */ - else if(board[row][col-2]==3) { - board[row][col-2]=7; - board[row][col-1]=board[row][col]; - board[row][col]=current_spot; - current_spot=1; - col--; - boxes_to_go--; - moves++; - } - } - else if (board[row][col-1]==7) { - /* if there is a wall then do not move the box */ - if(board[row][col-2]==2) { - /* do nothing */ - } - /* we are going from a home to a blank */ - else if(board[row][col-2]==1) { - board[row][col-2]=4; - board[row][col-1]=board[row][col]; - board[row][col]=current_spot; - current_spot=3; - col--; - boxes_to_go++; - moves++; - } - /* if we are going from a home to home */ - else if(board[row][col-2]==3) { - board[row][col-2]=7; - board[row][col-1]=board[row][col]; - board[row][col]=current_spot; - current_spot=3; - col--; - moves++; - } - } - lcd_clear_display(); - update_screen(); - } - - - if ( b & BUTTON_RIGHT ) { - /* if it is a blank spot */ - if (board[row][col+1]==1) { - board[row][col+1]=5; - board[row][col]=current_spot; - current_spot=1; - col++; - moves++; - } - /* if it is a home spot */ - else if (board[row][col+1]==3) { - board[row][col+1]=5; - board[row][col]=current_spot; - current_spot=3; - col++; - moves++; - } - else if (board[row][col+1]==4) { - /* if there is a wall then do not move the box */ - if(board[row][col+2]==2) { - /* do nothing */ - } - /* if we are going from blank to blank */ - else if(board[row][col+2]==1) { - board[row][col+2]=board[row][col+1]; - board[row][col+1]=board[row][col]; - board[row][col]=current_spot; - current_spot=1; - col++; - moves++; - } - /* if we are going from a blank to home */ - else if(board[row][col+2]==3) { - board[row][col+2]=7; - board[row][col+1]=board[row][col]; - board[row][col]=current_spot; - current_spot=1; - col++; - boxes_to_go--; - moves++; - } - } - else if (board[row][col+1]==7) { - /* if there is a wall then do not move the box */ - if(board[row][col+2]==2) { - /* do nothing */ - } - /* we are going from a home to a blank */ - else if(board[row][col+2]==1) { - board[row][col+2]=4; - board[row][col+1]=board[row][col]; - board[row][col]=current_spot; - current_spot=3; - col++; - boxes_to_go++; - moves++; - } - /* if we are going from a home to home */ - else if(board[row][col+2]==3) { - board[row][col+2]=7; - board[row][col+1]=board[row][col]; - board[row][col]=current_spot; - current_spot=3; - col++; - moves++; - } - } - lcd_clear_display(); - update_screen(); - } - - if ( b & BUTTON_UP ) { - /* if it is a blank spot */ - if (board[row-1][col]==1) { - board[row-1][col]=5; - board[row][col]=current_spot; - current_spot=1; - row--; - moves++; - } - /* if it is a home spot */ - else if (board[row-1][col]==3) { - board[row-1][col]=5; - board[row][col]=current_spot; - current_spot=3; - row--; - moves++; - } - else if (board[row-1][col]==4) { - /* if there is a wall then do not move the box */ - if(board[row-2][col]==2) { - /* do nothing */ - } - /* if we are going from blank to blank */ - else if(board[row-2][col]==1) { - board[row-2][col]=board[row-1][col]; - board[row-1][col]=board[row][col]; - board[row][col]=current_spot; - current_spot=1; - row--; - moves++; - } - /* if we are going from a blank to home */ - else if(board[row-2][col]==3) { - board[row-2][col]=7; - board[row-1][col]=board[row][col]; - board[row][col]=current_spot; - current_spot=1; - row--; - boxes_to_go--; - moves++; - } - } - else if (board[row-1][col]==7) { - /* if there is a wall then do not move the box */ - if(board[row-2][col]==2) { - /* do nothing */ - } - /* we are going from a home to a blank */ - else if(board[row-2][col]==1) { - board[row-2][col]=4; - board[row-1][col]=board[row][col]; - board[row][col]=current_spot; - current_spot=3; - row--; - boxes_to_go++; - moves++; - } - /* if we are going from a home to home */ - else if(board[row-2][col]==3) { - board[row-2][col]=7; - board[row-1][col]=board[row][col]; - board[row][col]=current_spot; - current_spot=3; - row--; - moves++; - } - } - lcd_clear_display(); - update_screen(); - } - - if ( b & BUTTON_DOWN ) { - /* if it is a blank spot */ - if (board[row+1][col]==1) { - board[row+1][col]=5; - board[row][col]=current_spot; - current_spot=1; - row++; - moves++; - } - /* if it is a home spot */ - else if (board[row+1][col]==3) { - board[row+1][col]=5; - board[row][col]=current_spot; - current_spot=3; - row++; - moves++; - } - else if (board[row+1][col]==4) { - /* if there is a wall then do not move the box */ - if(board[row+2][col]==2) { - /* do nothing */ - } - /* if we are going from blank to blank */ - else if(board[row+2][col]==1) { - board[row+2][col]=board[row+1][col]; - board[row+1][col]=board[row][col]; - board[row][col]=current_spot; - current_spot=1; - row++; - moves++; - } - /* if we are going from a blank to home */ - else if(board[row+2][col]==3) { - board[row+2][col]=7; - board[row+1][col]=board[row][col]; - board[row][col]=current_spot; - current_spot=1; - row++; - boxes_to_go--; - moves++; - } - } - else if (board[row+1][col]==7) { - /* if there is a wall then do not move the box */ - if(board[row+2][col]==2) { - /* do nothing */ - } - /* we are going from a home to a blank */ - else if(board[row+2][col]==1) { - board[row+2][col]=4; - board[row+1][col]=board[row][col]; - board[row][col]=current_spot; - current_spot=3; - row++; - boxes_to_go++; - moves++; - } - /* if we are going from a home to home */ - else if(board[row+2][col]==3) { - board[row+2][col]=7; - board[row+1][col]=board[row][col]; - board[row][col]=current_spot; - current_spot=3; - row++; - moves++; - } - } - lcd_clear_display(); - update_screen(); - } - - if (boxes_to_go==0) { moves=0; @@ -489,6 +920,8 @@ void sokoban_loop(void) { lcd_update(); lcd_invertrect(0,0,111,63); lcd_update(); + if ( button_get(false) ) + return; } return; } -- cgit v1.2.3