minesweeper-cl 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85de14a45adbabf9db52ef0e26a84edb2d1ef8f4
4
- data.tar.gz: bbdc5313b92f2e82c0fa41110f12b86beb872a9a
3
+ metadata.gz: c4c480ca41fdede1f0ab25f66f88d2f6b4f023b6
4
+ data.tar.gz: 17dff4c4323f1f26723b0cb58a48bbb40276f644
5
5
  SHA512:
6
- metadata.gz: 60a4364bfd95d6f17eef069117938e559eb5bf299857a37a65e591f6048e43a9271cfe860a1a6f2100ca2492f03cbed301651b613736f1e50eb2299ca2c33ccb
7
- data.tar.gz: 4d5013d5935cef2cc1e5b250cdbaae474ae5ce6bc4c9a52441946174a287427f3eab5d64014f5a615dc0db37e49f006f52ba3f32a1dae8f9e48f19c7ab442ec0
6
+ metadata.gz: e81ce953573f91a17a9854e2552a5b9018b906b29c50a5b66b71b5ddf58d24f6dfcc5327fb9d5e08808ae4a33c9bff7d8d4da912753c2aea8005be41aac35ee8
7
+ data.tar.gz: 70b6a22055d4974f1192a877337716e0aaf464c86b4404c9c30adda72a9de425eb473293c28964c7c7e7f06c75852f07afd116cd794ab49907e2383fd5d9d330
data/bin/minesweeper CHANGED
@@ -1,48 +1,27 @@
1
1
  #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/")
2
3
  require 'game_tile'
3
4
  require 'minesweeper_game'
4
5
  require 'game_board'
6
+ require 'minesweeper_parser'
5
7
 
6
8
  puts "Welcome to the game of minesweeper!"
7
- puts "-----------------------------------"
8
- puts "Please enter the amount of rows that you would like to play with, anything more than 20 will be truncated."
9
- rows = gets.chomp
10
9
 
11
- while /^(\d){1,2}\z/.match(rows) == nil do
12
- puts "Please enter the amount of rows that you would like to play with, anything more than 20 will be truncated."
13
- rows = gets.chomp
10
+ # Make the parser for parsing the data
11
+ begin
12
+ options = MinesweeperParser.new.parse!(ARGV)
13
+ rescue OptionParser::MissingArgument, OptionParser::InvalidArgument
14
+ help_msg = MinesweeperParser.parse!(["-h"])
15
+ puts help_msg
14
16
  end
15
17
 
16
- # Ternary statement to truncate the rows if value entered is greater than 20
17
- if rows.to_i > 20 then rows = 20 else rows = rows.to_i end
18
-
19
- puts "Please enter the amount of cols that you would like to play with, anything more than 20 will be truncated."
20
- cols = gets.chomp
21
-
22
- while /^(\d){1,2}\z/.match(cols) == nil do
23
- puts "Please enter the amount of cols that you would like to play with, anything more than 20 will be truncated."
24
- cols = gets.chomp
25
- end
26
-
27
- # Ternary statement to truncate the cols if value entered is greater than 20
28
- if cols.to_i > 20 then cols = 20 else cols = cols.to_i end
29
-
30
- puts "Please enter the % chance you want for a bomb to occur"
31
- puts "This number needs to be between 0 and 1"
32
- bomb_chance = gets.chomp.to_f
33
-
34
- while !bomb_chance.is_a?(Float) || bomb_chance > 1.0 || bomb_chance < 0.0 do
35
- puts "Please enter the % chance you want for a bomb to occur"
36
- puts "This number needs to be between 0 and 1"
37
- bomb_chance = gets.chomp.to_f
38
- end
39
-
40
- # Create the board
41
- board = GameBoard.new(rows, cols, bomb_chance)
18
+ # Create the board. We can now use the arguments that have been handled by the arguments
19
+ # parser class. The arguments have been stored in a hash in an OpenStruct object.
20
+ board = GameBoard.new(options.rows_cols_chance[:rows], options.rows_cols_chance[:cols], options.rows_cols_chance[:bomb_chance])
42
21
 
43
22
 
44
23
  # Use the MinesweeperGame class to play the game
45
- game = MinesweeperGame.new(board, rows, cols)
24
+ game = MinesweeperGame.new(board, options.rows_cols_chance[:rows], options.rows_cols_chance[:cols])
46
25
 
47
26
  game.print_the_board
48
27
  while !game.game_over? && !game.win? do
data/lib/game_board.rb CHANGED
@@ -1,5 +1,6 @@
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__)}/")
3
4
  require 'game_tile'
4
5
 
5
6
  class GameBoard
data/lib/minesweeper.rb CHANGED
@@ -1,48 +1,27 @@
1
1
  #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/")
2
3
  require 'game_tile'
3
4
  require 'minesweeper_game'
4
5
  require 'game_board'
6
+ require 'minesweeper_parser'
5
7
 
6
8
  puts "Welcome to the game of minesweeper!"
7
- puts "-----------------------------------"
8
- puts "Please enter the amount of rows that you would like to play with, anything more than 20 will be truncated."
9
- rows = gets.chomp
10
9
 
11
- while /^(\d){1,2}\z/.match(rows) == nil do
12
- puts "Please enter the amount of rows that you would like to play with, anything more than 20 will be truncated."
13
- rows = gets.chomp
10
+ # Make the parser for parsing the data
11
+ begin
12
+ options = MinesweeperParser.new.parse!(ARGV)
13
+ rescue OptionParser::MissingArgument, OptionParser::InvalidArgument
14
+ help_msg = MinesweeperParser.parse!(["-h"])
15
+ puts help_msg
14
16
  end
15
17
 
16
- # Ternary statement to truncate the rows if value entered is greater than 20
17
- if rows.to_i > 20 then rows = 20 else rows = rows.to_i end
18
-
19
- puts "Please enter the amount of cols that you would like to play with, anything more than 20 will be truncated."
20
- cols = gets.chomp
21
-
22
- while /^(\d){1,2}\z/.match(cols) == nil do
23
- puts "Please enter the amount of cols that you would like to play with, anything more than 20 will be truncated."
24
- cols = gets.chomp
25
- end
26
-
27
- # Ternary statement to truncate the cols if value entered is greater than 20
28
- if cols.to_i > 20 then cols = 20 else cols = cols.to_i end
29
-
30
- puts "Please enter the % chance you want for a bomb to occur"
31
- puts "This number needs to be between 0 and 1"
32
- bomb_chance = gets.chomp.to_f
33
-
34
- while !bomb_chance.is_a?(Float) || bomb_chance > 1.0 || bomb_chance < 0.0 do
35
- puts "Please enter the % chance you want for a bomb to occur"
36
- puts "This number needs to be between 0 and 1"
37
- bomb_chance = gets.chomp.to_f
38
- end
39
-
40
- # Create the board
41
- board = GameBoard.new(rows, cols, bomb_chance)
18
+ # Create the board. We can now use the arguments that have been handled by the arguments
19
+ # parser class. The arguments have been stored in a hash in an OpenStruct object.
20
+ board = GameBoard.new(options.rows_cols_chance[:rows], options.rows_cols_chance[:cols], options.rows_cols_chance[:bomb_chance])
42
21
 
43
22
 
44
23
  # Use the MinesweeperGame class to play the game
45
- game = MinesweeperGame.new(board, rows, cols)
24
+ game = MinesweeperGame.new(board, options.rows_cols_chance[:rows], options.rows_cols_chance[:cols])
46
25
 
47
26
  game.print_the_board
48
27
  while !game.game_over? && !game.win? do
@@ -0,0 +1,51 @@
1
+ require 'optparse'
2
+ require 'ostruct'
3
+ require 'pp'
4
+
5
+ # Include this line in order to show a help message if there are no args
6
+ ARGV << "-h" if ARGV.empty?
7
+
8
+ class MinesweeperParser
9
+ def initialize
10
+ end
11
+
12
+ def parse!(args)
13
+ options = OpenStruct.new
14
+ options.rows_cols_chance = {}
15
+ options.inplace = false
16
+ options.encoding = "utf8"
17
+ options.transfer_type = :auto
18
+ options.verbose = false
19
+
20
+ # Here is where we make the options parser
21
+ opt_parser = OptionParser.new do |opts|
22
+ opts.banner = "Usage: minesweeper [options]"
23
+
24
+ opts.on("-r ROWS", "--rows ROWS", Integer, "Specify the number of rows that you'd like your game to have. Truncated at 20.") do |rows|
25
+ if rows > 20 then rows = 20 elsif rows < 1 then rows = 1 end
26
+ options.rows_cols_chance[:rows] = rows
27
+ end
28
+
29
+ opts.on("-c COLUMNS", "--columns COLUMNS", Integer, "Specify the number of columns that you'd like your game to have. Truncated at 20.") do |cols|
30
+ if cols > 20 then cols = 20 elsif cols < 1 then cols = 1 end
31
+ options.rows_cols_chance[:cols] = cols
32
+ end
33
+
34
+ opts.on("-f BOMB_CHANCE", "--frequency BOMB_CHANCE", Float, "Specify frequency that bombs will appear on your board. Must be [0, 1].") do |chance|
35
+ if chance > 1.0 then chance = 1.0 elsif chance < 0.0 then chance = 0.0 end
36
+ options.rows_cols_chance[:bomb_chance] = chance
37
+ end
38
+
39
+ opts.on_tail("-h", "--help", "Show this message") do
40
+ puts opts
41
+ exit
42
+ end
43
+
44
+
45
+ end
46
+
47
+ opt_parser.parse!(args)
48
+ options
49
+ end
50
+ end
51
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minesweeper-cl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - gr1zzly_be4r
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-23 00:00:00.000000000 Z
11
+ date: 2015-02-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Minesweeper, ported to the command line. Please report any issues to
14
14
  the repo on github and star if you like it!
@@ -22,6 +22,7 @@ files:
22
22
  - "./lib/game_tile.rb"
23
23
  - "./lib/minesweeper.rb"
24
24
  - "./lib/minesweeper_game.rb"
25
+ - "./lib/minesweeper_parser.rb"
25
26
  - bin/minesweeper
26
27
  homepage: https://github.com/gr1zzly-be4r/ruby-minesweeper-cli
27
28
  licenses: