#!/usr/bin/ruby # # == Synopsis # # Play one move of a Trap the Cap game # # == Usage # clockwise-p1 # Game state file read on STDIN # Move produced on STDOUT # # == Author # Neil Smith require 'libttc' require 'rdoc/usage' def more_clockwise(this, other) here = this.destination.place there = other.destination.place # if both are on a spoke, choose the one with the lowest number (nearer the rim) if here =~ /.c./ and there =~ /.c./ # break ties by preferring the most clockwise arc if here[-1] == there[-1] return here[0] <=> there[0] else return -1 * (here[-1] <=> there[-1]) end # If one is one a spoke and the other is on the rim, choose the one on the rim elsif here =~ /.c./ or there =~ /.c./ return -1 * (here.length <=> there.length) # If one is in the 'f' rim section and the other in the 'a' rim section, choose the 'a' elsif here[0,1] == 'a' and there[0,1] == 'f' return +1 elsif here[0,1] == 'f' and there[0,1] == 'a' return -1 # Otherwise, choose the one with highest arc code else return here <=> there end end # Get the current game state game_lines = readlines game, moves, next_roll = Game.read_game(game_lines) game.apply_moves moves # The piece to move is the uncaptured one of mine with the lowest number my_pieces = game.pieces.values.select {|p| p.colour == game.current_player and not p.captured} piece_to_move = (my_pieces.sort_by {|p| p.name})[0] # find the most clockwise move potential_moves = game.possible_moves(next_roll).select {|m| m.piece == piece_to_move} sorted_moves = potential_moves.sort {|x, y| more_clockwise x, y} best_move = sorted_moves[-1] # return that move puts best_move