Initial commit
[trapthecap.git] / src / .svn / text-base / clockwise-p1.rb.netbeans-base
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
16 require 'libttc'
17 require 'rdoc/usage'
18
19 def more_clockwise(this, other)
20 here = this.destination.place
21 there = other.destination.place
22
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]
28 else
29 return -1 * (here[-1] <=> there[-1])
30 end
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'
36 return +1
37 elsif here[0,1] == 'f' and there[0,1] == 'a'
38 return -1
39 # Otherwise, choose the one with highest arc code
40 else
41 return here <=> there
42 end
43
44 end
45
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
50
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]
54
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]
59
60 # return that move
61 puts best_move
62
63