Gigabeat S: Do simple direct keypad scanning rather than triggering a separate scan...
[kugel-rb.git] / apps / plugins / reversi / reversi-gui.c
blob6f5d53da5804a8fd253734660580fd6bec890557
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (c) 2006 Alexander Levin
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 GUI part of reversi. Code is inspired by sudoku code by Dave Chapman
24 which is copyright (c) 2005 Dave Chapman and is released under the
25 GNU General Public License.
28 User instructions
29 -----------------
31 Use the arrow keys to move cursor, and press TOGGLE to place a stone.
33 At any time during the game, press MENU to bring up the game menu with
34 further options:
35 - Save
36 - Reload
37 - Clear
40 #include "plugin.h"
42 #include "reversi-game.h"
43 #include "reversi-strategy.h"
44 #include "reversi-gui.h"
46 #include "lib/playback_control.h"
50 /* This is initialized at the start of the plugin and used to determine the
51 * Appropriate game board size/legend spacing if the font is larger than a cell
52 * height/width.
54 static int font_width;
55 static int font_height;
57 /* Where the board begins */
58 #define XOFS 4
59 #define YOFS 4
61 #if LCD_HEIGHT > LCD_WIDTH
62 #define MARGIN_W (XOFS*2+1)
63 #define MARGIN_H (YOFS*2+1)
64 #define MARGIN_C_W 0
65 #define MARGIN_C_H 2
66 #else
67 #define MARGIN_W (XOFS*2 + 16)
68 #define MARGIN_H (YOFS*2+1)
69 #define MARGIN_C_W 1
70 #define MARGIN_C_H 0
71 #endif
73 #if ( (LCD_WIDTH - MARGIN_W) / (BOARD_SIZE+MARGIN_C_W)) < \
74 ( (LCD_HEIGHT - MARGIN_H) / (BOARD_SIZE+MARGIN_C_H))
76 #define CELL_PRE ( ( (LCD_WIDTH * LCD_PIXEL_ASPECT_WIDTH / \
77 LCD_PIXEL_ASPECT_HEIGHT) - MARGIN_W) / \
78 (BOARD_SIZE+MARGIN_C_W) )
80 #define CELL_WIDTH (CELL_PRE*LCD_PIXEL_ASPECT_HEIGHT / LCD_PIXEL_ASPECT_WIDTH)
81 #define CELL_HEIGHT (CELL_PRE)
82 #else
83 #define CELL_PRE ( ( (LCD_HEIGHT * LCD_PIXEL_ASPECT_HEIGHT / \
84 LCD_PIXEL_ASPECT_WIDTH) - MARGIN_H) / \
85 (BOARD_SIZE+MARGIN_C_H) )
87 #define CELL_WIDTH (CELL_PRE)
88 #define CELL_HEIGHT (CELL_PRE*LCD_PIXEL_ASPECT_WIDTH / LCD_PIXEL_ASPECT_HEIGHT)
89 #endif
91 /* Total width and height of the board without enclosing box */
92 #define BOARD_WIDTH (CELL_WIDTH*BOARD_SIZE)
93 #define BOARD_HEIGHT (CELL_HEIGHT*BOARD_SIZE)
95 /* Thickness of the white cells' lines */
96 #if (CELL_WIDTH >= 10) && (CELL_HEIGHT >= 10)
97 #define CELL_LINE_THICKNESS CELL_WIDTH/5
98 #else
99 #define CELL_LINE_THICKNESS 1
100 #endif
102 /* Margins within a cell */
103 #define STONE_MARGIN 2
105 #define CURSOR_MARGIN 1
107 /* Upper left corner of a cell */
108 #define CELL_X(c) (XOFS + (c)*CELL_WIDTH)
109 #define CELL_Y(r) (YOFS + (r)*CELL_HEIGHT)
111 /* Used for touchscreen to convert an X/Y location to a cell location */
112 #define CELL_C(x) (((x)-XOFS)/CELL_WIDTH)
113 #define CELL_R(y) (((y)-YOFS)/CELL_HEIGHT)
115 #if LCD_HEIGHT > LCD_WIDTH
116 #define LEGEND_X(lc) (CELL_X(lc))
117 #define LEGEND_Y(lr) ((CELL_HEIGHT > font_height) ? \
118 CELL_Y(BOARD_SIZE+lr) + YOFS + 1 : \
119 BOARD_HEIGHT + 2*YOFS + font_height*(lr-BOARD_SIZE))
120 #else
121 #define LEGEND_X(lc) (CELL_X(BOARD_SIZE+lc) + XOFS + 1)
122 #define LEGEND_Y(lr) (CELL_HEIGHT > font_height ? \
123 CELL_Y(lr) : font_height*(lr)+YOFS)
124 #endif
127 /* Board state */
128 static reversi_board_t game;
130 /* --- Setting values --- */
132 /* Playing strategies used by white and black players */
133 const game_strategy_t *white_strategy;
134 const game_strategy_t *black_strategy;
136 /* Cursor position */
137 static int cur_row, cur_col;
139 /* Color for the next move (BLACK/WHITE) */
140 static int cur_player;
142 /* Active cursor wrapping mode */
143 static cursor_wrap_mode_t cursor_wrap_mode;
145 static bool quit_plugin;
146 static bool game_finished;
148 #ifdef HAVE_TOUCHSCREEN
149 #include "lib/pluginlib_touchscreen.h"
150 /* This uses the touchscreen library functions/structures. */
152 /* This defines the number of buttons used; only used in this C file. */
153 #define TOUCHBUTTON_COUNT 3
155 /* Define the button locations, widths and heights */
157 #if LCD_HEIGHT < LCD_WIDTH
158 /* Define Menu button x, y, width, height */
159 #define B_MENU_X LEGEND_X(0)
160 #define B_MENU_Y (LCD_HEIGHT-LCD_HEIGHT/2)
161 #define B_MENU_W (LCD_WIDTH-LEGEND_X(0))
162 #define B_MENU_H (LCD_HEIGHT/4)
163 /* Define Quit Button x, y, width, height */
164 #define B_QUIT_X LEGEND_X(0)
165 #define B_QUIT_Y (LCD_HEIGHT-LCD_HEIGHT/4)
166 #define B_QUIT_W (LCD_WIDTH-LEGEND_X(0))
167 #define B_QUIT_H (LCD_HEIGHT/4)
168 #else
169 /* Define Menu button x, y, width, height */
170 #define B_MENU_X (LCD_WIDTH/2)
171 #define B_MENU_Y (CELL_HEIGHT*BOARD_SIZE+YOFS*2)
172 #define B_MENU_W (LCD_WIDTH/4)
173 #define B_MENU_H (2*CELL_HEIGHT)
174 /* Define Quit Button x, y, width, height */
175 #define B_QUIT_X (LCD_WIDTH-LCD_WIDTH/4)
176 #define B_QUIT_Y (CELL_HEIGHT*BOARD_SIZE+YOFS*2)
177 #define B_QUIT_W (LCD_WIDTH/4)
178 #define B_QUIT_H (2*CELL_HEIGHT)
179 #endif
181 /* This is the button initialization/definition. The first element is the
182 * Viewport. This is defined in lcd.h, but the elements are:
183 * int x - X location of button/viewport
184 * int y - Y location of button/viewport
185 * int width - Width of button/viewport
186 * int height - Height of button/viewport
187 * int font - Font to be used on button/viewport
188 * int drawmode- Modes defined in lcd.h
189 * unsigned fg_pattern - foreground color
190 * unsigned bg_pattern - backbround color
191 * unsigned lss_pattern - Selector colors (currently unused)
192 * unsigned lse_pattern - |
193 * unsigned lst_pattern - \/
195 * The rest of the touch button elements are:
196 * bool repeat - requires the area be held for the action
197 * int action - action this button will return
198 * bool invisible - Is this an invisible button?
199 * char *title - Specify a title
200 * fb_data *pixmap- Currently unused, but will allow for a graphic
202 struct touchbutton reversi_buttons[TOUCHBUTTON_COUNT] =
204 { {B_MENU_X, B_MENU_Y, B_MENU_W, B_MENU_H, 0, FONT_UI,
205 STYLE_DEFAULT, 0, 0xFFFF, 0, 0, 0},
206 false, REVERSI_BUTTON_MENU, false, "Menu", NULL },
208 { {B_QUIT_X, B_QUIT_Y, B_QUIT_W, B_QUIT_H, 0, FONT_UI,
209 STYLE_DEFAULT, 0, 0xFFFF, 0, 0, 0},
210 false, REVERSI_BUTTON_QUIT, false, "Quit", NULL },
212 { {0, 0, XOFS+BOARD_WIDTH, YOFS+BOARD_HEIGHT, 0, 0,
213 STYLE_DEFAULT, 0, 0xFFFF, 0, 0, 0},
214 false, REVERSI_BUTTON_MAKE_MOVE, true, NULL, NULL }
216 #endif
218 /* Initialises the state of the game (starts a new game) */
219 static void reversi_gui_init(void) {
220 reversi_init_game(&game);
221 game_finished = false;
222 cur_player = BLACK;
224 /* Place the cursor so that WHITE can make a move */
225 cur_row = 2;
226 cur_col = 3;
230 /* Draws the cursor in the specified cell. Cursor is drawn in the complement
231 * mode, i.e. drawing it twice will result in no changes on the screen.
233 static void reversi_gui_display_cursor(int row, int col) {
234 int old_mode, x, y;
235 old_mode = rb->lcd_get_drawmode();
236 x = CELL_X(col);
237 y = CELL_Y(row);
239 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
240 rb->lcd_drawline(x+CURSOR_MARGIN, y+CURSOR_MARGIN,
241 x+CELL_WIDTH-CURSOR_MARGIN, y+CELL_HEIGHT-CURSOR_MARGIN);
242 rb->lcd_drawline(x+CURSOR_MARGIN, y+CELL_HEIGHT-CURSOR_MARGIN,
243 x+CELL_WIDTH-CURSOR_MARGIN, y+CURSOR_MARGIN);
245 /* Draw the shadows */
246 rb->lcd_hline(x, x+CELL_WIDTH-1, YOFS-3);
247 rb->lcd_hline(x, x+CELL_WIDTH-1, YOFS+BOARD_HEIGHT+3);
248 rb->lcd_vline(XOFS-3, y, y+CELL_HEIGHT-1);
249 rb->lcd_vline(XOFS+BOARD_WIDTH+3, y, y+CELL_HEIGHT-1);
251 rb->lcd_set_drawmode(old_mode);
252 rb->lcd_update();
256 /* Draws the cell of the specified color (WHITE/BLACK) assuming that
257 * the upper left corner of the cell is at (x, y) */
258 static void reversi_gui_draw_cell(int x, int y, int color) {
259 int i;
260 if (color == WHITE) {
261 for (i = 0; i < CELL_LINE_THICKNESS; i++) {
262 rb->lcd_drawrect(
263 x+STONE_MARGIN+i,
264 y+STONE_MARGIN+i,
265 CELL_WIDTH+1-2*(STONE_MARGIN+i),
266 CELL_HEIGHT+1-2*(STONE_MARGIN+i)
269 } else if (color == BLACK) {
270 rb->lcd_fillrect(x+STONE_MARGIN, y+STONE_MARGIN,
271 CELL_WIDTH-STONE_MARGIN-1, CELL_HEIGHT-1-STONE_MARGIN);
272 } else {
273 /* Cell is free -> nothing to do */
278 /* Draws the complete screen */
279 static void reversi_gui_display_board(void) {
280 int x, y, r, c, x_width, x_height;
281 /* This viewport is used to draw a scrolling score */
282 struct viewport tempvp;
283 char buf[8];
285 /* Clear the display buffer */
286 rb->lcd_clear_display();
287 rb->lcd_set_drawmode(DRMODE_FG);
289 /* Thicker board box */
290 rb->lcd_drawrect(XOFS-1, YOFS-1, BOARD_WIDTH+3, BOARD_HEIGHT+3);
292 /* Draw the gridlines */
293 for (r=0, x=XOFS, y=YOFS; r<=BOARD_SIZE;
294 r++, x+=CELL_WIDTH, y+=CELL_HEIGHT) {
295 rb->lcd_hline(XOFS, XOFS+BOARD_WIDTH, y);
296 rb->lcd_vline(x, YOFS, YOFS+BOARD_HEIGHT);
299 /* Draw the stones. This is not the most efficient way but more readable */
300 for (r=0; r<BOARD_SIZE; r++) {
301 y = CELL_Y(r);
302 for (c=0; c<BOARD_SIZE; c++) {
303 x = CELL_X(c);
304 reversi_gui_draw_cell(x, y, game.board[r][c]);
308 /* Draw the cursor */
309 reversi_gui_display_cursor(cur_row, cur_col);
311 /* Draw the current score */
312 reversi_count_occupied_cells(&game, &r, &c);
313 rb->lcd_getstringsize("x", &x_width, &x_height);
315 x = LEGEND_X(0);
316 y = LEGEND_Y(0);
317 reversi_gui_draw_cell(x, y+(LEGEND_Y(1)-LEGEND_Y(0))/2-CELL_WIDTH/2, BLACK);
318 rb->snprintf(buf, sizeof(buf), "%01d", c);
320 tempvp.x=x+CELL_WIDTH+2;
321 tempvp.y=y;
322 tempvp.width=LCD_WIDTH-tempvp.x;
323 tempvp.height=LEGEND_Y(1);
325 tempvp.font=FONT_UI;
326 tempvp.drawmode=STYLE_DEFAULT;
327 #if LCD_DEPTH > 1
328 tempvp.fg_pattern=0;
329 tempvp.bg_pattern=0xFFFF;
330 #ifdef HAVE_LCD_COLOR
331 tempvp.lss_pattern=0;
332 tempvp.lse_pattern=0;
333 tempvp.lst_pattern=0;
334 #endif
335 #endif
337 rb->screens[SCREEN_MAIN]->set_viewport(&tempvp);
338 rb->lcd_puts_scroll(0, 0, buf);
339 rb->screens[SCREEN_MAIN]->set_viewport(NULL);
341 y = LEGEND_Y(1);
343 reversi_gui_draw_cell(x, y+(LEGEND_Y(1)-LEGEND_Y(0))/2-CELL_WIDTH/2, WHITE);
344 rb->snprintf(buf, sizeof(buf), "%01d", r);
346 tempvp.y=y;
347 rb->screens[SCREEN_MAIN]->set_viewport(&tempvp);
348 rb->lcd_puts_scroll(0, 0, buf);
349 rb->screens[SCREEN_MAIN]->set_viewport(NULL);
351 /* Draw the box around the current player */
352 r = (cur_player == BLACK ? 0 : 1);
353 y = LEGEND_Y(r)+(LEGEND_Y(1)-LEGEND_Y(0))/2-CELL_WIDTH/2;
354 rb->lcd_drawrect(x, y, CELL_WIDTH+1, CELL_HEIGHT+1);
356 #if defined(HAVE_TOUCHSCREEN)
357 touchbutton_draw(reversi_buttons, TOUCHBUTTON_COUNT);
358 #endif
360 /* Update the screen */
361 rb->lcd_update();
366 * Menu related stuff
369 /* Menu entries and the corresponding values for cursor wrap mode */
370 #define MENU_TEXT_WRAP_MODE "Cursor wrap mode"
371 static const struct opt_items cursor_wrap_mode_settings[] = {
372 { "Flat board", -1 },
373 { "Sphere", -1 },
374 { "Torus", -1 },
376 static const cursor_wrap_mode_t cursor_wrap_mode_values[3] = {
377 WRAP_FLAT, WRAP_SPHERE, WRAP_TORUS };
380 /* Menu entries and the corresponding values for available strategies */
381 #define MENU_TEXT_STRAT_WHITE "Strategy for white"
382 #define MENU_TEXT_STRAT_BLACK "Strategy for black"
384 static struct opt_items strategy_settings[] = {
385 { "Human", -1 },
386 { "Naive robot", -1 },
387 { "Simple robot", -1 },
388 //{ "AB robot", -1 },
390 static const game_strategy_t * const strategy_values[] = {
391 &strategy_human, &strategy_naive, &strategy_simple, /*&strategy_ab*/ };
394 /* Sets the strategy for the specified player. 'player' is the
395 pointer to the player to set the strategy for (actually,
396 either white_strategy or black_strategy). propmpt is the
397 text to show as the prompt in the menu */
398 static bool reversi_gui_choose_strategy(
399 const game_strategy_t **player, const char *prompt) {
400 int index = 0, i;
401 int num_items = sizeof(strategy_settings)/sizeof(strategy_settings[0]);
402 bool result;
404 for (i = 0; i < num_items; i++) {
405 if ((*player) == strategy_values[i]) {
406 index = i;
407 break;
411 result =
412 rb->set_option(prompt, &index, INT, strategy_settings, num_items, NULL);
414 (*player) = strategy_values[index];
416 if((*player)->init_func)
417 (*player)->init_func(&game);
419 return result;
423 /* Returns true iff USB ws connected while in the menu */
424 static bool reversi_gui_menu(void) {
425 int index, num_items, i;
426 int result;
428 MENUITEM_STRINGLIST(menu, "Reversi Menu", NULL,
429 "Start new game", "Pass the move",
430 MENU_TEXT_STRAT_BLACK, MENU_TEXT_STRAT_WHITE,
431 MENU_TEXT_WRAP_MODE, "Playback Control", "Quit");
433 #ifdef HAVE_TOUCHSCREEN
434 /* Entering Menu, set the touchscreen to the global setting */
435 rb->touchscreen_set_mode(rb->global_settings->touch_mode);
436 #endif
438 result = rb->do_menu(&menu, NULL, NULL, false);
440 switch (result) {
441 case 0: /* Start a new game */
442 reversi_gui_init();
443 break;
445 case 1: /* Pass the move to the partner */
446 cur_player = reversi_flipped_color(cur_player);
447 break;
449 case 2: /* Strategy for black */
450 reversi_gui_choose_strategy(&black_strategy, MENU_TEXT_STRAT_BLACK);
451 break;
453 case 3: /* Strategy for white */
454 reversi_gui_choose_strategy(&white_strategy, MENU_TEXT_STRAT_WHITE);
455 break;
457 case 4: /* Cursor wrap mode */
458 num_items = sizeof(cursor_wrap_mode_values) /
459 sizeof(cursor_wrap_mode_values[0]);
460 index = 0;
461 for (i = 0; i < num_items; i++) {
462 if (cursor_wrap_mode == cursor_wrap_mode_values[i]) {
463 index = i;
464 break;
467 rb->set_option(MENU_TEXT_WRAP_MODE, &index, INT,
468 cursor_wrap_mode_settings, 3, NULL);
469 cursor_wrap_mode = cursor_wrap_mode_values[index];
470 break;
472 case 5:
473 playback_control(NULL);
474 break;
476 case 6: /* Quit */
477 quit_plugin = true;
478 break;
481 #ifdef HAVE_TOUCHSCREEN
482 /* Leaving the menu, set back to pointer mode */
483 rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
484 #endif
486 return (result == MENU_ATTACHED_USB);
490 /* Calculates the new cursor position if the user wants to move it
491 * vertically as specified by delta. Current wrap mode is respected.
492 * The cursor is not actually moved.
494 * Returns true iff the cursor would be really moved. In any case, the
495 * new cursor position is stored in (new_row, new_col).
497 static bool
498 reversi_gui_cursor_pos_vmove(int row_delta, int *new_row, int *new_col) {
499 *new_row = cur_row + row_delta;
500 *new_col = cur_col;
502 if (*new_row < 0) {
503 switch (cursor_wrap_mode) {
504 case WRAP_FLAT:
505 *new_row = cur_row;
506 break;
507 case WRAP_SPHERE:
508 *new_row = BOARD_SIZE - 1;
509 break;
510 case WRAP_TORUS:
511 *new_row = BOARD_SIZE - 1;
512 (*new_col)--;
513 if (*new_col < 0) {
514 *new_col = BOARD_SIZE - 1;
516 break;
518 } else if (*new_row >= BOARD_SIZE) {
519 switch (cursor_wrap_mode) {
520 case WRAP_FLAT:
521 *new_row = cur_row;
522 break;
523 case WRAP_SPHERE:
524 *new_row = 0;
525 break;
526 case WRAP_TORUS:
527 *new_row = 0;
528 (*new_col)++;
529 if (*new_col >= BOARD_SIZE) {
530 *new_col = 0;
532 break;
536 return (cur_row != (*new_row)) || (cur_col != (*new_col));
540 /* Calculates the new cursor position if the user wants to move it
541 * horisontally as specified by delta. Current wrap mode is respected.
542 * The cursor is not actually moved.
544 * Returns true iff the cursor would be really moved. In any case, the
545 * new cursor position is stored in (new_row, new_col).
547 static bool
548 reversi_gui_cursor_pos_hmove(int col_delta, int *new_row, int *new_col) {
549 *new_row = cur_row;
550 *new_col = cur_col + col_delta;
552 if (*new_col < 0) {
553 switch (cursor_wrap_mode) {
554 case WRAP_FLAT:
555 *new_col = cur_col;
556 break;
557 case WRAP_SPHERE:
558 *new_col = BOARD_SIZE - 1;
559 break;
560 case WRAP_TORUS:
561 *new_col = BOARD_SIZE - 1;
562 (*new_row)--;
563 if (*new_row < 0) {
564 *new_row = BOARD_SIZE - 1;
566 break;
568 } else if (*new_col >= BOARD_SIZE) {
569 switch (cursor_wrap_mode) {
570 case WRAP_FLAT:
571 *new_col = cur_col;
572 break;
573 case WRAP_SPHERE:
574 *new_col = 0;
575 break;
576 case WRAP_TORUS:
577 *new_col = 0;
578 (*new_row)++;
579 if (*new_row >= BOARD_SIZE) {
580 *new_row = 0;
582 break;
586 return (cur_row != (*new_row)) || (cur_col != (*new_col));
590 /* Actually moves the cursor to the new position and updates the screen */
591 static void reversi_gui_move_cursor(int new_row, int new_col) {
592 int old_row, old_col;
594 old_row = cur_row;
595 old_col = cur_col;
597 cur_row = new_row;
598 cur_col = new_col;
600 /* Only update the changed cells since there are no global changes */
601 reversi_gui_display_cursor(old_row, old_col);
602 reversi_gui_display_cursor(new_row, new_col);
606 /* plugin entry point */
607 enum plugin_status plugin_start(const void *parameter) {
608 bool exit, draw_screen;
609 int button;
610 #ifdef HAVE_TOUCHSCREEN
611 int button_x, button_y;
612 #endif
613 int lastbutton = BUTTON_NONE;
614 int row, col;
615 int w_cnt, b_cnt;
617 /* Initialize Font Width and height */
618 rb->lcd_getstringsize("0", &font_width, &font_height);
620 #ifdef HAVE_TOUCHSCREEN
621 rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
622 #endif
624 #if LCD_DEPTH > 1
625 rb->lcd_set_backdrop(NULL);
626 rb->lcd_set_foreground(LCD_BLACK);
627 rb->lcd_set_background(LCD_WHITE);
628 #endif
630 /* Avoid compiler warnings */
631 (void)parameter;
633 rb->srand(*rb->current_tick); /* Some AIs use rand() */
634 white_strategy = &strategy_human;
635 black_strategy = &strategy_human;
637 reversi_gui_init();
638 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
639 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
640 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
641 cursor_wrap_mode = WRAP_TORUS;
642 #else
643 cursor_wrap_mode = WRAP_FLAT;
644 #endif
645 /* The main game loop */
646 exit = false;
647 quit_plugin = false;
648 draw_screen = true;
649 while (!exit && !quit_plugin) {
650 const game_strategy_t *cur_strategy = NULL;
651 if (draw_screen) {
652 reversi_gui_display_board();
653 draw_screen = false;
655 switch(cur_player) {
656 case BLACK:
657 cur_strategy = black_strategy;
658 break;
659 case WHITE:
660 cur_strategy = white_strategy;
661 break;
664 if(cur_strategy->is_robot && !game_finished) {
665 move_t m = cur_strategy->move_func(&game, cur_player);
666 reversi_make_move(&game, MOVE_ROW(m), MOVE_COL(m), cur_player);
667 cur_player = reversi_flipped_color(cur_player);
668 draw_screen = true;
669 /* TODO: Add some delay to prevent it from being too fast ? */
670 /* TODO: Don't duplicate end of game check */
671 if (reversi_game_is_finished(&game, cur_player)) {
672 reversi_count_occupied_cells(&game, &w_cnt, &b_cnt);
673 rb->splashf(HZ*2, "Game over. %s won.",
674 (w_cnt>b_cnt?"WHITE":"BLACK"));
675 draw_screen = true; /* Must update screen after splash */
676 game_finished = true;
678 continue;
681 /***********************************************************************
682 * Button handling code happens below here
683 **********************************************************************/
684 button = rb->button_get(true);
686 /* The touchscreen buttons can act as true buttons so OR them in */
687 #ifdef HAVE_TOUCHSCREEN
688 button |= touchbutton_get(reversi_buttons, button, TOUCHBUTTON_COUNT);
689 #endif
691 /* All of these button presses wait for the release event */
692 if(button&BUTTON_REL) {
693 #ifdef REVERSI_BUTTON_QUIT
694 if(button&REVERSI_BUTTON_QUIT) {
695 exit = true;
697 #endif
699 #ifdef HAVE_TOUCHSCREEN
700 if(button&BUTTON_TOUCHSCREEN) {
701 button_x = rb->button_get_data();
702 button_y = button_x & 0xffff;
703 button_x >>= 16;
705 /* Check if the click was in the gameboard, if so move cursor.
706 * This has to happen before MAKE_MOVE is processed.
708 if( (CELL_R(button_y)<BOARD_SIZE) &&
709 (CELL_C(button_x)<BOARD_SIZE) )
711 reversi_gui_move_cursor(CELL_R(button_y), CELL_C(button_x));
714 #endif
716 if( (button&REVERSI_BUTTON_MENU)
717 #if defined(REVERSI_BUTTON_MENU_LONGPRESS)
718 && (lastbutton&BUTTON_REPEAT)
719 #endif
721 if (reversi_gui_menu()) {
722 return PLUGIN_USB_CONNECTED;
724 draw_screen = true;
727 if(button&REVERSI_BUTTON_MAKE_MOVE
728 #if defined(REVERSI_BUTTON_MAKE_MOVE_SHORTPRESS)
729 && !(lastbutton&BUTTON_REPEAT)
730 #endif
732 /* If you touch the game board instead of hitting menu after it
733 * has completed the game will exit out.
735 if (game_finished)
736 break;
737 if (reversi_make_move(&game, cur_row, cur_col, cur_player) > 0) {
738 /* Move was made. Global changes on the board are possible */
739 draw_screen = true; /* Redraw the screen next time */
740 cur_player = reversi_flipped_color(cur_player);
741 if (reversi_game_is_finished(&game, cur_player)) {
742 reversi_count_occupied_cells(&game, &w_cnt, &b_cnt);
743 rb->splashf(HZ*2, "Game over. %s won.",
744 (w_cnt>b_cnt?"WHITE":"BLACK"));
745 draw_screen = true; /* Must update screen after splash */
746 game_finished = true;
748 } else {
749 /* An attempt to make an invalid move */
750 rb->splash(HZ/2, "Illegal move!");
751 draw_screen = true;
752 /* Ignore any button presses during the splash */
753 rb->button_clear_queue();
758 /* These button presses will run on a release or a repeat event */
759 if(button&BUTTON_REL || button&BUTTON_REPEAT) {
760 /* Move cursor left */
761 if(button&REVERSI_BUTTON_LEFT) {
762 if (reversi_gui_cursor_pos_hmove(-1, &row, &col)) {
763 reversi_gui_move_cursor(row, col);
766 /* Move cursor right */
767 if(button&REVERSI_BUTTON_RIGHT) {
768 if (reversi_gui_cursor_pos_hmove(1, &row, &col)) {
769 reversi_gui_move_cursor(row, col);
772 /* Move cursor up */
773 if(button&REVERSI_BUTTON_UP) {
774 if (reversi_gui_cursor_pos_vmove(-1, &row, &col)) {
775 reversi_gui_move_cursor(row, col);
778 /* Move cursor down */
779 if(button&REVERSI_BUTTON_DOWN) {
780 if (reversi_gui_cursor_pos_vmove(1, &row, &col)) {
781 reversi_gui_move_cursor(row, col);
786 if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
787 /* Quit if USB has been connected */
788 return PLUGIN_USB_CONNECTED;
791 if (button != BUTTON_NONE) {
792 lastbutton = button;
796 return PLUGIN_OK;