Sha256: a4e9b6f6b6bb96d123556c195a00f62d81b1b091030b68d3fcd7498dcf9e28bc

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

require_relative "../../lib/glimmer"
require_relative "tic_tac_toe_board"


class TicTacToe

  include_package 'org.eclipse.swt'
  include_package 'org.eclipse.swt.widgets'
  include_package 'org.eclipse.swt.layout'

  include Glimmer

  def initialize
    @tic_tac_toe_board = TicTacToeBoard.new
    @main =
    @shell = shell {
      text "Tic-Tac-Toe"
      composite {
        layout GridLayout.new(3,true)
        (1..3).each { |row_number|
          (1..3).each { |column_number|
            button {
              layout_data GridData.new(:fill.swt_constant, :fill.swt_constant, true, true)
              text        bind(@tic_tac_toe_board.box(row_number, column_number), :sign)
              enabled     bind(@tic_tac_toe_board.box(row_number, column_number), :empty)
              on_widget_selected {
                @tic_tac_toe_board.mark_box(row_number, column_number)
              }
            }
          }
        }
      }
    }
    @tic_tac_toe_board.extend(ObservableModel) #make board an observable model
    @tic_tac_toe_board.add_observer("game_status", self)
  end

  def update(game_status)
    display_win_message if game_status == TicTacToeBoard::WIN
    display_draw_message if game_status == TicTacToeBoard::DRAW
  end

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

  def display_draw_message()
    display_game_over_message("Draw!")
  end

  def display_game_over_message(message)
    message_box = MessageBox.new(@shell.widget)
    message_box.setText("Game Over")
    message_box.setMessage(message)
    message_box.open
    @tic_tac_toe_board.reset
  end

  def open 
    @main.open
  end
end

TicTacToe.new.open

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
glimmer-0.1.11.SWT4.14 samples/tictactoe/tic_tac_toe.rb
glimmer-0.1.11.470 samples/tictactoe/tic_tac_toe.rb
glimmer-0.1.10.470 samples/tictactoe/tic_tac_toe.rb
glimmer-0.1.9.470 samples/tictactoe/tic_tac_toe.rb
glimmer-0.1.8.470 samples/tictactoe/tic_tac_toe.rb