Codebreaker_RG2016 0.2.0 → 0.2.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/README.md +4 -2
- data/lib/codebreaker/console_game.rb +1 -13
- data/lib/codebreaker/game.rb +6 -7
- data/lib/codebreaker/modules/console.rb +9 -6
- data/lib/codebreaker/modules/marker.rb +7 -6
- data/lib/codebreaker/modules/score.rb +3 -2
- data/lib/codebreaker/score/score.txt +1 -0
- data/lib/codebreaker/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9faf642e4eb9433ed0fb046bd304de14d57d32ac
|
4
|
+
data.tar.gz: 879e3e7124e4314f14b8d8f8950f02a0831c0fc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbb29c54d09eb31826b6af3d82ac79ef929d4037777cc356e028d19b0295663323c17f5d56edc0d8323f48f559468738deb86671e4e6e0f4405031e10b6468a8
|
7
|
+
data.tar.gz: b623071b23e4f59a1ce061996b2fd441dbbc1214aee5b14f59eddaebae40c6e892220bb82716428c6325b87c29e206b9dc60cdf3597c3372d0c825775533c571
|
data/README.md
CHANGED
@@ -30,14 +30,14 @@ Or install it yourself as:
|
|
30
30
|
## Usage
|
31
31
|
|
32
32
|
$ require 'codebreaker'
|
33
|
+
|
34
|
+
$ Codebreaker::ConsoleGame.new.play
|
33
35
|
|
34
36
|
|
35
37
|
It looks like:
|
36
38
|
|
37
39
|
You should to break a secret code ****.
|
38
40
|
|
39
|
-
Please, enter your name: $ Alex
|
40
|
-
|
41
41
|
You have 7 attempts and 1 hint.
|
42
42
|
|
43
43
|
Type your secret code or "hint": $ hint
|
@@ -94,6 +94,8 @@ Do you want to save your score in score file?(y/n)
|
|
94
94
|
|
95
95
|
$ y
|
96
96
|
|
97
|
+
Please, enter your name: $ Alex
|
98
|
+
|
97
99
|
You score saved in file: [path]/score/Alex_score.txt
|
98
100
|
|
99
101
|
|
@@ -12,23 +12,11 @@ module Codebreaker
|
|
12
12
|
|
13
13
|
def play
|
14
14
|
puts 'You should to break a secret code ****.'
|
15
|
-
|
15
|
+
puts @game.check_guess(input_code) until @game.win? || @game.game_start == false
|
16
16
|
@game.score_count
|
17
17
|
puts "Your score: #{@game.score}!"
|
18
18
|
puts 'Do you want to start new game?(y/n)'
|
19
19
|
new_game ? play : save_score
|
20
20
|
end
|
21
|
-
|
22
|
-
private
|
23
|
-
def checking
|
24
|
-
until @game.win?
|
25
|
-
@game.check_guess(input_code)
|
26
|
-
return @game.check_win if @game.attempts.zero?
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def new_game
|
31
|
-
@game = Game.new(@game.score) if agree?
|
32
|
-
end
|
33
21
|
end
|
34
22
|
end
|
data/lib/codebreaker/game.rb
CHANGED
@@ -4,26 +4,25 @@ module Codebreaker
|
|
4
4
|
class Game
|
5
5
|
include Marker
|
6
6
|
|
7
|
-
ATTEMPTS =
|
7
|
+
ATTEMPTS = 6
|
8
8
|
HINT = 1
|
9
|
-
CODE_SIZE = 4
|
10
|
-
RANGE = 1..6
|
11
9
|
|
12
|
-
|
10
|
+
attr_reader :secret_code, :hint, :attempts, :score, :game_start
|
13
11
|
def initialize(score = 0)
|
14
|
-
@secret_code = Array.new(
|
12
|
+
@secret_code = Array.new(4) { rand(1..6) }.join
|
15
13
|
@player_code = ''
|
16
14
|
@hint = HINT
|
17
15
|
@attempts = ATTEMPTS
|
18
16
|
@score = score
|
17
|
+
@game_start = true
|
19
18
|
end
|
20
19
|
|
21
20
|
def check_guess(guess)
|
22
21
|
@player_code = guess
|
23
22
|
if @player_code == 'hint' && @hint.nonzero?
|
24
|
-
puts 'Hint: Secret code contains: ' + @secret_code[rand(0..3)]
|
25
|
-
@hint -= 1
|
26
23
|
@attempts += 1
|
24
|
+
@hint -= 1
|
25
|
+
puts "Hint: Secret code contains: #{@secret_code[rand(0..3)]}"
|
27
26
|
end
|
28
27
|
check_win
|
29
28
|
end
|
@@ -4,10 +4,6 @@ module Codebreaker
|
|
4
4
|
gets.chomp
|
5
5
|
end
|
6
6
|
|
7
|
-
def agree?
|
8
|
-
input =~ /^(yes|y)$/i ? true : false
|
9
|
-
end
|
10
|
-
|
11
7
|
def input_name
|
12
8
|
print 'Please, enter your name: '
|
13
9
|
input
|
@@ -17,13 +13,20 @@ module Codebreaker
|
|
17
13
|
puts "You have #{@game.attempts} attempts and #{@game.hint} hint."
|
18
14
|
print 'Type your secret code or "hint": '
|
19
15
|
player_code = input
|
20
|
-
if
|
21
|
-
player_code == 'hint'
|
16
|
+
if player_code =~ /^[1-6]{4}|hint$/
|
22
17
|
player_code
|
23
18
|
else
|
24
19
|
puts 'You should type 4 numbers in code or "hint"!!!'
|
25
20
|
input_code
|
26
21
|
end
|
27
22
|
end
|
23
|
+
|
24
|
+
def agree?
|
25
|
+
input =~ /^(yes|y)$/i ? true : false
|
26
|
+
end
|
27
|
+
|
28
|
+
def new_game
|
29
|
+
@game = Game.new(@game.score) if agree?
|
30
|
+
end
|
28
31
|
end
|
29
32
|
end
|
@@ -2,23 +2,24 @@ module Codebreaker
|
|
2
2
|
module Marker
|
3
3
|
def check_win
|
4
4
|
if win?
|
5
|
-
|
6
|
-
elsif @attempts
|
7
|
-
puts "Game over! Secret code is #{@secret_code}."
|
5
|
+
'Congratulation! You win!'
|
6
|
+
elsif @attempts.zero?
|
8
7
|
@hint = 0
|
8
|
+
@game_start = false
|
9
|
+
"Game over! Secret code is #{@secret_code}."
|
9
10
|
else
|
10
|
-
puts "+"*pluses + '-'*(minuses-pluses)
|
11
11
|
@attempts -= 1
|
12
|
+
'+' * pluses + '-' * (minuses - pluses)
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
16
|
def pluses
|
16
|
-
@player_code.chars.to_a.map.with_index { |num, index| '+' if num == @secret_code[index] }.compact.size
|
17
|
+
@player_code.to_s.chars.to_a.map.with_index { |num, index| '+' if num == @secret_code[index] }.compact.size
|
17
18
|
end
|
18
19
|
|
19
20
|
def minuses
|
20
21
|
secret_code = @secret_code.chars.to_a
|
21
|
-
@player_code.chars.to_a.map{ |num| secret_code[secret_code.find_index(num)] = '-' if secret_code.include?(num) }.count('-')
|
22
|
+
@player_code.to_s.chars.to_a.map { |num| secret_code[secret_code.find_index(num)] = '-' if secret_code.include?(num) }.count('-')
|
22
23
|
end
|
23
24
|
|
24
25
|
def win?
|
@@ -7,10 +7,11 @@ module Codebreaker
|
|
7
7
|
end
|
8
8
|
|
9
9
|
private
|
10
|
+
|
10
11
|
def save_score_file
|
11
12
|
name = input_name
|
12
|
-
path = File.expand_path(
|
13
|
-
File.open(path, 'a') { |file| file.puts "#{@game.score}
|
13
|
+
path = File.expand_path('../../score/score.txt', __FILE__)
|
14
|
+
File.open(path, 'a') { |file| file.puts "#{name}: #{@game.score} score on #{Time.now.asctime}" }
|
14
15
|
puts "You score saved in file: #{path}"
|
15
16
|
end
|
16
17
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Alex: 400 score on Sat Dec 17 19:18:52 2016
|
data/lib/codebreaker/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Codebreaker_RG2016
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ajiexwel4
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- lib/codebreaker/modules/console.rb
|
77
77
|
- lib/codebreaker/modules/marker.rb
|
78
78
|
- lib/codebreaker/modules/score.rb
|
79
|
+
- lib/codebreaker/score/score.txt
|
79
80
|
- lib/codebreaker/version.rb
|
80
81
|
homepage: https://github.com/Ajiexwel4/Codebreaker
|
81
82
|
licenses:
|