Class: Reversi::Player::BasePlayer

Inherits:
Object
  • Object
show all
Defined in:
lib/reversi/player/base_player.rb

Direct Known Subclasses

AlphaBetaAI, Human, MinMaxAI, NegaMaxAI, RandomAI

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#boardObject (readonly)

Returns the value of attribute board.



3
4
5
# File 'lib/reversi/player/base_player.rb', line 3

def board
  @board
end

#my_colorObject (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_colorObject (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.

Parameters:

  • my_color (Boolean) (defaults to: true)

    my color or opponent’s color

Returns:

  • (Integer)

    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.

Parameters:

  • my_color (Boolean) (defaults to: true)

    my color or opponent’s color

Returns:

  • (Array<Array<Integer, Integer>>)

    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.

Parameters:

  • x (Integer)

    the column number

  • y (Integer)

    the row number

  • my_color (Boolean) (defaults to: true)

    my color or opponent’s color



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

#statusHash{Symbol => Array<Integer, Integer>}

Returns a hash containing the coordinates of each color.

Returns:

  • (Hash{Symbol => Array<Integer, Integer>})


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