ep-codebreaker 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 +4 -4
- data/lib/ep-codebreaker/console_app.rb +4 -4
- data/lib/ep-codebreaker/game.rb +27 -11
- data/lib/ep-codebreaker/messages.rb +23 -15
- data/lib/ep-codebreaker/player.rb +9 -6
- data/lib/ep-codebreaker/version.rb +1 -1
- data/lib/ep-codebreaker.rb +0 -12
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: de0fd41f89af6ea0ec1d47b403970f9b78f16c6f
|
|
4
|
+
data.tar.gz: d04cb5e91d335f0bdc0f79174abd0caebef73b91
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5bdf87c0a1ef1d5f0ee230b2943dc86164c0b2155d34c18be704e77cc12153fba56dce4d73ef06310a05eeb2fd578a031bd4beb6e6cdfa6e4b8f59b7227501a4
|
|
7
|
+
data.tar.gz: eac26be24f7a587464eddc775ac31f8409985e1286c865ee9ebaf75f47acc5060ad3708ec578f3a3db3ebedce08e8ba1433919c46b94584aa2a1b4ee658023a2
|
|
@@ -38,14 +38,14 @@ module Codebreaker
|
|
|
38
38
|
input = gets.chomp
|
|
39
39
|
|
|
40
40
|
case input
|
|
41
|
-
when '1' then
|
|
41
|
+
when '1' then Messages.hint(@game.hint, @game.hints_left, @game.tries_left)
|
|
42
42
|
when '0' then return
|
|
43
43
|
else check_guess(input)
|
|
44
44
|
end
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
if @game.won?
|
|
48
|
-
Messages.win(@game.answer
|
|
48
|
+
Messages.win(@game.answer)
|
|
49
49
|
save_score
|
|
50
50
|
else
|
|
51
51
|
Messages.lose(@game.answer)
|
|
@@ -59,7 +59,7 @@ module Codebreaker
|
|
|
59
59
|
|
|
60
60
|
scores.map
|
|
61
61
|
.with_index { |player, i| format("%2i #{player.formatted}", i + 1) }
|
|
62
|
-
.unshift(format('%2s %10s %
|
|
62
|
+
.unshift(format('%2s %10s %6s', '', 'Name', 'Points'))
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
def check_guess(input)
|
|
@@ -77,7 +77,7 @@ module Codebreaker
|
|
|
77
77
|
begin
|
|
78
78
|
print 'Type your name: '
|
|
79
79
|
@game.save_score(gets.chomp)
|
|
80
|
-
puts 'Your score was saved'
|
|
80
|
+
puts 'Your score was saved.'
|
|
81
81
|
rescue ArgumentError => ex
|
|
82
82
|
puts ex.message, "\n"
|
|
83
83
|
retry
|
data/lib/ep-codebreaker/game.rb
CHANGED
|
@@ -2,12 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
module Codebreaker
|
|
4
4
|
class Game
|
|
5
|
-
|
|
5
|
+
MIN = 1
|
|
6
|
+
MAX = 6
|
|
7
|
+
LENGTH = 4
|
|
8
|
+
|
|
9
|
+
REGEXP = Regexp.new("^[#{MIN}-#{MAX}]{#{LENGTH}}$")
|
|
10
|
+
|
|
11
|
+
TRIES = 10
|
|
12
|
+
HINTS = 3
|
|
13
|
+
|
|
14
|
+
private_constant :MIN, :MAX, :LENGTH, :REGEXP, :TRIES, :HINTS
|
|
15
|
+
attr_reader :tries_left, :hints_left
|
|
6
16
|
|
|
7
17
|
def start
|
|
8
18
|
@code = Array.new(LENGTH) { rand(MIN..MAX) }.join
|
|
9
|
-
@
|
|
19
|
+
@array_for_hints = @code.chars
|
|
20
|
+
|
|
10
21
|
@tries_left = TRIES
|
|
22
|
+
@hints_left = HINTS
|
|
23
|
+
|
|
11
24
|
@finished = false
|
|
12
25
|
@won = false
|
|
13
26
|
end
|
|
@@ -22,8 +35,15 @@ module Codebreaker
|
|
|
22
35
|
result
|
|
23
36
|
end
|
|
24
37
|
|
|
25
|
-
def
|
|
26
|
-
@
|
|
38
|
+
def hint
|
|
39
|
+
return false if @hints_left.zero?
|
|
40
|
+
|
|
41
|
+
@tries_left -= 1
|
|
42
|
+
@hints_left -= 1
|
|
43
|
+
define_stage
|
|
44
|
+
|
|
45
|
+
index = rand(@array_for_hints.length)
|
|
46
|
+
@array_for_hints.delete_at(index)
|
|
27
47
|
end
|
|
28
48
|
|
|
29
49
|
def finished?
|
|
@@ -34,20 +54,16 @@ module Codebreaker
|
|
|
34
54
|
@won
|
|
35
55
|
end
|
|
36
56
|
|
|
37
|
-
def tries
|
|
38
|
-
TRIES - @tries_left
|
|
39
|
-
end
|
|
40
|
-
|
|
41
57
|
def answer
|
|
42
58
|
@code if finished?
|
|
43
59
|
end
|
|
44
60
|
|
|
45
61
|
def save_score(name, file_name = 'scores.yml')
|
|
46
|
-
player = Player.new(name,
|
|
62
|
+
player = Player.new(name, @tries_left, @hints_left)
|
|
47
63
|
|
|
48
64
|
scores = process_file(file_name)
|
|
49
65
|
scores << player
|
|
50
|
-
scores = scores.
|
|
66
|
+
scores = scores.max_by(10, &:points)
|
|
51
67
|
|
|
52
68
|
File.open(file_name, 'w') { |f| f.write scores.to_yaml }
|
|
53
69
|
end
|
|
@@ -71,7 +87,7 @@ module Codebreaker
|
|
|
71
87
|
('+' * num_of_pluses) + ('-' * num_of_minuses)
|
|
72
88
|
end
|
|
73
89
|
|
|
74
|
-
def define_stage(result)
|
|
90
|
+
def define_stage(result = '')
|
|
75
91
|
if result == ('+' * LENGTH)
|
|
76
92
|
@finished = true
|
|
77
93
|
@won = true
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Codebreaker
|
|
4
|
-
|
|
4
|
+
class Messages
|
|
5
5
|
class << self
|
|
6
6
|
def logo
|
|
7
7
|
puts " ]]]] ]]]] ]]]]] ]]]]] \n" \
|
|
@@ -24,31 +24,39 @@ module Codebreaker
|
|
|
24
24
|
|
|
25
25
|
def run
|
|
26
26
|
puts "\n" \
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
"1 - play \n" \
|
|
28
|
+
"2 - high scores \n" \
|
|
29
|
+
"0 - exit \n" \
|
|
30
|
+
"\n"
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
def play
|
|
34
34
|
puts "Let's go! \n" \
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
"\n" \
|
|
36
|
+
"1 - hint \n" \
|
|
37
|
+
"0 - end \n" \
|
|
38
|
+
"\n"
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
def win(answer
|
|
42
|
-
puts 'Congratulations! You are the winner!'
|
|
43
|
-
puts "It was #{answer} and you did it in #{tries} tries", "\n"
|
|
41
|
+
def win(answer)
|
|
42
|
+
puts 'Congratulations! You are the winner!', "It was #{answer}.", "\n"
|
|
44
43
|
end
|
|
45
44
|
|
|
46
45
|
def lose(answer)
|
|
47
|
-
puts 'Sorry, you lose(((', "It was #{answer}"
|
|
46
|
+
puts 'Sorry, you lose(((', "It was #{answer}.", "\n"
|
|
48
47
|
end
|
|
49
48
|
|
|
50
49
|
def tries_left(tries_left)
|
|
51
|
-
puts "#{tries_left} tries left", "\n"
|
|
50
|
+
puts "#{tries_left} tries left.", "\n"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def hint(hint, hints_left, tries_left)
|
|
54
|
+
if hint
|
|
55
|
+
puts "Secret code contains #{hint}.", "#{hints_left} hints_left."
|
|
56
|
+
tries_left(tries_left)
|
|
57
|
+
else
|
|
58
|
+
puts 'No hints left.', "\n"
|
|
59
|
+
end
|
|
52
60
|
end
|
|
53
61
|
|
|
54
62
|
def bye
|
|
@@ -56,7 +64,7 @@ module Codebreaker
|
|
|
56
64
|
end
|
|
57
65
|
|
|
58
66
|
def wrong_option
|
|
59
|
-
puts 'Wrong option'
|
|
67
|
+
puts 'Wrong option!'
|
|
60
68
|
end
|
|
61
69
|
end
|
|
62
70
|
end
|
|
@@ -2,25 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
module Codebreaker
|
|
4
4
|
class Player
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def initialize(name, tries)
|
|
5
|
+
def initialize(name, tries_left, hints_left)
|
|
8
6
|
self.name = name
|
|
9
|
-
@
|
|
7
|
+
@tries_left = tries_left
|
|
8
|
+
@hints_left = hints_left
|
|
10
9
|
end
|
|
11
10
|
|
|
12
11
|
def formatted
|
|
13
|
-
format('%10s %
|
|
12
|
+
format('%10s %6i', @name, points)
|
|
14
13
|
end
|
|
15
14
|
|
|
16
15
|
def as_json
|
|
17
|
-
{ name: @name,
|
|
16
|
+
{ name: @name, points: points }
|
|
18
17
|
end
|
|
19
18
|
|
|
20
19
|
def to_json(*options)
|
|
21
20
|
as_json.to_json(*options)
|
|
22
21
|
end
|
|
23
22
|
|
|
23
|
+
def points
|
|
24
|
+
100 * (@tries_left + 1) + 75 * @hints_left
|
|
25
|
+
end
|
|
26
|
+
|
|
24
27
|
private
|
|
25
28
|
|
|
26
29
|
def name=(value)
|
data/lib/ep-codebreaker.rb
CHANGED
|
@@ -4,15 +4,3 @@ require 'yaml'
|
|
|
4
4
|
require 'ep-codebreaker/version'
|
|
5
5
|
require 'ep-codebreaker/player'
|
|
6
6
|
require 'ep-codebreaker/game'
|
|
7
|
-
|
|
8
|
-
module Codebreaker
|
|
9
|
-
MIN = 1
|
|
10
|
-
MAX = 6
|
|
11
|
-
LENGTH = 4
|
|
12
|
-
|
|
13
|
-
REGEXP = Regexp.new("^[#{MIN}-#{MAX}]{#{LENGTH}}$")
|
|
14
|
-
|
|
15
|
-
TRIES = 10
|
|
16
|
-
|
|
17
|
-
private_constant :MIN, :MAX, :LENGTH, :REGEXP, :TRIES
|
|
18
|
-
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ep-codebreaker
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Eugene Poberezhny
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-06-
|
|
11
|
+
date: 2017-06-14 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|