summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine Cellerier <dionoea@videolan.org>2007-07-01 18:06:51 +0000
committerAntoine Cellerier <dionoea@videolan.org>2007-07-01 18:06:51 +0000
commit58f97f517a2d5918b8a18617130c2fce09af7683 (patch)
treeee02a66adbbfa82cd5dadde2c5ca0a1dbd06ddfa
parent932b20ec622ad743fc9dd9a523f7cbe3a7c0e04d (diff)
downloadrockbox-58f97f517a2d5918b8a18617130c2fce09af7683.tar.gz
rockbox-58f97f517a2d5918b8a18617130c2fce09af7683.zip
Really implement the simple strategy from hinversi.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13757 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/plugins/reversi/reversi-strategy-simple.c37
1 files changed, 32 insertions, 5 deletions
diff --git a/apps/plugins/reversi/reversi-strategy-simple.c b/apps/plugins/reversi/reversi-strategy-simple.c
index f1d2b83cc9..9064922cba 100644
--- a/apps/plugins/reversi/reversi-strategy-simple.c
+++ b/apps/plugins/reversi/reversi-strategy-simple.c
@@ -21,7 +21,8 @@
21 21
22/** 22/**
23 * Simple strategy: 23 * Simple strategy:
24 * A very simple strategy. Makes the highest scoring move. 24 * A very simple strategy. Makes the highest scoring move taking the other
25 * player's next best move into account.
25 * From Algorithm by Claudio Clemens in hinversi, simpleClient.c 26 * From Algorithm by Claudio Clemens in hinversi, simpleClient.c
26 */ 27 */
27 28
@@ -38,20 +39,44 @@ static void reversi_copy_board(reversi_board_t *dst,
38 39
39static int reversi_sim_move(const reversi_board_t *game, const int row, 40static int reversi_sim_move(const reversi_board_t *game, const int row,
40 const int col, const int player) { 41 const int col, const int player) {
41 /* Gruik */
42 reversi_board_t game_clone; 42 reversi_board_t game_clone;
43 reversi_copy_board(&game_clone,game); 43 reversi_copy_board(&game_clone,game);
44 return reversi_make_move(&game_clone,row,col,player); 44 return reversi_make_move(&game_clone,row,col,player);
45} 45}
46 46
47static int reversi_get_max_moves(const reversi_board_t *game, int player) {
48 int max = -BOARD_SIZE*BOARD_SIZE;
49 int row, col;
50 for(row=0; row<BOARD_SIZE; row++) {
51 for(col=0; col<BOARD_SIZE; col++) {
52 int v = reversi_sim_move(game,row,col,player);
53 if(v>max)
54 max = v;
55 }
56 }
57 return max;
58}
59
60static int reversi_sim_move2(const reversi_board_t *game, const int row,
61 const int col, const int player) {
62 /* Takes the other player's next best move into account */
63 int score;
64 reversi_board_t game_clone;
65 reversi_copy_board(&game_clone,game);
66 score = reversi_make_move(&game_clone,row,col,player);
67 return score - reversi_get_max_moves(&game_clone,
68 reversi_flipped_color(player));
69}
70
71
47static move_t simple_move_func(const reversi_board_t *game, int player) { 72static move_t simple_move_func(const reversi_board_t *game, int player) {
48 int max = 0; 73 int max = -BOARD_SIZE*BOARD_SIZE;
49 int count = 0; 74 int count = 0;
50 int row, col; 75 int row, col;
51 int r; 76 int r;
52 for(row=0; row<BOARD_SIZE; row++) { 77 for(row=0; row<BOARD_SIZE; row++) {
53 for(col=0; col<BOARD_SIZE; col++) { 78 for(col=0; col<BOARD_SIZE; col++) {
54 int v = reversi_sim_move(game,row,col,player); 79 int v = reversi_sim_move2(game,row,col,player);
55 if(v>max) { 80 if(v>max) {
56 max = v; 81 max = v;
57 count = 1; 82 count = 1;
@@ -62,12 +87,14 @@ static move_t simple_move_func(const reversi_board_t *game, int player) {
62 } 87 }
63 } 88 }
64 89
90 if(!count) return MOVE_INVALID;
91
65 /* chose one of the moves which scores highest */ 92 /* chose one of the moves which scores highest */
66 r = game->rb->rand()%count; 93 r = game->rb->rand()%count;
67 row = 0; 94 row = 0;
68 col = 0; 95 col = 0;
69 while(true) { 96 while(true) {
70 if(reversi_sim_move(game, row, col, player)==max) { 97 if(reversi_sim_move2(game, row, col, player)==max) {
71 r--; 98 r--;
72 if(r<0) { 99 if(r<0) {
73 return MAKE_MOVE(row,col,player); 100 return MAKE_MOVE(row,col,player);