summaryrefslogtreecommitdiffstats
path: root/apps/plugins/sliding_puzzle.c
blob: dfc081150676677e6d82831e1dbad1ea738f6649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Vicentini Martin
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#ifdef HAVE_LCD_BITMAP

PLUGIN_HEADER

/* variable button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD
#define PUZZLE_QUIT BUTTON_OFF
#define PUZZLE_UP BUTTON_UP
#define PUZZLE_DOWN BUTTON_DOWN
#define PUZZLE_SHUFFLE BUTTON_F1
#define PUZZLE_PICTURE BUTTON_F2

#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
#define PUZZLE_QUIT BUTTON_OFF
#define PUZZLE_UP BUTTON_UP
#define PUZZLE_DOWN BUTTON_DOWN
#define PUZZLE_SHUFFLE BUTTON_F1
#define PUZZLE_PICTURE BUTTON_F2

#elif CONFIG_KEYPAD == ONDIO_PAD
#define PUZZLE_QUIT BUTTON_OFF
#define PUZZLE_UP BUTTON_UP
#define PUZZLE_DOWN BUTTON_DOWN
#define PUZZLE_SHUFFLE_PICTURE_PRE BUTTON_MENU
#define PUZZLE_SHUFFLE (BUTTON_MENU | BUTTON_REPEAT)
#define PUZZLE_PICTURE (BUTTON_MENU | BUTTON_REL)

#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
      (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define PUZZLE_QUIT BUTTON_OFF
#define PUZZLE_UP BUTTON_UP
#define PUZZLE_DOWN BUTTON_DOWN
#define PUZZLE_SHUFFLE BUTTON_SELECT
#define PUZZLE_PICTURE BUTTON_ON

#define PUZZLE_RC_QUIT BUTTON_RC_STOP

#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
      (CONFIG_KEYPAD == IPOD_3G_PAD)
#define PUZZLE_QUIT    (BUTTON_SELECT | BUTTON_MENU)
#define PUZZLE_UP      BUTTON_MENU
#define PUZZLE_DOWN    BUTTON_PLAY
#define PUZZLE_SHUFFLE (BUTTON_SELECT | BUTTON_LEFT)
#define PUZZLE_PICTURE (BUTTON_SELECT | BUTTON_RIGHT)

#elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
#define PUZZLE_QUIT BUTTON_POWER
#define PUZZLE_UP BUTTON_UP
#define PUZZLE_DOWN BUTTON_DOWN
#define PUZZLE_SHUFFLE BUTTON_REC
#define PUZZLE_PICTURE BUTTON_PLAY

#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
#define PUZZLE_QUIT BUTTON_A
#define PUZZLE_UP BUTTON_UP
#define PUZZLE_DOWN BUTTON_DOWN
#define PUZZLE_SHUFFLE BUTTON_SELECT
#define PUZZLE_PICTURE BUTTON_POWER

#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
#define PUZZLE_QUIT BUTTON_POWER
#define PUZZLE_UP BUTTON_UP
#define PUZZLE_DOWN BUTTON_DOWN
#define PUZZLE_SHUFFLE BUTTON_REC
#define PUZZLE_PICTURE BUTTON_SELECT

#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
#define PUZZLE_QUIT BUTTON_POWER
#define PUZZLE_UP BUTTON_SCROLL_UP
#define PUZZLE_DOWN BUTTON_SCROLL_DOWN
#define PUZZLE_SHUFFLE BUTTON_REW
#define PUZZLE_PICTURE BUTTON_PLAY

#endif

static struct plugin_api* rb;
static int spots[20];
static int hole = 19, moves;
static char s[5];
static bool pic = true;
static unsigned char picture[20][32] = {
    { 0x00, 0x00, 0x00, 0x00, 0x18, 0x38, 0xf8, 0xd9,
      0x10, 0xb0, 0x60, 0xc0, 0x80, 0x00, 0x30, 0x78,
      0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x01, 0x07,
      0xbf, 0xf8, 0x43, 0x1c, 0x61, 0x5e, 0xfc, 0xfc },
    
    { 0x68, 0xc8, 0x48, 0x08, 0x98, 0x90, 0xb0, 0xa4,
      0xa0, 0xc0, 0xc0, 0x88, 0x14, 0x08, 0x00, 0x00,
      0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x03,
      0x00, 0x00, 0x30, 0x48, 0x79, 0x33, 0x06, 0x1c },
    
    { 0x20, 0x00, 0x06, 0x09, 0x09, 0x06, 0x00, 0x80,
      0x40, 0x80, 0x00, 0x08, 0x14, 0x08, 0x20, 0x00,
      0x60, 0x30, 0x18, 0xf9, 0x70, 0x00, 0x00, 0x00,
      0xf9, 0x18, 0x30, 0x60, 0x30, 0x18, 0x06, 0x32 },
    
    { 0x00, 0x80, 0x42, 0xa0, 0x50, 0x90, 0x88, 0x88,
      0x84, 0xa4, 0xa4, 0x64, 0x24, 0x18, 0x00, 0x40,
      0x79, 0x4a, 0x31, 0x02, 0x05, 0x2a, 0xd5, 0xaa,
      0x55, 0xaa, 0x55, 0xab, 0x56, 0xac, 0x58, 0xb0 },
    
    { 0x04, 0x0a, 0x04, 0x00, 0x80, 0x80, 0xc0, 0xc8,
      0x40, 0xc2, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00,
      0x60, 0x38, 0x9c, 0xe7, 0x59, 0x0c, 0xc4, 0xfc,
      0x3f, 0x07, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00 },
    
    { 0x00, 0x04, 0x00, 0x00, 0x80, 0xe0, 0x78, 0x1e,
      0xa7, 0xd9, 0xcc, 0x76, 0x3b, 0x0d, 0x1f, 0xff,
      0x00, 0x00, 0x00, 0x00, 0x03, 0x13, 0x03, 0x03,
      0x11, 0x29, 0x10, 0x00, 0x04, 0x2a, 0x0b, 0x0b },
    
    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0,
      0xc0, 0x8e, 0x10, 0x00, 0x80, 0x80, 0x60, 0x38,
      0x0a, 0x08, 0x05, 0x05, 0x07, 0x03, 0x03, 0x03,
      0x03, 0x03, 0x01, 0x11, 0x01, 0x00, 0x00, 0x80 },
    
    { 0x0e, 0x18, 0x31, 0x3e, 0x1c, 0x00, 0x00, 0x1c,
      0x3e, 0x31, 0x18, 0x0c, 0x0c, 0x18, 0x30, 0x20,
      0x00, 0x00, 0x08, 0x14, 0x08, 0x00, 0x02, 0x80,
      0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
    
    { 0x60, 0xc0, 0x48, 0xc7, 0x60, 0xb8, 0x57, 0xaa,
      0x55, 0xaa, 0x55, 0xaa, 0xd5, 0xaa, 0x75, 0x3a,
      0x00, 0x00, 0x01, 0x23, 0x05, 0x05, 0x09, 0x0a,
      0x0b, 0x0a, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00 },
    
    { 0xe5, 0x8d, 0x18, 0x30, 0x41, 0xc1, 0x8f, 0xfe,
      0xf0, 0x81, 0x0a, 0x01, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x01, 0x03, 0x02, 0x06, 0x06, 0x04, 0x04,
      0x04, 0x05, 0x07, 0x06, 0x00, 0x00, 0x00, 0x00 },
    
    { 0x00, 0x00, 0x00, 0x10, 0x28, 0x10, 0x00, 0x04,
      0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
      0xc0, 0xb0, 0x18, 0xcc, 0x24, 0x86, 0x42, 0x22,
      0x31, 0x69, 0xd1, 0xa9, 0x51, 0xa1, 0x41, 0x02 },
    
    { 0x00, 0x00, 0x00, 0x00, 0x40, 0xa1, 0x40, 0x00,
      0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x12, 0x16, 0x5c, 0x58, 0xf0, 0xc0, 0x00, 0x00,
      0x06, 0x09, 0x09, 0x86, 0x60, 0x18, 0xc4, 0xf2 },
    
    { 0x00, 0x00, 0x00, 0x80, 0x80, 0x40, 0x40, 0x40,
      0x40, 0x40, 0x40, 0x40, 0xc0, 0x80, 0x8c, 0x12,
      0x6a, 0xa5, 0x95, 0xd2, 0xca, 0xc9, 0xe9, 0xe9,
      0xe1, 0xe9, 0xe1, 0xe1, 0x62, 0x62, 0xc5, 0xc5 },
    
    { 0x12, 0x0c, 0x00, 0x80, 0x80, 0x40, 0x40, 0x40,
      0x40, 0x40, 0x40, 0x82, 0x85, 0x02, 0x00, 0x00,
      0x8a, 0x32, 0x6f, 0xd6, 0xaa, 0x55, 0x83, 0x01,
      0x81, 0x01, 0x81, 0x82, 0x82, 0x0d, 0xbe, 0xdc },
    
    { 0x00, 0x00, 0x00, 0x82, 0x00, 0x00, 0x00, 0x00,
      0x41, 0x00, 0x00, 0x00, 0x02, 0x05, 0x02, 0x00,
      0x00, 0x00, 0x01, 0x02, 0x01, 0x04, 0x00, 0x60,
      0x90, 0x90, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00 },
    
    { 0x3f, 0x6a, 0xc0, 0x9f, 0x20, 0x27, 0x48, 0x4b,
      0x47, 0x22, 0x50, 0x0f, 0x95, 0x4a, 0x25, 0x90,
      0x07, 0x0f, 0x17, 0x23, 0x43, 0x42, 0x44, 0x40,
      0x44, 0x40, 0x42, 0x40, 0x60, 0x30, 0x3c, 0x1f },
    
    { 0xd0, 0x50, 0x11, 0xb9, 0xef, 0x07, 0x80, 0x40,
      0x30, 0x98, 0x9c, 0xbf, 0x60, 0xc7, 0x0d, 0x36,
      0x59, 0x11, 0x21, 0x23, 0x22, 0x21, 0x30, 0x10,
      0x0d, 0x42, 0x01, 0x80, 0x00, 0x40, 0x03, 0x0c },
    
    { 0xc3, 0x81, 0x81, 0x00, 0x00, 0x01, 0x03, 0x03,
      0x03, 0x03, 0x02, 0x01, 0x00, 0x00, 0x80, 0x80,
      0x10, 0x91, 0x23, 0x23, 0x67, 0xe6, 0xe6, 0xa6,
      0xa6, 0xa6, 0xa6, 0xa6, 0xb3, 0x93, 0x59, 0x49 },
    
    { 0xc1, 0x71, 0xff, 0x54, 0x0a, 0x02, 0x06, 0x0e,
      0x0e, 0x0a, 0x06, 0x02, 0x02, 0xc5, 0x7b, 0x17,
      0x24, 0x10, 0x3b, 0x7f, 0x92, 0xa6, 0xa4, 0xa4,
      0xa4, 0xa4, 0xa4, 0x66, 0x23, 0x11, 0x12, 0x0c },
    
    { 0x55, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
      0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0xaa,
      0x55, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80,
      0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0xaa }
};

/* draws a spot at the coordinates (x,y), range of p is 1-20 */
static void draw_spot(int p, int x, int y)
{
    if (pic || p==20) {
        rb->lcd_mono_bitmap (picture[p-1], x, y, 16, 16);
    } else {
        rb->lcd_drawrect(x, y, 16, 16);
        rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
        rb->lcd_fillrect(x+1, y+1, 14, 14);
        rb->lcd_set_drawmode(DRMODE_SOLID);
        rb->snprintf(s, sizeof(s), "%d", p);
        rb->lcd_putsxy(x+2, y+4, (unsigned char *)s);
    }
}
    
/* check if the puzzle is solved */
static bool puzzle_finished(void)
{
    int i;
    for (i=0; i<20; i++)
    if (spots[i] != (i+1))
        return false;
    return true;
}
    
/* move a piece in any direction */
static void move_spot(int x, int y)
{
    int i;
    spots[hole] = spots[hole-x-5*y];
    hole -= (x+5*y);
    moves++;
    rb->snprintf(s, sizeof(s), "%d", moves);
    rb->lcd_putsxy(85, 20, (unsigned char *)s);

    for (i=4; i<=16; i+=4) {
        draw_spot(20, (hole%5)*16, (hole/5)*16);
        draw_spot(spots[hole], (hole%5)*16 + x*i, (hole/5)*16 + y*i);
        rb->lcd_update();
    }
    spots[hole] = 20;
}
    
/* initializes the puzzle */
static void puzzle_init(void)
{
    int i, r, temp, tsp[20];
    moves = 0;
    rb->lcd_clear_display();
    rb->lcd_drawrect(80, 0, 32, 64);
    rb->lcd_putsxy(81, 10, (unsigned char *)"Moves");
    rb->snprintf(s, sizeof(s), "%d", moves);
    rb->lcd_putsxy(85, 20, (unsigned char *)s);
    
    /* shuffle spots */
    for (i=19; i>=0; i--) {
        r = (rb->rand() % (i+1));
    
        temp = spots[r];
        spots[r] = spots[i];
        spots[i] = temp;
    
        if (spots[i]==20)
            hole = i;
    }
    
    /* test if the puzzle is solvable */
    for (i=0; i<20; i++)
        tsp[i] = spots[i];
    r=0;

    /* First, check if the problem has even or odd parity,
       depending on where the empty square is */
    if (((4-hole%5) + (3-hole/5))%2 == 1)
        ++r;

    /* Now check how many swaps we need to solve it */
    for (i=0; i<19; i++) {
        while (tsp[i] != (i+1)) {
            temp = tsp[i];
            tsp[i] = tsp[temp-1];
            tsp[temp-1] = temp;
            ++r;
        }
    }
    
    /* if the random puzzle isn't solvable just change two spots */
    if (r%2 == 1) {
        if (spots[0]!=20 && spots[1]!=20) {
            temp = spots[0];
            spots[0] = spots[1];
            spots[1] = temp;
        } else {
            temp = spots[2];
            spots[2] = spots[3];
            spots[3] = temp;
        }
    }
    
    /* draw spots to the lcd */
    for (i=0; i<20; i++)
        draw_spot(spots[i], (i%5)*16, (i/5)*16);
    rb->lcd_update();
}

/* the main game loop */
static int puzzle_loop(void)
{
    int button;
    int lastbutton = BUTTON_NONE;
    int i;
    puzzle_init();
    while(true) {
        button = rb->button_get(true);
        switch (button) {
#ifdef PUZZLE_RC_QUIT
            case PUZZLE_RC_QUIT:
#endif
            case PUZZLE_QUIT:
                /* get out of here */
                return PLUGIN_OK;

            case PUZZLE_SHUFFLE:
#ifdef PUZZLE_SHUFFLE_PICTURE_PRE
                if (lastbutton != PUZZLE_SHUFFLE_PICTURE_PRE)
                    break;
#endif
                /* mix up the pieces */
                puzzle_init();
                break;
    
            case PUZZLE_PICTURE:
#ifdef PUZZLE_SHUFFLE_PICTURE_PRE
                if (lastbutton != PUZZLE_SHUFFLE_PICTURE_PRE)
                    break;
#endif
                /* change picture */
                pic = (pic==true?false:true);
                for (i=0; i<20; i++)
                    draw_spot(spots[i], (i%5)*16, (i/5)*16);
                rb->lcd_update();
                break;
    
            case BUTTON_LEFT:
                if ((hole%5)<4 && !puzzle_finished())
                    move_spot(-1, 0);
                break;
    
            case BUTTON_RIGHT:
                if ((hole%5)>0 && !puzzle_finished())
                    move_spot(1, 0);
                break;
    
            case PUZZLE_UP:
                if ((hole/5)<3 && !puzzle_finished())
                    move_spot(0, -1);
                break;
    
            case PUZZLE_DOWN:
                if ((hole/5)>0 && !puzzle_finished())
                    move_spot(0, 1);
                break;
    
            default:
                if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
                    return PLUGIN_USB_CONNECTED;
                break;
        }
        if (button != BUTTON_NONE)
            lastbutton = button;
    }
}
    
enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
    int i, w, h;

    (void)parameter;
    rb = api;
    
    /* print title */
    rb->lcd_getstringsize((unsigned char *)"Sliding Puzzle", &w, &h);
    w = (w+1)/2;
    h = (h+1)/2;
    rb->lcd_clear_display();
    rb->lcd_putsxy(LCD_WIDTH/2-w, (LCD_HEIGHT/2)-h, (unsigned char *)"Sliding Puzzle");
    rb->lcd_update();
    rb->sleep(HZ);

    /* print instructions */
    rb->lcd_clear_display();
    rb->lcd_setfont(FONT_SYSFIXED);
#if CONFIG_KEYPAD == RECORDER_PAD
    rb->lcd_putsxy(3, 18, "[OFF] to stop");
    rb->lcd_putsxy(3, 28, "[F1] shuffle");
    rb->lcd_putsxy(3, 38, "[F2] change pic");
#elif CONFIG_KEYPAD == ONDIO_PAD
    rb->lcd_putsxy(0, 18, "[OFF] to stop");
    rb->lcd_putsxy(0, 28, "[MODE..] shuffle");
    rb->lcd_putsxy(0, 38, "[MODE] change pic");
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
      (CONFIG_KEYPAD == IPOD_3G_PAD)
    rb->lcd_putsxy(0, 18, "[S-MENU] to stop");
    rb->lcd_putsxy(0, 28, "[S-LEFT] shuffle");
    rb->lcd_putsxy(0, 38, "[S-RIGHT] change pic");
#endif
    rb->lcd_update();
    rb->button_get_w_tmo(HZ*2);
    
    rb->lcd_clear_display();
    rb->lcd_drawrect(80, 0, 32, 64);
    rb->lcd_putsxy(81, 10, (unsigned char *)"Moves");
    for (i=0; i<20; i++) {
        spots[i]=(i+1);
        draw_spot(spots[i], (i%5)*16, (i/5)*16);
    }
    hole = 19;
    pic = true;
    rb->lcd_update();
    rb->sleep(HZ*2);
    
    return puzzle_loop();
}

#endif