summaryrefslogtreecommitdiff
path: root/apps/recorder/sokoban.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/recorder/sokoban.c')
-rw-r--r--apps/recorder/sokoban.c586
1 files changed, 586 insertions, 0 deletions
diff --git a/apps/recorder/sokoban.c b/apps/recorder/sokoban.c
new file mode 100644
index 0000000000..159516e361
--- /dev/null
+++ b/apps/recorder/sokoban.c
@@ -0,0 +1,586 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: sokoban.c,v 0.01 2002/06/15
9 *
10 * Copyright (C) 2002 Eric Linenberg
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include "config.h"
21
22#ifdef HAVE_LCD_BITMAP
23
24#include "sokoban.h"
25#include "lcd.h"
26#include "button.h"
27#include "kernel.h"
28
29#ifdef SIMULATOR
30#include <stdio.h>
31#endif
32#include <string.h>
33
34#define SOKOBAN_TITLE "Sokoban"
35#define SOKOBAN_TITLE_FONT 2
36#define NUM_LEVELS 4
37
38int board[16][20];
39int current_level=0;
40int moves=0;
41int row=0;
42int col=0;
43int boxes_to_go=0;
44int current_spot=1;
45
46/* 320 boxes per level */
47int levels[320*NUM_LEVELS] = {
48/* level 01 */
490,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
500,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
510,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
520,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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2,1,1,2,2,2,2,0,0,0,0,0,0,0,0,
1310,0,0,0,0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,
1320,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 */
133};
134
135
136
137void load_level(int level_to_load) {
138 int a = 0;
139 int b = 0;
140 int c = 0;
141
142 /* load level into board */
143 /* get to the current level in the level array */
144 a = level_to_load*320;
145
146 for(b=0 ; b<16 ; b++) {
147 for (c=0 ; c<20 ; c++) {
148 board[b][c] = levels[a];
149 a++;
150 if (board[b][c]==5) {
151 row = b;
152 col = c;
153 }
154 if (board[b][c]==3)
155 boxes_to_go++;
156 }
157 }
158 return;
159}
160
161void update_screen(void) {
162 int b = 0;
163 int c = 0;
164 char s[25];
165
166 /* load the board to the screen */
167 for(b=0 ; b<16 ; b++) {
168 for (c=0 ; c<20 ; c++) {
169
170 /* this is a black space */
171 if (board[b][c]==0) {
172 lcd_drawrect (c*4, b*4, c*4+3, b*4+3);
173 lcd_drawrect (c*4+1, b*4+1, c*4+2, b*4+2);
174 }
175 /* this is a wall */
176 if (board[b][c]==2) {
177 lcd_drawpixel (c*4, b*4);
178 lcd_drawpixel (c*4+2, b*4);
179 lcd_drawpixel (c*4+1, b*4+1);
180 lcd_drawpixel (c*4+3, b*4+1);
181 lcd_drawpixel (c*4, b*4+2);
182 lcd_drawpixel (c*4+2, b*4+2);
183 lcd_drawpixel (c*4+1, b*4+3);
184 lcd_drawpixel (c*4+3, b*4+3);
185 }
186 /* this is a box */
187 if (board[b][c]==4) {
188 lcd_drawrect (c*4, b*4, c*4+3, b*4+3);
189 }
190 /* this is a home location */
191 if (board[b][c]==3) {
192 lcd_drawrect (c*4+1, b*4+1, c*4+2, b*4+2);
193 }
194 /* this is you */
195 if (board[b][c]==5) {
196 lcd_drawline (c*4+1, b*4, c*4+2, b*4);
197 lcd_drawline (c*4, b*4+1, c*4+3, b*4+1);
198 lcd_drawline (c*4+1, b*4+2, c*4+2, b*4+2);
199 lcd_drawpixel (c*4, b*4+3);
200 lcd_drawpixel (c*4+3, b*4+3);
201 }
202 /* this is a box on a home spot */
203 if (board[b][c]==7) {
204 lcd_drawrect (c*4, b*4, c*4+3, b*4+3);
205 lcd_drawrect (c*4+1, b*4+1, c*4+2, b*4+2);
206 }
207 }
208 }
209
210
211 snprintf (s, sizeof(s), "%d", current_level+1);
212 lcd_putsxy (86, 20, s, 0);
213 snprintf (s, sizeof(s), "%d", moves);
214 lcd_putsxy (86, 52, s, 0);
215
216 lcd_drawrect (80,0,111,31);
217 lcd_drawrect (80,32,111,63);
218 lcd_putsxy (81, 10, "Level", 0);
219 lcd_putsxy (81, 42, "Moves", 0);
220 /* print out the screen */
221 lcd_update();
222}
223
224
225
226void sokoban_loop(void) {
227 int ii = 0;
228 int b = 0;
229 moves = 0;
230 current_level = 0;
231 load_level(current_level);
232 update_screen();
233
234 while(1) {
235 b = button_get(false);
236
237 if ( b & BUTTON_OFF ) {
238 /* get out of here */
239 return;
240 }
241
242
243 if ( b & BUTTON_F1 ) {
244 /* get out of here */
245 boxes_to_go=0;
246 }
247
248 if ( b & BUTTON_LEFT ) {
249 /* if it is a blank spot */
250 if (board[row][col-1]==1) {
251 board[row][col-1]=5;
252 board[row][col]=current_spot;
253 current_spot=1;
254 col--;
255 moves++;
256 }
257 /* if it is a home spot */
258 else if (board[row][col-1]==3) {
259 board[row][col-1]=5;
260 board[row][col]=current_spot;
261 current_spot=3;
262 col--;
263 moves++;
264 }
265 else if (board[row][col-1]==4) {
266 /* if there is a wall then do not move the box */
267 if(board[row][col-2]==2) {
268 /* do nothing */
269 }
270 /* if we are going from blank to blank */
271 else if(board[row][col-2]==1) {
272 board[row][col-2]=board[row][col-1];
273 board[row][col-1]=board[row][col];
274 board[row][col]=current_spot;
275 current_spot=1;
276 col--;
277 moves++;
278 }
279 /* if we are going from a blank to home */
280 else if(board[row][col-2]==3) {
281 board[row][col-2]=7;
282 board[row][col-1]=board[row][col];
283 board[row][col]=current_spot;
284 current_spot=1;
285 col--;
286 boxes_to_go--;
287 moves++;
288 }
289 }
290 else if (board[row][col-1]==7) {
291 /* if there is a wall then do not move the box */
292 if(board[row][col-2]==2) {
293 /* do nothing */
294 }
295 /* we are going from a home to a blank */
296 else if(board[row][col-2]==1) {
297 board[row][col-2]=4;
298 board[row][col-1]=board[row][col];
299 board[row][col]=current_spot;
300 current_spot=3;
301 col--;
302 boxes_to_go++;
303 moves++;
304 }
305 /* if we are going from a home to home */
306 else if(board[row][col-2]==3) {
307 board[row][col-2]=7;
308 board[row][col-1]=board[row][col];
309 board[row][col]=current_spot;
310 current_spot=3;
311 col--;
312 moves++;
313 }
314 }
315 lcd_clear_display();
316 update_screen();
317 }
318
319
320 if ( b & BUTTON_RIGHT ) {
321 /* if it is a blank spot */
322 if (board[row][col+1]==1) {
323 board[row][col+1]=5;
324 board[row][col]=current_spot;
325 current_spot=1;
326 col++;
327 moves++;
328 }
329 /* if it is a home spot */
330 else if (board[row][col+1]==3) {
331 board[row][col+1]=5;
332 board[row][col]=current_spot;
333 current_spot=3;
334 col++;
335 moves++;
336 }
337 else if (board[row][col+1]==4) {
338 /* if there is a wall then do not move the box */
339 if(board[row][col+2]==2) {
340 /* do nothing */
341 }
342 /* if we are going from blank to blank */
343 else if(board[row][col+2]==1) {
344 board[row][col+2]=board[row][col+1];
345 board[row][col+1]=board[row][col];
346 board[row][col]=current_spot;
347 current_spot=1;
348 col++;
349 moves++;
350 }
351 /* if we are going from a blank to home */
352 else if(board[row][col+2]==3) {
353 board[row][col+2]=7;
354 board[row][col+1]=board[row][col];
355 board[row][col]=current_spot;
356 current_spot=1;
357 col++;
358 boxes_to_go--;
359 moves++;
360 }
361 }
362 else if (board[row][col+1]==7) {
363 /* if there is a wall then do not move the box */
364 if(board[row][col+2]==2) {
365 /* do nothing */
366 }
367 /* we are going from a home to a blank */
368 else if(board[row][col+2]==1) {
369 board[row][col+2]=4;
370 board[row][col+1]=board[row][col];
371 board[row][col]=current_spot;
372 current_spot=3;
373 col++;
374 boxes_to_go++;
375 moves++;
376 }
377 /* if we are going from a home to home */
378 else if(board[row][col+2]==3) {
379 board[row][col+2]=7;
380 board[row][col+1]=board[row][col];
381 board[row][col]=current_spot;
382 current_spot=3;
383 col++;
384 moves++;
385 }
386 }
387 lcd_clear_display();
388 update_screen();
389 }
390
391 if ( b & BUTTON_UP ) {
392 /* if it is a blank spot */
393 if (board[row-1][col]==1) {
394 board[row-1][col]=5;
395 board[row][col]=current_spot;
396 current_spot=1;
397 row--;
398 moves++;
399 }
400 /* if it is a home spot */
401 else if (board[row-1][col]==3) {
402 board[row-1][col]=5;
403 board[row][col]=current_spot;
404 current_spot=3;
405 row--;
406 moves++;
407 }
408 else if (board[row-1][col]==4) {
409 /* if there is a wall then do not move the box */
410 if(board[row-2][col]==2) {
411 /* do nothing */
412 }
413 /* if we are going from blank to blank */
414 else if(board[row-2][col]==1) {
415 board[row-2][col]=board[row-1][col];
416 board[row-1][col]=board[row][col];
417 board[row][col]=current_spot;
418 current_spot=1;
419 row--;
420 moves++;
421 }
422 /* if we are going from a blank to home */
423 else if(board[row-2][col]==3) {
424 board[row-2][col]=7;
425 board[row-1][col]=board[row][col];
426 board[row][col]=current_spot;
427 current_spot=1;
428 row--;
429 boxes_to_go--;
430 moves++;
431 }
432 }
433 else if (board[row-1][col]==7) {
434 /* if there is a wall then do not move the box */
435 if(board[row-2][col]==2) {
436 /* do nothing */
437 }
438 /* we are going from a home to a blank */
439 else if(board[row-2][col]==1) {
440 board[row-2][col]=4;
441 board[row-1][col]=board[row][col];
442 board[row][col]=current_spot;
443 current_spot=3;
444 row--;
445 boxes_to_go++;
446 moves++;
447 }
448 /* if we are going from a home to home */
449 else if(board[row-2][col]==3) {
450 board[row-2][col]=7;
451 board[row-1][col]=board[row][col];
452 board[row][col]=current_spot;
453 current_spot=3;
454 row--;
455 moves++;
456 }
457 }
458 lcd_clear_display();
459 update_screen();
460 }
461
462 if ( b & BUTTON_DOWN ) {
463 /* if it is a blank spot */
464 if (board[row+1][col]==1) {
465 board[row+1][col]=5;
466 board[row][col]=current_spot;
467 current_spot=1;
468 row++;
469 moves++;
470 }
471 /* if it is a home spot */
472 else if (board[row+1][col]==3) {
473 board[row+1][col]=5;
474 board[row][col]=current_spot;
475 current_spot=3;
476 row++;
477 moves++;
478 }
479 else if (board[row+1][col]==4) {
480 /* if there is a wall then do not move the box */
481 if(board[row+2][col]==2) {
482 /* do nothing */
483 }
484 /* if we are going from blank to blank */
485 else if(board[row+2][col]==1) {
486 board[row+2][col]=board[row+1][col];
487 board[row+1][col]=board[row][col];
488 board[row][col]=current_spot;
489 current_spot=1;
490 row++;
491 moves++;
492 }
493 /* if we are going from a blank to home */
494 else if(board[row+2][col]==3) {
495 board[row+2][col]=7;
496 board[row+1][col]=board[row][col];
497 board[row][col]=current_spot;
498 current_spot=1;
499 row++;
500 boxes_to_go--;
501 moves++;
502 }
503 }
504 else if (board[row+1][col]==7) {
505 /* if there is a wall then do not move the box */
506 if(board[row+2][col]==2) {
507 /* do nothing */
508 }
509 /* we are going from a home to a blank */
510 else if(board[row+2][col]==1) {
511 board[row+2][col]=4;
512 board[row+1][col]=board[row][col];
513 board[row][col]=current_spot;
514 current_spot=3;
515 row++;
516 boxes_to_go++;
517 moves++;
518 }
519 /* if we are going from a home to home */
520 else if(board[row+2][col]==3) {
521 board[row+2][col]=7;
522 board[row+1][col]=board[row][col];
523 board[row][col]=current_spot;
524 current_spot=3;
525 row++;
526 moves++;
527 }
528 }
529 lcd_clear_display();
530 update_screen();
531 }
532
533 if (boxes_to_go==0) {
534 moves=0;
535 current_level++;
536 if (current_level == NUM_LEVELS) {
537 lcd_clear_display();
538 lcd_putsxy(10, 20, "YOU WIN!!", 2);
539
540 lcd_update();
541 for (ii=0 ; ii<20 ; ii++) {
542 lcd_invertrect(0,0,111,63);
543 lcd_update();
544 }
545 return;
546 }
547 load_level(current_level);
548 lcd_clear_display();
549 update_screen();
550 }
551 }
552}
553
554
555void sokoban(void)
556{
557 int w, h;
558 int len = strlen(SOKOBAN_TITLE);
559
560 lcd_getfontsize(SOKOBAN_TITLE_FONT, &w, &h);
561
562 /* Get horizontel centering for text */
563 len *= w;
564 if (len%2 != 0)
565 len = ((len+1)/2)+(w/2);
566 else
567 len /= 2;
568
569 if (h%2 != 0)
570 h = (h/2)+1;
571 else
572 h /= 2;
573
574 lcd_clear_display();
575 lcd_putsxy(LCD_WIDTH/2-len, (LCD_HEIGHT/2)-h, SOKOBAN_TITLE,
576 SOKOBAN_TITLE_FONT);
577 lcd_putsxy( 3,42, "[Off] to stop", 0);
578 lcd_putsxy( 3,52, "[F1] + level",0);
579
580 lcd_update();
581 sleep(HZ*2);
582 lcd_clear_display();
583 sokoban_loop();
584}
585
586#endif