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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe.rb', line 27

def initialize
  @tic_tac_toe_board = Board.new
  @shell = shell {
    text "Tic-Tac-Toe"
    minimum_size 150, 178
    composite {
      grid_layout 3, true
      (1..3).each { |row|
        (1..3).each { |column|
          button {
            layout_data :fill, :fill, true, true
            text        <= [@tic_tac_toe_board[row, column], :sign]
            enabled     <= [@tic_tac_toe_board[row, column], :empty]
            font        style: :bold, height: 20
            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



59
60
61
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe.rb', line 59

def display_draw_message
  display_game_over_message("Draw!")
end

#display_game_over_message(message_text) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe.rb', line 63

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

#display_win_messageObject



55
56
57
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe.rb', line 55

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

#openObject



71
72
73
# File 'lib/glimmer-dsl-opal/samples/elaborate/tic_tac_toe.rb', line 71

def open
  @shell.open
end