code_breaker_gem_holtobin_anton 0.1.8 → 0.1.9
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/Rakefile +1 -3
- data/lib/code_breaker_gem_holtobin_anton/version.rb +1 -1
- data/lib/code_breaker_gem_holtobin_anton.rb +135 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c5bd10a1465baa72d356c824a03822b2eff3f08
|
4
|
+
data.tar.gz: fcaf1c0ba1b545548c286b82062ce51543c5b1b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 806489e7ac873853bafbd542526a469e3c89574fd5de03a31d1cdaa065b26fafe3952df9c8ca54d4ff525a5c3c9a7613f41fca03227026a03b3502bf802f9600
|
7
|
+
data.tar.gz: f26b6d75b8491fb362ff9372ad8025c57fd22a99ea57d8add397a6c6f39975f2a07d5d7ae44bfe98edd936da7a6210122922c4274e9b89e0b830c72af943780f
|
data/Rakefile
CHANGED
@@ -1,9 +1,138 @@
|
|
1
|
-
require
|
2
|
-
require "code_breaker_gem_holtobin_anton/game"
|
3
|
-
require 'code_breaker_gem_holtobin_anton/game_io'
|
4
|
-
require 'code_breaker_gem_holtobin_anton/data_check'
|
5
|
-
require 'code_breaker_gem_holtobin_anton/view'
|
1
|
+
require 'code_breaker_gem_holtobin_anton/version'
|
6
2
|
|
7
3
|
module CodeBreakerGemHoltobinAnton
|
8
|
-
|
4
|
+
module DataCheck
|
5
|
+
def data_checking?(data)
|
6
|
+
hint if data.capitalize == 'Hint'
|
7
|
+
exit if data.capitalize == 'Exit'
|
8
|
+
data =~ /(^([1-6]{4})$)/? false : true
|
9
|
+
end
|
10
|
+
|
11
|
+
def check_victory?
|
12
|
+
@secret_code.eql?(@user_code)
|
13
|
+
end
|
14
|
+
|
15
|
+
def check_attempts_ended?
|
16
|
+
@choice_did >= @choice_size? true : false
|
17
|
+
end
|
18
|
+
|
19
|
+
def check_for_match
|
20
|
+
secr_temp, user_temp = @secret_code.clone, @user_code.clone
|
21
|
+
secr_temp.each_char.with_index(index = 0) do |item, index|
|
22
|
+
if item == user_temp[index]
|
23
|
+
secr_temp[index],user_temp[index] = '0', '0'
|
24
|
+
@result << '+'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
secr_temp.each_char do |item|
|
29
|
+
next if item == '0'
|
30
|
+
if user_temp.include?(item)
|
31
|
+
@result << '-'
|
32
|
+
secr_temp.gsub!(item,'0')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module GameIO
|
39
|
+
def save_game(str)
|
40
|
+
File.open('code_breaker_gem_holtobin_anton/data/save_game.yaml','a') do |file|
|
41
|
+
file.write(str)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module View
|
47
|
+
def start_mess
|
48
|
+
puts 'Hello, let is start playing'
|
49
|
+
puts 'You can enter a "hint" to display a random number'
|
50
|
+
puts 'You can enter a "exit" to exit the game'
|
51
|
+
end
|
52
|
+
|
53
|
+
def hint_mess(num)
|
54
|
+
puts "Your tip is #{num}"
|
55
|
+
end
|
56
|
+
|
57
|
+
def end_game_mess(str)
|
58
|
+
puts "You #{str}!"
|
59
|
+
puts "Secret code = #{@secret_code}" if str == 'lost'
|
60
|
+
puts 'Enter your name :'
|
61
|
+
name = gets.chomp.capitalize
|
62
|
+
"#{name} choice did #{@choice_did} out of #{@choice_size}.Hint used = #{@hint_used} #{str} \n"
|
63
|
+
end
|
64
|
+
|
65
|
+
def restart_game_mess?
|
66
|
+
puts 'Do you want to start the game again? (Yes/No)'
|
67
|
+
gets.chomp.capitalize == 'Yes'? true : false
|
68
|
+
end
|
69
|
+
|
70
|
+
def attempts_ended
|
71
|
+
puts 'You have no more moves!'
|
72
|
+
end
|
73
|
+
|
74
|
+
def print_marking
|
75
|
+
puts @result
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class Game
|
80
|
+
attr_accessor :user_code, :choice_did, :hint_used, :choice_size
|
81
|
+
attr_reader :secret_code, :result
|
82
|
+
SECRET_CODE_SIZE = 4
|
83
|
+
|
84
|
+
include(DataCheck)
|
85
|
+
include(GameIO)
|
86
|
+
include(View)
|
87
|
+
|
88
|
+
def initialize
|
89
|
+
@choice_size = 10
|
90
|
+
@secret_code, @user_code, @result = '', '', ''
|
91
|
+
@hint_used, @choice_did = 0, 0
|
92
|
+
end
|
93
|
+
|
94
|
+
def start
|
95
|
+
@secret_code = SECRET_CODE_SIZE.times.map{1 + Random.rand(6)}.join
|
96
|
+
end
|
97
|
+
|
98
|
+
def submit
|
99
|
+
@result = ''
|
100
|
+
begin
|
101
|
+
puts 'Please enter :'
|
102
|
+
end while data_checking?(@user_code = gets.chomp)
|
103
|
+
check_for_match
|
104
|
+
@choice_did += 1
|
105
|
+
end
|
106
|
+
|
107
|
+
def inspection_game
|
108
|
+
return end_game if(check_attempts_ended?)
|
109
|
+
return end_game if(check_victory?)
|
110
|
+
true
|
111
|
+
end
|
112
|
+
|
113
|
+
def end_game
|
114
|
+
attempts_ended if(check_attempts_ended?)
|
115
|
+
str = check_victory??end_game_mess('won') : end_game_mess('lost')
|
116
|
+
save_game(str)
|
117
|
+
false
|
118
|
+
end
|
119
|
+
|
120
|
+
def hint
|
121
|
+
@hint_used += 1
|
122
|
+
hint_mess(@secret_code[rand(4)])
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
while(true)
|
127
|
+
flag = true
|
128
|
+
game = Game.new
|
129
|
+
game.start
|
130
|
+
game.start_mess
|
131
|
+
while(flag)
|
132
|
+
game.submit
|
133
|
+
game.print_marking
|
134
|
+
flag = game.inspection_game
|
135
|
+
end
|
136
|
+
exit unless(game.restart_game_mess?)
|
137
|
+
end
|
9
138
|
end
|