5 # Play one move of a Trap the Cap game
9 # Game state file read on STDIN
10 # Move produced on STDOUT
19 def more_clockwise(this
, other
)
20 here
= this
.destination
.place
21 there
= other
.destination
.place
23 # if both are on a spoke, choose the one with the lowest number (nearer the rim)
24 if here
=~
/.c./ and there
=~
/.c./
25 # break ties by preferring the most clockwise arc
26 if here
[-1] == there
[-1]
27 return here
[0] <=> there
[0]
29 return -1 * (here
[-1] <=> there
[-1])
31 # If one is one a spoke and the other is on the rim, choose the one on the rim
32 elsif here
=~
/.c./ or there
=~
/.c./
33 return -1 * (here
.length
<=> there
.length
)
34 # If one is in the 'f' rim section and the other in the 'a' rim section, choose the 'a'
35 elsif here
[0,1] == 'a' and there
[0,1] == 'f'
37 elsif here
[0,1] == 'f' and there
[0,1] == 'a'
39 # Otherwise, choose the one with highest arc code
46 # Get the current game state
47 game_lines
= readlines
48 game
, moves
, next_roll
= Game
.read_game(game_lines
)
49 game
.apply_moves moves
51 # The piece to move is the uncaptured one of mine with the lowest number
52 my_pieces
= game
.pieces
.values
.select
{|p
| p
.colour
== game
.current_player
and not p
.captured
}
53 piece_to_move
= (my_pieces
.sort_by
{|p
| p
.name
})[0]
55 # find the most clockwise move
56 potential_moves
= game
.possible_moves(next_roll
).select
{|m
| m
.piece
== piece_to_move
}
57 sorted_moves
= potential_moves
.sort
{|x
, y
| more_clockwise x
, y
}
58 best_move
= sorted_moves
[-1]