Class: ComputerPlayer
- Inherits:
-
Object
- Object
- ComputerPlayer
- Defined in:
- lib/players/computer_player.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
writeonly
Sets the attribute board.
-
#color ⇒ Object
readonly
Returns the value of attribute color.
Instance Method Summary collapse
-
#best_move(good_moves) ⇒ Object
finds highest value target from good_moves GOOD MOVES MUST BE AN ARRAY OF CAPTURE MOVES.
- #death_move?(move) ⇒ Boolean
-
#initialize(color) ⇒ ComputerPlayer
constructor
A new instance of ComputerPlayer.
- #make_move ⇒ Object
Constructor Details
#initialize(color) ⇒ ComputerPlayer
Returns a new instance of ComputerPlayer.
5 6 7 |
# File 'lib/players/computer_player.rb', line 5 def initialize(color) @color = color end |
Instance Attribute Details
#board=(value) ⇒ Object (writeonly)
Sets the attribute board
3 4 5 |
# File 'lib/players/computer_player.rb', line 3 def board=(value) @board = value end |
#color ⇒ Object (readonly)
Returns the value of attribute color.
2 3 4 |
# File 'lib/players/computer_player.rb', line 2 def color @color end |
Instance Method Details
#best_move(good_moves) ⇒ Object
finds highest value target from good_moves GOOD MOVES MUST BE AN ARRAY OF CAPTURE MOVES
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/players/computer_player.rb', line 47 def best_move(good_moves) best_move = good_moves.sample good_moves.each do |move| if @board.piece_at(move.last).value > @board.piece_at(best_move.last).value best_move = move end end best_move end |
#death_move?(move) ⇒ Boolean
58 59 60 61 62 |
# File 'lib/players/computer_player.rb', line 58 def death_move?(move) duped_board = @board.deep_dup duped_board.move!(move.first, move.last) duped_board.piece_at(move.last).in_range_of_enemy? end |
#make_move ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/players/computer_player.rb', line 9 def make_move valid_moves_hash = {} @board.pieces(@color).each do |piece| valid_moves_hash[piece.pos] = piece.valid_moves unless piece.valid_moves.empty? end rand_key = valid_moves_hash.keys.sample random_move = [rand_key, valid_moves_hash[rand_key].sample] good_moves = [] neutral_moves = [] death_moves = [] valid_moves_hash.each_pair do |start_pos, potential_positions| potential_positions.each do |potential_pos| if death_move?([start_pos, potential_pos]) death_moves << [start_pos, potential_pos] elsif @board.piece_at(potential_pos) good_moves << [start_pos, potential_pos] else neutral_moves << [start_pos, potential_pos] end end end sleep(0.001) if !good_moves.empty? chosen_move = best_move(good_moves) elsif !neutral_moves.empty? chosen_move = neutral_moves.sample else chosen_move = death_moves.sample end chosen_move end |