Class: ConnectNGame::Prudent
- Defined in:
- lib/connect_n_game/players/prudent.rb
Overview
The EchoMoves echoes moves when possible.
Instance Attribute Summary
Attributes inherited from Player
Instance Method Summary collapse
-
#check_opponent(rack, piece) ⇒ Object
Check for the opponent’s best moves at this level.
-
#initialize(name = "Prudent") ⇒ Prudent
constructor
Build the player.
-
#losers_comments ⇒ Object
The agony of defeat.
-
#make_move(game, piece) ⇒ Object
Make a move.
-
#winners_comments ⇒ Object
The thrill of victory.
Methods inherited from Player
Constructor Details
#initialize(name = "Prudent") ⇒ Prudent
Build the player
9 10 11 |
# File 'lib/connect_n_game/players/prudent.rb', line 9 def initialize(name = "Prudent") super(name, "Minimum tactical analysis with some defense.", :silicon) end |
Instance Method Details
#check_opponent(rack, piece) ⇒ Object
Check for the opponent’s best moves at this level
41 42 43 44 45 46 47 48 49 50 51 52 |
# 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 ⇒ Object
The agony of defeat
60 61 62 |
# 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) ⇒ Object
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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 ⇒ Object
The thrill of victory.
55 56 57 |
# File 'lib/connect_n_game/players/prudent.rb', line 55 def winners_comments "#{name} says 'Good moves must give way to better!'" end |