Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (c) 2006 Alexander Levin |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | /* |
| 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. |
| 26 | |
| 27 | |
| 28 | User instructions |
| 29 | ----------------- |
| 30 | |
| 31 | Use the arrow keys to move cursor, and press TOGGLE to place a stone. |
| 32 | |
| 33 | At any time during the game, press MENU to bring up the game menu with |
| 34 | further options: |
| 35 | |
| 36 | - Save |
| 37 | - Reload |
| 38 | - Clear |
| 39 | |
| 40 | */ |
| 41 | |
| 42 | #include "plugin.h" |
| 43 | |
| 44 | #ifdef HAVE_LCD_BITMAP |
| 45 | |
| 46 | #include "reversi-game.h" |
| 47 | #include "reversi-strategy.h" |
| 48 | #include "reversi-gui.h" |
| 49 | |
| 50 | #include "../lib/oldmenuapi.h" |
| 51 | |
| 52 | PLUGIN_HEADER |
| 53 | |
| 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 */ |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 57 | static const struct plugin_api* rb; |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 58 | |
| 59 | /* Thickness of the grid lines */ |
| 60 | #define LINE_THCK 1 |
| 61 | |
| 62 | #if LCD_HEIGHT <= LCD_WIDTH /* Horizontal layout */ |
| 63 | |
| 64 | #if (LCD_HEIGHT==64) && (LCD_WIDTH==112) |
| 65 | /* Archos Recorders and Ondios - 112x64, 8 cells @ 8x6 with 9 border lines */ |
| 66 | |
| 67 | /* Internal dimensions of a cell */ |
| 68 | #define CELL_WIDTH 8 |
| 69 | #define CELL_HEIGHT 6 |
| 70 | #define SMALL_BOARD |
| 71 | |
Marianne Arnold | 12ddb8e | 2007-09-20 10:49:48 +0000 | [diff] [blame] | 72 | #elif (LCD_HEIGHT==80) && (LCD_WIDTH==132) |
| 73 | /* Sansa C200 - 132x80, 8 cells @ 9x9 with 9 border lines */ |
| 74 | |
| 75 | /* Internal dimensions of a cell */ |
| 76 | #define CELL_WIDTH 8 |
| 77 | #define CELL_HEIGHT 8 |
| 78 | #define SMALL_BOARD |
| 79 | |
Jens Arnold | 106ac75 | 2008-03-22 14:20:04 +0000 | [diff] [blame] | 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 */ |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 84 | /* iPod Mini - 138x110, 8 cells @ 10x10 with 9 border lines */ |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 85 | /* iriver H10 5-6GB - 128x128, 8 cells @ 10x10 with 9 border lines */ |
| 86 | |
| 87 | /* Internal dimensions of a cell */ |
| 88 | #define CELL_WIDTH 10 |
| 89 | #define CELL_HEIGHT 10 |
| 90 | |
Jens Arnold | 106ac75 | 2008-03-22 14:20:04 +0000 | [diff] [blame] | 91 | #elif ((LCD_HEIGHT==128) && (LCD_WIDTH==160)) \ |
| 92 | || ((LCD_HEIGHT==132) && (LCD_WIDTH==176)) |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 93 | /* iAudio X5, Iriver H1x0, iPod G3, G4 - 160x128; */ |
| 94 | /* iPod Nano - 176x132, 8 cells @ 12x12 with 9 border lines */ |
| 95 | |
| 96 | /* Internal dimensions of a cell */ |
| 97 | #define CELL_WIDTH 12 |
| 98 | #define CELL_HEIGHT 12 |
| 99 | |
| 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 */ |
| 103 | |
| 104 | /* Internal dimensions of a cell */ |
| 105 | #define CELL_WIDTH 16 |
| 106 | #define CELL_HEIGHT 16 |
| 107 | |
| 108 | #elif (LCD_HEIGHT>=240) && (LCD_WIDTH>=320) |
| 109 | /* iPod Video - 320x240, 8 cells @ 24x24 with 9 border lines */ |
| 110 | |
| 111 | /* Internal dimensions of a cell */ |
| 112 | #define CELL_WIDTH 24 |
| 113 | #define CELL_HEIGHT 24 |
| 114 | |
| 115 | #else |
| 116 | #error REVERSI: Unsupported LCD size |
| 117 | #endif |
| 118 | |
| 119 | #else /* Vertical layout */ |
| 120 | #define VERTICAL_LAYOUT |
| 121 | |
| 122 | #if (LCD_HEIGHT>=320) && (LCD_WIDTH>=240) |
| 123 | /* Gigabeat - 240x320, 8 cells @ 24x24 with 9 border lines */ |
| 124 | |
| 125 | /* Internal dimensions of a cell */ |
| 126 | #define CELL_WIDTH 24 |
| 127 | #define CELL_HEIGHT 24 |
| 128 | |
| 129 | #elif (LCD_HEIGHT>=220) && (LCD_WIDTH>=176) |
| 130 | /* e200 - 176x220, 8 cells @ 12x12 with 9 border lines */ |
| 131 | |
| 132 | /* Internal dimensions of a cell */ |
| 133 | #define CELL_WIDTH 18 |
| 134 | #define CELL_HEIGHT 18 |
| 135 | |
| 136 | #else |
| 137 | #error REVERSI: Unsupported LCD size |
| 138 | #endif |
| 139 | |
| 140 | #endif /* Layout */ |
| 141 | |
| 142 | |
| 143 | /* Where the board begins */ |
| 144 | #define XOFS 4 |
| 145 | #define YOFS 4 |
| 146 | |
| 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)) |
| 150 | |
| 151 | /* Thickness of the white cells' lines */ |
| 152 | #if (CELL_WIDTH >= 15) && (CELL_HEIGHT >= 15) |
| 153 | #define CELL_LINE_THICKNESS 2 |
| 154 | #else |
| 155 | #define CELL_LINE_THICKNESS 1 |
| 156 | #endif |
| 157 | |
| 158 | /* Margins within a cell */ |
| 159 | #if (CELL_WIDTH >= 10) && (CELL_HEIGHT >= 10) |
| 160 | #define STONE_MARGIN 2 |
| 161 | #else |
| 162 | #define STONE_MARGIN 1 |
| 163 | #endif |
| 164 | |
| 165 | #define CURSOR_MARGIN (STONE_MARGIN + CELL_LINE_THICKNESS) |
| 166 | |
| 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) |
| 170 | |
| 171 | |
| 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) |
| 175 | #else |
| 176 | #define LEGEND_X(lc) (CELL_X(BOARD_SIZE+(lc)) + CELL_WIDTH/2) |
| 177 | #define LEGEND_Y(lr) (CELL_Y(lr)) |
| 178 | #endif |
| 179 | |
| 180 | |
| 181 | /* Board state */ |
| 182 | static reversi_board_t game; |
| 183 | |
| 184 | /* --- Setting values --- */ |
| 185 | |
| 186 | /* Playing strategies used by white and black players */ |
| 187 | const game_strategy_t *white_strategy; |
| 188 | const game_strategy_t *black_strategy; |
| 189 | |
| 190 | /* Cursor position */ |
| 191 | static int cur_row, cur_col; |
| 192 | |
| 193 | /* Color for the next move (BLACK/WHITE) */ |
| 194 | static int cur_player; |
| 195 | |
| 196 | /* Active cursor wrapping mode */ |
| 197 | static cursor_wrap_mode_t cursor_wrap_mode; |
| 198 | |
| 199 | static bool quit_plugin; |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 200 | static bool game_finished; |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 201 | |
| 202 | |
| 203 | /* Initialises the state of the game (starts a new game) */ |
| 204 | static void reversi_gui_init(void) { |
| 205 | reversi_init_game(&game); |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 206 | game_finished = false; |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 207 | cur_player = BLACK; |
| 208 | |
| 209 | /* Place the cursor so that WHITE can make a move */ |
| 210 | cur_row = 2; |
| 211 | cur_col = 3; |
| 212 | } |
| 213 | |
| 214 | |
| 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. |
| 217 | */ |
| 218 | static void reversi_gui_display_cursor(int row, int col) { |
| 219 | int old_mode, x, y; |
| 220 | old_mode = rb->lcd_get_drawmode(); |
| 221 | x = CELL_X(col); |
| 222 | y = CELL_Y(row); |
| 223 | |
| 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); |
| 229 | |
| 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); |
| 235 | |
| 236 | rb->lcd_set_drawmode(old_mode); |
| 237 | rb->lcd_update(); |
| 238 | } |
| 239 | |
| 240 | |
| 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) { |
| 244 | int i; |
| 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)); |
| 249 | } |
| 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); |
| 253 | } else { |
| 254 | /* Cell is free -> nothing to do */ |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /* Draws the complete screen */ |
| 260 | static void reversi_gui_display_board(void) { |
| 261 | int x, y, r, c, x_width, x_height; |
| 262 | char buf[8]; |
| 263 | |
| 264 | /* Clear the display buffer */ |
| 265 | rb->lcd_clear_display(); |
| 266 | rb->lcd_set_drawmode(DRMODE_FG); |
| 267 | |
| 268 | /* Thicker board box */ |
| 269 | rb->lcd_drawrect(XOFS-1, YOFS-1, BOARD_WIDTH+2, BOARD_HEIGHT+2); |
| 270 | |
| 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); |
| 276 | } |
| 277 | |
| 278 | /* Draw the stones. This is not the most efficient way but more readable */ |
| 279 | for (r=0; r<BOARD_SIZE; r++) { |
| 280 | y = CELL_Y(r); |
| 281 | for (c=0; c<BOARD_SIZE; c++) { |
| 282 | x = CELL_X(c); |
| 283 | reversi_gui_draw_cell(x, y, game.board[r][c]); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /* Draw the cursor */ |
| 288 | reversi_gui_display_cursor(cur_row, cur_col); |
| 289 | |
| 290 | /* Draw the current score */ |
| 291 | reversi_count_occupied_cells(&game, &r, &c); |
| 292 | rb->lcd_getstringsize("x", &x_width, &x_height); |
| 293 | |
| 294 | x = LEGEND_X(0); |
| 295 | y = LEGEND_Y(0); |
| 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); |
| 300 | |
| 301 | y = LEGEND_Y(1); |
| 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); |
| 306 | |
| 307 | /* Draw the box around the current player */ |
| 308 | r = (cur_player == BLACK ? 0 : 1); |
| 309 | y = LEGEND_Y(r); |
| 310 | rb->lcd_drawrect(x-1, y-1, CELL_WIDTH+2, CELL_HEIGHT+2); |
| 311 | |
| 312 | /* Update the screen */ |
| 313 | rb->lcd_update(); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /* |
| 318 | * Menu related stuff |
| 319 | */ |
| 320 | |
| 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[] = { |
Michael Sevakis | 0b0c23f | 2007-10-15 07:59:13 +0000 | [diff] [blame] | 324 | { "Flat board", -1 }, |
| 325 | { "Sphere", -1 }, |
| 326 | { "Torus", -1 }, |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 327 | }; |
| 328 | static const cursor_wrap_mode_t cursor_wrap_mode_values[3] = { |
| 329 | WRAP_FLAT, WRAP_SPHERE, WRAP_TORUS }; |
| 330 | |
| 331 | |
| 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" |
| 335 | |
| 336 | static struct opt_items strategy_settings[] = { |
Michael Sevakis | 0b0c23f | 2007-10-15 07:59:13 +0000 | [diff] [blame] | 337 | { "Human", -1 }, |
| 338 | { "Naive robot", -1 }, |
| 339 | { "Simple robot", -1 }, |
| 340 | //{ "AB robot", -1 }, |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 341 | }; |
| 342 | static const game_strategy_t * const strategy_values[] = { |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 343 | &strategy_human, &strategy_naive, &strategy_simple, /*&strategy_ab*/ }; |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 344 | |
| 345 | |
| 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) { |
| 352 | int index = 0, i; |
| 353 | int num_items = sizeof(strategy_settings)/sizeof(strategy_settings[0]); |
| 354 | bool result; |
| 355 | |
| 356 | for (i = 0; i < num_items; i++) { |
| 357 | if ((*player) == strategy_values[i]) { |
| 358 | index = i; |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | result = rb->set_option(prompt, &index, INT, strategy_settings, num_items, NULL); |
| 363 | (*player) = strategy_values[index]; |
| 364 | |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 365 | if((*player)->init_func) |
| 366 | (*player)->init_func(&game); |
| 367 | |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 368 | return result; |
| 369 | } |
| 370 | |
| 371 | |
| 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; |
| 375 | int result; |
| 376 | |
| 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 }, |
| 383 | { "Quit", NULL }, |
| 384 | }; |
| 385 | |
| 386 | m = menu_init(rb, items, sizeof(items) / sizeof(*items), |
| 387 | NULL, NULL, NULL, NULL); |
| 388 | |
| 389 | result = menu_show(m); |
| 390 | |
| 391 | switch (result) { |
| 392 | case 0: /* Start a new game */ |
| 393 | reversi_gui_init(); |
| 394 | break; |
| 395 | |
| 396 | case 1: /* Pass the move to the partner */ |
| 397 | cur_player = reversi_flipped_color(cur_player); |
| 398 | break; |
| 399 | |
| 400 | case 2: /* Strategy for black */ |
| 401 | reversi_gui_choose_strategy(&black_strategy, MENU_TEXT_STRAT_BLACK); |
| 402 | break; |
| 403 | |
| 404 | case 3: /* Strategy for white */ |
| 405 | reversi_gui_choose_strategy(&white_strategy, MENU_TEXT_STRAT_WHITE); |
| 406 | break; |
| 407 | |
| 408 | case 4: /* Cursor wrap mode */ |
| 409 | num_items = sizeof(cursor_wrap_mode_values)/sizeof(cursor_wrap_mode_values[0]); |
| 410 | index = 0; |
| 411 | for (i = 0; i < num_items; i++) { |
| 412 | if (cursor_wrap_mode == cursor_wrap_mode_values[i]) { |
| 413 | index = i; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 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]; |
| 420 | break; |
| 421 | |
| 422 | case 5: /* Quit */ |
| 423 | quit_plugin = true; |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | menu_exit(m); |
| 428 | |
| 429 | return (result == MENU_ATTACHED_USB); |
| 430 | } |
| 431 | |
| 432 | |
| 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. |
| 436 | * |
| 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). |
| 439 | */ |
| 440 | static bool reversi_gui_cursor_pos_vmove(int row_delta, int *new_row, int *new_col) { |
| 441 | *new_row = cur_row + row_delta; |
| 442 | *new_col = cur_col; |
| 443 | |
| 444 | if (*new_row < 0) { |
| 445 | switch (cursor_wrap_mode) { |
| 446 | case WRAP_FLAT: |
| 447 | *new_row = cur_row; |
| 448 | break; |
| 449 | case WRAP_SPHERE: |
| 450 | *new_row = BOARD_SIZE - 1; |
| 451 | break; |
| 452 | case WRAP_TORUS: |
| 453 | *new_row = BOARD_SIZE - 1; |
| 454 | (*new_col)--; |
| 455 | if (*new_col < 0) { |
| 456 | *new_col = BOARD_SIZE - 1; |
| 457 | } |
| 458 | break; |
| 459 | } |
| 460 | } else if (*new_row >= BOARD_SIZE) { |
| 461 | switch (cursor_wrap_mode) { |
| 462 | case WRAP_FLAT: |
| 463 | *new_row = cur_row; |
| 464 | break; |
| 465 | case WRAP_SPHERE: |
| 466 | *new_row = 0; |
| 467 | break; |
| 468 | case WRAP_TORUS: |
| 469 | *new_row = 0; |
| 470 | (*new_col)++; |
| 471 | if (*new_col >= BOARD_SIZE) { |
| 472 | *new_col = 0; |
| 473 | } |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | return (cur_row != (*new_row)) || (cur_col != (*new_col)); |
| 479 | } |
| 480 | |
| 481 | |
| 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. |
| 485 | * |
| 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). |
| 488 | */ |
| 489 | static bool reversi_gui_cursor_pos_hmove(int col_delta, int *new_row, int *new_col) { |
| 490 | *new_row = cur_row; |
| 491 | *new_col = cur_col + col_delta; |
| 492 | |
| 493 | if (*new_col < 0) { |
| 494 | switch (cursor_wrap_mode) { |
| 495 | case WRAP_FLAT: |
| 496 | *new_col = cur_col; |
| 497 | break; |
| 498 | case WRAP_SPHERE: |
| 499 | *new_col = BOARD_SIZE - 1; |
| 500 | break; |
| 501 | case WRAP_TORUS: |
| 502 | *new_col = BOARD_SIZE - 1; |
| 503 | (*new_row)--; |
| 504 | if (*new_row < 0) { |
| 505 | *new_row = BOARD_SIZE - 1; |
| 506 | } |
| 507 | break; |
| 508 | } |
| 509 | } else if (*new_col >= BOARD_SIZE) { |
| 510 | switch (cursor_wrap_mode) { |
| 511 | case WRAP_FLAT: |
| 512 | *new_col = cur_col; |
| 513 | break; |
| 514 | case WRAP_SPHERE: |
| 515 | *new_col = 0; |
| 516 | break; |
| 517 | case WRAP_TORUS: |
| 518 | *new_col = 0; |
| 519 | (*new_row)++; |
| 520 | if (*new_row >= BOARD_SIZE) { |
| 521 | *new_row = 0; |
| 522 | } |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return (cur_row != (*new_row)) || (cur_col != (*new_col)); |
| 528 | } |
| 529 | |
| 530 | |
| 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; |
| 534 | |
| 535 | old_row = cur_row; |
| 536 | old_col = cur_col; |
| 537 | |
| 538 | cur_row = new_row; |
| 539 | cur_col = new_col; |
| 540 | |
| 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); |
| 544 | } |
| 545 | |
| 546 | |
| 547 | /* plugin entry point */ |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 548 | enum plugin_status plugin_start(const struct plugin_api *api, const void *parameter) { |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 549 | bool exit, draw_screen; |
| 550 | int button; |
| 551 | int lastbutton = BUTTON_NONE; |
| 552 | int row, col; |
| 553 | int w_cnt, b_cnt; |
| 554 | char msg_buf[30]; |
| 555 | |
| 556 | /* plugin init */ |
| 557 | rb = api; |
| 558 | /* end of plugin init */ |
| 559 | |
| 560 | #if LCD_DEPTH > 1 |
| 561 | rb->lcd_set_backdrop(NULL); |
| 562 | rb->lcd_set_foreground(LCD_BLACK); |
| 563 | rb->lcd_set_background(LCD_WHITE); |
| 564 | #endif |
| 565 | |
| 566 | /* Avoid compiler warnings */ |
| 567 | (void)parameter; |
| 568 | |
Antoine Cellerier | 9af4289 | 2007-07-01 17:51:38 +0000 | [diff] [blame] | 569 | game.rb = rb; |
| 570 | rb->srand(*rb->current_tick); /* Some AIs use rand() */ |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 571 | white_strategy = &strategy_human; |
| 572 | black_strategy = &strategy_human; |
Antoine Cellerier | 9af4289 | 2007-07-01 17:51:38 +0000 | [diff] [blame] | 573 | |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 574 | reversi_gui_init(); |
Antoine Cellerier | 68ddbc0 | 2007-07-01 22:25:35 +0000 | [diff] [blame] | 575 | #if (CONFIG_KEYPAD == IPOD_4G_PAD) || \ |
Jens Arnold | b701322 | 2007-07-27 09:57:27 +0000 | [diff] [blame] | 576 | (CONFIG_KEYPAD == IPOD_3G_PAD) || \ |
| 577 | (CONFIG_KEYPAD == IPOD_1G2G_PAD) |
Antoine Cellerier | abf9232 | 2007-07-01 22:21:35 +0000 | [diff] [blame] | 578 | cursor_wrap_mode = WRAP_TORUS; |
| 579 | #else |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 580 | cursor_wrap_mode = WRAP_FLAT; |
Antoine Cellerier | abf9232 | 2007-07-01 22:21:35 +0000 | [diff] [blame] | 581 | #endif |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 582 | /* The main game loop */ |
| 583 | exit = false; |
| 584 | quit_plugin = false; |
| 585 | draw_screen = true; |
| 586 | while (!exit && !quit_plugin) { |
Antoine Cellerier | 9af4289 | 2007-07-01 17:51:38 +0000 | [diff] [blame] | 587 | const game_strategy_t *cur_strategy = NULL; |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 588 | if (draw_screen) { |
| 589 | reversi_gui_display_board(); |
| 590 | draw_screen = false; |
| 591 | } |
Antoine Cellerier | 9af4289 | 2007-07-01 17:51:38 +0000 | [diff] [blame] | 592 | switch(cur_player) { |
| 593 | case BLACK: |
| 594 | cur_strategy = black_strategy; |
| 595 | break; |
| 596 | case WHITE: |
| 597 | cur_strategy = white_strategy; |
| 598 | break; |
| 599 | } |
| 600 | |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 601 | if(cur_strategy->is_robot && !game_finished) { |
Antoine Cellerier | 9af4289 | 2007-07-01 17:51:38 +0000 | [diff] [blame] | 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); |
| 605 | draw_screen = true; |
| 606 | /* TODO: Add some delay to prevent it from being too fast ? */ |
| 607 | /* TODO: Don't duplicate end of game check */ |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 608 | if (reversi_game_is_finished(&game, cur_player)) { |
Antoine Cellerier | 9af4289 | 2007-07-01 17:51:38 +0000 | [diff] [blame] | 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 */ |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 615 | game_finished = true; |
Antoine Cellerier | 9af4289 | 2007-07-01 17:51:38 +0000 | [diff] [blame] | 616 | } |
| 617 | continue; |
| 618 | } |
| 619 | |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 620 | button = rb->button_get(true); |
| 621 | |
| 622 | switch (button) { |
| 623 | #ifdef REVERSI_BUTTON_QUIT |
| 624 | /* Exit game */ |
| 625 | case REVERSI_BUTTON_QUIT: |
| 626 | exit = true; |
| 627 | break; |
| 628 | #endif |
| 629 | |
| 630 | #ifdef REVERSI_BUTTON_ALT_MAKE_MOVE |
| 631 | case REVERSI_BUTTON_ALT_MAKE_MOVE: |
| 632 | #endif |
| 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)) |
| 637 | break; |
| 638 | #endif |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 639 | if (game_finished) break; |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 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); |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 644 | if (reversi_game_is_finished(&game, cur_player)) { |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 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 */ |
Antoine Cellerier | cd82964 | 2007-07-01 20:48:51 +0000 | [diff] [blame] | 651 | game_finished = true; |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 652 | } |
| 653 | } else { |
| 654 | /* An attempt to make an invalid move */ |
| 655 | rb->splash(HZ/2, "Illegal move!"); |
| 656 | draw_screen = true; |
| 657 | /* Ignore any button presses during the splash */ |
| 658 | rb->button_clear_queue(); |
| 659 | } |
| 660 | break; |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 661 | /* Move cursor left */ |
Antoine Cellerier | abf9232 | 2007-07-01 22:21:35 +0000 | [diff] [blame] | 662 | #ifdef REVERSI_BUTTON_ALT_LEFT |
| 663 | case REVERSI_BUTTON_ALT_LEFT: |
Antoine Cellerier | 36de1a4 | 2007-07-01 22:36:01 +0000 | [diff] [blame] | 664 | case (REVERSI_BUTTON_ALT_LEFT | BUTTON_REPEAT): |
Antoine Cellerier | abf9232 | 2007-07-01 22:21:35 +0000 | [diff] [blame] | 665 | #endif |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 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); |
| 670 | } |
| 671 | break; |
| 672 | |
| 673 | /* Move cursor right */ |
Antoine Cellerier | abf9232 | 2007-07-01 22:21:35 +0000 | [diff] [blame] | 674 | #ifdef REVERSI_BUTTON_ALT_RIGHT |
| 675 | case REVERSI_BUTTON_ALT_RIGHT: |
Antoine Cellerier | 36de1a4 | 2007-07-01 22:36:01 +0000 | [diff] [blame] | 676 | case (REVERSI_BUTTON_ALT_RIGHT | BUTTON_REPEAT): |
Antoine Cellerier | abf9232 | 2007-07-01 22:21:35 +0000 | [diff] [blame] | 677 | #endif |
Antoine Cellerier | b4b34f0 | 2007-06-30 20:04:42 +0000 | [diff] [blame] | 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); |
| 682 | } |
| 683 | break; |
| 684 | |
| 685 | /* Move cursor up */ |
| 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); |
| 690 | } |
| 691 | break; |
| 692 | |
| 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); |
| 698 | } |
| 699 | break; |
| 700 | |
| 701 | case REVERSI_BUTTON_MENU: |
| 702 | #ifdef REVERSI_BUTTON_MENU_PRE |
| 703 | if (lastbutton != REVERSI_BUTTON_MENU_PRE) { |
| 704 | break; |
| 705 | } |
| 706 | #endif |
| 707 | if (reversi_gui_menu()) { |
| 708 | return PLUGIN_USB_CONNECTED; |
| 709 | } |
| 710 | draw_screen = true; |
| 711 | break; |
| 712 | |
| 713 | default: |
| 714 | if (rb->default_event_handler(button) == SYS_USB_CONNECTED) { |
| 715 | /* Quit if USB has been connected */ |
| 716 | return PLUGIN_USB_CONNECTED; |
| 717 | } |
| 718 | break; |
| 719 | } |
| 720 | if (button != BUTTON_NONE) { |
| 721 | lastbutton = button; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | return PLUGIN_OK; |
| 726 | } |
| 727 | |
| 728 | #endif |