Class: ConnectN::ComputerPlayer
- Includes:
- Winnable
- Defined in:
- lib/connect_n/player/computer_player/computer_player.rb
Instance Attribute Summary collapse
-
#delay ⇒ Object
Returns the value of attribute delay.
-
#difficulty ⇒ Object
Returns the value of attribute difficulty.
-
#min_to_win ⇒ Object
readonly
Returns the value of attribute min_to_win.
-
#opponent_disc ⇒ Object
readonly
Returns the value of attribute opponent_disc.
Attributes inherited from Player
Instance Method Summary collapse
-
#initialize(board:, opponent_disc:, name: 'Computer', disc: '🧊', min_to_win: 4, difficulty: 0, delay: 0) ⇒ ComputerPlayer
constructor
A new instance of ComputerPlayer.
- #pick ⇒ Object
Methods included from Winnable
Constructor Details
#initialize(board:, opponent_disc:, name: 'Computer', disc: '🧊', min_to_win: 4, difficulty: 0, delay: 0) ⇒ ComputerPlayer
Returns a new instance of ComputerPlayer.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/connect_n/player/computer_player/computer_player.rb', line 13 def initialize( board:, opponent_disc:, name: 'Computer', disc: '🧊', min_to_win: 4, difficulty: 0, delay: 0 ) super(name: name, disc: disc) @min_to_win = min_to_win @difficulty = difficulty @delay = delay @board = board @opponent_disc = opponent_disc @scores = { disc => 10_000, opponent_disc => -10_000 } end |
Instance Attribute Details
#delay ⇒ Object
Returns the value of attribute delay.
10 11 12 |
# File 'lib/connect_n/player/computer_player/computer_player.rb', line 10 def delay @delay end |
#difficulty ⇒ Object
Returns the value of attribute difficulty.
10 11 12 |
# File 'lib/connect_n/player/computer_player/computer_player.rb', line 10 def difficulty @difficulty end |
#min_to_win ⇒ Object (readonly)
Returns the value of attribute min_to_win.
11 12 13 |
# File 'lib/connect_n/player/computer_player/computer_player.rb', line 11 def min_to_win @min_to_win end |
#opponent_disc ⇒ Object (readonly)
Returns the value of attribute opponent_disc.
11 12 13 |
# File 'lib/connect_n/player/computer_player/computer_player.rb', line 11 def opponent_disc @opponent_disc end |
Instance Method Details
#pick ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/connect_n/player/computer_player/computer_player.rb', line 29 def pick sleep delay best_score = -Float::INFINITY best_pick = nil @board.cols_amount.times do |pick| next unless @board.valid_pick?(pick) board_copy = @board.clone row_num, col_num = board_copy.drop_disc(disc, at_col: pick) score = minimax(board_copy, disc, row_num, col_num) if score >= best_score best_score = score best_pick = pick end end best_pick end |