-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.rb
205 lines (174 loc) · 6.06 KB
/
Minesweeper.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
require 'matrix'
class Minesweeper
def initialize(width, height, num_mines, test = nil)
@width = width
@height = height
@num_mines = num_mines
@isTest = false
@stillPlaying = true
@findAll = 0
@lastSelect = nil
#verifica se eh um teste
unless test.nil?
num_mines = test.x.length
@num_mines = num_mines
@isTest = true
end
#levanta uma exception
raise "A quantidade de minas supera o tamanho do campo. " if @num_mines > @width * @height
elements = Hash.new
#dicionario de minas
while num_mines > 0
x = rand(width) #+ 1
y = rand(height) #+ 1
#para o caso de teste
if @isTest
x,y = test.x.shift.to_i, test.y.shift.to_i
end
uid = x.to_s + "-" + y.to_s
if not elements.has_key?(uid)
#puts uid
elements[uid] = {"x"=> x, "y"=> y, "tipo"=>"bomb", "flag" =>0, "find" => 0, "show" => '.', "value" => "#"}
num_mines -= 1
end
end
#dicionario de vizinhos
#trecho que circunda o ponto da bomba, vizinhos
bombs = elements.clone #faco um clone para que seja possivel iterar
bombs.each do |key, value|
x = value["x"]
y = value["y"]
for i in x-1..x+1
for j in y-1..y+1
nuid = i.to_s + "-" + j.to_s
if i >=0 and j >=0 and i <= width-1 and j <=height-1 and not (i == x and j == y ) and not bombs.has_key?(nuid)
#elements.has_key?(nuid)?elements[nuid] += 1 : elements[nuid]=1
if elements.has_key?(nuid) and elements[nuid]["tipo"]=="neighbor"
elements[nuid] = {"x"=> i, "y"=> j, "tipo"=>"neighbor", "flag" =>0, "find" => 0, "show" => '.', "value" => elements[nuid]["value"] + 1}
else
elements[nuid] = {"x"=> i, "y"=> j, "tipo"=>"neighbor", "flag" =>0, "find" => 0, "show" => '.', "value" => 1}
end
end
end
end
end
#dicionario de Zeros, devido necessidade de flag
for i in 0..width-1
for j in 0..height-1
nuid = i.to_s + "-" + j.to_s
if not elements.has_key?(nuid)
elements[nuid]= {"x"=> i, "y"=> j, "tipo"=>"zero", "flag" =>0, "find" => 0, "show" => '.', "value" => 0}
end
end
end
@elements = elements
end
#retorna o tamanho/quantidade de campos do jogo
def quantityFields
return @elements.length
end
def board_state(input = nil)
outStruct = Struct.new(:width, :height, :elements, :board_format, :input, :stillPlaying)
out = outStruct.new
out.width = @width
out.height = @height
out.elements = @elements
out.board_format = {unknown_cell: '.', clear_cell: '', bomb: '#', flag: 'F'}
out.input = input
out.stillPlaying = still_playing
return out# @elements
end
#set flag, somente poe flag se item nao encontrado
def flag(x,y)
cell = @elements["#{x}-#{y}"]
if @stillPlaying == false
return false
elsif cell["find"] == 0
if cell["flag"] == 0
cell["flag"] = 1
cell["show"] = 'F'
else
cell["flag"] = 0
cell["show"] = '.'
end
return true
end
return false
end
#Acao de escolher uma coluna
def play(x,y)
cell = @elements["#{x}-#{y}"]
#puts cell
out = false
if @stillPlaying == false or cell["flag"]==1 or cell["find"] == 1 #nao permite selecionar uma celula com flag ou ja descoberta
return false
elsif cell["flag"]==0 and cell["tipo"] == "bomb"
@stillPlaying = false
cell["find"] = 1
@findAll +=1
cell["show"] = '#'
@lastSelect = cell["show"]
return true
elsif cell["flag"]==0 and cell["tipo"] == "zero"
cell["find"] = 1
@findAll +=1
cell["show"] = ''
#cell["value"] = ''
@lastSelect = cell["show"]
openArea(x , y)
return true
elsif cell["flag"]==0 and cell["tipo"] == "neighbor"
cell["find"] = 1
@findAll +=1
cell["show"] = cell["value"]
@lastSelect = cell["value"]
return true
end
end
def showValue
return "[#{@lastSelect}]"
end
#retorna true se o jogo acabou com vitoria
def victory
if @findAll + @num_mines >= quantityFields
return true
else
return false
end
end
def victory?
return victory
end
#retorna true (ainda em jogo), false (game over por derrota ou por vitoria)
def still_playing
if victory == true
@stillPlaying = false
end
return @stillPlaying
end
def still_playing?
return still_playing
end
#responsavel por abrir uma area de Zeros a partir do ponto selecionado
private
def openArea(x, y)
for i in x-1..x+1
for j in y-1..y+1
cell = @elements["#{i}-#{j}"]
if i >=0 and j >=0 and i <= @width-1 and j <= @height-1 and cell["find"] == 0 and cell["flag"] == 0
if cell["tipo"] == "zero"
cell["find"] = 1
@findAll +=1
cell["show"] = ''
#cell["value"] = ''
openArea(i, j)
else
cell["find"] = 1
cell["show"] = cell["value"]
@findAll +=1
end
end
end
end
end
end