-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrid.rb
100 lines (83 loc) · 2 KB
/
grid.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
require 'ruby2d'
require_relative 'tile'
require_relative 'grid_helpers'
class Grid < Square
include GridHelpers
attr_accessor :tiles, :dots, :winner, :win_type, :last_occupied, :occupied_spaces, :columns_size, :heaps
attr_reader :grid_size, :x, :y, :tile_size, :margin
def initialize()
@tiles = []
@heaps = [6, 6, 6, 6, 6, 6, 6]
@columns_size = 7
@row_count = 6
@upper_bound = 7 * 6
@x = 20
@y = 400 #used to initialized layout, start low work up
@tile_size = 70
@margin = 2
@occupied_spaces = {}
@winner = nil
draw_grid
end
def update_heaps(column, amount)
heaps[column] -= amount
end
def column_height(column)
@heaps[column] - 1
end
def remove_chips_from_heap(column, amount)
removal_position = column + column_height(column) * @columns_size
amount.times do
remove_chip(removal_position)
removal_position = removal_position - @columns_size
end
update_heaps(column, amount)
end
def remove_chip(position)
# replace red dot with one matching background
draw_dot('navy', position)
end
def draw_grid
y = @y
@row_count.times do
x = @x
@columns_size.times do
@tiles << Tile.new(
x: x,
y: y,
size: @tile_size,
color: 'navy'
)
x += @tile_size + @margin
end
y -= @tile_size + @margin
end
@tiles.each_with_index do |tile, index|
draw_dot('red', index)
end
@tiles
end
def tile_center_position(tile)
x = tile.x + (tile.size / 2)
y = tile.y + (tile.size / 2)
[x, y]
end
def draw_dot(color, position)
instanciate_dot(@tiles[position], color)
end
def instanciate_dot(tile, color)
points = tile_center_position(tile)
points_x = points[0]
points_y = points[1]
tile.dot = Circle.new(
x: points_x,
y: points_y,
radius: 33,
sectors: 32,
color: color,
)
end
def winning_state?
@heaps == [0, 0, 0, 0, 0, 0, 0]
end
end