#!/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 # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . require 'libttc' def more_clockwise(this, other) here = this.place there = other.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 least clockwise of my uncaptured pieces # Always move pieces on the base first my_pieces = game.pieces.values.select {|p| p.colour == game.current_player and not p.captured} piece_to_move = (my_pieces.sort {|x, y| more_clockwise x.position, y.position})[0] # Find the most clockwise move for this piece potential_moves = game.possible_moves(next_roll).select {|m| m.piece == piece_to_move} sorted_moves = potential_moves.sort {|x, y| more_clockwise x.destination, y.destination} best_move = sorted_moves[-1] # return that move puts best_move