-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNard.rb
189 lines (176 loc) · 5.2 KB
/
Nard.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
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
class Nard
attr_reader :selected_index
attr_writer :selected_index
def initialize(window, side)
@selected_index = -1
@side = side
if side == 1
@nard = Gosu::Image.new(window, "images/white-nard3.png", true, 0, 0, 35, 35)
position_x = 13
position_y = 2
else
@nard = Gosu::Image.new(window, "images/black-nard.png", true, 0, 0, 35, 35)
position_x = 12
position_y = 1
end
@nard_x_array = {}
@nard_y_array = {}
@nard_lap_array = {}
@nard_position_x_array = {}
@nard_position_y_array = {}
15.times { |i|
@nard_x_array[i] = get_x_position(position_x)
@nard_y_array[i] = get_y_position(position_x, position_y)
@nard_lap_array[i] = 4
@nard_position_x_array[i] = position_x
@nard_position_y_array[i] = position_y
}
end
def draw
15.times{ |i|
@nard.draw(@nard_x_array[i], @nard_y_array[i], @nard_y_array[i])
}
end
def get_x_position(position)
if position > 6
x = OFFSET_LEFT + 25
(position).times{x += 41}
else
x = OFFSET_LEFT + 3
(position - 1).times{x += 41}
end
if position == 13
x += 38
end
return x
end
def get_y_position(position_x, position_y)
if position_y == 1
y = OFFSET_TOP
get_count_nards_on_position(position_x, position_y).times{y = y + 15}
else
y = WINDOW_SIZE_Y - OFFSET_BOTTOM * 3
get_count_nards_on_position(position_x, position_y).times{y = y - 15}
end
return y
end
def get_count_nards_on_position(position_x, position_y)
count = 0
@nard_y_array.size.times { |i|
count = count + 1 if @nard_position_x_array[i] == position_x && @nard_position_y_array[i] == position_y
}
return count
end
def select_nard (position_x, position_y)
@selected_index = -1
if position_y == 1
y = 0
else
y = 999
end
15.times{
|i|
if @nard_position_y_array[i] == position_y && @nard_position_x_array[i] == position_x
if position_y == 2
if y > @nard_y_array[i]
@selected_index = i
y = @nard_y_array[i]
end
else
if y < @nard_y_array[i]
@selected_index = i
y = @nard_y_array[i]
end
end
end
}
end
def selected_nard?
@selected_index != -1
end
def move_selected_to_position(position_x, position_y, index = false)
unless index
if @nard_position_x_array[@selected_index] == position_x && @nard_position_y_array[@selected_index] == position_y
return false
else
@nard_x_array[@selected_index] = get_x_position(position_x)
@nard_y_array[@selected_index] = get_y_position(position_x, position_y)
@nard_position_x_array[@selected_index] = position_x
if position_y != @nard_position_y_array[@selected_index]
@nard_position_y_array[@selected_index] = position_y
@nard_lap_array[@selected_index] += 1
end
if position_y == 1
recalculate_coords(position_x, position_y)
end
return true
end
else
@nard_x_array[index] = get_x_position(position_x)
@nard_y_array[index] = get_y_position(position_x, position_y)
@nard_position_x_array[index] = position_x
@nard_position_y_array[index] = position_y
if position_y == 1
recalculate_coords(position_x, position_y)
end
true
end
end
def recalculate_coords(position_x, position_y)
y = OFFSET_TOP
15.times{
|i|
if @nard_position_x_array[i] == position_x && @nard_position_y_array == position_y
@nard_y_array[i] = y
y = y + 15
end
}
end
def all_in_home?
if @side == 1
@nard_position_y_array.each{|key, value| return false if value == 1}
else
@nard_position_y_array.each{|key, value| return false if value == 2}
end
@nard_position_x_array.each{|key, value| return false if value < 7}
@nard_lap_array.each{|key, value| return false if value % 2 != 0}
return true
end
def win?
@nard_position_x_array.each{|key, value| return false if value != 13}
return true
end
def can_move_to_position(position_x, position_y, count_movement)
if @nard_position_y_array[@selected_index] == 1
count_movement.each{ |value|
if position_y == 1
if @nard_position_x_array[@selected_index] - value == position_x
count_movement.delete_at count_movement.find_index value
return true
end
else
if @nard_position_x_array[@selected_index] - 1 == value - position_x
count_movement.delete_at count_movement.find_index value
return true
end
end
}
else
count_movement.each{ |value|
if position_y == 2
if @nard_position_x_array[@selected_index] + value == position_x
count_movement.delete_at count_movement.find_index value
return true
end
else
temp = 12 - @nard_position_x_array[@selected_index]
if 12 - value + temp + 1 == position_x
count_movement.delete_at count_movement.find_index value
return true
end
end
}
end
return false
end
end