-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.rb
151 lines (117 loc) · 2.87 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
require 'byebug'
require_relative 'board.rb'
require_relative 'tile.rb'
require 'yaml'
class Minesweeper
attr_accessor :board, :checked_tiles, :saved_game
def initialize
@board=Board.new
@checked_tiles = []
@saved_game = nil
end
def reveal(pos) #pos = x, y
self.board[pos].reveal
end
def flag_bomb(pos)
self.board[pos].flag
end
def save_game
saved_game = self.to_yaml #to a file
File.open("saved_game", "w") do |f|
f.puts saved_game
end
end
def load_game
puts "Do you want to load game? (Y/N) "
answer = gets.chomp.upcase
if answer == "Y"
puts "Enter file name"
file_name = gets.chomp
game = File.read(file_name)
YAML.load(game).run
end
#load a file
end
def over?
return true if won?
self.board.tiles.any? {|x| x.bombed? && x.revealed?}
end
def won?
self.board.tiles.select {|x| !x.bombed?}.all? {|y| y.revealed?}
end
def explore(tile)
return if tile.neighbor_bomb_count > 0
tile.neighbors.reject { |tile| self.checked_tiles.include?(tile) }.each do |neighbor_tile|
self.checked_tiles << neighbor_tile
neighbor_tile.revealed = true
explore(neighbor_tile)
end
end
def play_turn
input = get_input
unless input.include?("F")
pos = input.map(&:to_i)
tile = @board[pos]
check_move(tile)
self.checked_tiles << tile
tile.revealed = true
tile.flagged = false
return "You lose! " if tile.bombed?
explore(tile)
end
if input.include?("F")
pos = input[1..2].map(&:to_i)
tile = @board[pos]
check_move(tile)
tile.flagged = true
end
end
# def valid_input?(input)
# return false unless input.count.between?(2,3)
# if input.count == 2
# return false if !input[0].between?(0,self.board.grid.length-1) &&
# !input[1].between?(0,self.board.grid.length-1)
# elsif input.count == 3
# return false if !input[1].between?(0,self.board.grid.length-1) &&
# !input[2].between?(0,self.board.grid.length-1)
# else
# return false if input[0] != "F"
# end
# true
# end
def check_move(tile)
if self.checked_tiles.include?(tile)
puts "Already revealed position"
play_turn
end
end
def run
until over?
self.board.render
play_turn
end
if won?
puts "congratulations! you won!"
else
puts "you lose!"
end
end
def get_input
input = []
puts "Please enter a coordinate "
puts "Place F in front for a flag"
puts "Examples: 0,0 1,1 3,7"
puts "Examples: F,0,0 F,1,1 F,3,7"
puts "(Input S to Save Game)"
input = gets.chomp.upcase.split(",")
if input == ["S"]
save_game
exit
end
input
end
end
if $PROGRAM_NAME == __FILE__
b = Minesweeper.new.load_game
a= Minesweeper.new.run
end