Class: Games::SimplifiedTictactoe

Inherits:
Gtk::VBox
  • Object
show all
Includes:
Gtk::BaseModule
Defined in:
lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb

Constant Summary collapse

UNHAPPY_SMILEY =
#

UNHAPPY_SMILEY

#
Cyberweb.web_images(:unhappy_smiley)
NAMESPACE =
#

NAMESPACE

#
inspect
TITLE =
#

TITLE

#
'TicTacToe'
WIN_MESSAGE =
#

WIN_MESSAGE

#
'Du hast gewonnen!'
LOSE_MESSAGE =
#

LOSE_MESSAGE

#
'Du hast verloren!'
DRAW_MESSAGE =
#

DRAW_MESSAGE

#
'Das Spiel endet unentschieden.'
XXX =
#

XXX

#
'xxx'
OOO =
#

OOO

#
'ooo'

Instance Method Summary collapse

Constructor Details

#initializeSimplifiedTictactoe

#

initialize

#


79
80
81
82
83
84
85
86
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 79

def initialize
  super()
  reset
  register_keybindings
  create_skeleton
  populate_table
  connect_skeleton
end

Instance Method Details

#button_was_pressedObject

#

button_was_pressed

#


136
137
138
139
140
141
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 136

def button_was_pressed
  @n_clicks += 1
  check_for_game_condition # check before computer places his piece
  randomly_set_computer_piece unless @n_clicks >= 5
  check_for_game_condition if ! @game_over # immer wenn ein button gedrückt wurde
end

#check_for_game_conditionObject

#

check_for_game_condition

Can be lost or won. Should be checked ONLY ONCE.

#


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
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 148

def check_for_game_condition
  @array_buttons.each { |button|
    @array_storing_values << button.label.to_s
  }
  #  ZUERST checken ob wir gewonnen haben, denn wir 
  # setzen ZUERST einen button.
  if @array_storing_values[0]+@array_storing_values[1]+@array_storing_values[2] == XXX
    player_has_won
  elsif @array_storing_values[3]+@array_storing_values[4]+@array_storing_values[5] == XXX
    player_has_won
  elsif @array_storing_values[6]+@array_storing_values[7]+@array_storing_values[8] == XXX
    player_has_won
  elsif @array_storing_values[0]+@array_storing_values[3]+@array_storing_values[6] == XXX
    player_has_won
  elsif @array_storing_values[1]+@array_storing_values[4]+@array_storing_values[7] == XXX
    player_has_won
  elsif @array_storing_values[2]+@array_storing_values[5]+@array_storing_values[8] == XXX
    player_has_won
  elsif @array_storing_values[0]+@array_storing_values[4]+@array_storing_values[8] == XXX
    player_has_won
  elsif @array_storing_values[2]+@array_storing_values[4]+@array_storing_values[6] == XXX
    player_has_won
  elsif @n_clicks >= 5
    @status_bar.push(@status_bar_context_id, DRAW_MESSAGE)
  else #pass through
  end
  
  if @array_storing_values[0]+@array_storing_values[1]+@array_storing_values[2] == OOO
    player_has_lost
  elsif @array_storing_values[3]+@array_storing_values[4]+@array_storing_values[5] == OOO
    player_has_lost
  elsif @array_storing_values[6]+@array_storing_values[7]+@array_storing_values[8] == OOO
    player_has_lost
  elsif @array_storing_values[0]+@array_storing_values[3]+@array_storing_values[6] == OOO
    player_has_lost
  elsif @array_storing_values[1]+@array_storing_values[4]+@array_storing_values[7] == OOO
    player_has_lost
  elsif @array_storing_values[2]+@array_storing_values[5]+@array_storing_values[8] == OOO
    player_has_lost
  elsif @array_storing_values[0]+@array_storing_values[4]+@array_storing_values[8] == OOO
    player_has_lost
  elsif @array_storing_values[2]+@array_storing_values[4]+@array_storing_values[6] == OOO
    player_has_lost
  end
  @array_storing_values = [] # empty array at end.
end

#connect_skeletonObject

#

connect_skeleton

#


303
304
305
306
307
308
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 303

def connect_skeleton
  add @main_table
  hbox = gtk_hbox
  hbox.add(@status_bar).add(@button_restart)
  add(hbox)
end

#create_buttonsObject

#

create_buttons

#


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 249

def create_buttons
  @array_buttons = []
  @array_buttons << ::Gtk::TicButton.new('', self)
  @array_buttons << ::Gtk::TicButton.new('', self)
  @array_buttons << ::Gtk::TicButton.new('', self)
  @array_buttons << ::Gtk::TicButton.new('', self)
  @array_buttons << ::Gtk::TicButton.new('', self)
  @array_buttons << ::Gtk::TicButton.new('', self)
  @array_buttons << ::Gtk::TicButton.new('', self)
  @array_buttons << ::Gtk::TicButton.new('', self)
  @array_buttons << ::Gtk::TicButton.new('', self)
  @button_restart = coloured_button('_Restart_lavenderblush')
  @button_restart.on_clicked {
    # e 'Restarting game.'
    @array_buttons.each { |button|
      button.label = ''
      button.set_sensitive(true) # alle wieder sensitiv
    }
    @n_clicks = 0 # set n clicks to 0
    # empty status bar again.
    @status_bar.push(@status_bar_context_id, '')
    @game_over = false
  }
end

#create_main_tableObject

#

create_main_table

#


239
240
241
242
243
244
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 239

def create_main_table
  @main_table = Gtk::Table.new(3, 3, true)
  @main_table.set_column_spacings( 0 )
  @main_table.set_row_spacings( 0 )
  @main_table.set_border_width( 5 )
end

#create_skeletonObject

#

create_skeleton

#


115
116
117
118
119
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 115

def create_skeleton
  create_main_table
  create_buttons
  create_status_bar
end

#create_status_barObject

#

create_status_bar

#


215
216
217
218
219
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 215

def create_status_bar
  @status_bar = gtk_statusbar
  @status_bar_context_id = @status_bar.get_context_id('Status')
  @status_bar.push(@status_bar_context_id, ' Status')
end

#favicon?Boolean

#

favicon?

#

Returns:

  • (Boolean)


366
367
368
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 366

def favicon?
  '/home/x/data/images/games/TICTACTOE/TICTACTOE.png'
end

#fill_up_restObject

#

fill_up_rest

Every member that has no ‘x’ or ‘o’ will be filled up with ‘_’ Also makes the game over.

#


202
203
204
205
206
207
208
209
210
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 202

def fill_up_rest
  @array_buttons.each { |button|
    if button.label.empty?
      button.label = '__'
      button.set_sensitive(false)
    end 
  }
  @game_over = true
end

#focus(i) ⇒ Object

#

focus

#


277
278
279
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 277

def focus(i)
  pp i
end

#padding?Boolean

#

padding?

#

Returns:

  • (Boolean)


352
353
354
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 352

def padding?
  4
end

#player_has_lostObject

#

player_has_lost

Invoked when the player has lost.

#


345
346
347
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 345

def player_has_lost
  player_won_or_lost :lost
end

#player_has_wonObject

#

player_has_won

Invoked when the player has won.

#


315
316
317
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 315

def player_has_won
  player_won_or_lost :won
end

#player_won_or_lost(i) ⇒ Object

#

player_won_or_lost

#


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 322

def player_won_or_lost(i)
  case i
  # ======================================================================= #
  # === :lost
  # ======================================================================= #
  when :lost
    i = LOSE_MESSAGE
  # ======================================================================= #
  # === :won
  # ======================================================================= #
  when :won
    i = WIN_MESSAGE
  end
  cliner { opn; e i }
  @status_bar.push(@status_bar_context_id, i)
  fill_up_rest
end

#populate_tableObject

#

populate_table

#


224
225
226
227
228
229
230
231
232
233
234
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 224

def populate_table
  @main_table.attach_defaults(@array_buttons[0],0,1,0,1)
  @main_table.attach_defaults(@array_buttons[1],1,2,0,1)
  @main_table.attach_defaults(@array_buttons[2],2,3,0,1)
  @main_table.attach_defaults(@array_buttons[3],0,1,1,2)
  @main_table.attach_defaults(@array_buttons[4],1,2,1,2)
  @main_table.attach_defaults(@array_buttons[5],2,3,1,2)    
  @main_table.attach_defaults(@array_buttons[6],0,1,2,3)
  @main_table.attach_defaults(@array_buttons[7],1,2,2,3)
  @main_table.attach_defaults(@array_buttons[8],2,3,2,3)
end

#randomly_set_computer_pieceObject

#

randomly_set_computer_piece

This is the “AI” right now for the game.

#


286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 286

def randomly_set_computer_piece
  loop {
    cntr = rand(9)
    case @array_buttons[cntr].label
    when 'x','o'
      # pass through
    else
      @array_buttons[cntr].label = 'o'
      @array_buttons[cntr].sensitive = false
      break
    end
  }
end

#register_keybindingsObject

#

register_keybindings

#


124
125
126
127
128
129
130
131
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 124

def register_keybindings
  # MOD1_MASK
  signal_connect(:key_press_event) { e 'Clicked key.'}
  #self.connect( Gdk::Keyval.from_name(second.to_s), Gdk::Window::MOD1_MASK, Gtk::ACCEL_VISIBLE) do
  #  the_entry.set_focus( true )
  #  the_entry.select_region( 0, -1 ) # select_region(start, )
  # end 
end

#resetObject

#

reset

#


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 91

def reset
  reset_the_internal_variables
  # ======================================================================= #
  # === @configuration
  # ======================================================================= #
  @configuration = [true, __dir__, NAMESPACE]
  # ======================================================================= #
  # === @array_storing_values
  # ======================================================================= #
  @array_storing_values = []
  # ======================================================================= #
  # === @n_clicks
  # ======================================================================= #
  @n_clicks = 0
  # ======================================================================= #
  # === @game_over
  # ======================================================================= #
  @game_over = false
  increase_font_size
end

#title?Boolean

#

title?

#

Returns:

  • (Boolean)


359
360
361
# File 'lib/games_and_rpg_paradise/gui/gtk2/simplified_tictactoe/simplified_tictactoe.rb', line 359

def title?
  TITLE
end