minesweeper-cl 0.1.2 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/minesweeper +6 -2
- data/lib/game_board.rb +1 -2
- data/lib/game_tile.rb +18 -10
- data/lib/minesweeper.rb +6 -2
- data/lib/minesweeper_game.rb +22 -8
- data/lib/minesweeper_parser.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ddfac9f3e1ba4cd189b026bafc4fb5285fd32a6a
|
4
|
+
data.tar.gz: fd73a710d135c59ac590f20938d0b4456d6b49de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 152addc517e56e4b3df345d3c23a1f433e1941c9f90b7f62565ce93f3f89dd7fe640a0654497286c50718a515d5f805514764df6e1ec40700c4d6f481808027e
|
7
|
+
data.tar.gz: 2ffc81e0202a8802a77620c0f81f7b4ef938b4505bdfa90a7d01105c4562a1e4c57dbfaa9901e10bcda8c4def9495e679aaee545f95293fd1a0acbba9291b701
|
data/bin/minesweeper
CHANGED
@@ -9,9 +9,9 @@ puts "Welcome to the game of minesweeper!"
|
|
9
9
|
|
10
10
|
# Make the parser for parsing the data
|
11
11
|
begin
|
12
|
-
options = MinesweeperParser.new.parse
|
12
|
+
options = MinesweeperParser.new.parse(ARGV)
|
13
13
|
rescue OptionParser::MissingArgument, OptionParser::InvalidArgument
|
14
|
-
help_msg = MinesweeperParser.parse
|
14
|
+
help_msg = MinesweeperParser.new.parse(["-h"])
|
15
15
|
puts help_msg
|
16
16
|
end
|
17
17
|
|
@@ -29,8 +29,12 @@ while !game.game_over? && !game.win? do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
if game.game_over?
|
32
|
+
puts "\n Underlying board shown below\n"
|
33
|
+
game.print_ending_board
|
32
34
|
puts "Oops! You selected a bomb. Please play again!"
|
33
35
|
elsif game.win?
|
36
|
+
puts "\n Underlying board shown below\n"
|
37
|
+
game.print_ending_board
|
34
38
|
puts "Congrats you won the game!"
|
35
39
|
end
|
36
40
|
|
data/lib/game_board.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# GameBoard class will be used by the minesweeper game to keep track of tiles,
|
2
2
|
# bombs, and the conditions necessary to win the game.
|
3
|
-
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/")
|
4
3
|
require 'game_tile'
|
5
4
|
|
6
5
|
class GameBoard
|
@@ -16,7 +15,7 @@ class GameBoard
|
|
16
15
|
cols.times do |column|
|
17
16
|
if rand < bomb_chance
|
18
17
|
@board["(#{row}, #{column})"] = GameTile.new(self, nil, nil, nil, nil, nil, nil, nil, nil, true)
|
19
|
-
@win_value -= 1
|
18
|
+
# @win_value -= 1
|
20
19
|
else
|
21
20
|
@board["(#{row}, #{column})"] = GameTile.new(self, nil, nil, nil, nil, nil, nil, nil, nil, false)
|
22
21
|
end
|
data/lib/game_tile.rb
CHANGED
@@ -20,7 +20,7 @@ class GameTile
|
|
20
20
|
}
|
21
21
|
@is_bomb = is_bomb
|
22
22
|
@adjacent_bombs = 0
|
23
|
-
@adjacent_zeroes =
|
23
|
+
@adjacent_zeroes = []
|
24
24
|
@been_played = false
|
25
25
|
@been_flagged = false
|
26
26
|
@board = board
|
@@ -45,22 +45,30 @@ class GameTile
|
|
45
45
|
# This method will "play" all of the adjacent cells that have zero mines surrounding them.
|
46
46
|
# The minesweeper game that comes standard with most computers has this behavior.
|
47
47
|
def find_adjacent_zeroes
|
48
|
-
@adjacent.each do |key,
|
48
|
+
@adjacent.each do |key, tile|
|
49
49
|
begin
|
50
|
-
if
|
51
|
-
@adjacent_zeroes
|
50
|
+
if tile.adjacent_bombs == 0
|
51
|
+
@adjacent_zeroes << tile
|
52
52
|
end
|
53
53
|
rescue
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
58
|
+
# Recusively play all of the zero tiles around the tile that has been played.
|
59
|
+
# The recursion stops when the method reaches a tile that has adjacent bombs.
|
60
|
+
def play_adjacent_zeroes(board)
|
61
|
+
if @adjacent_bombs > 0
|
62
|
+
@been_played = true
|
63
|
+
board.num_played += 1
|
64
|
+
return
|
65
|
+
else
|
66
|
+
board.num_played += 1 unless !@been_played
|
67
|
+
@been_played = true
|
68
|
+
@adjacent.each do |key, tile|
|
69
|
+
if !tile.nil? && !tile.been_played
|
70
|
+
tile.been_played = true
|
71
|
+
tile.play_adjacent_zeroes(board)
|
64
72
|
end
|
65
73
|
end
|
66
74
|
end
|
data/lib/minesweeper.rb
CHANGED
@@ -9,9 +9,9 @@ puts "Welcome to the game of minesweeper!"
|
|
9
9
|
|
10
10
|
# Make the parser for parsing the data
|
11
11
|
begin
|
12
|
-
options = MinesweeperParser.new.parse
|
12
|
+
options = MinesweeperParser.new.parse(ARGV)
|
13
13
|
rescue OptionParser::MissingArgument, OptionParser::InvalidArgument
|
14
|
-
help_msg = MinesweeperParser.parse
|
14
|
+
help_msg = MinesweeperParser.new.parse(["-h"])
|
15
15
|
puts help_msg
|
16
16
|
end
|
17
17
|
|
@@ -29,8 +29,12 @@ while !game.game_over? && !game.win? do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
if game.game_over?
|
32
|
+
puts "\n Underlying board shown below\n"
|
33
|
+
game.print_ending_board
|
32
34
|
puts "Oops! You selected a bomb. Please play again!"
|
33
35
|
elsif game.win?
|
36
|
+
puts "\n Underlying board shown below\n"
|
37
|
+
game.print_ending_board
|
34
38
|
puts "Congrats you won the game!"
|
35
39
|
end
|
36
40
|
|
data/lib/minesweeper_game.rb
CHANGED
@@ -27,7 +27,6 @@ class MinesweeperGame
|
|
27
27
|
end
|
28
28
|
puts
|
29
29
|
end
|
30
|
-
|
31
30
|
end
|
32
31
|
|
33
32
|
def play_the_game
|
@@ -37,18 +36,18 @@ class MinesweeperGame
|
|
37
36
|
puts
|
38
37
|
player_choice = gets.chomp
|
39
38
|
|
40
|
-
if /^(help|h)\z/.match(player_choice) then print_help_message end
|
39
|
+
if /^(help|h)\z/.match(player_choice.downcase) then print_help_message end
|
41
40
|
|
42
41
|
# Now match the player response with a regular expression
|
43
|
-
while /^([pfPF]) (\d
|
42
|
+
while /^([pfPF]) (\d{1,2}), (\d{1,2})\z/.match(player_choice) == nil
|
44
43
|
puts "Please put two numbers corresponding to the row and column that you'd like to play in the form"
|
45
44
|
puts "<pf> <row>, <column>"
|
46
45
|
puts "Numbers greater than the number of rows or cols will be truncated to the max row/col"
|
47
46
|
puts
|
48
47
|
player_choice = gets.chomp
|
49
|
-
if /^(help|h)\z/.match(player_choice) then print_help_message end
|
48
|
+
if /^(help|h)\z/.match(player_choice.downcase) then print_help_message end
|
50
49
|
end
|
51
|
-
match = /^([pfPF]) (\d
|
50
|
+
match = /^([pfPF]) (\d{1,2}), (\d{1,2})\z/.match(player_choice)
|
52
51
|
row = match[2].to_i
|
53
52
|
col = match[3].to_i
|
54
53
|
|
@@ -72,15 +71,16 @@ class MinesweeperGame
|
|
72
71
|
if @board.board["(#{row}, #{column})"].is_bomb?
|
73
72
|
@game_over = true
|
74
73
|
else
|
74
|
+
@board.num_played += 1 unless @board.board["(#{row}, #{column})"].been_played || @board.board["(#{row}, #{column})"].adjacent_bombs == 0
|
75
75
|
@board.board["(#{row}, #{column})"].been_played = true
|
76
|
-
@board.num_played += 1
|
77
76
|
if @board.board["(#{row}, #{column})"].adjacent_bombs == 0
|
78
|
-
@board.board["(#{row}, #{column})"].play_adjacent_zeroes
|
77
|
+
@board.board["(#{row}, #{column})"].play_adjacent_zeroes(@board)
|
79
78
|
end
|
80
79
|
if @board.num_played == @board.win_value
|
81
80
|
@win = true
|
82
81
|
end
|
83
82
|
end
|
83
|
+
|
84
84
|
end
|
85
85
|
|
86
86
|
def flag_tile(row, column)
|
@@ -88,8 +88,8 @@ class MinesweeperGame
|
|
88
88
|
@board.board["(#{row}, #{column})"].been_flagged = false
|
89
89
|
else
|
90
90
|
@board.board["(#{row}, #{column})"].been_flagged = true
|
91
|
+
@board.num_played += 1 unless @board.board["(#{row}, #{column})"].been_played
|
91
92
|
@board.board["(#{row}, #{column})"].been_played = true
|
92
|
-
@board.num_played += 1
|
93
93
|
if @board.num_played == @board.win_value
|
94
94
|
@win = true
|
95
95
|
end
|
@@ -103,6 +103,20 @@ class MinesweeperGame
|
|
103
103
|
def win?
|
104
104
|
@win
|
105
105
|
end
|
106
|
+
|
107
|
+
def print_ending_board
|
108
|
+
@rows.times do |row|
|
109
|
+
@columns.times do |col|
|
110
|
+
if @board.board["(#{row}, #{col})"].is_bomb?
|
111
|
+
print "[ *] "
|
112
|
+
else
|
113
|
+
print "[ " + @board.board["(#{row}, #{col})"].adjacent_bombs.to_s + "] "
|
114
|
+
end
|
115
|
+
end
|
116
|
+
puts
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
106
120
|
|
107
121
|
private
|
108
122
|
def print_help_message
|
data/lib/minesweeper_parser.rb
CHANGED