Class: GamesAndRpgParadise::GUI::Gtk::TicTacToe

Inherits:
Gtk::Box
  • Object
show all
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

Instance Method Summary collapse

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)
  create_the_buttons
  menu_bar = gtk_menu_bar
  game_menu = gtk_menu
  game_menu_reset = gtk_menu_item('Reset')
  gameMenuEraseMemory = gtk_menu_item('Erase Memory')
  game_menu_quit = gtk_menu_item('Quit Game')

  game_menu.append(game_menu_reset)
  game_menu.append(gameMenuEraseMemory)
  game_menu.append(game_menu_quit)

  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
  game_menu_quit.signal_connect(:activate) {
    do_quit
  }

  game_menu_reset.show
  gameMenuEraseMemory.show
  game_menu_quit.show
  # The help menu comes after the Game menu.
  help_menu = create_menu
  helpMenuAbout = gtk_menu_item('About TicTacToe')
  help_menu.append(helpMenuAbout)
  helpMenuAbout.signal_connect(:activate) {
    display_the_about_dialog
  }
  helpMenuAbout.show

  game_item = gtk_menu_item('Game')
  game_item.set_submenu(game_menu)
  help_item = gtk_menu_item('Help')
  help_item.set_submenu(help_menu)

  menu_bar.append(game_item)
  menu_bar.append(help_item)

  button_1 = button(' ')
  button_1.relief_half
  button_2 = button(' ')
  button_2.relief_half
  button_3 = button(' ')
  button_3.relief_half
  button_4 = button(' ')
  button_4.relief_half
  button_5 = button(' ')
  button_5.relief_half
  button_6 = button(' ')
  button_6.relief_half
  button_7 = button(' ')
  button_7.relief_half
  button_8 = button(' ')
  button_8.relief_half
  button_9 = button(' ')
  button_9.relief_half

  buttons = [
    game_status_label,
    button_1 ,
    button_2 ,
    button_3 ,
    button_4 ,
    button_5 ,
    button_6 ,
    button_7 ,
    button_8 ,
    button_9
  ]
  button_1.on_clicked {
    ongoing = advanced_the_game(@game_board, buttons,1, ongoing)
  }
  button_2.on_clicked {
    ongoing = advanced_the_game(@game_board, buttons,2, ongoing)
  }
  button_3.on_clicked {
    ongoing = advanced_the_game(@game_board, buttons,3, ongoing)
  }
  button_4.on_clicked {
    ongoing = advanced_the_game(@game_board, buttons,4, ongoing)
  }
  button_5.on_clicked {
    ongoing = advanced_the_game(@game_board, buttons,5, ongoing)
  }
  button_6.on_clicked {
    ongoing = advanced_the_game(@game_board, buttons,6, ongoing)
  }
  button_7.on_clicked {
    ongoing = advanced_the_game(@game_board, buttons,7, ongoing)
  }
  button_8.on_clicked {
    ongoing = advanced_the_game(@game_board, buttons,8, ongoing)
  }
  button_9.on_clicked {
    ongoing = advanced_the_game(@game_board, buttons,9, ongoing)
  }
  @button_reset.on_clicked {
    @game_board.reset
    #player = 0
    ongoing = 0
    button_1.set_label(' ')
    button_2.set_label(' ')
    button_3.set_label(' ')
    button_4.set_label(' ')
    button_5.set_label(' ')
    button_6.set_label(' ')
    button_7.set_label(' ')
    button_8.set_label(' ')
    button_9.set_label(' ')
    game_status_label.set_text('Ready.')
  }
  game_menu_reset.on_activate {
    @game_board.reset
    #player = 0
    ongoing = 0
    button_1.set_label(' ')
    button_2.set_label(' ')
    button_3.set_label(' ')
    button_4.set_label(' ')
    button_5.set_label(' ')
    button_6.set_label(' ')
    button_7.set_label(' ')
    button_8.set_label(' ')
    button_9.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(menu_bar)
  row_0.set_child_packing(
    menu_bar,
    expand:     false,
    fill:       false,
    padding:    0,
    pack_type:  0
  )
  menu_bar.show
  row_1.maximal(button_1, 0)
  row_1.maximal(button_2, 0)
  row_1.maximal(button_3, 0)
  row_2.maximal(button_4, 0)
  row_2.maximal(button_5, 0)
  row_2.maximal(button_6, 0)
  row_3.maximal(button_7, 0)
  row_3.maximal(button_8, 0)
  row_3.maximal(button_9, 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,
    array_holding_the_widgets,
    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)
      array_holding_the_widgets[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
          array_holding_the_widgets[aiBox].set_label('0')
          ongoing = theGame.check_the_status
          if ongoing == 1
            array_holding_the_widgets[0].set_text('Draw.')
          elsif ongoing == 2
            array_holding_the_widgets[0].set_text(
              MSG_THE_COMPUTER_HAS_WON
            )
            array_holding_the_widgets[0].markify
            _ = File.open(FILE_USEFUL_MOVES,'a')
            _.write theGame.progress
            _.write "\n"
            _.close
            report_game_written_into_which_file
          end
        end
      when 1
        array_holding_the_widgets[0].set_text('Draw.')
      when 2
        array_holding_the_widgets[0].set_text(MESSAGE_THE_HUMAN_PLAYER_HAS_WON)
        array_holding_the_widgets[0].markify
        _ = File.open(FILE_USEFUL_MOVES,'a')
        _.write theGame.progress
        _.write "\n"
        _.close
        report_game_written_into_which_file
      end
    end
  else
    array_holding_the_widgets[0].set_text('Press Reset!')
  end
  return ongoing
end

#border_size?Boolean

#

border_size?

#

Returns:

  • (Boolean)


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_skeletonObject

#

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_skeletonObject

#

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_buttonsObject

#

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 create_the_buttons
  # ======================================================================= #
  # Add the reset-button next:
  # ======================================================================= #
  @button_reset = button('_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_dialogObject

#

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.set_authors( ['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?

#

Returns:

  • (Boolean)


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

#resetObject

#

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

#runObject

#

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