Class: Reversi::Player::BasePlayer
- Inherits:
-
Object
- Object
- Reversi::Player::BasePlayer
- Defined in:
- lib/reversi/player/base_player.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#my_color ⇒ Object
readonly
Returns the value of attribute my_color.
-
#opponent_color ⇒ Object
readonly
Returns the value of attribute opponent_color.
Instance Method Summary collapse
-
#count_disks(my_color = true) ⇒ Integer
Returns a number of the supplied color’s disks.
-
#initialize(color, board) ⇒ BasePlayer
constructor
Initializes a new BasePlayer object.
-
#move(board) ⇒ Object
Override this method in your original subclass.
-
#next_moves(my_color = true) ⇒ Array<Array<Integer, Integer>>
Returns an array of the next moves.
-
#put_disk(x, y, my_color = true) ⇒ Object
Places a supplied color’s disk on specified position, and flips the opponent’s disks.
-
#status ⇒ Hash{Symbol => Array<Integer, Integer>}
Returns a hash containing the coordinates of each color.
Constructor Details
#initialize(color, board) ⇒ BasePlayer
Initializes a new BasePlayer object.
6 7 8 9 10 11 12 |
# File 'lib/reversi/player/base_player.rb', line 6 def initialize(color, board) @my_color = color @opponent_color = @my_color == Reversi::Board::DISK[:white] ? Reversi::Board::DISK[:black] : Reversi::Board::DISK[:white] @board = board end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
3 4 5 |
# File 'lib/reversi/player/base_player.rb', line 3 def board @board end |
#my_color ⇒ Object (readonly)
Returns the value of attribute my_color.
3 4 5 |
# File 'lib/reversi/player/base_player.rb', line 3 def my_color @my_color end |
#opponent_color ⇒ Object (readonly)
Returns the value of attribute opponent_color.
3 4 5 |
# File 'lib/reversi/player/base_player.rb', line 3 def opponent_color @opponent_color end |
Instance Method Details
#count_disks(my_color = true) ⇒ Integer
Returns a number of the supplied color’s disks.
43 44 45 46 |
# File 'lib/reversi/player/base_player.rb', line 43 def count_disks(my_color = true) color = my_color ? @my_color : @opponent_color @board.count_disks(color) end |
#move(board) ⇒ Object
Override this method in your original subclass.
15 16 |
# File 'lib/reversi/player/base_player.rb', line 15 def move(board) end |
#next_moves(my_color = true) ⇒ Array<Array<Integer, Integer>>
Returns an array of the next moves.
34 35 36 37 |
# File 'lib/reversi/player/base_player.rb', line 34 def next_moves(my_color = true) color = my_color ? @my_color : @opponent_color @board.next_moves(color) end |
#put_disk(x, y, my_color = true) ⇒ Object
Places a supplied color’s disk on specified position, and flips the opponent’s disks.
24 25 26 27 28 |
# File 'lib/reversi/player/base_player.rb', line 24 def put_disk(x, y, my_color = true) @board.push_stack color = my_color ? @my_color : @opponent_color @board.flip_disks(x, y, color) end |
#status ⇒ Hash{Symbol => Array<Integer, Integer>}
Returns a hash containing the coordinates of each color.
51 52 53 54 55 56 57 |
# File 'lib/reversi/player/base_player.rb', line 51 def status convert = { :black => @my_color == Reversi::Board::DISK[:black] ? :mine : :opponent, :white => @my_color == Reversi::Board::DISK[:white] ? :mine : :opponent, :none => :none } Hash[*@board.status.map{ |k, v| [convert[k], v] }.flatten(1)] end |