1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2003 Pierre Delore
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #ifdef HAVE_LCD_CHARCELLS
25 /* NIM game for the player
30 Two players (you and the cpu) alternately pick a certain number of matches and the one,
31 who takes the last match, loses.
37 First release of the game
39 I Change the patterns definition in order to have a clean code
41 Patch from JB that change:
42 . the win and lose message
43 . the BUTTON_STOP code
45 I suppress the exit variable
46 I suppress or translates the comments which were in French
47 I put min=1 at the of the main loop ( When there are 21 matches you can decide not to
48 take a match. Later you are obliged to take at least one.)
53 /*Pattern for the game*/
54 static unsigned char smile
[]={0x00, 0x11, 0x04, 0x04, 0x00, 0x11, 0x0E}; /* :-) */
55 static unsigned char cry
[] ={0x00, 0x11, 0x04, 0x04, 0x00, 0x0E, 0x11}; /* :-( */
56 static unsigned char pattern3
[]={0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15}; /*3 parts*/
57 static unsigned char pattern2
[]={0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14}; /*2 parts*/
58 static unsigned char pattern1
[]={0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}; /*1 part*/
60 static unsigned char str
[12]; /*String use to display the first line*/
61 static unsigned long hsmile
,hcry
,h1
,h2
; /*Handle for the new pattern*/
63 static bool end
; /*If true game is finished*/
64 static const struct plugin_api
* rb
;
67 /*Display that the action it's impossible*/
68 static void impossible(void)
70 rb
->lcd_puts(0,1,"Impossible!");
76 /*Display that the CPU lose :) */
77 static void lose(void)
79 rb
->lcd_define_pattern(hsmile
,smile
);
80 rb
->lcd_puts(0,1,"You Win!!");
81 rb
->lcd_putc(8,1,hsmile
);
89 /* Display that the CPU win :( */
92 rb
->lcd_define_pattern(hcry
,cry
);
93 rb
->lcd_puts(0,1,"You Lose!!");
94 rb
->lcd_putc(9,1,hcry
);
102 /*Display the first line*/
103 static void display_first_line(int x
)
107 rb
->snprintf(str
,sizeof(str
)," =%d",x
);
108 rb
->lcd_puts(0,0,str
);
110 rb
->lcd_define_pattern(h1
,pattern3
);
112 rb
->lcd_putc(i
,0,h1
);
116 rb
->lcd_define_pattern(h2
,pattern2
);
117 rb
->lcd_putc(i
,0,h2
);
121 rb
->lcd_define_pattern(h2
,pattern1
);
122 rb
->lcd_putc(i
,0,h2
);
126 /* Call when the program end */
127 static void nim_exit(void *parameter
)
131 /*Restore the old pattern*/
132 rb
->lcd_unlock_pattern(h1
);
133 rb
->lcd_unlock_pattern(h2
);
134 rb
->lcd_unlock_pattern(hsmile
);
135 rb
->lcd_unlock_pattern(hcry
);
138 rb
->lcd_clear_display();
142 /* this is the plugin entry point */
143 enum plugin_status
plugin_start(const struct plugin_api
* api
, const void* parameter
)
150 /* if you don't use the parameter, you can do like
151 this to avoid the compiler warning about it */
154 /* if you are using a global api pointer, don't forget to copy it!
155 otherwise you will get lovely "I04: IllInstr" errors... :-) */
158 /*Get the pattern handle*/
159 h1
=rb
->lcd_get_locked_pattern();
160 h2
=rb
->lcd_get_locked_pattern();
161 hcry
=rb
->lcd_get_locked_pattern();
162 hsmile
=rb
->lcd_get_locked_pattern();
165 rb
->splash(HZ
, "NIM V1.2");
166 rb
->lcd_clear_display();
178 /*Empty the event queue*/
179 rb
->button_clear_queue();
188 display_first_line(x
);
190 rb
->snprintf(str
,sizeof(str
),"[%d..%d]?=%d",min
,v
,y
);
191 rb
->lcd_puts(0,1,str
);
197 button
= rb
->button_get(true);
200 case BUTTON_STOP
|BUTTON_REL
:
206 case BUTTON_PLAY
|BUTTON_REL
:
210 case BUTTON_LEFT
|BUTTON_REL
:
216 case BUTTON_RIGHT
|BUTTON_REL
:
223 if (rb
->default_event_handler_ex(button
, nim_exit
,
224 NULL
) == SYS_USB_CONNECTED
)
225 return PLUGIN_USB_CONNECTED
;
228 display_first_line(x
);
229 rb
->snprintf(str
,sizeof(str
),"[%d..%d]?=%d",min
,v
,y
);
230 rb
->lcd_puts(0,1,str
);
234 if ( (y
==0) && (x
<21))
241 if (y
!=0) /*If y=0 and x=21 jump to CPU code */
260 display_first_line(x
);
283 if ((y
==0) && (x
>14))
290 rb
->snprintf(str
,sizeof(str
),"I take=%d",y
);
291 rb
->lcd_puts(0,1,str
);