diff options
author | Linus Nielsen Feltzing <linus@haxx.se> | 2003-11-04 00:30:49 +0000 |
---|---|---|
committer | Linus Nielsen Feltzing <linus@haxx.se> | 2003-11-04 00:30:49 +0000 |
commit | c70a750a0d0967efb651cf4a31b7fd46741a0db5 (patch) | |
tree | a19d97b6a241ac5fec0a4603b830cf10db4bd1d9 | |
parent | 8737a930b5227c0761c571e9669c14d2a020b21c (diff) | |
download | rockbox-c70a750a0d0967efb651cf4a31b7fd46741a0db5.tar.gz rockbox-c70a750a0d0967efb651cf4a31b7fd46741a0db5.zip |
Patch #780511 by Pierre Delore, a NIM game for Players
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4011 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | apps/plugins/nim.c | 295 |
1 files changed, 295 insertions, 0 deletions
diff --git a/apps/plugins/nim.c b/apps/plugins/nim.c new file mode 100644 index 0000000000..94e8dfab11 --- /dev/null +++ b/apps/plugins/nim.c | |||
@@ -0,0 +1,295 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2003 Pierre Delore | ||
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 | #include "plugin.h" | ||
20 | |||
21 | #ifdef HAVE_LCD_CHARCELLS | ||
22 | |||
23 | /* NIM game for the player | ||
24 | |||
25 | Rules of nim game | ||
26 | ----------------- | ||
27 | There are 21 matches. | ||
28 | Two players (you and the cpu) alternately pick a certain number of matches and the one, | ||
29 | who takes the last match, loses. | ||
30 | |||
31 | |||
32 | History: | ||
33 | ------- | ||
34 | V1.0 : 2003-07-22 | ||
35 | First release of the game | ||
36 | V1.1 : 2003-07-22 | ||
37 | I Change the patterns definition in order to have a clean code | ||
38 | V1.2 : 2003-07-30 | ||
39 | Patch from JB that change: | ||
40 | . the win and lose message | ||
41 | . the BUTTON_STOP code | ||
42 | . Add a test | ||
43 | I suppress the exit variable | ||
44 | I suppress or translates the comments which were in French | ||
45 | I put min=1 at the of the main loop ( When there are 21 matches you can decide not to | ||
46 | take a match. Later you are obliged to take at least one.) | ||
47 | */ | ||
48 | |||
49 | |||
50 | /*Pattern for the game*/ | ||
51 | static unsigned char smile[]={0x00, 0x11, 0x04, 0x04, 0x00, 0x11, 0x0E}; /* :-) */ | ||
52 | static unsigned char cry[] ={0x00, 0x11, 0x04, 0x04, 0x00, 0x0E, 0x11}; /* :-( */ | ||
53 | static unsigned char pattern3[]={0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15}; /*3 parts*/ | ||
54 | static unsigned char pattern2[]={0x14, 0x14, 0x14, 0x14, 0x14, 0x14, 0x14}; /*2 parts*/ | ||
55 | static unsigned char pattern1[]={0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10}; /*1 part*/ | ||
56 | |||
57 | static unsigned char str[12]; /*String use to display the first line*/ | ||
58 | static unsigned char hsmile,hcry,h1,h2; /*Handle for the new pattern*/ | ||
59 | |||
60 | static bool end; /*If true game is finished*/ | ||
61 | static struct plugin_api* rb; | ||
62 | |||
63 | |||
64 | /*Display that the action it's impossible*/ | ||
65 | static void impossible(void) | ||
66 | { | ||
67 | rb->lcd_puts(0,1,"Impossible!"); | ||
68 | rb->sleep(HZ); | ||
69 | return; | ||
70 | } | ||
71 | |||
72 | /*Display that the CPU lose :) */ | ||
73 | static void lose(void) | ||
74 | { | ||
75 | rb->lcd_define_pattern(hsmile,smile); | ||
76 | rb->snprintf(str,sizeof(str),"You Win!!%c",hsmile); | ||
77 | rb->lcd_puts(0,1,str); | ||
78 | end=true; | ||
79 | rb->sleep(HZ*2); | ||
80 | return; | ||
81 | } | ||
82 | |||
83 | |||
84 | /* Display that the CPU win :( */ | ||
85 | static void win(void) | ||
86 | { | ||
87 | rb->lcd_define_pattern(hcry,cry); | ||
88 | rb->snprintf(str,sizeof(str),"You Lose!!%c",hcry); | ||
89 | rb->lcd_puts(0,1,str); | ||
90 | end=true; | ||
91 | rb->sleep(HZ*2); | ||
92 | return; | ||
93 | } | ||
94 | |||
95 | |||
96 | /*Display the first line*/ | ||
97 | static void display_first_line(int x) | ||
98 | { | ||
99 | int i; | ||
100 | |||
101 | rb->snprintf(str,sizeof(str)," =%d",x); | ||
102 | |||
103 | rb->lcd_define_pattern(h1,pattern3); | ||
104 | for(i=0;i<x/3;i++) | ||
105 | str[i]=h1; | ||
106 | |||
107 | if (x%3==2) | ||
108 | { | ||
109 | rb->lcd_define_pattern(h2,pattern2); | ||
110 | str[i]=h2; | ||
111 | } | ||
112 | if (x%3==1) | ||
113 | { | ||
114 | rb->lcd_define_pattern(h2,pattern1); | ||
115 | str[i]=h2; | ||
116 | } | ||
117 | rb->lcd_puts(0,0,str); | ||
118 | } | ||
119 | |||
120 | /* Call when the program end */ | ||
121 | static void nim_exit(void) | ||
122 | { | ||
123 | /*Restore the old pattern*/ | ||
124 | rb->lcd_unlock_pattern(h1); | ||
125 | rb->lcd_unlock_pattern(h2); | ||
126 | rb->lcd_unlock_pattern(hsmile); | ||
127 | rb->lcd_unlock_pattern(hcry); | ||
128 | |||
129 | /*Clear the screen*/ | ||
130 | rb->lcd_clear_display(); | ||
131 | } | ||
132 | |||
133 | /* this is the plugin entry point */ | ||
134 | enum plugin_status plugin_start(struct plugin_api* api, void* parameter) | ||
135 | { | ||
136 | int y,z,button; | ||
137 | int x,v,min; | ||
138 | bool ok; | ||
139 | bool go; | ||
140 | |||
141 | /* this macro should be called as the first thing you do in the plugin. | ||
142 | it test that the api version and model the plugin was compiled for | ||
143 | matches the machine it is running on */ | ||
144 | TEST_PLUGIN_API(api); | ||
145 | |||
146 | /* if you don't use the parameter, you can do like | ||
147 | this to avoid the compiler warning about it */ | ||
148 | (void)parameter; | ||
149 | |||
150 | /* if you are using a global api pointer, don't forget to copy it! | ||
151 | otherwise you will get lovely "I04: IllInstr" errors... :-) */ | ||
152 | rb = api; | ||
153 | |||
154 | /*Get the pattern handle*/ | ||
155 | h1=rb->lcd_get_locked_pattern(); | ||
156 | h2=rb->lcd_get_locked_pattern(); | ||
157 | hcry=rb->lcd_get_locked_pattern(); | ||
158 | hsmile=rb->lcd_get_locked_pattern(); | ||
159 | |||
160 | |||
161 | rb->splash(HZ, 0, true, "NIM V1.2"); | ||
162 | rb->lcd_clear_display(); | ||
163 | |||
164 | /* Main loop */ | ||
165 | while (1) | ||
166 | { | ||
167 | /* Init */ | ||
168 | x=21; | ||
169 | v=1; | ||
170 | y=1; | ||
171 | end=false; | ||
172 | min=0; | ||
173 | |||
174 | /*Empty the event queue*/ | ||
175 | while (rb->button_get(false)!=BUTTON_NONE); | ||
176 | |||
177 | /* Game loop */ | ||
178 | while(end!=true) | ||
179 | { | ||
180 | do | ||
181 | { | ||
182 | ok=1; | ||
183 | y=1; | ||
184 | display_first_line(x); | ||
185 | |||
186 | rb->snprintf(str,sizeof(str),"[%d..%d]?=%d",min,v,y); | ||
187 | rb->lcd_puts(0,1,str); | ||
188 | |||
189 | go=false; | ||
190 | while (!go) | ||
191 | { | ||
192 | button = rb->button_get(true); | ||
193 | switch ( button ) | ||
194 | { | ||
195 | case BUTTON_STOP|BUTTON_REL: | ||
196 | go = true; | ||
197 | nim_exit(); | ||
198 | return PLUGIN_OK; | ||
199 | break; | ||
200 | |||
201 | case BUTTON_PLAY|BUTTON_REL: | ||
202 | go=true; | ||
203 | break; | ||
204 | |||
205 | case BUTTON_LEFT|BUTTON_REL: | ||
206 | go=false; | ||
207 | if (y>min) | ||
208 | y--; | ||
209 | break; | ||
210 | |||
211 | case BUTTON_RIGHT|BUTTON_REL: | ||
212 | go=false; | ||
213 | if (y<v) | ||
214 | y++; | ||
215 | break; | ||
216 | |||
217 | case SYS_USB_CONNECTED: | ||
218 | rb->usb_screen(); | ||
219 | nim_exit(); | ||
220 | return PLUGIN_USB_CONNECTED; | ||
221 | } | ||
222 | display_first_line(x); | ||
223 | rb->snprintf(str,sizeof(str),"[%d..%d]?=%d",min,v,y); | ||
224 | rb->lcd_puts(0,1,str); | ||
225 | } | ||
226 | |||
227 | if ( (y==0) && (x<21)) | ||
228 | { | ||
229 | impossible(); | ||
230 | ok=false; | ||
231 | } | ||
232 | else | ||
233 | { | ||
234 | if (y!=0) /*If y=0 and x=21 jump to CPU code */ | ||
235 | { | ||
236 | if ((y>v) || (y>x)) | ||
237 | { | ||
238 | impossible(); | ||
239 | ok=false; | ||
240 | } | ||
241 | if (y-x==0) | ||
242 | win(); | ||
243 | else | ||
244 | { | ||
245 | v=y*2; | ||
246 | x-=y; | ||
247 | } | ||
248 | } | ||
249 | } | ||
250 | } | ||
251 | while (ok==false); | ||
252 | |||
253 | display_first_line(x); | ||
254 | |||
255 | /*CPU*/ | ||
256 | if (x==1) | ||
257 | lose(); | ||
258 | else | ||
259 | if (x==2) | ||
260 | win(); | ||
261 | y=0; | ||
262 | if (end==false) | ||
263 | { | ||
264 | for (z=v;z>=1;z--) | ||
265 | { | ||
266 | if (x-z==1) | ||
267 | y=z; | ||
268 | } | ||
269 | if (y<=0) | ||
270 | { | ||
271 | for(z=v;z>=1;z--) | ||
272 | { | ||
273 | if(x-(z*3)==2) | ||
274 | y=z; | ||
275 | } | ||
276 | if ((y==0) && (x>14)) | ||
277 | y=v; | ||
278 | if (y==0) | ||
279 | y=1; | ||
280 | } | ||
281 | v=y*2; | ||
282 | x-=y; | ||
283 | rb->snprintf(str,sizeof(str),"I take=%d",y); | ||
284 | rb->lcd_puts(0,1,str); | ||
285 | rb->sleep(HZ); | ||
286 | } | ||
287 | if ((x==1)&&(!end)) | ||
288 | win(); | ||
289 | min=1; | ||
290 | } | ||
291 | } | ||
292 | nim_exit(); | ||
293 | return PLUGIN_OK; | ||
294 | } | ||
295 | #endif | ||