summaryrefslogtreecommitdiffstats
path: root/apps/plugins/nim.c
blob: 70cf8dcec417871395c56801592d9c7842397c04 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2003 Pierre Delore
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "lib/pluginlib_exit.h"

/* NIM game for the player

Rules of nim game
-----------------
There are 21 matches.
Two players (you and the cpu) alternately pick a certain number of matches and the one,
who takes the last match, loses.


History:
-------
V1.0 : 2003-07-22
    First release of the game
V1.1 : 2003-07-22
    I Change the patterns definition in order to have a clean code
V1.2 : 2003-07-30
    Patch from JB that change:
    . the win and lose message
    . the BUTTON_STOP code
    . Add a test
    I suppress the exit variable
    I suppress or translates the comments which were in French
    I put min=1 at the of the main loop ( When there are 21 matches you can decide not to
     take a match. Later you are obliged to take at least one.)
*/



/*Pattern for the game*/
static unsigned char smile[]={0x00, 0x11, 0x04, 0x04, 0x00, 0x11, 0x0E};    /* :-) */
static unsigned char cry[]  ={0x00, 0x11, 0x04, 0x04, 0x00, 0x0E, 0x11};    /* :-( */
static unsigned char pattern3[]={0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15}; /*3 parts*/
static unsigned char pattern2[]={0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14}; /*2 parts*/
static unsigned char pattern1[]={0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}; /*1 part*/

static unsigned long hsmile,hcry,h1,h2; /*Handle for the new pattern*/

static bool end;    /*If true game is finished*/

/*Display that the action it's impossible*/
static void impossible(void)
{
    rb->lcd_puts(0,1,"Impossible!");
    rb->lcd_update();
    rb->sleep(HZ);
    return;
}

/*Display that the CPU lose :) */
static void lose(void)
{
    rb->lcd_define_pattern(hsmile,smile);
    rb->lcd_puts(0,1,"You Win!!");
    rb->lcd_putc(8,1,hsmile);
    rb->lcd_update();
    end=true;
    rb->sleep(HZ*2);
    return;
}


/* Display that the CPU win :( */
static void win(void)
{
    rb->lcd_define_pattern(hcry,cry);
    rb->lcd_puts(0,1,"You Lose!!");
    rb->lcd_putc(9,1,hcry);
    rb->lcd_update();
    end=true;
    rb->sleep(HZ*2);
    return;
}


/*Display the first line*/
static void display_first_line(int x)
{
    int i;

    rb->lcd_putsf(0,0,"       =%d",x);

    rb->lcd_define_pattern(h1,pattern3);
    for (i=0;i<x/3;i++)
        rb->lcd_putc(i,0,h1);

    if (x%3==2)
    {
        rb->lcd_define_pattern(h2,pattern2);
        rb->lcd_putc(i,0,h2);
    }
    if (x%3==1)
    {
        rb->lcd_define_pattern(h2,pattern1);
        rb->lcd_putc(i,0,h2);
    }
}

/* Call when the program end */
static void nim_exit(void)
{
    /*Restore the old pattern*/
    rb->lcd_unlock_pattern(h1);
    rb->lcd_unlock_pattern(h2);
    rb->lcd_unlock_pattern(hsmile);
    rb->lcd_unlock_pattern(hcry);

    /*Clear the screen*/
    rb->lcd_clear_display();
    rb->lcd_update();
}

/* this is the plugin entry point */
enum plugin_status plugin_start(const void* parameter)
{
    int y,z,button;
    int x,v,min;
    bool ok;
    bool go;
    atexit(nim_exit);

    /* if you don't use the parameter, you can do like
       this to avoid the compiler warning about it */
    (void)parameter;

    /*Get the pattern handle*/
    h1=rb->lcd_get_locked_pattern();
    h2=rb->lcd_get_locked_pattern();
    hcry=rb->lcd_get_locked_pattern();
    hsmile=rb->lcd_get_locked_pattern();


    rb->splash(HZ, "NIM V1.2");
    rb->lcd_clear_display();

    /* Main loop */
    while (1)
    {
        /* Init */
        x=21;
        v=1;
        y=1;
        end=false;
        min=0;

        /*Empty the event queue*/
        rb->button_clear_queue();

        /* Game loop */
        while(end!=true)
        {
            do
            {
                ok=1;
                y=1;
                display_first_line(x);

                rb->lcd_putsf(0,1,"[%d..%d]?=%d",min,v,y);
                rb->lcd_update();

                go=false;
                while (!go)
                {
                    button = rb->button_get(true);
                    switch ( button )
                    {
                        case BUTTON_STOP|BUTTON_REL:
                            go = true;
                            return PLUGIN_OK;
                            break;

                        case BUTTON_PLAY|BUTTON_REL:
                            go=true;
                            break;

                        case BUTTON_LEFT|BUTTON_REL:
                            go=false;
                            if (y>min)
                                y--;
                            break;

                        case BUTTON_RIGHT|BUTTON_REL:
                            go=false;
                            if (y<v)
                                y++;
                            break;

                        default:
                            exit_on_usb(button);
                            break;
                    }
                    display_first_line(x);
                    rb->lcd_putsf(0,1,"[%d..%d]?=%d",min,v,y);
                    rb->lcd_update();
                }

                if ( (y==0) && (x<21))
                {
                    impossible();
                    ok=false;
                }
                else
                {
                    if (y!=0) /*If y=0 and x=21 jump to CPU code */
                    {
                        if ((y>v) || (y>x))
                        {
                            impossible();
                            ok=false;
                        }
                        if (y-x==0)
                            win();
                        else
                        {
                            v=y*2;
                            x-=y;
                        }
                    }
                }
            }
            while (ok==false);

            display_first_line(x);

            /*CPU*/
            if (x==1)
                lose();
            else
                if (x==2)
                    win();
            y=0;
            if (end==false)
            {
                for (z=v;z>=1;z--)
                {
                    if (x-z==1)
                        y=z;
                }
                if (y<=0)
                {
                    for(z=v;z>=1;z--)
                    {
                        if(x-(z*3)==2)
                            y=z;
                    }
                    if ((y==0) && (x>14))
                        y=v;
                    if (y==0)
                        y=1;
                }
                v=y*2;
                x-=y;
                rb->lcd_putsf(0,1,"I take=%d",y);
                rb->lcd_update();
                rb->sleep(HZ);
            }
            if ((x==1)&&(!end))
                win();
            min=1;
        }
    }
    return PLUGIN_OK;
}