1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (c) 2007 Antoine Cellerier
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 ****************************************************************************/
22 #include "reversi-strategy.h"
26 * A very simple strategy. Makes the highest scoring move taking the other
27 * player's next best move into account.
28 * From Algorithm by Claudio Clemens in hinversi, simpleClient.c
31 static void reversi_copy_board(reversi_board_t
*dst
,
32 const reversi_board_t
*src
) {
35 dst
->rb
->memcpy(dst
->history
,src
->history
,
36 (BOARD_SIZE
*BOARD_SIZE
- INIT_STONES
)*sizeof(move_t
));
37 for(i
=0;i
<BOARD_SIZE
;i
++) {
38 dst
->rb
->memcpy(dst
->board
[i
],src
->board
[i
],BOARD_SIZE
*sizeof(int));
42 static int reversi_sim_move(const reversi_board_t
*game
, const int row
,
43 const int col
, const int player
) {
44 reversi_board_t game_clone
;
45 reversi_copy_board(&game_clone
,game
);
46 return reversi_make_move(&game_clone
,row
,col
,player
);
49 static int reversi_get_max_moves(const reversi_board_t
*game
, int player
) {
50 int max
= -BOARD_SIZE
*BOARD_SIZE
;
52 for(row
=0; row
<BOARD_SIZE
; row
++) {
53 for(col
=0; col
<BOARD_SIZE
; col
++) {
54 int v
= reversi_sim_move(game
,row
,col
,player
);
62 static int reversi_sim_move2(const reversi_board_t
*game
, const int row
,
63 const int col
, const int player
) {
64 /* Takes the other player's next best move into account */
66 reversi_board_t game_clone
;
67 reversi_copy_board(&game_clone
,game
);
68 score
= reversi_make_move(&game_clone
,row
,col
,player
);
69 return score
- reversi_get_max_moves(&game_clone
,
70 reversi_flipped_color(player
));
74 static move_t
simple_move_func(const reversi_board_t
*game
, int player
) {
75 int max
= -BOARD_SIZE
*BOARD_SIZE
;
79 for(row
=0; row
<BOARD_SIZE
; row
++) {
80 for(col
=0; col
<BOARD_SIZE
; col
++) {
81 int v
= reversi_sim_move2(game
,row
,col
,player
);
92 if(!count
) return MOVE_INVALID
;
94 /* chose one of the moves which scores highest */
95 r
= game
->rb
->rand()%count
;
99 if(reversi_sim_move2(game
, row
, col
, player
)==max
) {
102 return MAKE_MOVE(row
,col
,player
);
106 if(col
==BOARD_SIZE
) {
109 if(row
==BOARD_SIZE
) {
116 const game_strategy_t strategy_simple
= {