Class: GamesAndRpgParadise::GUI::Gtk::TicTacToe
- Inherits:
-
Gtk::Box
- Object
- Gtk::Box
- GamesAndRpgParadise::GUI::Gtk::TicTacToe
- Includes:
- Gtk::BaseModule
- Defined in:
- lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb,
lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb
Defined Under Namespace
Classes: GameBoard
Constant Summary collapse
- NAMESPACE =
#
NAMESPACE
#
inspect
- FILE_USEFUL_MOVES =
#
FILE_USEFUL_MOVES
#
"#{::GamesAndRpgParadise::PROJECT_BASE_DIRECTORY}games/tic_tac_toe/useful_moves.md"
- TITLE =
#
TITLE
#
'Tic-Tac-Toe (via ruby-gtk3)'
- HEIGHT =
#
HEIGHT
#
'40% or 560px minimum'
- WIDTH =
#
WIDTH
#
'40% or 480px minimum'
- USE_THIS_FONT =
#
USE_THIS_FONT
#
:dejavu_condensed_20
- MESSAGE_THE_HUMAN_PLAYER_HAS_WON =
#
MESSAGE_THE_HUMAN_PLAYER_HAS_WON
#
"The <b>human player</b> has won. \\o/"
- MSG_THE_COMPUTER_HAS_WON =
#
MSG_THE_COMPUTER_HAS_WON
#
'The <b>computer</b> has won. :('
Class Method Summary collapse
-
.run(i = ARGV) ⇒ Object
# === GamesAndRpgParadise::GUI::Gtk::TicTacToe.run ========================================================================= #.
Instance Method Summary collapse
-
#advanced_the_game(theGame, array_holding_the_widgets, boxNumber, ongoing) ⇒ Object
# === advanced_the_game ========================================================================= #.
-
#border_size? ⇒ Boolean
# === border_size? ========================================================================= #.
-
#connect_skeleton ⇒ Object
# === connect_skeleton (connect tag) ========================================================================= #.
-
#create_skeleton ⇒ Object
# === create_skeleton (create tag) ========================================================================= #.
-
#create_the_buttons ⇒ Object
# === create_the_buttons (buttons tag, button tag) ========================================================================= #.
-
#display_the_about_dialog ⇒ Object
# === display_the_about_dialog ========================================================================= #.
-
#initialize(commandline_arguments = ARGV, run_already = true) ⇒ TicTacToe
constructor
# === initialize ========================================================================= #.
-
#padding? ⇒ Boolean
# === padding? ========================================================================= #.
-
#report_game_written_into_which_file(this_file = FILE_USEFUL_MOVES) ⇒ Object
# === report_game_written_into_which_file ========================================================================= #.
-
#reset ⇒ Object
# === reset (reset tag) ========================================================================= #.
-
#run ⇒ Object
# === run (run tag) ========================================================================= #.
Constructor Details
#initialize(commandline_arguments = ARGV, run_already = true) ⇒ TicTacToe
#
initialize
#
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 69 def initialize( commandline_arguments = ARGV, run_already = true ) super(:vertical) reset set_commandline_arguments( commandline_arguments ) signal_connect(:delete_event) { do_quit } ongoing = 0 # player = 0 # 0 = player one, 1 = player 2 # moveX = 0 # moveY = 0 game_status_label = gtk_label('Ready', false) = = = ('Reset') gameMenuEraseMemory = ('Erase Memory') = ('Quit Game') .append() .append(gameMenuEraseMemory) .append() if File.exist? FILE_USEFUL_MOVES gameMenuEraseMemory.on_activate { gameDB = File.open(FILE_USEFUL_MOVES,'w') gameDB.write "\n" gameDB.close game_status_label.set_text('Memory Erased') } end .signal_connect(:activate) { do_quit } .show gameMenuEraseMemory.show .show # The help menu comes after the Game menu. = helpMenuAbout = ('About TicTacToe') .append(helpMenuAbout) helpMenuAbout.signal_connect(:activate) { display_the_about_dialog } helpMenuAbout.show game_item = ('Game') game_item.() help_item = ('Help') help_item.() .append(game_item) .append(help_item) = (' ') .relief_half = (' ') .relief_half = (' ') .relief_half = (' ') .relief_half = (' ') .relief_half = (' ') .relief_half = (' ') .relief_half = (' ') .relief_half = (' ') .relief_half = [ game_status_label, , , , , , , , , ] .on_clicked { ongoing = advanced_the_game(@game_board, ,1, ongoing) } .on_clicked { ongoing = advanced_the_game(@game_board, ,2, ongoing) } .on_clicked { ongoing = advanced_the_game(@game_board, ,3, ongoing) } .on_clicked { ongoing = advanced_the_game(@game_board, ,4, ongoing) } .on_clicked { ongoing = advanced_the_game(@game_board, ,5, ongoing) } .on_clicked { ongoing = advanced_the_game(@game_board, ,6, ongoing) } .on_clicked { ongoing = advanced_the_game(@game_board, ,7, ongoing) } .on_clicked { ongoing = advanced_the_game(@game_board, ,8, ongoing) } .on_clicked { ongoing = advanced_the_game(@game_board, ,9, ongoing) } @button_reset.on_clicked { @game_board.reset #player = 0 ongoing = 0 .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') game_status_label.set_text('Ready.') } .on_activate { @game_board.reset #player = 0 ongoing = 0 .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') .set_label(' ') game_status_label.set_text('Ready.') } row_0 = create_hbox row_1 = create_hbox row_2 = create_hbox row_3 = create_hbox row_0.pack_start() row_0.set_child_packing( , expand: false, fill: false, padding: 0, pack_type: 0 ) .show row_1.maximal(, 0) row_1.maximal(, 0) row_1.maximal(, 0) row_2.maximal(, 0) row_2.maximal(, 0) row_2.maximal(, 0) row_3.maximal(, 0) row_3.maximal(, 0) row_3.maximal(, 0) mainbox = create_vbox mainbox.add(row_0) mainbox.set_child_packing( row_0, expand: false, fill: false, padding: 0, pack_type: 0 ) mainbox.add(row_1) mainbox.add(row_2) mainbox.add(row_3) mainbox.minimal(game_status_label, 5) hbox = create_hbox hbox.set_halign(::Gtk::Align::CENTER) hbox.minimal(@button_reset, 4) mainbox.minimal(hbox, 5) add(mainbox) run if run_already end |
Class Method Details
.run(i = ARGV) ⇒ Object
#
GamesAndRpgParadise::GUI::Gtk::TicTacToe.run
#
417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 417 def self.run( i = ARGV ) require 'gtk_paradise/run' _ = ::GamesAndRpgParadise::GUI::Gtk::TicTacToe.new(i) r = ::Gtk.run r << _ r.infer_the_size_automatically r.automatic_size r.automatic_title r.set_border_width(10) r.top_left_then_run end |
Instance Method Details
#advanced_the_game(theGame, array_holding_the_widgets, boxNumber, ongoing) ⇒ Object
#
advanced_the_game
#
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 299 def advanced_the_game( theGame, , boxNumber, ongoing ) case ongoing when 0 moveX = ( (boxNumber+2)/3 ).to_i moveY = ( boxNumber + 2) % 3 + 1 if theGame.make_a_move(0, moveX.to_i, moveY.to_i) [boxNumber].set_label('X') ongoing = theGame.check_the_status case ongoing when 0 aiMove = theGame.let_the_AI_make_a_move_next( 0 ) moveX = aiMove[0] + 1 moveY = aiMove[1] + 1 if theGame.make_a_move(1, moveX.to_i, moveY.to_i) aiBox = (moveX-1)*3 + moveY [aiBox].set_label('0') ongoing = theGame.check_the_status if ongoing == 1 [0].set_text('Draw.') elsif ongoing == 2 [0].set_text( MSG_THE_COMPUTER_HAS_WON ) [0].markify _ = File.open(FILE_USEFUL_MOVES,'a') _.write theGame.progress _.write "\n" _.close report_game_written_into_which_file end end when 1 [0].set_text('Draw.') when 2 [0].set_text(MESSAGE_THE_HUMAN_PLAYER_HAS_WON) [0].markify _ = File.open(FILE_USEFUL_MOVES,'a') _.write theGame.progress _.write "\n" _.close report_game_written_into_which_file end end else [0].set_text('Press Reset!') end return ongoing end |
#border_size? ⇒ Boolean
#
border_size?
#
286 287 288 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 286 def border_size? 2 end |
#connect_skeleton ⇒ Object
#
connect_skeleton (connect tag)
#
385 386 387 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 385 def connect_skeleton abort_on_exception end |
#create_skeleton ⇒ Object
#
create_skeleton (create tag)
#
293 294 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 293 def create_skeleton end |
#create_the_buttons ⇒ Object
#
create_the_buttons (buttons tag, button tag)
#
399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 399 def # ======================================================================= # # Add the reset-button next: # ======================================================================= # @button_reset = ('_Reset') @button_reset.clear_background @button_reset.disallow_resizing @button_reset.width_height(120, 60) @button_reset.on_hover(:lightgreen) @button_reset.bblack2 @button_reset.hint = "<b>Clicking</b> on this button will \n"\ "<b>reset to the start-state again</b>. " end |
#display_the_about_dialog ⇒ Object
#
display_the_about_dialog
#
356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 356 def display_the_about_dialog about_dialog = create_about_dialog about_dialog.set_name('Tic Tac Toe') about_dialog.set_version('1.0') about_dialog.( ['GtkParadise Project Authors'] ) about_dialog.set_copyright('Copyright 2020') about_dialog.set_license('Same licence as the gtk_paradise project.') about_dialog.set_comments('A trivial implementation of the tic-tac-toe game.') about_dialog.signal_connect(:response) { about_dialog.destroy } about_dialog.show_all end |
#padding? ⇒ Boolean
#
padding?
#
279 280 281 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 279 def padding? 2 end |
#report_game_written_into_which_file(this_file = FILE_USEFUL_MOVES) ⇒ Object
#
report_game_written_into_which_file
#
373 374 375 376 377 378 379 380 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 373 def report_game_written_into_which_file( this_file = FILE_USEFUL_MOVES ) e 'The game-state has been written into the following file:' e e " #{this_file}" e end |
#reset ⇒ Object
#
reset (reset tag)
#
261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 261 def reset reset_the_internal_variables # ======================================================================= # # === @configuration # ======================================================================= # @configuration = [true, __dir__, NAMESPACE] title_width_height_font(TITLE, WIDTH, HEIGHT, USE_THIS_FONT) # ======================================================================= # # Instantiate a new gameboard next: # ======================================================================= # @game_board = GameBoard.new use_gtk_paradise_project_css_file infer_the_size_automatically end |
#run ⇒ Object
#
run (run tag)
#
392 393 394 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/tic_tac_toe.rb', line 392 def run super() end |