-
Notifications
You must be signed in to change notification settings - Fork 0
/
peg_solitaire.rb
183 lines (151 loc) · 4.15 KB
/
peg_solitaire.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
class PegSolitaire
PEG_PRESENT = 1
PEG_MISSING = 0
PEG_OUTSIDE_BOARD = -1
COLS = 7
ROWS = 7
INIT_POSITION = [3, 3].freeze
OUT_OF_BOARD_POSITIONS = [
[0, 0],
[0, 1],
[1, 0],
[1, 1],
[0, 5],
[0, 6],
[1, 5],
[1, 6],
[5, 0],
[5, 1],
[5, 5],
[5, 6],
[6, 0],
[6, 1],
[6, 5],
[6, 6]
].freeze
def initialize
@solution_found = false
@pegs_count = 32
@pegs_board = build_pegs_board
@solution = []
@movements = []
end
def call
backtracking
end
private
def move(i, j, check_i1, check_j1, check_i2, check_j2, movement)
do_move(i, j, check_i1, check_j1, check_i2, check_j2, movement)
backtracking
undo_move(i, j, check_i1, check_j1, check_i2, check_j2)
end
def do_move(i, j, check_i1, check_j1, check_i2, check_j2, movement)
@pegs_board[i][j] = PEG_MISSING
@pegs_board[check_i1][check_j1] = PEG_MISSING
@pegs_board[check_i2][check_j2] = PEG_PRESENT
@solution[@pegs_count - 1] = [i, j]
@movements[@pegs_count - 1] = movement
@pegs_count -= 1
end
def undo_move(i, j, check_i1, check_j1, check_i2, check_j2)
@pegs_board[i][j] = PEG_PRESENT
@pegs_board[check_i1][check_j1] = PEG_PRESENT
@pegs_board[check_i2][check_j2] = PEG_MISSING
@pegs_count += 1
end
def backtracking
return if @solution_found
show_solution if @pegs_count == 1
(0..6).to_a.each do |i|
(0..6).to_a.each do |j|
next unless @pegs_board[i][j] == 1
if can_move_diagonal_right_down?(i, j)
move(i, j, i + 1, j + 1, i + 2, j + 2, '↘')
end
if can_move_right?(i, j)
move(i, j, i, j + 1, i, j + 2, '→')
end
if can_move_diagonal_left_up?(i, j)
move(i, j, i - 1, j - 1, i - 2, j - 2, '↖')
end
if can_move_up?(i, j)
move(i, j, i - 1, j, i - 2, j, '↑')
end
if can_move_diagonal_right_up?(i, j)
move(i, j, i - 1, j + 1, i - 2, j + 2, '↗')
end
if can_move_down?(i, j)
move(i, j, i + 1, j, i + 2, j, '↓')
end
if can_move_left?(i, j)
move(i, j, i, j - 1, i, j - 2, '←')
end
if can_move_diagonal_left_down?(i, j)
move(i, j, i + 1, j - 1, i + 2, j - 2, '↙')
end
end
end
end
def can_move_diagonal_right_up?(i, j)
i - 2 > 0 &&
j + 2 < 7 &&
@pegs_board[i - 1][j + 1] == PEG_PRESENT &&
@pegs_board[i - 2][j + 2] == PEG_MISSING
end
def can_move_diagonal_left_up?(i, j)
i - 2 > 0 &&
j - 2 > 0 &&
@pegs_board[i - 1][j - 1] == PEG_PRESENT &&
@pegs_board[i - 2][j - 2] == PEG_MISSING
end
def can_move_diagonal_left_down?(i, j)
i + 2 < 7 &&
j - 2 > 0 &&
@pegs_board[i + 1][j - 1] == PEG_PRESENT &&
@pegs_board[i + 2][j - 2] == PEG_MISSING
end
def can_move_diagonal_right_down?(i, j)
i + 2 < 7 &&
j + 2 < 7 &&
@pegs_board[i + 1][j + 1] == PEG_PRESENT &&
@pegs_board[i + 2][j + 2] == PEG_MISSING
end
def can_move_up?(i, j)
i - 2 > 0 &&
@pegs_board[i - 1][j] == PEG_PRESENT &&
@pegs_board[i - 2][j] == PEG_MISSING
end
def can_move_right?(i, j)
j + 2 < 7 &&
@pegs_board[i][j + 1] == PEG_PRESENT &&
@pegs_board[i][j + 2] == PEG_MISSING
end
def can_move_down?(i, j)
i + 2 < 7 &&
@pegs_board[i + 1][j] == PEG_PRESENT &&
@pegs_board[i + 2][j] == PEG_MISSING
end
def can_move_left?(i, j)
j - 2 > 0 &&
@pegs_board[i][j - 1] == PEG_PRESENT &&
@pegs_board[i][j - 2] == PEG_MISSING
end
def show_solution
@solution_found = true
puts 'Solution found! -->'
@solution.compact.reverse.each_with_index do |position, index|
puts "Position: [#{position[0]}, #{position[1]}], Movement: #{@movements.compact.reverse[index]}"
end
end
def build_pegs_board
pegs_board = Array.new(ROWS) { build_row }
OUT_OF_BOARD_POSITIONS.each do |position|
pegs_board[position[0]][position[1]] = PEG_OUTSIDE_BOARD
end
pegs_board[INIT_POSITION[0]][INIT_POSITION[1]] = PEG_MISSING
pegs_board
end
def build_row
Array.new(COLS) { PEG_PRESENT }
end
end