Class: GamesAndRpgParadise::GUI::Gtk::TicTacToe::GameBoard
- Inherits:
-
Gtk::Box
- Object
- Gtk::Box
- GamesAndRpgParadise::GUI::Gtk::TicTacToe::GameBoard
- Includes:
- Gtk::BaseModule
- Defined in:
- lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb
Constant Summary collapse
- NAMESPACE =
#
NAMESPACE
#
inspect
- TITLE =
#
TITLE
#
'Game Board for Tic Tac Toe'
- WIDTH =
#
WIDTH
#
1200
- HEIGHT =
#
HEIGHT
#
500
- USE_THIS_FONT =
#
USE_THIS_FONT
#
:dejavu_condensed_20
Class Method Summary collapse
-
.run(i = ARGV) ⇒ Object
# === GamesAndRpgParadise::GUI::Gtk::TicTacToe.run ========================================================================= #.
Instance Method Summary collapse
-
#border_size? ⇒ Boolean
# === border_size? ========================================================================= #.
-
#check_the_status ⇒ Object
# === check_the_status ========================================================================= #.
-
#connect_skeleton ⇒ Object
# === connect_skeleton (connect tag) ========================================================================= #.
-
#create_skeleton ⇒ Object
# === create_skeleton (create tag) ========================================================================= #.
-
#initialize(commandline_arguments = ARGV, run_already = true) ⇒ GameBoard
constructor
# === initialize ========================================================================= #.
-
#is_good(pattern, len, this_file = FILE_USEFUL_MOVES) ⇒ Object
# === is_good ========================================================================= #.
-
#let_the_AI_make_a_move_next(player) ⇒ Object
# === let_the_AI_make_a_move_next ========================================================================= #.
-
#make_a_move(player, x, y) ⇒ Object
# === make_a_move ========================================================================= #.
-
#padding? ⇒ Boolean
# === padding? ========================================================================= #.
-
#print_board ⇒ Object
# === print_board.
-
#progress? ⇒ Boolean
(also: #progress)
# === progress? ========================================================================= #.
-
#reset ⇒ Object
# === reset (reset tag) ========================================================================= #.
-
#run ⇒ Object
# === run (run tag) ========================================================================= #.
Constructor Details
#initialize(commandline_arguments = ARGV, run_already = true) ⇒ GameBoard
#
initialize
#
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 53 def initialize( commandline_arguments = ARGV, run_already = true ) super(:vertical) reset set_commandline_arguments( commandline_arguments ) run if run_already end |
Class Method Details
.run(i = ARGV) ⇒ Object
#
GamesAndRpgParadise::GUI::Gtk::TicTacToe.run
#
300 301 302 303 304 305 306 307 308 309 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 300 def self.run( i = ARGV ) require 'gtk_paradise/run' _ = ::GamesAndRpgParadise::GUI::Gtk::TicTacToe::GameBoard.new(i) r = ::Gtk.run r << _ r.automatic_size_then_automatic_title r.top_left_then_run end |
Instance Method Details
#border_size? ⇒ Boolean
#
border_size?
#
114 115 116 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 114 def border_size? 2 end |
#check_the_status ⇒ Object
#
check_the_status
#
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 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 190 def check_the_status # 0 = ongoing # 1 = draw # 2 = win status = 1 winType = [ 0, 0, 0, 0, 0, 0, 0, 0] winType[0] = @current_status[0][0] + @current_status[0][1] + @current_status[0][2] winType[1] = @current_status[1][0] + @current_status[1][1] + @current_status[1][2] winType[2] = @current_status[2][0] + @current_status[2][1] + @current_status[2][2] winType[3] = @current_status[0][0] + @current_status[1][0] + @current_status[2][0] winType[4] = @current_status[0][1] + @current_status[1][1] + @current_status[2][1] winType[5] = @current_status[0][2] + @current_status[1][2] + @current_status[2][2] winType[6] = @current_status[0][0] + @current_status[1][1] + @current_status[2][2] winType[7] = @current_status[0][2] + @current_status[1][1] + @current_status[2][0] for i in 0...3 for j in 0...3 if @current_status[i][j] == 0 status = 0 end end end for k in 0...8 if winType[k] == 3 or winType[k] == 12 status = 2 end end return status end |
#connect_skeleton ⇒ Object
#
connect_skeleton (connect tag)
#
127 128 129 130 131 132 133 134 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 127 def connect_skeleton abort_on_exception minimal( gtk_left_aligned_label( 'Hello world! This is a test.' ) ) end |
#create_skeleton ⇒ Object
#
create_skeleton (create tag)
#
121 122 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 121 def create_skeleton end |
#is_good(pattern, len, this_file = FILE_USEFUL_MOVES) ⇒ Object
#
is_good
#
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 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 139 def is_good( pattern, len, this_file = FILE_USEFUL_MOVES ) # ======================================================================= # # The weighting scheme is as follows: # # 0 = pattern not found # 1 = pattern found and good # -1 = pattern found and bad # # ======================================================================= # result = 0 game_database_containing_the_useful_moves = File.open(this_file,'r') game_database_containing_the_useful_moves.each { |scanned| if scanned =~ /^#{pattern}/ if (scanned.length) % 2 == len result -= 1 end if (scanned.length+1) % 2 == len result += 1 end end } game_database_containing_the_useful_moves.close return result end |
#let_the_AI_make_a_move_next(player) ⇒ Object
#
let_the_AI_make_a_move_next
#
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 253 def let_the_AI_make_a_move_next(player) cloned = Marshal.load(Marshal.dump(@current_status)) move = [] for i in 0...3 for j in 0...3 if cloned[i][j] == 1 or cloned[i][j] == 4 cloned[i][j] = -1000 end end end for i in 0...3 for j in 0...3 if cloned[i][j] == 0 possibility = @progress.to_s + (3*i + j+1).to_s cloned[i][j] = is_good(possibility, player) end end end the_current_best_value = -500 for m in 0...3 for n in 0...3 if cloned[m][n] >= the_current_best_value move = [m,n] the_current_best_value = cloned[m][n] end end end return move end |
#make_a_move(player, x, y) ⇒ Object
#
make_a_move
#
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 173 def make_a_move(player, x, y) made = false valid = false if x < 4 and x > 0 and y < 4 and y > 0 valid = true end if @current_status[x-1][y-1] == 0 and valid @current_status[x-1][y-1] = @indicators[player] @progress = @progress.to_s + (3*(x-1) + y).to_s made = true end return made end |
#padding? ⇒ Boolean
#
padding?
#
107 108 109 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 107 def padding? 2 end |
#print_board ⇒ Object
#
print_board
This is mostly for debugging.
#
240 241 242 243 244 245 246 247 248 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 240 def print_board for i in 0...3 for j in 0...3 print @current_status[i][j] print " " end print "\n" end end |
#progress? ⇒ Boolean Also known as: progress
#
progress?
#
286 287 288 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 286 def progress? @progress end |
#reset ⇒ Object
#
reset (reset tag)
#
68 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 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 68 def reset reset_the_internal_variables # ======================================================================= # # === @configuration # ======================================================================= # @configuration = [true, __dir__, NAMESPACE] # ======================================================================= # # === @title # ======================================================================= # @title = TITLE # ======================================================================= # # === @width # ======================================================================= # set_width(WIDTH) # ======================================================================= # # === @height # ======================================================================= # set_height(HEIGHT) # ======================================================================= # # === @progress # ======================================================================= # @progress = nil @indicators = [1,4] # ======================================================================= # # === @current_status # ======================================================================= # @current_status = [ [0,0,0] , [0,0,0] , [0,0,0] ] # ======================================================================= # # === @progress # ======================================================================= # @progress = nil set_use_this_font(USE_THIS_FONT) use_gtk_paradise_project_css_file # or use use_project_css_file infer_the_size_automatically end |
#run ⇒ Object
#
run (run tag)
#
293 294 295 |
# File 'lib/games_and_rpg_paradise/gui/gtk3/tic_tac_toe/game_board.rb', line 293 def run create_skeleton_then_connect_skeleton end |