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
) {
34 rb
->memcpy(dst
->history
,src
->history
,
35 (BOARD_SIZE
*BOARD_SIZE
- INIT_STONES
)*sizeof(move_t
));
36 for(i
=0;i
<BOARD_SIZE
;i
++) {
37 rb
->memcpy(dst
->board
[i
],src
->board
[i
],BOARD_SIZE
*sizeof(int));
41 static int reversi_sim_move(const reversi_board_t
*game
, const int row
,
42 const int col
, const int player
) {
43 reversi_board_t game_clone
;
44 reversi_copy_board(&game_clone
,game
);
45 return reversi_make_move(&game_clone
,row
,col
,player
);
48 static int reversi_get_max_moves(const reversi_board_t
*game
, int player
) {
49 int max
= -BOARD_SIZE
*BOARD_SIZE
;
51 for(row
=0; row
<BOARD_SIZE
; row
++) {
52 for(col
=0; col
<BOARD_SIZE
; col
++) {
53 int v
= reversi_sim_move(game
,row
,col
,player
);
61 static int reversi_sim_move2(const reversi_board_t
*game
, const int row
,
62 const int col
, const int player
) {
63 /* Takes the other player's next best move into account */
65 reversi_board_t game_clone
;
66 reversi_copy_board(&game_clone
,game
);
67 score
= reversi_make_move(&game_clone
,row
,col
,player
);
68 return score
- reversi_get_max_moves(&game_clone
,
69 reversi_flipped_color(player
));
73 static move_t
simple_move_func(const reversi_board_t
*game
, int player
) {
74 int max
= -BOARD_SIZE
*BOARD_SIZE
;
78 for(row
=0; row
<BOARD_SIZE
; row
++) {
79 for(col
=0; col
<BOARD_SIZE
; col
++) {
80 int v
= reversi_sim_move2(game
,row
,col
,player
);
91 if(!count
) return MOVE_INVALID
;
93 /* chose one of the moves which scores highest */
98 if(reversi_sim_move2(game
, row
, col
, player
)==max
) {
101 return MAKE_MOVE(row
,col
,player
);
105 if(col
==BOARD_SIZE
) {
108 if(row
==BOARD_SIZE
) {
115 const game_strategy_t strategy_simple
= {