Class: TicTacToe::Square

Inherits:
Object
  • Object
show all
Defined in:
lib/tic_tac_toe_mchliakh/board/square.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#numberObject (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

#playerObject (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

Returns:

  • (Boolean)


33
34
35
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 33

def empty?
  player.nil?
end

#line_countObject



22
23
24
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 22

def line_count
  @lines.size
end

#serializeObject



45
46
47
# File 'lib/tic_tac_toe_mchliakh/board/square.rb', line 45

def serialize
  player
end

#take(player) ⇒ Object

Raises:



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

Returns:

  • (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

Returns:

  • (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