Class: TicTacToe::Square
- Inherits:
-
Object
- Object
- TicTacToe::Square
- Defined in:
- lib/tic_tac_toe_mchliakh/board/square.rb
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#player ⇒ Object
readonly
Returns the value of attribute player.
Instance Method Summary collapse
- #<<(line) ⇒ Object
- #==(other) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(board, number, saved = nil) ⇒ Square
constructor
A new instance of Square.
- #line_count ⇒ Object
- #serialize ⇒ Object
- #take(player) ⇒ Object
- #taken_by?(player) ⇒ Boolean
- #taken_by_opponent?(player) ⇒ Boolean
Constructor Details
#initialize(board, number, saved = nil) ⇒ Square
Returns a new instance of Square.
7 8 9 10 11 12 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 7 def initialize(board, number, saved=nil) @board = board @number = number @player = saved @lines = [] end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
5 6 7 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 5 def number @number end |
#player ⇒ Object (readonly)
Returns the value of attribute player.
5 6 7 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 5 def player @player end |
Instance Method Details
#<<(line) ⇒ Object
18 19 20 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 18 def <<(line) @lines << line end |
#==(other) ⇒ Object
14 15 16 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 14 def ==(other) other.player == player end |
#empty? ⇒ Boolean
33 34 35 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 33 def empty? player.nil? end |
#line_count ⇒ Object
22 23 24 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 22 def line_count @lines.size end |
#serialize ⇒ Object
45 46 47 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 45 def serialize player end |
#take(player) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 26 def take(player) raise IllegalMoveError, 'Square already taken' unless empty? @player = player notify end |
#taken_by?(player) ⇒ Boolean
37 38 39 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 37 def taken_by?(player) self.player == player end |
#taken_by_opponent?(player) ⇒ Boolean
41 42 43 |
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 41 def taken_by_opponent?(player) !empty? && !taken_by?(player) end |