summaryrefslogtreecommitdiff
path: root/apps/plugins/reversi/reversi-gui.c
diff options
context:
space:
mode:
authorAntoine Cellerier <dionoea@videolan.org>2007-07-01 20:48:51 +0000
committerAntoine Cellerier <dionoea@videolan.org>2007-07-01 20:48:51 +0000
commitcd82964e5d42b0252b8f14f15fc80709def37984 (patch)
treec9ef436d40b69f70b3d8d4c9967d8fdc812f3277 /apps/plugins/reversi/reversi-gui.c
parent58f97f517a2d5918b8a18617130c2fce09af7683 (diff)
downloadrockbox-cd82964e5d42b0252b8f14f15fc80709def37984.tar.gz
rockbox-cd82964e5d42b0252b8f14f15fc80709def37984.zip
Make sure that reversi doesn't enter an infinite loop when using 1 or 2 AIs.
+ some changes that will be needed for more advanced AIs (which aren't ready to commit yet) git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13758 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/reversi/reversi-gui.c')
-rw-r--r--apps/plugins/reversi/reversi-gui.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/apps/plugins/reversi/reversi-gui.c b/apps/plugins/reversi/reversi-gui.c
index 5c759a02f1..dcf4dd8ccb 100644
--- a/apps/plugins/reversi/reversi-gui.c
+++ b/apps/plugins/reversi/reversi-gui.c
@@ -190,13 +190,13 @@ static int cur_player;
190static cursor_wrap_mode_t cursor_wrap_mode; 190static cursor_wrap_mode_t cursor_wrap_mode;
191 191
192static bool quit_plugin; 192static bool quit_plugin;
193static bool game_finished;
193 194
194 195
195/* Initialises the state of the game (starts a new game) */ 196/* Initialises the state of the game (starts a new game) */
196static void reversi_gui_init(void) { 197static void reversi_gui_init(void) {
197 reversi_init_game(&game); 198 reversi_init_game(&game);
198 white_strategy = &strategy_human; 199 game_finished = false;
199 black_strategy = &strategy_human;
200 cur_player = BLACK; 200 cur_player = BLACK;
201 201
202 /* Place the cursor so that WHITE can make a move */ 202 /* Place the cursor so that WHITE can make a move */
@@ -330,9 +330,10 @@ static struct opt_items strategy_settings[] = {
330 { "Human", NULL }, 330 { "Human", NULL },
331 { "Naive robot", NULL }, 331 { "Naive robot", NULL },
332 { "Simple robot", NULL }, 332 { "Simple robot", NULL },
333 //{ "AB robot", NULL },
333}; 334};
334static const game_strategy_t * const strategy_values[] = { 335static const game_strategy_t * const strategy_values[] = {
335 &strategy_human, &strategy_naive, &strategy_simple }; 336 &strategy_human, &strategy_naive, &strategy_simple, /*&strategy_ab*/ };
336 337
337 338
338/* Sets the strategy for the specified player. 'player' is the 339/* Sets the strategy for the specified player. 'player' is the
@@ -354,6 +355,9 @@ static bool reversi_gui_choose_strategy(
354 result = rb->set_option(prompt, &index, INT, strategy_settings, num_items, NULL); 355 result = rb->set_option(prompt, &index, INT, strategy_settings, num_items, NULL);
355 (*player) = strategy_values[index]; 356 (*player) = strategy_values[index];
356 357
358 if((*player)->init_func)
359 (*player)->init_func(&game);
360
357 return result; 361 return result;
358} 362}
359 363
@@ -557,6 +561,8 @@ enum plugin_status plugin_start(struct plugin_api *api, void *parameter) {
557 561
558 game.rb = rb; 562 game.rb = rb;
559 rb->srand(*rb->current_tick); /* Some AIs use rand() */ 563 rb->srand(*rb->current_tick); /* Some AIs use rand() */
564 white_strategy = &strategy_human;
565 black_strategy = &strategy_human;
560 566
561 reversi_gui_init(); 567 reversi_gui_init();
562 cursor_wrap_mode = WRAP_FLAT; 568 cursor_wrap_mode = WRAP_FLAT;
@@ -580,21 +586,21 @@ enum plugin_status plugin_start(struct plugin_api *api, void *parameter) {
580 break; 586 break;
581 } 587 }
582 588
583 if(cur_strategy->is_robot) { 589 if(cur_strategy->is_robot && !game_finished) {
584 /* TODO: Check move validity */
585 move_t m = cur_strategy->move_func(&game, cur_player); 590 move_t m = cur_strategy->move_func(&game, cur_player);
586 reversi_make_move(&game, MOVE_ROW(m), MOVE_COL(m), cur_player); 591 reversi_make_move(&game, MOVE_ROW(m), MOVE_COL(m), cur_player);
587 cur_player = reversi_flipped_color(cur_player); 592 cur_player = reversi_flipped_color(cur_player);
588 draw_screen = true; 593 draw_screen = true;
589 /* TODO: Add some delay to prevent it from being too fast ? */ 594 /* TODO: Add some delay to prevent it from being too fast ? */
590 /* TODO: Don't duplicate end of game check */ 595 /* TODO: Don't duplicate end of game check */
591 if (reversi_game_is_finished(&game)) { 596 if (reversi_game_is_finished(&game, cur_player)) {
592 reversi_count_occupied_cells(&game, &w_cnt, &b_cnt); 597 reversi_count_occupied_cells(&game, &w_cnt, &b_cnt);
593 rb->snprintf(msg_buf, sizeof(msg_buf), 598 rb->snprintf(msg_buf, sizeof(msg_buf),
594 "Game over. %s have won.", 599 "Game over. %s have won.",
595 (w_cnt>b_cnt?"WHITE":"BLACK")); 600 (w_cnt>b_cnt?"WHITE":"BLACK"));
596 rb->splash(HZ*2, msg_buf); 601 rb->splash(HZ*2, msg_buf);
597 draw_screen = true; /* Must update screen after splash */ 602 draw_screen = true; /* Must update screen after splash */
603 game_finished = true;
598 } 604 }
599 continue; 605 continue;
600 } 606 }
@@ -618,17 +624,19 @@ enum plugin_status plugin_start(struct plugin_api *api, void *parameter) {
618 && (lastbutton != REVERSI_BUTTON_MAKE_MOVE_PRE)) 624 && (lastbutton != REVERSI_BUTTON_MAKE_MOVE_PRE))
619 break; 625 break;
620#endif 626#endif
627 if (game_finished) break;
621 if (reversi_make_move(&game, cur_row, cur_col, cur_player) > 0) { 628 if (reversi_make_move(&game, cur_row, cur_col, cur_player) > 0) {
622 /* Move was made. Global changes on the board are possible */ 629 /* Move was made. Global changes on the board are possible */
623 draw_screen = true; /* Redraw the screen next time */ 630 draw_screen = true; /* Redraw the screen next time */
624 cur_player = reversi_flipped_color(cur_player); 631 cur_player = reversi_flipped_color(cur_player);
625 if (reversi_game_is_finished(&game)) { 632 if (reversi_game_is_finished(&game, cur_player)) {
626 reversi_count_occupied_cells(&game, &w_cnt, &b_cnt); 633 reversi_count_occupied_cells(&game, &w_cnt, &b_cnt);
627 rb->snprintf(msg_buf, sizeof(msg_buf), 634 rb->snprintf(msg_buf, sizeof(msg_buf),
628 "Game over. %s have won.", 635 "Game over. %s have won.",
629 (w_cnt>b_cnt?"WHITE":"BLACK")); 636 (w_cnt>b_cnt?"WHITE":"BLACK"));
630 rb->splash(HZ*2, msg_buf); 637 rb->splash(HZ*2, msg_buf);
631 draw_screen = true; /* Must update screen after splash */ 638 draw_screen = true; /* Must update screen after splash */
639 game_finished = true;
632 } 640 }
633 } else { 641 } else {
634 /* An attempt to make an invalid move */ 642 /* An attempt to make an invalid move */