1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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.
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
44 #ifdef HAVE_LCD_BITMAP
46 #include "reversi-game.h"
47 #include "reversi-strategy.h"
48 #include "reversi-gui.h"
50 #include "../lib/oldmenuapi.h"
54 /* The global api struct pointer. While not strictly necessary,
55 it's nice not to have to pass the api pointer in all function
56 calls in the plugin */
57 static const struct plugin_api
* rb
;
59 /* Thickness of the grid lines */
62 #if LCD_HEIGHT <= LCD_WIDTH /* Horizontal layout */
64 #if (LCD_HEIGHT==64) && (LCD_WIDTH==112)
65 /* Archos Recorders and Ondios - 112x64, 8 cells @ 8x6 with 9 border lines */
67 /* Internal dimensions of a cell */
72 #elif (LCD_HEIGHT==80) && (LCD_WIDTH==132)
73 /* Sansa C200 - 132x80, 8 cells @ 9x9 with 9 border lines */
75 /* Internal dimensions of a cell */
80 #elif (LCD_HEIGHT==96) && (LCD_WIDTH==128) \
81 || (LCD_HEIGHT==110) && (LCD_WIDTH==138) \
82 || (LCD_HEIGHT==128) && (LCD_WIDTH==128)
83 /* iAudio M3 - 138x110, 8 cells @ 10x10 with 9 border lines */
84 /* iPod Mini - 138x110, 8 cells @ 10x10 with 9 border lines */
85 /* iriver H10 5-6GB - 128x128, 8 cells @ 10x10 with 9 border lines */
87 /* Internal dimensions of a cell */
89 #define CELL_HEIGHT 10
91 #elif ((LCD_HEIGHT==128) && (LCD_WIDTH==160)) \
92 || ((LCD_HEIGHT==132) && (LCD_WIDTH==176))
93 /* iAudio X5, Iriver H1x0, iPod G3, G4 - 160x128; */
94 /* iPod Nano - 176x132, 8 cells @ 12x12 with 9 border lines */
96 /* Internal dimensions of a cell */
98 #define CELL_HEIGHT 12
100 #elif ((LCD_HEIGHT==176) && (LCD_WIDTH==220)) || \
101 ((LCD_HEIGHT==220) && (LCD_WIDTH==176))
102 /* Iriver h300, iPod Color/Photo - 220x176, 8 cells @ 16x16 with 9 border lines */
104 /* Internal dimensions of a cell */
105 #define CELL_WIDTH 16
106 #define CELL_HEIGHT 16
108 #elif (LCD_HEIGHT>=240) && (LCD_WIDTH>=320)
109 /* iPod Video - 320x240, 8 cells @ 24x24 with 9 border lines */
111 /* Internal dimensions of a cell */
112 #define CELL_WIDTH 24
113 #define CELL_HEIGHT 24
116 #error REVERSI: Unsupported LCD size
119 #else /* Vertical layout */
120 #define VERTICAL_LAYOUT
122 #if (LCD_HEIGHT>=320) && (LCD_WIDTH>=240)
123 /* Gigabeat - 240x320, 8 cells @ 24x24 with 9 border lines */
125 /* Internal dimensions of a cell */
126 #define CELL_WIDTH 24
127 #define CELL_HEIGHT 24
129 #elif (LCD_HEIGHT>=220) && (LCD_WIDTH>=176)
130 /* e200 - 176x220, 8 cells @ 12x12 with 9 border lines */
132 /* Internal dimensions of a cell */
133 #define CELL_WIDTH 18
134 #define CELL_HEIGHT 18
137 #error REVERSI: Unsupported LCD size
143 /* Where the board begins */
147 /* Total width and height of the board without enclosing box */
148 #define BOARD_WIDTH (CELL_WIDTH*BOARD_SIZE + LINE_THCK*(BOARD_SIZE+1))
149 #define BOARD_HEIGHT (CELL_HEIGHT*BOARD_SIZE + LINE_THCK*(BOARD_SIZE+1))
151 /* Thickness of the white cells' lines */
152 #if (CELL_WIDTH >= 15) && (CELL_HEIGHT >= 15)
153 #define CELL_LINE_THICKNESS 2
155 #define CELL_LINE_THICKNESS 1
158 /* Margins within a cell */
159 #if (CELL_WIDTH >= 10) && (CELL_HEIGHT >= 10)
160 #define STONE_MARGIN 2
162 #define STONE_MARGIN 1
165 #define CURSOR_MARGIN (STONE_MARGIN + CELL_LINE_THICKNESS)
167 /* Upper left corner of a cell */
168 #define CELL_X(c) (XOFS + (c)*CELL_WIDTH + ((c)+1)*LINE_THCK)
169 #define CELL_Y(r) (YOFS + (r)*CELL_HEIGHT + ((r)+1)*LINE_THCK)
172 #ifdef VERTICAL_LAYOUT
173 #define LEGEND_X(lc) (CELL_X(lc))
174 #define LEGEND_Y(lr) (CELL_Y(BOARD_SIZE+(lr)) + CELL_HEIGHT/2)
176 #define LEGEND_X(lc) (CELL_X(BOARD_SIZE+(lc)) + CELL_WIDTH/2)
177 #define LEGEND_Y(lr) (CELL_Y(lr))
182 static reversi_board_t game
;
184 /* --- Setting values --- */
186 /* Playing strategies used by white and black players */
187 const game_strategy_t
*white_strategy
;
188 const game_strategy_t
*black_strategy
;
190 /* Cursor position */
191 static int cur_row
, cur_col
;
193 /* Color for the next move (BLACK/WHITE) */
194 static int cur_player
;
196 /* Active cursor wrapping mode */
197 static cursor_wrap_mode_t cursor_wrap_mode
;
199 static bool quit_plugin
;
200 static bool game_finished
;
203 /* Initialises the state of the game (starts a new game) */
204 static void reversi_gui_init(void) {
205 reversi_init_game(&game
);
206 game_finished
= false;
209 /* Place the cursor so that WHITE can make a move */
215 /* Draws the cursor in the specified cell. Cursor is drawn in the complement
216 * mode, i.e. drawing it twice will result in no changes on the screen.
218 static void reversi_gui_display_cursor(int row
, int col
) {
220 old_mode
= rb
->lcd_get_drawmode();
224 rb
->lcd_set_drawmode(DRMODE_COMPLEMENT
);
225 rb
->lcd_drawline(x
+CURSOR_MARGIN
, y
+CURSOR_MARGIN
,
226 x
+CELL_WIDTH
-CURSOR_MARGIN
-1, y
+CELL_HEIGHT
-CURSOR_MARGIN
-1);
227 rb
->lcd_drawline(x
+CURSOR_MARGIN
, y
+CELL_HEIGHT
-CURSOR_MARGIN
-1,
228 x
+CELL_WIDTH
-CURSOR_MARGIN
-1, y
+CURSOR_MARGIN
);
230 /* Draw the shadows */
231 rb
->lcd_hline(x
, x
+CELL_WIDTH
-1, YOFS
-3);
232 rb
->lcd_hline(x
, x
+CELL_WIDTH
-1, YOFS
+BOARD_HEIGHT
+2);
233 rb
->lcd_vline(XOFS
-3, y
, y
+CELL_HEIGHT
-1);
234 rb
->lcd_vline(XOFS
+BOARD_WIDTH
+2, y
, y
+CELL_HEIGHT
-1);
236 rb
->lcd_set_drawmode(old_mode
);
241 /* Draws the cell of the specified color (WHITE/BLACK) assuming that
242 * the upper left corner of the cell is at (x, y) */
243 static void reversi_gui_draw_cell(int x
, int y
, int color
) {
245 if (color
== WHITE
) {
246 for (i
= 0; i
< CELL_LINE_THICKNESS
; i
++) {
247 rb
->lcd_drawrect(x
+STONE_MARGIN
+i
, y
+STONE_MARGIN
+i
,
248 CELL_WIDTH
-2*(STONE_MARGIN
+i
), CELL_HEIGHT
-2*(STONE_MARGIN
+i
));
250 } else if (color
== BLACK
) {
251 rb
->lcd_fillrect(x
+STONE_MARGIN
, y
+STONE_MARGIN
,
252 CELL_WIDTH
-2*STONE_MARGIN
, CELL_HEIGHT
-2*STONE_MARGIN
);
254 /* Cell is free -> nothing to do */
259 /* Draws the complete screen */
260 static void reversi_gui_display_board(void) {
261 int x
, y
, r
, c
, x_width
, x_height
;
264 /* Clear the display buffer */
265 rb
->lcd_clear_display();
266 rb
->lcd_set_drawmode(DRMODE_FG
);
268 /* Thicker board box */
269 rb
->lcd_drawrect(XOFS
-1, YOFS
-1, BOARD_WIDTH
+2, BOARD_HEIGHT
+2);
271 /* Draw the gridlines */
272 for (r
=0, x
=XOFS
, y
=YOFS
; r
<=BOARD_SIZE
;
273 r
++, x
+=CELL_WIDTH
+LINE_THCK
, y
+=CELL_HEIGHT
+LINE_THCK
) {
274 rb
->lcd_hline(XOFS
, XOFS
+BOARD_WIDTH
-1, y
);
275 rb
->lcd_vline(x
, YOFS
, YOFS
+BOARD_HEIGHT
-1);
278 /* Draw the stones. This is not the most efficient way but more readable */
279 for (r
=0; r
<BOARD_SIZE
; r
++) {
281 for (c
=0; c
<BOARD_SIZE
; c
++) {
283 reversi_gui_draw_cell(x
, y
, game
.board
[r
][c
]);
287 /* Draw the cursor */
288 reversi_gui_display_cursor(cur_row
, cur_col
);
290 /* Draw the current score */
291 reversi_count_occupied_cells(&game
, &r
, &c
);
292 rb
->lcd_getstringsize("x", &x_width
, &x_height
);
296 reversi_gui_draw_cell(x
, y
, BLACK
);
297 rb
->snprintf(buf
, sizeof(buf
), "%d", c
);
298 y
+= (CELL_HEIGHT
-x_height
) / 2;
299 rb
->lcd_putsxy(x
+ CELL_WIDTH
+ CELL_WIDTH
/2, y
, buf
);
302 reversi_gui_draw_cell(x
, y
, WHITE
);
303 rb
->snprintf(buf
, sizeof(buf
), "%d", r
);
304 y
+= (CELL_HEIGHT
-x_height
) / 2;
305 rb
->lcd_putsxy(x
+ CELL_WIDTH
+ CELL_WIDTH
/2, y
, buf
);
307 /* Draw the box around the current player */
308 r
= (cur_player
== BLACK
? 0 : 1);
310 rb
->lcd_drawrect(x
-1, y
-1, CELL_WIDTH
+2, CELL_HEIGHT
+2);
312 /* Update the screen */
321 /* Menu entries and the corresponding values for cursor wrap mode */
322 #define MENU_TEXT_WRAP_MODE "Cursor wrap mode"
323 static const struct opt_items cursor_wrap_mode_settings
[] = {
324 { "Flat board", -1 },
328 static const cursor_wrap_mode_t cursor_wrap_mode_values
[3] = {
329 WRAP_FLAT
, WRAP_SPHERE
, WRAP_TORUS
};
332 /* Menu entries and the corresponding values for available strategies */
333 #define MENU_TEXT_STRAT_WHITE "Strategy for white"
334 #define MENU_TEXT_STRAT_BLACK "Strategy for black"
336 static struct opt_items strategy_settings
[] = {
338 { "Naive robot", -1 },
339 { "Simple robot", -1 },
340 //{ "AB robot", -1 },
342 static const game_strategy_t
* const strategy_values
[] = {
343 &strategy_human
, &strategy_naive
, &strategy_simple
, /*&strategy_ab*/ };
346 /* Sets the strategy for the specified player. 'player' is the
347 pointer to the player to set the strategy for (actually,
348 either white_strategy or black_strategy). propmpt is the
349 text to show as the prompt in the menu */
350 static bool reversi_gui_choose_strategy(
351 const game_strategy_t
**player
, const char *prompt
) {
353 int num_items
= sizeof(strategy_settings
)/sizeof(strategy_settings
[0]);
356 for (i
= 0; i
< num_items
; i
++) {
357 if ((*player
) == strategy_values
[i
]) {
362 result
= rb
->set_option(prompt
, &index
, INT
, strategy_settings
, num_items
, NULL
);
363 (*player
) = strategy_values
[index
];
365 if((*player
)->init_func
)
366 (*player
)->init_func(&game
);
372 /* Returns true iff USB ws connected while in the menu */
373 static bool reversi_gui_menu(void) {
374 int m
, index
, num_items
, i
;
377 static const struct menu_item items
[] = {
378 { "Start new game", NULL
},
379 { "Pass the move", NULL
},
380 { MENU_TEXT_STRAT_BLACK
, NULL
},
381 { MENU_TEXT_STRAT_WHITE
, NULL
},
382 { MENU_TEXT_WRAP_MODE
, NULL
},
386 m
= menu_init(rb
, items
, sizeof(items
) / sizeof(*items
),
387 NULL
, NULL
, NULL
, NULL
);
389 result
= menu_show(m
);
392 case 0: /* Start a new game */
396 case 1: /* Pass the move to the partner */
397 cur_player
= reversi_flipped_color(cur_player
);
400 case 2: /* Strategy for black */
401 reversi_gui_choose_strategy(&black_strategy
, MENU_TEXT_STRAT_BLACK
);
404 case 3: /* Strategy for white */
405 reversi_gui_choose_strategy(&white_strategy
, MENU_TEXT_STRAT_WHITE
);
408 case 4: /* Cursor wrap mode */
409 num_items
= sizeof(cursor_wrap_mode_values
)/sizeof(cursor_wrap_mode_values
[0]);
411 for (i
= 0; i
< num_items
; i
++) {
412 if (cursor_wrap_mode
== cursor_wrap_mode_values
[i
]) {
417 rb
->set_option(MENU_TEXT_WRAP_MODE
, &index
, INT
,
418 cursor_wrap_mode_settings
, 3, NULL
);
419 cursor_wrap_mode
= cursor_wrap_mode_values
[index
];
429 return (result
== MENU_ATTACHED_USB
);
433 /* Calculates the new cursor position if the user wants to move it
434 * vertically as specified by delta. Current wrap mode is respected.
435 * The cursor is not actually moved.
437 * Returns true iff the cursor would be really moved. In any case, the
438 * new cursor position is stored in (new_row, new_col).
440 static bool reversi_gui_cursor_pos_vmove(int row_delta
, int *new_row
, int *new_col
) {
441 *new_row
= cur_row
+ row_delta
;
445 switch (cursor_wrap_mode
) {
450 *new_row
= BOARD_SIZE
- 1;
453 *new_row
= BOARD_SIZE
- 1;
456 *new_col
= BOARD_SIZE
- 1;
460 } else if (*new_row
>= BOARD_SIZE
) {
461 switch (cursor_wrap_mode
) {
471 if (*new_col
>= BOARD_SIZE
) {
478 return (cur_row
!= (*new_row
)) || (cur_col
!= (*new_col
));
482 /* Calculates the new cursor position if the user wants to move it
483 * horisontally as specified by delta. Current wrap mode is respected.
484 * The cursor is not actually moved.
486 * Returns true iff the cursor would be really moved. In any case, the
487 * new cursor position is stored in (new_row, new_col).
489 static bool reversi_gui_cursor_pos_hmove(int col_delta
, int *new_row
, int *new_col
) {
491 *new_col
= cur_col
+ col_delta
;
494 switch (cursor_wrap_mode
) {
499 *new_col
= BOARD_SIZE
- 1;
502 *new_col
= BOARD_SIZE
- 1;
505 *new_row
= BOARD_SIZE
- 1;
509 } else if (*new_col
>= BOARD_SIZE
) {
510 switch (cursor_wrap_mode
) {
520 if (*new_row
>= BOARD_SIZE
) {
527 return (cur_row
!= (*new_row
)) || (cur_col
!= (*new_col
));
531 /* Actually moves the cursor to the new position and updates the screen */
532 static void reversi_gui_move_cursor(int new_row
, int new_col
) {
533 int old_row
, old_col
;
541 /* Only update the changed cells since there are no global changes */
542 reversi_gui_display_cursor(old_row
, old_col
);
543 reversi_gui_display_cursor(new_row
, new_col
);
547 /* plugin entry point */
548 enum plugin_status
plugin_start(const struct plugin_api
*api
, const void *parameter
) {
549 bool exit
, draw_screen
;
551 int lastbutton
= BUTTON_NONE
;
558 /* end of plugin init */
561 rb
->lcd_set_backdrop(NULL
);
562 rb
->lcd_set_foreground(LCD_BLACK
);
563 rb
->lcd_set_background(LCD_WHITE
);
566 /* Avoid compiler warnings */
570 rb
->srand(*rb
->current_tick
); /* Some AIs use rand() */
571 white_strategy
= &strategy_human
;
572 black_strategy
= &strategy_human
;
575 #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
576 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
577 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
578 cursor_wrap_mode
= WRAP_TORUS
;
580 cursor_wrap_mode
= WRAP_FLAT
;
582 /* The main game loop */
586 while (!exit
&& !quit_plugin
) {
587 const game_strategy_t
*cur_strategy
= NULL
;
589 reversi_gui_display_board();
594 cur_strategy
= black_strategy
;
597 cur_strategy
= white_strategy
;
601 if(cur_strategy
->is_robot
&& !game_finished
) {
602 move_t m
= cur_strategy
->move_func(&game
, cur_player
);
603 reversi_make_move(&game
, MOVE_ROW(m
), MOVE_COL(m
), cur_player
);
604 cur_player
= reversi_flipped_color(cur_player
);
606 /* TODO: Add some delay to prevent it from being too fast ? */
607 /* TODO: Don't duplicate end of game check */
608 if (reversi_game_is_finished(&game
, cur_player
)) {
609 reversi_count_occupied_cells(&game
, &w_cnt
, &b_cnt
);
610 rb
->snprintf(msg_buf
, sizeof(msg_buf
),
611 "Game over. %s have won.",
612 (w_cnt
>b_cnt
?"WHITE":"BLACK"));
613 rb
->splash(HZ
*2, msg_buf
);
614 draw_screen
= true; /* Must update screen after splash */
615 game_finished
= true;
620 button
= rb
->button_get(true);
623 #ifdef REVERSI_BUTTON_QUIT
625 case REVERSI_BUTTON_QUIT
:
630 #ifdef REVERSI_BUTTON_ALT_MAKE_MOVE
631 case REVERSI_BUTTON_ALT_MAKE_MOVE
:
633 case REVERSI_BUTTON_MAKE_MOVE
:
634 #ifdef REVERSI_BUTTON_MAKE_MOVE_PRE
635 if ((button
== REVERSI_BUTTON_MAKE_MOVE
)
636 && (lastbutton
!= REVERSI_BUTTON_MAKE_MOVE_PRE
))
639 if (game_finished
) break;
640 if (reversi_make_move(&game
, cur_row
, cur_col
, cur_player
) > 0) {
641 /* Move was made. Global changes on the board are possible */
642 draw_screen
= true; /* Redraw the screen next time */
643 cur_player
= reversi_flipped_color(cur_player
);
644 if (reversi_game_is_finished(&game
, cur_player
)) {
645 reversi_count_occupied_cells(&game
, &w_cnt
, &b_cnt
);
646 rb
->snprintf(msg_buf
, sizeof(msg_buf
),
647 "Game over. %s have won.",
648 (w_cnt
>b_cnt
?"WHITE":"BLACK"));
649 rb
->splash(HZ
*2, msg_buf
);
650 draw_screen
= true; /* Must update screen after splash */
651 game_finished
= true;
654 /* An attempt to make an invalid move */
655 rb
->splash(HZ
/2, "Illegal move!");
657 /* Ignore any button presses during the splash */
658 rb
->button_clear_queue();
661 /* Move cursor left */
662 #ifdef REVERSI_BUTTON_ALT_LEFT
663 case REVERSI_BUTTON_ALT_LEFT
:
664 case (REVERSI_BUTTON_ALT_LEFT
| BUTTON_REPEAT
):
666 case REVERSI_BUTTON_LEFT
:
667 case (REVERSI_BUTTON_LEFT
| BUTTON_REPEAT
):
668 if (reversi_gui_cursor_pos_hmove(-1, &row
, &col
)) {
669 reversi_gui_move_cursor(row
, col
);
673 /* Move cursor right */
674 #ifdef REVERSI_BUTTON_ALT_RIGHT
675 case REVERSI_BUTTON_ALT_RIGHT
:
676 case (REVERSI_BUTTON_ALT_RIGHT
| BUTTON_REPEAT
):
678 case REVERSI_BUTTON_RIGHT
:
679 case (REVERSI_BUTTON_RIGHT
| BUTTON_REPEAT
):
680 if (reversi_gui_cursor_pos_hmove(1, &row
, &col
)) {
681 reversi_gui_move_cursor(row
, col
);
686 case REVERSI_BUTTON_UP
:
687 case (REVERSI_BUTTON_UP
| BUTTON_REPEAT
):
688 if (reversi_gui_cursor_pos_vmove(-1, &row
, &col
)) {
689 reversi_gui_move_cursor(row
, col
);
693 /* Move cursor down */
694 case REVERSI_BUTTON_DOWN
:
695 case (REVERSI_BUTTON_DOWN
| BUTTON_REPEAT
):
696 if (reversi_gui_cursor_pos_vmove(1, &row
, &col
)) {
697 reversi_gui_move_cursor(row
, col
);
701 case REVERSI_BUTTON_MENU
:
702 #ifdef REVERSI_BUTTON_MENU_PRE
703 if (lastbutton
!= REVERSI_BUTTON_MENU_PRE
) {
707 if (reversi_gui_menu()) {
708 return PLUGIN_USB_CONNECTED
;
714 if (rb
->default_event_handler(button
) == SYS_USB_CONNECTED
) {
715 /* Quit if USB has been connected */
716 return PLUGIN_USB_CONNECTED
;
720 if (button
!= BUTTON_NONE
) {