Class: GamesAndRpgParadise::TicTacToe::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_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

Instance Method Summary collapse

Constructor Details

#initialize(w, h) ⇒ Board

#

initialize

#


32
33
34
35
36
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 32

def initialize(w, h)
  @w = w
  @h = h
  reset
end

Instance Attribute Details

#marginObject (readonly)

Returns the value of attribute margin.



13
14
15
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 13

def margin
  @margin
end

Instance Method Details

#bottomObject

#

bottom

#


245
246
247
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 245

def bottom
  @h - @margin
end

#bottom_box_yObject

#

bottom_box_y

#


160
161
162
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 160

def bottom_box_y
  square_at(0, 2).window_coordinates[1]
end

#check_win?Boolean

#

check_win?

#

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 74

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

#


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 180

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

#drawObject

#

draw

#


53
54
55
56
57
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 53

def draw
  draw_grid
  @squares.each(&:draw)
  draw_winning_strike
end

#draw_gridObject

#

draw_grid

#


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 103

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_strikeObject

#

draw_winning_strike

#


62
63
64
65
66
67
68
69
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 62

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?

#

Returns:

  • (Boolean)


96
97
98
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 96

def full?
  @squares.all?(&:occupied?)
end

#generate_boardObject

#

generate_board

#


167
168
169
170
171
172
173
174
175
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 167

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

#leftObject

#

left

#


231
232
233
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 231

def left
  @margin
end

#left_box_xObject

#

left_box_x

#


125
126
127
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 125

def left_box_x
  @margin
end

#mark_letter(letter, x, y) ⇒ Object

#

mark_letter

#


206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 206

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_xObject

#

middle_box_x

#


132
133
134
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 132

def middle_box_x
  square_at(1, 0).window_coordinates[0]
end

#middle_box_yObject

#

middle_box_y

#


153
154
155
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 153

def middle_box_y
  square_at(0, 1).window_coordinates[1]
end

#resetObject Also known as: clear

#

reset

#


41
42
43
44
45
46
47
48
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 41

def reset
  # === @margin
  @margin = 40
  @color = Gosu::Color.argb(0xff_666666)
  @font = Gosu::Font.new(100)
  @squares = generate_board
  @strike_points = nil
end

#rightObject

#

right

#


252
253
254
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 252

def right
  @w - @margin
end

#right_box_xObject

#

right_box_x

#


139
140
141
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 139

def right_box_x
  square_at(2, 0).window_coordinates[0]
end

#square_at(x, y) ⇒ Object

#

square_at

#


222
223
224
225
226
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 222

def square_at(x, y)
  @squares.select {|entry|
    entry.board_coordinates == [x,y]
  }.first
end

#square_sizeObject

#

square_size

#


118
119
120
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 118

def square_size
  (@w - @margin * 2) / 3
end

#topObject

#

top

#


238
239
240
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 238

def top
  @margin
end

#top_box_yObject

#

top_box_y

#


146
147
148
# File 'lib/games_and_rpg_paradise/gui/gosu/tic_tac_toe/board.rb', line 146

def top_box_y
  @margin
end