Class: TicTacToe

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

Defined Under Namespace

Classes: Board, Cell

Instance Method Summary collapse

Constructor Details

#initializeTicTacToe

Returns a new instance of TicTacToe.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe.rb', line 6

def initialize
  @tic_tac_toe_board = Board.new
  @shell = shell {
    text "Tic-Tac-Toe"
    composite {
      grid_layout 3, true
      (1..3).each { |row|
        (1..3).each { |column|
          button {
            layout_data :fill, :fill, true, true
            text        bind(@tic_tac_toe_board[row, column], :sign)
            enabled     bind(@tic_tac_toe_board[row, column], :empty)
            on_widget_selected {
              @tic_tac_toe_board.mark(row, column)
            }
          }
        }
      }
    }
  }
  observe(@tic_tac_toe_board, :game_status) { |game_status|
    display_win_message if game_status == Board::WIN
    display_draw_message if game_status == Board::DRAW
  }
end

Instance Method Details

#display_draw_messageObject



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

def display_draw_message
  display_game_over_message("Draw!")
end

#display_game_over_message(message_text) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe.rb', line 40

def display_game_over_message(message_text)
  message_box(@shell) {
    text 'Game Over'
    message message_text
  }.open
  @tic_tac_toe_board.reset
end

#display_win_messageObject



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

def display_win_message
  display_game_over_message("Player #{@tic_tac_toe_board.winning_sign} has won!")
end

#openObject



48
49
50
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe.rb', line 48

def open
  @shell.open
end