Class: GamesParadise::TicTacToe::Board
- Inherits:
-
Object
- Object
- GamesParadise::TicTacToe::Board
- Defined in:
- lib/games_paradise/gui/gosu/tic_tac_toe/board.rb
Constant Summary collapse
- WIN_CONDITIONS =
#
WIN_CONDITIONS
#
[ [ [0,0], [1,0], [2,0] ], # top row [ [0,1], [1,1], [2,1] ], # middle row [ [0,2], [1,2], [2,2] ], # bottom row [ [0,0], [0,1], [0,2] ], # left column [ [1,0], [1,1], [1,2] ], # middle column [ [2,0], [2,1], [2,2] ], # right column [ [0,0], [1,1], [2,2] ], # diagonal top left [ [2,0], [1,1], [0,2] ], # diagonal bottom left ]
Instance Attribute Summary collapse
-
#margin ⇒ Object
readonly
Returns the value of attribute margin.
Instance Method Summary collapse
-
#bottom ⇒ Object
# === bottom ========================================================================= #.
-
#bottom_box_y ⇒ Object
# === bottom_box_y ========================================================================= #.
-
#check_win? ⇒ Boolean
# === check_win? ========================================================================= #.
-
#coords_from_point(x, y) ⇒ Object
# === coords_from_point ========================================================================= #.
-
#draw ⇒ Object
# === draw ========================================================================= #.
-
#draw_grid ⇒ Object
# === draw_grid ========================================================================= #.
-
#draw_winning_strike ⇒ Object
# === draw_winning_strike ========================================================================= #.
-
#full? ⇒ Boolean
# === full? ========================================================================= #.
-
#generate_board ⇒ Object
# === generate_board ========================================================================= #.
-
#initialize(w, h) ⇒ Board
constructor
# === initialize ========================================================================= #.
-
#left ⇒ Object
# === left ========================================================================= #.
-
#left_box_x ⇒ Object
# === left_box_x ========================================================================= #.
-
#mark_letter(letter, x, y) ⇒ Object
# === mark_letter ========================================================================= #.
-
#middle_box_x ⇒ Object
# === middle_box_x ========================================================================= #.
-
#middle_box_y ⇒ Object
# === middle_box_y ========================================================================= #.
-
#reset ⇒ Object
(also: #clear)
# === reset ========================================================================= #.
-
#right ⇒ Object
# === right ========================================================================= #.
-
#right_box_x ⇒ Object
# === right_box_x ========================================================================= #.
-
#square_at(x, y) ⇒ Object
# === square_at ========================================================================= #.
-
#square_size ⇒ Object
# === square_size ========================================================================= #.
-
#top ⇒ Object
# === top ========================================================================= #.
-
#top_box_y ⇒ Object
# === top_box_y ========================================================================= #.
Constructor Details
#initialize(w, h) ⇒ Board
#
initialize
#
32 33 34 35 36 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 32 def initialize(w, h) @w = w @h = h reset end |
Instance Attribute Details
#margin ⇒ Object (readonly)
Returns the value of attribute margin.
13 14 15 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 13 def margin @margin end |
Instance Method Details
#bottom ⇒ Object
#
bottom
#
244 245 246 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 244 def bottom @h - @margin end |
#bottom_box_y ⇒ Object
#
bottom_box_y
#
159 160 161 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 159 def bottom_box_y square_at(0, 2).window_coordinates[1] end |
#check_win? ⇒ Boolean
#
check_win?
#
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 73 def check_win? winning_squares = WIN_CONDITIONS.select {|wc| squares = wc.map {|coords| square_at(*coords)} letters = squares.map(&:letter) !letters.any?(&:nil?) && letters.uniq.length == 1 }.first if winning_squares squares = winning_squares.map {|c| square_at(*c) } @strike_points = [ squares.first.center_point, squares.last.center_point ].flatten true else false end end |
#coords_from_point(x, y) ⇒ Object
#
coords_from_point
#
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 179 def coords_from_point(x, y) pct_across_x = x/@w cx = case when pct_across_x < 0.3 0 when pct_across_x > 0.6 2 else 1 end pct_across_y = y / @h cy = case when pct_across_y < 0.3 0 when pct_across_y > 0.6 2 else 1 end [cx, cy] end |
#draw ⇒ Object
#
draw
#
52 53 54 55 56 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 52 def draw draw_grid @squares.each(&:draw) draw_winning_strike end |
#draw_grid ⇒ Object
#
draw_grid
#
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 102 def draw_grid x1 = middle_box_x x2 = right_box_x y1 = middle_box_y y2 = bottom_box_y Gosu.draw_line x1, top, @color, x1, bottom, @color Gosu.draw_line x2, top, @color, x2, bottom, @color Gosu.draw_line left, y1, @color, right, y1, @color Gosu.draw_line left, y2, @color, right, y2, @color end |
#draw_winning_strike ⇒ Object
#
draw_winning_strike
#
61 62 63 64 65 66 67 68 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 61 def draw_winning_strike return unless @strike_points x1, y1, x2, y2 = @strike_points if x1 && x2 && y1 && y2 c = Gosu::Color::RED Gosu.draw_line(x1, y1, c, x2, y2, c) end end |
#full? ⇒ Boolean
#
full?
#
95 96 97 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 95 def full? @squares.all?(&:occupied?) end |
#generate_board ⇒ Object
#
generate_board
#
166 167 168 169 170 171 172 173 174 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 166 def generate_board 9.times.map { |i| bx = i % 3 by = i.div(3) wx = bx * square_size + margin wy = by * square_size + margin Square.new([bx, by], [wx, wy], square_size) } end |
#left ⇒ Object
#
left
#
230 231 232 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 230 def left @margin end |
#left_box_x ⇒ Object
#
left_box_x
#
124 125 126 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 124 def left_box_x @margin end |
#mark_letter(letter, x, y) ⇒ Object
#
mark_letter
#
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 205 def mark_letter(letter, x, y) coords = coords_from_point(x, y) cx = coords[0] cy = coords[1] sq = square_at(cx, cy) if sq.occupied? false else sq.mark_letter(letter) true end end |
#middle_box_x ⇒ Object
#
middle_box_x
#
131 132 133 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 131 def middle_box_x square_at(1, 0).window_coordinates[0] end |
#middle_box_y ⇒ Object
#
middle_box_y
#
152 153 154 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 152 def middle_box_y square_at(0, 1).window_coordinates[1] end |
#reset ⇒ Object Also known as: clear
#
reset
#
41 42 43 44 45 46 47 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 41 def reset @margin = 40 @color = Gosu::Color.argb(0xff_666666) @font = Gosu::Font.new(100) @squares = generate_board @strike_points = nil end |
#right ⇒ Object
#
right
#
251 252 253 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 251 def right @w - @margin end |
#right_box_x ⇒ Object
#
right_box_x
#
138 139 140 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 138 def right_box_x square_at(2, 0).window_coordinates[0] end |
#square_at(x, y) ⇒ Object
#
square_at
#
221 222 223 224 225 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 221 def square_at(x, y) @squares.select {|entry| entry.board_coordinates == [x,y] }.first end |
#square_size ⇒ Object
#
square_size
#
117 118 119 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 117 def square_size (@w - @margin * 2) / 3 end |
#top ⇒ Object
#
top
#
237 238 239 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 237 def top @margin end |
#top_box_y ⇒ Object
#
top_box_y
#
145 146 147 |
# File 'lib/games_paradise/gui/gosu/tic_tac_toe/board.rb', line 145 def top_box_y @margin end |