Initial commit
[trapthecap.git] / src / html-files / robots / 2 / runme
1 #!/usr/bin/ruby
2 #
3 # == Synopsis
4 #
5 # Play one move of a Trap the Cap game
6 #
7 # == Usage
8 # clockwise-p1
9 # Game state file read on STDIN
10 # Move produced on STDOUT
11 #
12 # == Author
13 # Neil Smith
14 #
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.
19 #
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.
24 #
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/>.
27
28
29 require 'libttc'
30
31 def more_clockwise(this, other)
32 here = this.place
33 there = other.place
34
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]
40 else
41 return -1 * (here[-1] <=> there[-1])
42 end
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'
48 return +1
49 elsif here[0,1] == 'f' and there[0,1] == 'a'
50 return -1
51 # Otherwise, choose the one with highest arc code
52 else
53 return here <=> there
54 end
55
56 end
57
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
62
63
64 # The piece to move is the least clockwise of my uncaptured pieces
65 # Always move pieces on the base first
66 my_pieces = game.pieces.values.select {|p| p.colour == game.current_player and not p.captured}
67 piece_to_move = (my_pieces.sort {|x, y| more_clockwise x.position, y.position})[0]
68
69 # Find the most clockwise move for this piece
70 potential_moves = game.possible_moves(next_roll).select {|m| m.piece == piece_to_move}
71 sorted_moves = potential_moves.sort {|x, y| more_clockwise x.destination, y.destination}
72 best_move = sorted_moves[-1]
73
74 # return that move
75 puts best_move
76
77