Codebreaker_RG2016 0.1.6 → 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: f4683b0f634dfe3bfb3d849372e4755790584e8c
4
- data.tar.gz: 732dacf154d721d247f9182e1678f971517992ab
3
+ metadata.gz: 80a5820a737140c18749591776c349993640cc8e
4
+ data.tar.gz: f4677aacf673297c24d1f5a7ced93e3a1e1c17b6
5
5
  SHA512:
6
- metadata.gz: 08378c29701023d112efe921a34e9635ccc04ae4becc60d3b4078e7d24a9cdf89d530631e110c7fc1ecc78c746fbfd9f9d8c0dd6d93a3e9cf369633ac740c3a5
7
- data.tar.gz: 092f09456062104048668516552945aeb0e9891ab9a764524557317cf25961ee85da2359bf6dcc2c03b9cd150f0a8768fa95f5e150598a3d1a030f38ce6f46ce
6
+ metadata.gz: 3e70b1591648c4ee791a8ca36e5c01fa09b998cb848b8e7a88e57ff1473e027138809df3a45c7018a89dcd1b4aecb0f8289b0531aabb61d4be5e69247e9feade
7
+ data.tar.gz: ee79e9f71d6ceb9ae75d179f883db95d2250ce2c7b3147e3e6bc1b3f6b98e3e1bf74f88586130d183b5103fde41c0fb2c4b6cb0bfb38c31e699807976b4ac3ae
data/README.md CHANGED
@@ -28,12 +28,9 @@ Or install it yourself as:
28
28
  $ gem install codebreaker
29
29
 
30
30
  ## Usage
31
-
32
- $ ruby lib/codebreaker.rb
33
- or
34
- install gem Codebreaker_RG2016 from rubygems.org and from irb type:
31
+
35
32
  $ require 'codebreaker'
36
-
33
+
37
34
 
38
35
  It looks like:
39
36
 
@@ -0,0 +1,34 @@
1
+ require_relative 'modules/console'
2
+ require_relative 'modules/score'
3
+
4
+ module Codebreaker
5
+ class ConsoleGame
6
+ include Console
7
+ include Score
8
+
9
+ def initialize
10
+ @game = Game.new
11
+ end
12
+
13
+ def play
14
+ puts 'You should to break a secret code ****.'
15
+ checking
16
+ @game.score_count
17
+ puts "Your score: #{@game.score}!"
18
+ puts 'Do you want to start new game?(y/n)'
19
+ new_game ? play : save_score
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
+ end
34
+ end
@@ -1,35 +1,36 @@
1
+ require_relative 'modules/marker'
2
+
1
3
  module Codebreaker
2
4
  class Game
3
- include Engine
4
- include Score
5
+ include Marker
5
6
 
6
7
  ATTEMPTS = 7
7
8
  HINT = 1
8
9
  CODE_SIZE = 4
9
10
  RANGE = 1..6
10
11
 
11
- attr_accessor :secret_code, :player, :hint, :attempts
12
- def initialize(player = Player.new)
12
+ attr_accessor :secret_code, :player_code, :hint, :attempts, :score
13
+ def initialize(score = 0)
13
14
  @secret_code = Array.new(CODE_SIZE) { rand(RANGE) }.join
14
- @player = player
15
- @hint = HINT
16
- @attempts = ATTEMPTS
17
-
18
- start
15
+ @player_code = ''
16
+ @hint = HINT
17
+ @attempts = ATTEMPTS
18
+ @score = score
19
19
  end
20
20
 
21
- def start
22
- puts "You have #{@attempts} attempts and #{@hint} hint."
23
- check_guess
24
- score
25
- new_game? ? start : save_score?
21
+ def check_guess(guess)
22
+ @player_code = guess
23
+ if @player_code == 'hint' && @hint.nonzero?
24
+ puts 'Hint: Secret code contains: ' + @secret_code[rand(0..3)]
25
+ @hint -= 1
26
+ @attempts += 1
27
+ end
28
+ check_win
26
29
  end
27
30
 
28
- private
29
-
30
- def new_game?
31
- puts 'Do you want to start new game?(y/n)'
32
- Game.new(@player) if @player.agree?
31
+ def score_count
32
+ @score += 250 if win?
33
+ @score += @hint * 100 + @attempts * 50
33
34
  end
34
35
  end
35
36
  end
@@ -1,33 +1,21 @@
1
1
  module Codebreaker
2
- class Player
3
- attr_accessor :name, :player_code, :score
4
- def initialize(score = 0)
5
- @name = input_name
6
- @player_code = ''
7
- @score = score
8
- end
9
-
10
- def guess
11
- print 'Type your secret code or "hint": '
12
- @player_code = input_code
2
+ module Console
3
+ def input
4
+ gets.chomp
13
5
  end
14
6
 
15
7
  def agree?
16
8
  input =~ /^(yes|y)$/i ? true : false
17
9
  end
18
10
 
19
- private
20
-
21
- def input
22
- STDIN.gets.chomp
23
- end
24
-
25
11
  def input_name
26
12
  print 'Please, enter your name: '
27
13
  input
28
14
  end
29
15
 
30
16
  def input_code
17
+ puts "You have #{@game.attempts} attempts and #{@game.hint} hint."
18
+ print 'Type your secret code or "hint": '
31
19
  player_code = input
32
20
  if player_code.size == Codebreaker::Game::CODE_SIZE ||
33
21
  player_code == 'hint'
@@ -0,0 +1,28 @@
1
+ module Codebreaker
2
+ module Marker
3
+ def check_win
4
+ if win?
5
+ puts 'Congratulation! You win!'
6
+ elsif @attempts == 0
7
+ puts "Game over! Secret code is #{@secret_code}."
8
+ @hint = 0
9
+ else
10
+ puts "+"*pluses + '-'*(minuses-pluses)
11
+ @attempts -= 1
12
+ end
13
+ end
14
+
15
+ def pluses
16
+ @player_code.chars.to_a.map.with_index { |num, index| '+' if num == @secret_code[index] }.compact.size
17
+ end
18
+
19
+ def minuses
20
+ 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
+ end
23
+
24
+ def win?
25
+ @secret_code == @player_code
26
+ end
27
+ end
28
+ end
@@ -1,22 +1,16 @@
1
1
  module Codebreaker
2
2
  module Score
3
- def score
4
- @player.score += 250 if win?
5
- @player.score += @hint * 100 + @attempts * 50
6
- puts "#{player.name}, your score: #{@player.score}!"
7
- end
8
-
9
- def save_score?
3
+ def save_score
10
4
  puts 'Do you want to save your score in score file?(y/n)'
11
- save_score_file if @player.agree?
5
+ save_score_file if agree?
12
6
  exit
13
7
  end
14
8
 
15
9
  private
16
-
17
10
  def save_score_file
18
- path = File.expand_path("../../score/#{@player.name}_score.txt", __FILE__)
19
- File.open(path, 'a') {|file| file.puts "#{@player.score} - #{Time.now.asctime}" }
11
+ name = input_name
12
+ path = File.expand_path("../../score/#{name}_score.txt", __FILE__)
13
+ File.open(path, 'a') { |file| file.puts "#{@game.score} - #{Time.now.asctime}" }
20
14
  puts "You score saved in file: #{path}"
21
15
  end
22
16
  end
@@ -1,3 +1,3 @@
1
1
  module Codebreaker
2
- VERSION = '0.1.6'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/codebreaker.rb CHANGED
@@ -1,10 +1,5 @@
1
1
  require_relative 'codebreaker/version'
2
- require_relative 'codebreaker/modules/score'
3
- require_relative 'codebreaker/modules/engine'
4
2
  require_relative 'codebreaker/game'
5
- require_relative 'codebreaker/player'
3
+ require_relative 'codebreaker/console_game'
6
4
 
7
- module Codebreaker
8
- puts 'You should to break a secret code ****.'
9
- Game.new
10
- end
5
+ Codebreaker::ConsoleGame.new.play
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.1.6
4
+ version: 0.2.0
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-07 00:00:00.000000000 Z
11
+ date: 2016-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,11 +71,11 @@ files:
71
71
  - bin/setup
72
72
  - codebreaker.gemspec
73
73
  - lib/codebreaker.rb
74
+ - lib/codebreaker/console_game.rb
74
75
  - lib/codebreaker/game.rb
75
- - lib/codebreaker/modules/engine.rb
76
+ - lib/codebreaker/modules/console.rb
77
+ - lib/codebreaker/modules/marker.rb
76
78
  - lib/codebreaker/modules/score.rb
77
- - lib/codebreaker/player.rb
78
- - lib/codebreaker/score/Alex_score.txt
79
79
  - lib/codebreaker/version.rb
80
80
  homepage: https://github.com/Ajiexwel4/Codebreaker
81
81
  licenses:
@@ -1,46 +0,0 @@
1
- module Codebreaker
2
- module Engine
3
- def check_guess
4
- @player_code = @player.guess
5
- if @player_code == 'hint' && @hint.nonzero?
6
- puts 'Hint: Secret code contains: ' + @secret_code[rand(0..3)]
7
- @hint -= 1
8
- @attempts += 1
9
- end
10
- check_win
11
- end
12
-
13
- private
14
-
15
- def check_win
16
- if win?
17
- puts 'Congratulation! You win!'
18
- elsif @attempts.zero?
19
- puts "Game over! Secret code is #{@secret_code}."
20
- @hint = 0
21
- else
22
- puts((pluses + minuses).join)
23
- @attempts -= 1
24
- start
25
- end
26
- end
27
-
28
- def array_player_code
29
- @player_code.chars.to_a
30
- end
31
-
32
- def pluses
33
- array_player_code.map.with_index { |num, index| '+' if num == @secret_code[index] }.compact
34
- end
35
-
36
- def minuses
37
- secret_code = @secret_code.chars.to_a
38
- array_player_code.map{ |num| secret_code[secret_code.find_index(num)] = "-" if secret_code.include?(num)}
39
- secret_code.select{ |num| num == "-" }.drop(pluses.size)
40
- end
41
-
42
- def win?
43
- @secret_code == @player_code
44
- end
45
- end
46
- end
@@ -1 +0,0 @@
1
- 400 - Tue Dec 6 22:52:47 2016