Class: TicTacToe::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe/board.rb

Constant Summary collapse

DRAW =
:draw
IN_PROGRESS =
:in_progress
WIN =
:win

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBoard

Returns a new instance of Board.



11
12
13
14
15
16
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe/board.rb', line 11

def initialize
  @sign_state_machine = {nil => "X", "X" => "O", "O" => "X"}
  build_grid
  @winning_sign = Cell::EMPTY
  @game_status = IN_PROGRESS
end

Instance Attribute Details

#game_statusObject

Returns the value of attribute game_status.



9
10
11
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe/board.rb', line 9

def game_status
  @game_status
end

#winning_signObject (readonly)

Returns the value of attribute winning_sign.



8
9
10
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe/board.rb', line 8

def winning_sign
  @winning_sign
end

Instance Method Details

#[](row, column) ⇒ Object



28
29
30
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe/board.rb', line 28

def [](row, column)
  @grid[row-1][column-1]
end

#current_signObject



24
25
26
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe/board.rb', line 24

def current_sign
  @current_sign = @sign_state_machine[@current_sign]
end

#game_over?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe/board.rb', line 32

def game_over?
   win? or draw?
end

#mark(row, column) ⇒ Object

row and column numbers are 1-based



19
20
21
22
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe/board.rb', line 19

def mark(row, column)
  self[row, column].mark(current_sign)
  game_over? #updates winning sign
end

#resetObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe/board.rb', line 42

def reset
  (1..3).each do |row|
    (1..3).each do |column|
      self[row, column].reset
    end
  end
  @winning_sign = Cell::EMPTY
  @current_sign = nil
  self.game_status=IN_PROGRESS
end

#win?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe/board.rb', line 36

def win?
  win = (row_win? or column_win? or diagonal_win?)
  self.game_status=WIN if win
  win
end