5 # Play one move of a Trap the Cap game
9 # Game state file read on STDIN
10 # Move produced on STDOUT
15 # This program is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program. If not, see <http://www.gnu.org/licenses/>.
31 def more_clockwise(this
, other
)
32 here
= this
.destination
.place
33 there
= other
.destination
.place
35 # if both are on a spoke, choose the one with the lowest number (nearer the rim)
36 if here
=~
/.c./ and there
=~
/.c./
37 # break ties by preferring the most clockwise arc
38 if here
[-1] == there
[-1]
39 return here
[0] <=> there
[0]
41 return -1 * (here
[-1] <=> there
[-1])
43 # If one is one a spoke and the other is on the rim, choose the one on the rim
44 elsif here
=~
/.c./ or there
=~
/.c./
45 return -1 * (here
.length
<=> there
.length
)
46 # If one is in the 'f' rim section and the other in the 'a' rim section, choose the 'a'
47 elsif here
[0,1] == 'a' and there
[0,1] == 'f'
49 elsif here
[0,1] == 'f' and there
[0,1] == 'a'
51 # Otherwise, choose the one with highest arc code
58 # Get the current game state
59 game_lines
= readlines
60 game
, moves
, next_roll
= Game
.read_game(game_lines
)
61 game
.apply_moves
! moves
63 # The piece to move is the uncaptured one of mine with the lowest number
64 my_pieces
= game
.pieces
.values
.select
{|p
| p
.colour
== game
.current_player
and not p
.captured
}
65 piece_to_move
= (my_pieces
.sort_by
{|p
| p
.name
})[0]
67 # find the most clockwise move
68 potential_moves
= game
.possible_moves(next_roll
).select
{|m
| m
.piece
== piece_to_move
}
69 sorted_moves
= potential_moves
.sort
{|x
, y
| more_clockwise x
, y
}
70 best_move
= sorted_moves
[-1]