This repository was archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmswp.rb
More file actions
231 lines (188 loc) · 4.85 KB
/
mswp.rb
File metadata and controls
231 lines (188 loc) · 4.85 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# -*- coding: utf-8 -*-
require './mdarray.rb'
class MSwp
class Cell
def initialize
@inner_state = 0
@state = 0
end
def mine
@inner_state = -1
end
def setNumberOfNeighborMines(n)
@inner_state = n
end
def isMined
@inner_state == -1
end
def getNumberOfNeighborMines
@inner_state
end
def touch
@state = -1
end
def reset
@state = 0
end
def flag
@state = 1
end
def doubt
@state = 2
end
def isTouched
@state == -1
end
def isFlagged
@state == 1
end
def isDoubted
@state == 2
end
end
class GameOverException < Exception
end
class GameClearException < Exception
end
def initialize(length, nr_mines)
@map = MDArray.new(length)
@nr_mines = nr_mines
if @nr_mines >= @map.size
raise ArgumentError.new
end
@map.fill { |i| Cell.new }
@active = false
end
def isTouched(pos)
if ! @active
return
end
@map[pos].isTouched
end
def touch(pos)
if ! @active
setup(pos)
end
cell = @map[pos]
if cell.isFlagged || cell.isTouched
return
end
if cell.isMined
@active = false
raise GameOverException.new
else
cell.touch
@nr_untouched_cells -= 1
if @nr_untouched_cells == @nr_mines
raise GameClearException.new
end
if cell.getNumberOfNeighborMines == 0
__touchNeighbors(pos)
end
end
end
def toggleFlag(pos)
if ! @active
return
end
cell = @map[pos]
if cell.isTouched
return
end
if cell.isFlagged
cell.reset
@nr_flagged_cells -= 1
else
cell.flag
@nr_flagged_cells += 1
end
end
def toggleDoubt(pos)
if ! @active
return
end
cell = @map[pos]
if cell.isTouched
return
end
if cell.isDoubted
cell.reset
else
if cell.isFlagged
@nr_flagged_cells -= 1
end
cell.doubt
end
end
def touchNeighbors(pos)
if ! @active
return
end
cell = @map[pos]
if ! cell.isTouched
return
end
nr_flagged_cells = @map.neighbor8_with_index(pos).inject(0) { |sum, (neighbor, i)|
sum + (neighbor.isFlagged ? 1 : 0)
}
if nr_flagged_cells != cell.getNumberOfNeighborMines
return
end
__touchNeighbors(pos)
end
def flagNeighbors(pos)
if ! @active
return
end
cell = @map[pos]
if ! cell.isTouched
return
end
nr_untouched_cells = @map.neighbor8_with_index(pos).inject(0) { |sum, (neighbor, i)|
sum + (neighbor.isTouched ? 0 : 1)
}
if nr_untouched_cells != cell.getNumberOfNeighborMines
return
end
@map.neighbor8_with_index(pos).each { |(neighbor, i)|
if ! (neighbor.isTouched || neighbor.isFlagged)
neighbor.flag
@nr_flagged_cells += 1
end
}
end
def neighbors(pos)
@map.neighbor8_with_index(pos).map { |(neighbor, i)|
[Marshal.load(Marshal.dump(neighbor)).freeze, i]
}
end
def each
@map.each_with_index { |cell, pos|
yield(Marshal.load(Marshal.dump(cell)).freeze, pos)
}
end
attr_reader :active, :nr_mines, :nr_untouched_cells, :nr_flagged_cells
private
def setup(pos)
# 地雷の配置
(@map.all - [@map[pos]] - (@nr_mines <= @map.size - 3 ** @map.dimension ?
@map.neighbor8_with_index(pos).map { |(cell, i)| cell } :
[])).sort_by { rand }[0...@nr_mines].each { |cell| cell.mine }
# 近傍地雷数の計算
@map.each_with_index { |cell, i|
if ! cell.isMined
cell.setNumberOfNeighborMines(@map.neighbor8_with_index(i).inject(0) { |sum, (neighbor, j)|
sum += neighbor.isMined ? 1 : 0
})
end
}
@nr_untouched_cells = @map.size
@nr_flagged_cells = 0
@active = true
end
def __touchNeighbors(pos)
@map.neighbor8_with_index(pos).each { |(cell, i)|
touch(i)
}
end
end