codeguessing 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8bd4ea46cffddc9ac2c2b8f477b10e0b3954f77e
4
- data.tar.gz: 78c4704c19a987743c9ef6e425c8dd59abf07010
3
+ metadata.gz: 61b21ffaab19a1a63dcb02b74ae830577b491f19
4
+ data.tar.gz: 06fbebd32d6c8d8cc650fe6a5a10b6aa97f2ce77
5
5
  SHA512:
6
- metadata.gz: 33cdfbbf867d5a0b03a714aee3c2336ebd810db0db61cf081f8100dbe0722851d3e13ddabd3b6b41dddff9dce0ee1d47832758cb44af6256a02c5b35caf1df11
7
- data.tar.gz: fb09f6de2f4a84390614d962822c1eb19b0ce0bf04082e44e70becfa12b83044c52ada50f54cc9cff7b4db8f715c2e49a24bae9c9e44793c0b5c766ab82632ea
6
+ metadata.gz: 96063313e8e199857583e99e07bdfc69849648a156c44e12281364b642e19cee3b6dff54d6f8d7f8cc497994e765451da7d62ed058c330755ba6e73fd7691ec2
7
+ data.tar.gz: a87df9cec2e9f84de0535190c2ca1cb37046b5b055990c64ca97f3d836971d85e8faab851c4f66539187c5483af29f7a53bdb16da5ee8d73cb5f4714d757649b
@@ -2,36 +2,37 @@ module Codeguessing
2
2
  class Console
3
3
  attr_reader :game, :scores, :path
4
4
 
5
+ RULES = [
6
+ '-----------------Rules!----------------------',
7
+ "You need guess secret code. This four-digit number with symbols from 1 to 6",
8
+ "You have #{Game::MAX_ATTEMPTS} attempt(s) and #{Game::MAX_HINT} hint(s)",
9
+ "If you want get hint write 'hint'",
10
+ '---------------------------------------------'
11
+ ]
12
+
5
13
  def initialize(opt = {})
6
14
  @path = File.join(File.dirname(__FILE__), 'scores.yml')
7
15
  @scores = load(@path)
8
16
  @game = Game.new(opt)
9
17
  end
10
18
 
11
- def go(know = true)
12
- rules if know
19
+ def go(knowed = false)
20
+ rules unless knowed
13
21
  puts "Attempt(s): #{@game.attempts} | Hint(s): #{@game.hint_count}"
14
- case @game.state
15
- when 'true'
16
- win
17
- when 'false'
18
- loose
19
- else
20
- gaming
22
+ case @game.win?
23
+ when true
24
+ win
25
+ when false
26
+ loose
27
+ else
28
+ gaming
21
29
  end
22
30
  end
23
31
 
24
32
  def rules
25
33
  puts "Do you know rules? (Y/N)"
26
- rules = [
27
- '-----------------Rules!----------------------',
28
- "You need guess secret code. This four-digit number with symbols from 1 to 6",
29
- "You have #{@game.attempts} attempt(s) and #{@game.hint_count} hint(s)",
30
- "If you want get hint write 'hint'",
31
- '---------------------------------------------'
32
- ]
33
34
  unless confirm?
34
- puts rules.join("\n")
35
+ puts RULES.join("\n")
35
36
  end
36
37
  end
37
38
 
@@ -59,7 +60,7 @@ module Codeguessing
59
60
  end
60
61
 
61
62
  def save!(name = 'Anonim')
62
- if @game.state != 'true'
63
+ unless @game.win?
63
64
  return puts 'You cant save game'
64
65
  end
65
66
  name.chomp!
@@ -96,7 +97,7 @@ module Codeguessing
96
97
  puts 'Do you want start again? (Y/N)'
97
98
  if confirm?
98
99
  @game = Game.new
99
- return go(false)
100
+ return go(true)
100
101
  else
101
102
  puts '-----------Scores----------'
102
103
  p @scores
@@ -14,59 +14,42 @@ module Codeguessing
14
14
  @answer = opt[:answer] || ''
15
15
  end
16
16
 
17
- def guess(code)
18
- loose unless check?(use_attempt)
17
+ def guess(code)
18
+ @state = 'loose' unless check?(use_attempt)
19
+ @state = 'win' if code == secret_code
20
+ @answer = get_mark(code)
21
+ end
22
+
23
+ def get_mark(code)
19
24
  return false unless valid?(code)
20
25
  hash = {}
21
- res = ''
22
- remaine_chars = code
23
- right_chars = []
26
+ res = ''
24
27
  secret_code.each_char.with_index do |char, i|
25
- case
26
- when code[i] == char
27
- res += '+'
28
- right_chars << char
29
- remaine_chars[i] = '*'
30
- hash.delete(char) if right_chars.include?(char)
31
- when remaine_chars.include?(char)
32
- hash[char] = '-'
28
+ if code[i] == char
29
+ res += '+'
30
+ code[i] = '_'
31
+ hash.delete(char)
32
+ elsif code.include?(char)
33
+ hash[char] = '-'
33
34
  end
34
35
  end
35
36
  res += hash.values.join('')
36
- win if res == '+' * MAX_SIZE
37
- @answer = res
38
37
  end
39
38
 
40
39
  def hint
41
- res = ''
42
- need_index = rand(0...MAX_SIZE)
43
- secret_code.each_char.with_index do |char, index|
44
- if index == need_index
45
- res += char
46
- else
47
- res += '*'
48
- end
49
- end
50
40
  return '' unless check?(hint_count)
51
41
  use_hint
52
- res
53
- end
54
-
55
- def cur_game
56
- hash = {}
57
- self.instance_variables.each do |k, v|
58
- new_k = k.to_s.gsub('@','').to_sym
59
- hash[new_k] = self.instance_variable_get(k)
60
- end
61
- hash
42
+ hint = '*' * MAX_SIZE
43
+ index = rand(0...MAX_SIZE)
44
+ code_char = secret_code[index]
45
+ hint[index] = code_char
46
+ hint
62
47
  end
63
48
 
64
49
  def cur_score(name = 'Anonim')
65
50
  hash = cur_game
66
51
  hash[:name] = name
67
52
  hash[:date] = Time.now.to_i
68
- hash.delete(:answer)
69
- hash.delete(:state)
70
53
  hash
71
54
  end
72
55
 
@@ -75,11 +58,6 @@ module Codeguessing
75
58
  false
76
59
  end
77
60
 
78
- def check?(varible)
79
- return false if varible == 0
80
- true
81
- end
82
-
83
61
  def use_attempt
84
62
  @attempts -= 1
85
63
  end
@@ -88,13 +66,19 @@ module Codeguessing
88
66
  @hint_count -= 1
89
67
  end
90
68
 
91
- def win
92
- @state = 'true'
69
+ def win?
70
+ case @state
71
+ when 'win' then true
72
+ when 'loose' then false
73
+ end
93
74
  end
94
75
 
95
- def loose
96
- @state = 'false'
97
- end
76
+ private
77
+
78
+ def check?(varible)
79
+ return false if varible == 0
80
+ true
81
+ end
98
82
 
99
83
  def random
100
84
  code = ''
@@ -102,5 +86,14 @@ module Codeguessing
102
86
  code
103
87
  end
104
88
 
89
+ def cur_game
90
+ hash = {}
91
+ self.instance_variables.each do |k, v|
92
+ new_k = k.to_s.gsub('@','').to_sym
93
+ hash[new_k] = self.instance_variable_get(k)
94
+ end
95
+ hash
96
+ end
97
+
105
98
  end
106
99
  end
@@ -1,3 +1,3 @@
1
1
  module Codeguessing
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codeguessing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - bezrukavyi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-07 00:00:00.000000000 Z
11
+ date: 2016-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize