codebreaker_artem 0.1.2 → 0.2.0

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: 32e2161da8a38b5778969f48ca3f4636bf9ac9b3
4
- data.tar.gz: aa01c2a39f9ace6dbe1c5620a28499f15c4550ee
3
+ metadata.gz: f6f9c76b7528af7bfa95a034eba2f812cd52de8f
4
+ data.tar.gz: e98a42641fe37f68b43c78b84fb4a421a4089137
5
5
  SHA512:
6
- metadata.gz: 22265ea1bb7e523f1bb015c4afe0a88513aa6e1ea67c1d01cf1e925da3ddd812e1463e713f9c33f7c4e0ab3afd013a0068215619156ee3e0d7afcb42951944bb
7
- data.tar.gz: 1e0a10cd4167681fa3e5c367abc7b26892573e4db9f07b942b84c8316912b13cd292b47c437a53f9174dced338f492887f1302ad6dc12cbac61951df4471ab1a
6
+ metadata.gz: 64933c5dbd38c054136f3f188508056bbab922fb2b0ce7e635f835e4a120a08544da2be3a6946f5f0be8c197bc7ad2a7a4d0efcea4085453b1fa402436c235fb
7
+ data.tar.gz: f446aca616a3b05fa63e1e908ef643bb8f31854715b13bef64a88e7ceec84c2d711305b8f1fab13e465b1f1cc185ba97c08ee6149f65fe132f73ef41635e5594
@@ -1,7 +1,12 @@
1
+ require_relative 'game_utils'
2
+
1
3
  module CodebreakerArtem
2
4
  class Game
5
+ include GameUtils
3
6
  MAX_GUESS_NUMBER = 10
4
7
 
8
+ attr_reader :secret_code
9
+
5
10
  def initialize
6
11
  initial_values_set
7
12
  end
@@ -9,7 +14,7 @@ module CodebreakerArtem
9
14
  def play
10
15
  start
11
16
  loop do
12
- print "Please enter your guess no. #{@guess_count}: "
17
+ guess_prompt
13
18
  mark_guess(submit_guess)
14
19
  return lose if @guess_count >= MAX_GUESS_NUMBER
15
20
  end
@@ -35,24 +40,6 @@ module CodebreakerArtem
35
40
  4.times { @secret_code << rand(1..6).to_s }
36
41
  end
37
42
 
38
- def welcome_msg
39
- print "Welcome to the Codebreaker Game!\n" \
40
- "New secret code has just been generated\n" \
41
- "You have #{MAX_GUESS_NUMBER} attempts\n" \
42
- "Type 'hint' to take a hint\n" \
43
- "Type 'exit' to exit the game\n\n"
44
- end
45
-
46
- def submit_guess
47
- guess = $stdin.gets.chomp
48
- exit?(guess)
49
- return false if take_hint(guess)
50
- return wrong_code_length unless guess.length == 4
51
- return wrong_code_numbers unless guess =~ /[1-6]{4}/
52
- @guess_count += 1
53
- guess
54
- end
55
-
56
43
  def mark_guess(guess)
57
44
  return false unless guess
58
45
  counts = plus_minus_count(guess)
@@ -81,89 +68,25 @@ module CodebreakerArtem
81
68
  (MAX_GUESS_NUMBER - @guess_count + 1)
82
69
  end
83
70
 
71
+ def hint_available?
72
+ return one_hint_only unless @hint_available
73
+ true
74
+ end
75
+
84
76
  def win
85
- puts "\nCONGRATULATIONS! You've won the Codebreaker game!"
77
+ win_msg
86
78
  finish_game
87
79
  end
88
80
 
89
81
  def lose
90
- puts "You've used all your attempts (#{MAX_GUESS_NUMBER})" \
91
- " and LOST THE GAME\n"
82
+ lose_msg
92
83
  finish_game
93
84
  end
94
85
 
95
86
  def finish_game
96
- puts "The secret code was #{@secret_code}\n"
87
+ reveal_code
97
88
  save_score
98
89
  play_again
99
90
  end
100
-
101
- def save_score
102
- return false unless yes? { 'Do you want to save your score? (y/n): ' }
103
- begin
104
- Dir.mkdir('./score') unless File.exist?('./score')
105
- file = File.new('./score/score.txt', 'a')
106
- rescue
107
- puts "Can't create file './score/score.txt'"
108
- return
109
- end
110
- write_score_to_file file
111
- file.close
112
- end
113
-
114
- def write_score_to_file(file)
115
- print 'Please enter your name: '
116
- name = $stdin.gets.chomp
117
- file << "Name: #{name}\n"
118
- file << "Time: #{Time.now}\n"
119
- file << "Score: #{@score}\n\n"
120
- puts "Your data was added to a file #{file.path}"
121
- end
122
-
123
- def take_hint(input = nil)
124
- return false unless input =~ /hint/i
125
- return false unless hint_available?
126
- position = @numbers_guess_count.index(@numbers_guess_count.min)
127
- secret_number = @secret_code[position]
128
- puts "HINT: Number #{secret_number} is in position #{position + 1}"
129
- @hint_available = false
130
- secret_number
131
- end
132
-
133
- def hint_available?
134
- return puts 'You have only one hint' unless @hint_available
135
- true
136
- end
137
-
138
- def play_again
139
- exit unless yes? { 'Would you like to play one more time? (y/n): ' }
140
- start
141
- end
142
-
143
- def exit?(input)
144
- exit if input =~ /exit/
145
- false
146
- end
147
-
148
- def yes?
149
- print yield if block_given?
150
- loop do
151
- answer = $stdin.gets.chomp
152
- exit?(answer)
153
- return false if answer =~ /^n/i
154
- return true if answer =~ /^y/i
155
- puts "Please enter 'y' or 'n': "
156
- end
157
- end
158
-
159
- def wrong_code_length
160
- puts 'Code contains 4 numbers, please try again'
161
- false
162
- end
163
-
164
- def wrong_code_numbers
165
- puts 'All numbers are between 1 and 6, please try again'
166
- false
167
- end
168
91
  end
169
92
  end
@@ -0,0 +1,107 @@
1
+ require_relative 'game'
2
+
3
+ module GameUtils
4
+ include CodebreakerArtem
5
+
6
+ def welcome_msg
7
+ print "Welcome to the Codebreaker Game!\n" \
8
+ "New secret code has just been generated\n" \
9
+ "You have #{Game::MAX_GUESS_NUMBER} attempts\n" \
10
+ "Type 'hint' to take a hint\n" \
11
+ "Type 'exit' to exit the game\n\n"
12
+ end
13
+
14
+ def guess_prompt
15
+ print "Please enter your guess no. #{@guess_count}: "
16
+ end
17
+
18
+ def submit_guess
19
+ guess = $stdin.gets.chomp
20
+ exit?(guess)
21
+ return false if take_hint(guess)
22
+ return wrong_code_length unless guess.length == 4
23
+ return wrong_code_numbers unless guess =~ /[1-6]{4}/
24
+ @guess_count += 1
25
+ guess
26
+ end
27
+
28
+ def save_score
29
+ return false unless yes? { 'Do you want to save your score? (y/n): ' }
30
+ begin
31
+ Dir.mkdir('./score') unless File.exist?('./score')
32
+ file = File.new('./score/score.txt', 'a')
33
+ rescue
34
+ puts "Can't create file './score/score.txt'"
35
+ return
36
+ end
37
+ write_score_to_file file
38
+ file.close
39
+ end
40
+
41
+ def write_score_to_file(file)
42
+ print 'Please enter your name: '
43
+ name = $stdin.gets.chomp
44
+ file << "Name: #{name}\n"
45
+ file << "Time: #{Time.now}\n"
46
+ file << "Score: #{@score}\n\n"
47
+ puts "Your data was added to a file #{file.path}"
48
+ end
49
+
50
+ def take_hint(input = nil)
51
+ return false unless input =~ /hint/i
52
+ return false unless hint_available?
53
+ position = @numbers_guess_count.index(@numbers_guess_count.min)
54
+ secret_number = @secret_code[position]
55
+ puts "HINT: Number #{secret_number} is in position #{position + 1}"
56
+ @hint_available = false
57
+ secret_number
58
+ end
59
+
60
+ def one_hint_only
61
+ puts 'You have only one hint'
62
+ end
63
+
64
+ def play_again
65
+ exit unless yes? { 'Would you like to play one more time? (y/n): ' }
66
+ start
67
+ end
68
+
69
+ def exit?(input)
70
+ exit if input =~ /exit/
71
+ false
72
+ end
73
+
74
+ def yes?
75
+ print yield if block_given?
76
+ loop do
77
+ answer = $stdin.gets.chomp
78
+ exit?(answer)
79
+ return false if answer =~ /^n/i
80
+ return true if answer =~ /^y/i
81
+ puts "Please enter 'y' or 'n': "
82
+ end
83
+ end
84
+
85
+ def wrong_code_length
86
+ puts 'Code contains 4 numbers, please try again'
87
+ false
88
+ end
89
+
90
+ def wrong_code_numbers
91
+ puts 'All numbers are between 1 and 6, please try again'
92
+ false
93
+ end
94
+
95
+ def win_msg
96
+ puts "\nCONGRATULATIONS! You've won the Codebreaker game!"
97
+ end
98
+
99
+ def lose_msg
100
+ puts "You've used all your attempts (#{Game::MAX_GUESS_NUMBER})" \
101
+ " and LOST THE GAME\n"
102
+ end
103
+
104
+ def reveal_code
105
+ puts "The secret code was #{@secret_code}\n"
106
+ end
107
+ end
@@ -1,3 +1,3 @@
1
1
  module CodebreakerArtem
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codebreaker_artem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - b-artem
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-07 00:00:00.000000000 Z
11
+ date: 2017-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,6 +75,7 @@ files:
75
75
  - codebreaker_artem.gemspec
76
76
  - lib/codebreaker_artem.rb
77
77
  - lib/codebreaker_artem/game.rb
78
+ - lib/codebreaker_artem/game_utils.rb
78
79
  - lib/codebreaker_artem/version.rb
79
80
  homepage: https://github.com/b-artem/codebreaker_artem
80
81
  licenses: