class ConnectNGame::Prudent
The Prudent
bot picks the move with the highest score.
Public Class Methods
new(name = "Prudent")
click to toggle source
Build the player
Calls superclass method
ConnectNGame::Player::new
# File lib/connect_n_game/players/prudent.rb, line 9 def initialize(name = "Prudent") super(name, "Minimum tactical analysis with some defense.", :silicon) end
Public Instance Methods
check_opponent(rack, piece)
click to toggle source
Check for the opponent's best moves at this level
# File lib/connect_n_game/players/prudent.rb, line 41 def check_opponent(rack, piece) threshold = rack.order - 1 result = 0 (1..rack.width).each do |channel| score = rack.score_move(channel, piece) score *= 2 if score > threshold result += score if score >= threshold end result end
losers_comments()
click to toggle source
The agony of defeat
# File lib/connect_n_game/players/prudent.rb, line 60 def losers_comments "#{name} says 'How could I have missed this?'" end
make_move(game, piece)
click to toggle source
Make a move. This bot picks the move with the highest score.
Parameters
-
game - the game being played.
-
piece - the piece to be played, 1 or 2.
Returns
-
A move, 1 .. rack.width
# File lib/connect_n_game/players/prudent.rb, line 19 def make_move(game, piece) #Compute the moves of the first ply. first_ply = (game.rack.weights.each_with_index.map do |weight, index| channel = index + 1 [weight + game.rack.score_move(channel, piece), channel] end).sort.show_weights("Scan 1") #If we're done, stop. return first_ply.last[1] if first_ply.last[0] >= game.rack.order #Factor in the behavior of the opponent. (0...(first_ply.length)).each do |index| copy = game.rack.clone.deep_clone copy.rack[first_ply[index][1]-1] << piece first_ply[index][0] -= check_opponent(copy, (piece % 2) + 1) end first_ply.sort.show_weights("Scan 2").last[1] end
winners_comments()
click to toggle source
The thrill of victory.
# File lib/connect_n_game/players/prudent.rb, line 55 def winners_comments "#{name} says 'Good moves must give way to better!'" end