class TicTacToe
Copyright © 2021-2022 Andy Maleh
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Public Class Methods
# File examples/tic_tac_toe.rb, line 8 def initialize @tic_tac_toe_board = Board.new end
Public Instance Methods
# File examples/tic_tac_toe.rb, line 25 def create_gui @main_window = window('Tic-Tac-Toe', 180, 180) { resizable false vertical_box { padded false 3.times.map do |row| horizontal_box { padded false 3.times.map do |column| area { square(0, 0, 60) { stroke :black, thickness: 2 } text(23, 19) { string { font family: 'Arial', size: OS.mac? ? 20 : 16 # data-bind string property of area text attributed string to tic tac toe board cell sign string <= [@tic_tac_toe_board[row + 1, column + 1], :sign] # board model is 1-based } } on_mouse_up do @tic_tac_toe_board.mark(row + 1, column + 1) # board model is 1-based end } end } end } } end
# File examples/tic_tac_toe.rb, line 63 def display_draw_message display_game_over_message("Draw!") end
# File examples/tic_tac_toe.rb, line 67 def display_game_over_message(message_text) Glimmer::LibUI.queue_main do msg_box('Game Over', message_text) @tic_tac_toe_board.reset! end end
# File examples/tic_tac_toe.rb, line 59 def display_win_message display_game_over_message("Player #{@tic_tac_toe_board.winning_sign} has won!") end
# File examples/tic_tac_toe.rb, line 12 def launch create_gui register_observers @main_window.show end
# File examples/tic_tac_toe.rb, line 18 def register_observers observe(@tic_tac_toe_board, :game_status) do |game_status| display_win_message if game_status == Board::WIN display_draw_message if game_status == Board::DRAW end end