3 # Library to support a Trap the Cap player that uses potentials to select the
10 # Version 1.1:: 23 April 2008
14 # Play Trap the Cap by using potential fields. For each possible move,
15 # calculate the field strength and pick the move with the lowest potential
16 class Potential_player
18 attr_reader
:friend_pull, :enemy_pull, :base_pull,
19 :safe_bonus, :capture_bonus
21 def initialize(args
, verbose
= false)
22 @friend_pull = args
[:friend_pull] || 1
23 @enemy_pull = args
[:enemy_pull] || 0.5
24 @base_pull = args
[:base_pull] || 2
25 @safe_bonus = args
[:safe_bonus] || 8
26 @capture_bonus = args
[:capture_bonus] || 10
31 # Find the best move of the possible ones
32 def best_move(game
, die_result
)
33 me
= game
.current_player
34 possible_moves
= game
.possible_moves(die_result
, me
)
35 scored_moves
= possible_moves
.collect
do |m
|
38 score
= score_position(game
, me
)
45 puts
"#{m} scores #{score}" if @verbose
48 best_move
= (scored_moves
.max
{|a
, b
| a
[1] <=> b
[1]})[0]
51 # Calculate the potential score of a position for a given player
52 def score_position(game
, player
)
54 game
.pieces
.each_value
do |piece
|
56 if piece
.colour
== player
57 game
.pieces
.each_value
do |other_piece
|
58 if other_piece
.colour
== player
59 score
+= game
.board
.distance_between
[here
.place
][other_piece
.position
.place
] * @friend_pull
61 score
+= game
.board
.distance_between
[here
.place
][other_piece
.position
.place
] * @enemy_pull
64 score
+= piece
.contains
.length
* @capture_bonus
65 score
+= game
.board
.distance_between
[here
.place
][game
.board
.positions
[player
].place
] *
66 piece
.contains
.length
* @base_pull
67 elsif here
== game
.board
.positions
[player
]