game_codebreaker 0.1.0 → 1.0.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: 68f61b18a1da7c5996935e6a33984fadb2748592
4
- data.tar.gz: 3db34ff020d6d625f2dff9a3c96556c8f1b963aa
3
+ metadata.gz: f41e15542918f40e4ae0f293cfe2c671d9595652
4
+ data.tar.gz: 43138a706137a5facad5cf3d8960adefcc913f17
5
5
  SHA512:
6
- metadata.gz: 4498f36e35a838ce22f419ed55b32de26fa8748d6c7b77b1ad9059d4c59d846fa1ded2c6818ed9b25ccdcb2c2a40c084981c0de659d250851a03f81fefc76455
7
- data.tar.gz: 0494839f786847662b28d629eea761c5a71158a9fc4c317c661d085cf7704a6095c2508533264f4ea8074e1103b6f0d88bde5f97d92308bf2b9bb1ef8c9d8583
6
+ metadata.gz: c18c5ffab41071699dcb81159d4e98251cbe46a98a326a9283df9fe6c4f7a6ea0d7934791dcf7926b48f60041b1ce6f0ff64203d4a5557f0a159d20bf552a8c0
7
+ data.tar.gz: 90b77e41a7936fb6bf5b53dff7c9044e76fd4bccaf83c186bb8549d083e4263ff38988022644127e992884aae17d30e445c93aa0d637e2d4fc3e1418c210bfa2
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.DS_Store
11
+ *~
12
+ *.gem
13
+ /dump
14
+ /.byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in game_codebreaker.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # GameCodebreaker for RubyGarage
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/game_codebreaker`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ use in console: game_codebreaker start
13
+
14
+ gem 'game_codebreaker'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install game_codebreaker
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/game_codebreaker.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
43
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ # require "rspec/core/rake_task"
3
+
4
+ # RSpec::Core::RakeTask.new(:spec)
5
+
6
+ # task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "game_codebreaker"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/game_codebreaker CHANGED
@@ -1,5 +1,126 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "game_codebreaker"
4
+ require "byebug"
4
5
 
5
- GameCodebreaker::Main.new
6
+ module GameCodebreaker
7
+
8
+ class Interface
9
+
10
+ def initialize
11
+ @games = []
12
+ end
13
+
14
+ def start
15
+ level = question( mess :level )
16
+ length = question( mess :length ).to_i
17
+ length = 4 if length <= 0
18
+ game = Game.new(level: (Options.level(level))[:level], hint: (Options.level(level))[:hint], length: length)
19
+ play( game )
20
+ end
21
+
22
+ def play( game )
23
+ string = question( mess :enter )
24
+ out if string == "exit"
25
+ get_hint( string, game )
26
+ cheat( string, game )
27
+ game.respond( string )
28
+ p game.history.last.last
29
+ game_over?( game )
30
+ play( game )
31
+ end
32
+
33
+ def question( mess )
34
+ p mess
35
+ value = $stdin.gets.chomp
36
+ end
37
+
38
+ def out
39
+ p mess :collector
40
+ exit 1
41
+ end
42
+
43
+ def cheat( string, game )
44
+ if string == "cheat"
45
+ p game.code
46
+ play(game)
47
+ end
48
+ end
49
+
50
+ def get_hint( string, game )
51
+ if string == "get hint"
52
+ game.get_hint
53
+ p game.hints.last
54
+ play(game)
55
+ end
56
+ end
57
+
58
+ def game_over?( game )
59
+ if game.game_over?
60
+ game.win? ? ( p "YOU WIN !" ) : ( p game.code; p 'YOU LOSE !' )
61
+ restart?( game )
62
+ save?( game )
63
+ out
64
+ end
65
+ end
66
+
67
+ def get_user_data
68
+ user = question( mess :data )
69
+ get_user_data unless user =~ /^(.+)\s(.+)\s(.+)$/
70
+ array = [name = $1, surname = $2, age = $3]
71
+ end
72
+
73
+ def restart?( game )
74
+ if question( mess :replay ).capitalize == "Y"
75
+ @games << game
76
+ start
77
+ end
78
+ end
79
+
80
+ def save?( game )
81
+ user_data = get_user_data if question( mess :save ).capitalize == "Y"
82
+ if user_data
83
+ @games << game
84
+ user = User.new( user_data[0], user_data[1], user_data[2], @games )
85
+ memory = Memory.new( "./dump" )
86
+ memory.save( user )
87
+ print_info( memory.info(user) )
88
+ end
89
+ end
90
+
91
+ def print_info( array )
92
+ p "Name: " << array[0]
93
+ p "Surname: " << array[1]
94
+ p "Age " << array[2]
95
+ p "Total games " << array[3].to_s
96
+ p "Total wins " << array[4].to_s
97
+ p "Total losses " << array[5].to_s
98
+ p "Average amount of turns " << array[6].to_s
99
+ p "Average amount of level " << array[7].to_s
100
+ end
101
+
102
+ def mess( value )
103
+ case value
104
+ when :enter
105
+ %Q^ Enter your integer and press 'Enter' ^
106
+ when :replay
107
+ %Q^ Replay ? -- Y/N ^
108
+ when :level
109
+ %Q^ Select and enter level. ( low | normal | hard ) ^
110
+ when :save
111
+ %Q^ Save games? -- Y/N ^
112
+ when :collector
113
+ %Q^ I am waiting the GARBAGE COLLECTOR ^
114
+ when :data
115
+ %Q^ Please enter your data separated by a space and press 'Enter'. Example: Name Surname Age ^
116
+ when :length
117
+ %Q^ Please enter quantity digits ^
118
+ else
119
+ %Q^ читер ? ^
120
+ end
121
+ end
122
+
123
+ end
124
+ end
125
+
126
+ ( p "Game Code-Breaker for RubyGarage"; GameCodebreaker::Interface.new().start ) if ARGV[0] == "start"
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'game_codebreaker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "game_codebreaker"
8
+ spec.version = GameCodebreaker::VERSION
9
+ spec.authors = ["woodcrust"]
10
+ spec.email = ["roboucrop@gmail.com"]
11
+
12
+ spec.summary = %q{This is game codebreaker for your terminal}
13
+ spec.description = %q{PLAY NOW!}
14
+ spec.homepage = "https://github.com/woodcrust/Codebreaker"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+
20
+ # spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ # # spec.files = Dir['lib/**/*']
22
+ # # spec.bindir = "exe"
23
+ # spec.bindir = "bin"
24
+ # # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ # spec.executables = ["game_codebreaker"]
26
+ # spec.require_paths = ["lib"]
27
+
28
+ spec.files = `git ls-files`.split("\n")
29
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
30
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
31
+ spec.require_paths = ["./"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.10"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.4"
36
+ spec.add_development_dependency "rspec-collection_matchers", "~> 1.1"
37
+ spec.add_development_dependency 'byebug', '~> 8.0'
38
+ end
@@ -1,9 +1,79 @@
1
- class Game
1
+ require "game_codebreaker/options"
2
2
 
3
- attr_reader :code, :turns, :history, :win, :level
3
+ module GameCodebreaker
4
+ class Game
5
+
6
+ JUST_A_MAGIC_NUMBER = 10
7
+
8
+ attr_reader :code, :length, :level, :turns, :history, :hint, :win, :hints, :game_over
9
+
10
+ def initialize( code: "", length: 4, level: (Options.level)[:level], turns: 0, history: [],
11
+ hint: (Options.level)[:hint], hinted: 0, win: false, game_over: false,
12
+ hints: [] )
13
+ @code, @length = code, length
14
+ @level, @turns = level, turns
15
+ @history, @hint = history, hint
16
+ @hinted, @win = hinted, win
17
+ @hints, @game_over = hints, game_over
18
+ generate_code if @code == ""
19
+ raise ArgumentError, 'Length code must be equal to variable with name \'length\'' if @code.size != @length
20
+ end
21
+
22
+ def respond( string )
23
+ return if game_over?
24
+ skynet = Array.new(@code.split(//))
25
+ human = string.split(//)
26
+ list = [@code, human.join("")]
27
+ result = process( skynet, human )
28
+ check_game( result )
29
+ @turns += 1; @history << ( list << result )
30
+ end
31
+
32
+ def get_hint
33
+ ( @hints << "there is no hints anymore"; @hints.uniq!; return ) if @hint == 0
34
+ pozition = nil; old_pozitions = []
35
+ @hints.each do |str|
36
+ str.split(//).each_with_index { |word, index| old_pozitions << index unless word.to_i == 0 }
37
+ end
38
+ ( @length*JUST_A_MAGIC_NUMBER ).times { pozition = rand( @length ); break unless old_pozitions.include?( pozition ) }
39
+ result = ""
40
+ @length.times { |i| pozition == i ? result << @code.to_s[i] : result << "-" }
41
+ @hint -= 1; @hints << result
42
+ end
43
+
44
+ def check_game( string )
45
+ @game_over = true if @turns == @level
46
+ (@win = true; @game_over = true) if string.count("+") == @length
47
+ end
48
+
49
+ def game_over?
50
+ @game_over
51
+ end
52
+
53
+ def win?
54
+ @win
55
+ end
56
+
57
+ private
58
+
59
+ def generate_code
60
+ @length.times{ @code << rand(1..6).to_s }
61
+ end
62
+
63
+ def process( skynet, human )
64
+ result = ""
65
+ @length.times do |i|
66
+ if skynet[i] == human[i]
67
+ result += "+"
68
+ skynet[i], human[i] = nil, nil
69
+ end
70
+ end
71
+ skynet.compact!; human.compact!;
72
+ human.each { |val| result += "-" if skynet.include?( val ) }
73
+ result
74
+ end
4
75
 
5
- def initialize( code, turns, history, win, level )
6
- @code, @turns, @history, @win, @level = code, turns, history, win, level
7
76
  end
77
+ end
78
+
8
79
 
9
- end
@@ -0,0 +1,53 @@
1
+ require "game_codebreaker/user"
2
+
3
+ module GameCodebreaker
4
+ class Memory
5
+
6
+ attr_accessor :users, :path
7
+
8
+ def initialize( path )
9
+ @path = path
10
+ File.exists?( path ) ? @users = load.users : @users = []
11
+ end
12
+
13
+ def add_games( object )
14
+ @users.each { |user| @user = user if user.hash == object.hash }
15
+ object.games.each { |game| @user.games << game }
16
+ end
17
+
18
+ def exists?( object )
19
+ @users.each { |user| return true if user.hash == object.hash }
20
+ false
21
+ end
22
+
23
+ def get_user( object )
24
+ @users.each { |user| @user = user if user.hash == object.hash }
25
+ @user
26
+ end
27
+
28
+ def info( user )
29
+ a = []
30
+ a << name = user.name
31
+ a << surname = user.surname
32
+ a << age = user.age
33
+ a << total_game = user.games.size
34
+ array_win = user.games.select { |game| game.win == true }
35
+ a << total_win = array_win.size
36
+ a << total_losses = ( user.games.select { |game| game.win == false } ).size
37
+ average_turns = 0 and array_win.each { |game| average_turns += game.turns }
38
+ total_win != 0 ? a << average_turns /= total_win : a << 0
39
+ average_level = 0 and user.games.select { |game| average_level += game.level }
40
+ a << average_level /= total_game
41
+ end
42
+
43
+ def save( user )
44
+ exists?( user ) ? ( add_games(user); user = get_user(user) ) : @users << user
45
+ File.open(@path,'w') { |f| f.write Marshal.dump self }
46
+ end
47
+
48
+ def load
49
+ return Marshal.load File.open(@path,'r').read if File.exists?( @path )
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,18 @@
1
+ module GameCodebreaker
2
+ class Options
3
+
4
+ def self.level( level=nil )
5
+ case level
6
+ when "low"
7
+ { level: 15, hint: 2 }
8
+ when "normal"
9
+ { level: 10, hint: 1 }
10
+ when "hard"
11
+ { level: 5, hint: 0 }
12
+ else
13
+ { level: 15, hint: 2 }
14
+ end
15
+ end
16
+
17
+ end
18
+ end
@@ -1,13 +1,15 @@
1
- class User
1
+ module GameCodebreaker
2
+ class User
2
3
 
3
- attr_accessor :name, :surname, :age, :games
4
+ attr_accessor :name, :surname, :age, :games
4
5
 
5
- def initialize( name, surname, age, games=[] )
6
- @name, @surname, @age, @games = name, surname, age, games
7
- end
6
+ def initialize( name, surname, age, games=[] )
7
+ @name, @surname, @age, @games = name, surname, age, games
8
+ end
8
9
 
9
- def hash
10
- [@name, @surname].hash
11
- end
10
+ def hash
11
+ [@name, @surname].hash
12
+ end
12
13
 
14
+ end
13
15
  end
@@ -1,3 +1,3 @@
1
1
  module GameCodebreaker
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,28 +1,7 @@
1
1
  require "game_codebreaker/version"
2
- require "game_codebreaker/message"
3
- require "game_codebreaker/play"
4
2
  require "game_codebreaker/user"
5
- require "game_codebreaker/remember"
3
+ require "game_codebreaker/memory"
6
4
  require "game_codebreaker/game"
7
5
 
8
- module GameCodebreaker
9
-
10
- output = Message.new
11
- games = []
12
- loop do
13
- g = Play.new
14
- games << g.start
15
- break unless output.question( output.mess 2 ).capitalize == "Y"
16
- end
17
-
18
- if user_data = output.user_data
19
- user = User.new( user_data[0], user_data[1], user_data[2], games )
20
- memory = Remember.new unless memory = Remember.load("./dump")
21
- memory.exists?( user ) ? ( memory.add_games(user); user = memory.get_user(user) ) : memory.users << user
22
- memory.save("./dump")
23
- output.print_info( memory.info(user) )
24
- end
25
-
26
- p output.mess 5
27
- # Your code goes here...
28
- end
6
+ # module GameCodebreaker
7
+ # end
Binary file
@@ -0,0 +1,149 @@
1
+ require 'spec_helper'
2
+
3
+ module GameCodebreaker
4
+
5
+ describe Game do
6
+
7
+ let(:game) { Game.new }
8
+
9
+ context "initialize" do
10
+ it 'return error if Length code must not be equal to variable with name \'length\'' do
11
+ expect{ Game.new( code: '1234', length: 5) }.to raise_error
12
+ end
13
+
14
+ it 'code is not changed' do
15
+ game = Game.new( code: '5555' )
16
+ expect( game.code ).to eq( '5555' )
17
+ end
18
+
19
+ it 'code must be exists' do
20
+ expect( game.code ).not_to eq( '' )
21
+ end
22
+ end
23
+
24
+ context "generate_code" do
25
+ it 'must return the string with digits between 1..6' do
26
+ game.send( :generate_code )
27
+ expect(game.code).to match(/[1-6]+/)
28
+ end
29
+ end
30
+
31
+ context "process" do
32
+
33
+ let(:skynet) { "1234".split(//) }
34
+
35
+ it 'with input the string "0000"' do
36
+ human = "0000".split(//)
37
+ expect( game.send( :process, skynet, human ) ).to eq( "" )
38
+ end
39
+
40
+ it 'with input the string "0001"' do
41
+ human = "0001".split(//)
42
+ expect( game.send( :process, skynet, human ) ).to eq( "-" )
43
+ end
44
+
45
+ it 'with input the string "1111"' do
46
+ human = "1111".split(//)
47
+ expect( game.send( :process, skynet, human ) ).to eq( "+" )
48
+ end
49
+
50
+ it 'with input the string "1200"' do
51
+ human = "1200".split(//)
52
+ expect( game.send( :process, skynet, human ) ).to eq( "++" )
53
+ end
54
+
55
+ it 'with input the string "1243"' do
56
+ human = "1243".split(//)
57
+ expect( game.send( :process, skynet, human ) ).to eq( "++--" )
58
+ end
59
+
60
+ it 'with input the string "1234"' do
61
+ human = "1234".split(//)
62
+ expect( game.send( :process, skynet, human ) ).to eq( "++++" )
63
+ end
64
+
65
+ it 'with input the string "4321"' do
66
+ human = "4321".split(//)
67
+ expect( game.send( :process, skynet, human ) ).to eq( "----" )
68
+ end
69
+
70
+ context 'repeating' do
71
+
72
+ let(:skynet) { "0011".split(//) }
73
+
74
+ it 'with repeating digits in code' do
75
+ human = "0123".split(//)
76
+ expect( game.send( :process, skynet, human ) ).to eq( "+-" )
77
+ end
78
+
79
+ it 'with repeating digits in code' do
80
+ human = "1100".split(//)
81
+ expect( game.send( :process, skynet, human ) ).to eq( "----" )
82
+ end
83
+
84
+ end
85
+
86
+ end
87
+
88
+ context "respond" do
89
+
90
+ let(:string) { "1234" }
91
+ let(:game2) { Game.new( code: "1234", game_over: true ) }
92
+ let(:game3) { Game.new( code: "1234" ) }
93
+
94
+ it 'when (game_over = true), turns not be changed' do
95
+ expect{ game2.respond( string ) }.not_to change{ game.turns }
96
+ end
97
+
98
+ it 'standard behavior - turns must be changed' do
99
+ allow(game).to receive(:check_game).and_return(true)
100
+ allow(game).to receive(:process).and_return(true)
101
+ expect{ game.respond( string ) }.to change{ game.turns }.by(1)
102
+ end
103
+
104
+ it 'standard behavior - @history must to have the array' do
105
+ game3.respond( string )
106
+ allow(game).to receive(:check_game).and_return(true)
107
+ allow(game).to receive(:process).and_return("++++")
108
+ expect( game3.history ).to include( ["1234", "1234", "++++"] )
109
+ end
110
+ end
111
+
112
+ context "get_hint" do
113
+
114
+ let(:game) { Game.new( code: "12", length: 2, hints: ["1-"] ) }
115
+
116
+ it 'hints must not be repeating ' do
117
+ game.get_hint
118
+ expect( game.hints ).to eq( ["1-", "-2"] )
119
+ end
120
+
121
+ it 'hint must be change' do
122
+ game.get_hint
123
+ expect{ game.get_hint }.to change{ game.hint }.by(-1)
124
+ end
125
+
126
+ end
127
+
128
+ context "check_game" do
129
+
130
+ let(:game2) { Game.new( turns: 15 ) }
131
+
132
+ it 'you win and game over' do
133
+ game.check_game( "++++" )
134
+ expect( game.win? ).to eq( true )
135
+ expect( game.game_over? ).to eq( true )
136
+ end
137
+
138
+ it 'you lose and game over' do
139
+ game2.check_game( "+++-" )
140
+ expect( game2.win? ).to eq( false )
141
+ expect( game2.game_over? ).to eq( true )
142
+ end
143
+
144
+ end
145
+
146
+ end
147
+ end
148
+
149
+
@@ -0,0 +1,166 @@
1
+ require 'spec_helper'
2
+
3
+ module GameCodebreaker
4
+
5
+ describe Interface do
6
+
7
+ let(:interface) { Interface.new }
8
+ let(:game) { Game.new(code: '1234') }
9
+
10
+ context "start" do
11
+ it 'create object game with params and receive method \'play\'' do
12
+ expect(interface).to receive(:question).and_return( 'low', '5' )
13
+ expect(Game).to receive(:new).with(length: 5, hint: 2, level: 15)
14
+ expect(interface).to receive(:play)
15
+ interface.start
16
+ end
17
+ end
18
+
19
+ context "play" do
20
+ it 'out of this programm if string eq \'exit\'' do
21
+ expect(interface).to receive(:question).and_return('exit')
22
+ expect{ interface.play(game) }.to raise_error SystemExit
23
+ end
24
+
25
+ # it 'standard behavior' do
26
+ # Interface.class_eval do
27
+ # alias :play2 :play
28
+ # end
29
+ # expect(interface).to receive(:question)
30
+ # expect(interface).to receive(:get_hint)
31
+ # expect(interface).to receive(:cheat)
32
+ # expect(game).to receive(:respond)
33
+ # expect(interface).to receive(:p)
34
+ # interface.stub_chain(:game, :history, :last, :last)
35
+ # expect(interface).to receive(:get_hint)
36
+ # expect(interface).to receive(:game_over?)
37
+ # interface.play2(game)
38
+ # end
39
+ end
40
+
41
+ context "mess" do
42
+ it { expect(interface.mess :enter).to eq(%Q^ Enter your integer and press 'Enter' ^) }
43
+ it { expect(interface.mess :replay).to eq(%Q^ Replay ? -- Y/N ^) }
44
+ it { expect(interface.mess :level).to eq(%Q^ Select and enter level. ( low | normal | hard ) ^) }
45
+ it { expect(interface.mess :save).to eq(%Q^ Save games? -- Y/N ^) }
46
+ it { expect(interface.mess :collector).to eq(%Q^ I am waiting the GARBAGE COLLECTOR ^) }
47
+ it { expect(interface.mess :data).to eq(%Q^ Please enter your data separated by a space and press 'Enter'. Example: Name Surname Age ^) }
48
+ it { expect(interface.mess :length).to eq(%Q^ Please enter quantity digits ^) }
49
+ it { expect(interface.mess :else).to eq(%Q^ читер ? ^) }
50
+ end
51
+
52
+ context "question" do
53
+ it 'must to return entered value' do
54
+ $stdin.stub_chain(:gets, :chomp)
55
+ expect(interface).to receive(:p).with('test')
56
+ interface.question('test')
57
+ end
58
+ end
59
+
60
+ context "out" do
61
+ it 'must return message and get out of this programm' do
62
+ expect(interface).to receive(:p).with(%Q^ I am waiting the GARBAGE COLLECTOR ^)
63
+ expect{ interface.out }.to raise_error SystemExit
64
+ end
65
+ end
66
+
67
+ context "get_hint" do
68
+ it 'must called get_hint, p and play' do
69
+ expect(game).to receive(:get_hint)
70
+ expect(interface).to receive(:p)
71
+ expect(interface).to receive(:play)
72
+ interface.get_hint('get hint', game)
73
+ end
74
+ end
75
+
76
+ context "cheat" do
77
+ it 'must called p with code and play' do
78
+ expect(interface).to receive(:p).with(game.code)
79
+ expect(interface).to receive(:play)
80
+ interface.cheat('cheat', game)
81
+ end
82
+ end
83
+
84
+ context "game_over?" do
85
+ it 'game_over == false' do
86
+ expect( interface.game_over?(game) ).to eq(nil)
87
+ end
88
+
89
+ context 'game_over == true' do
90
+ before(:each) do
91
+ allow(interface).to receive(:restart?)
92
+ allow(interface).to receive(:save?)
93
+ allow(interface).to receive(:out)
94
+ end
95
+
96
+ it 'if win' do
97
+ game = Game.new(code: '1234', game_over: true, win: true)
98
+ expect(interface).to receive(:p).with('YOU WIN !')
99
+ interface.game_over?(game)
100
+ end
101
+
102
+ it 'if lose' do
103
+ game = Game.new(code: '1234', game_over: true, win: false)
104
+ expect(interface).to receive(:p).with(game.code)
105
+ expect(interface).to receive(:p).with('YOU LOSE !')
106
+ interface.game_over?(game)
107
+ end
108
+ end
109
+ end
110
+
111
+ context 'get_user_data' do
112
+ it 'valid data' do
113
+ allow(interface).to receive(:question).and_return('Name Surname 100')
114
+ expect(interface.get_user_data).to eq(['Name', 'Surname', '100'])
115
+ end
116
+
117
+ # it 'invalid data' do
118
+ # allow(interface).to receive(:question).and_return('NameSurname 100')
119
+ # expect(interface.get_user_data).to eq(false)
120
+ # end
121
+ end
122
+
123
+ context 'restart?' do
124
+ it 'restart game' do
125
+ allow(interface).to receive(:question).and_return('y')
126
+ expect(interface).to receive(:start)
127
+ interface.restart?(game)
128
+ expect( interface.instance_variable_get(:@games) ).to include( game )
129
+ end
130
+
131
+ it 'end game' do
132
+ allow(interface).to receive(:question).and_return('n')
133
+ expect(interface).to receive(:start).never
134
+ expect( interface.restart?(game) ).to eq(nil)
135
+ end
136
+ end
137
+
138
+ context 'save?' do
139
+ # it 'save game' do
140
+ # end
141
+
142
+ it 'not save game' do
143
+ expect(interface).to receive(:question).and_return('n')
144
+ expect( interface.save?(game) ).to eq(nil)
145
+ end
146
+ end
147
+
148
+ context 'print_info' do
149
+ array = '12345678'.split(//)
150
+ it 'print info' do
151
+ expect(interface).to receive(:p).with('Name: 1')
152
+ expect(interface).to receive(:p).with('Surname: 2')
153
+ expect(interface).to receive(:p).with('Age 3')
154
+ expect(interface).to receive(:p).with('Total games 4')
155
+ expect(interface).to receive(:p).with('Total wins 5')
156
+ expect(interface).to receive(:p).with('Total losses 6')
157
+ expect(interface).to receive(:p).with('Average amount of turns 7')
158
+ expect(interface).to receive(:p).with('Average amount of level 8')
159
+ interface.print_info(array)
160
+ end
161
+ end
162
+
163
+ end
164
+ end
165
+
166
+
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ module GameCodebreaker
4
+
5
+ describe Memory do
6
+
7
+ let(:memory) { Memory.new( "./spec/game_codebreaker/fixtures/dump" ) }
8
+ let(:memory2) { Memory.new( "./spec/game_codebreaker/fixtures/save_dump" ) }
9
+ let(:game) { Game.new }
10
+ let(:user) { User.new( "Name", "Surname", "25", [] ) }
11
+ let(:user2) { User.new( "Name", "Surname", "25", [game] ) }
12
+ let(:user3) { User.new( "Name2", "Surname2", "25", [] ) }
13
+
14
+ context 'initialize' do
15
+
16
+ it "must to have users" do
17
+ expect( memory.users ).not_to eq( [] )
18
+ end
19
+
20
+ it "must not to have users" do
21
+ expect( memory2.users ).to eq( [] )
22
+ end
23
+
24
+ end
25
+
26
+ context 'add_games' do
27
+ it "add game to user" do
28
+ memory.users = [user]
29
+ memory.add_games( user2 )
30
+ expect( memory.users.first.games ).to include( game )
31
+ end
32
+ end
33
+
34
+ context 'exists?' do
35
+ it "user must be exist" do
36
+ memory.users = [user]
37
+ expect( memory.exists?( user ) ).to eq( true )
38
+ end
39
+
40
+ it "user must not to be existent" do
41
+ memory.users = [user]
42
+ expect( memory.exists?( user3 ) ).to eq( false )
43
+ end
44
+ end
45
+
46
+ context 'get_user' do
47
+ it "get user" do
48
+ memory.users = [user]
49
+ expect( memory.get_user( user ) ).to eq( user )
50
+ end
51
+ end
52
+
53
+ context 'info' do
54
+ it "user info" do
55
+ memory.users = [user2]
56
+ expect( memory.info( user2 ).size ).to eq( 8 )
57
+ end
58
+ end
59
+
60
+ context 'save' do
61
+ it "save user" do
62
+ allow(memory).to receive(:exists?).and_return(false)
63
+ memory2.save( user2 )
64
+ expect( File.exists?( memory2.path ) ).to eq( true )
65
+ end
66
+ end
67
+
68
+ context 'load' do
69
+ it "must be returned object of class Memory" do
70
+ expect( memory.load.class ).to eq( Memory )
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ module GameCodebreaker
4
+
5
+ describe User do
6
+
7
+ context 'hash' do
8
+ let(:game) { Game.new }
9
+ let(:user) { User.new( "Name", "Surname", "25", [game] ) }
10
+
11
+ it "return control sum" do
12
+ expect( user.hash ).to eq( ["Name", "Surname"].hash )
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ # $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'bundler/setup'
3
+ require 'game_codebreaker'
4
+ require 'rspec/collection_matchers'
5
+ load 'bin/game_codebreaker'
6
+
7
+
8
+ RSpec.configure do |config|
9
+
10
+ config.after(:each) do
11
+ dir = "./spec/game_codebreaker/fixtures/save_dump"
12
+ File.delete( dir ) if File.exists?( dir )
13
+ end
14
+
15
+ config.mock_with :rspec do |c|
16
+ c.syntax = [:should, :expect]
17
+ end
18
+
19
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: game_codebreaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - woodcrust
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-10 00:00:00.000000000 Z
11
+ date: 2015-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,22 +38,81 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-collection_matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '8.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '8.0'
41
83
  description: PLAY NOW!
42
84
  email:
43
85
  - roboucrop@gmail.com
44
86
  executables:
87
+ - console
45
88
  - game_codebreaker
89
+ - setup
46
90
  extensions: []
47
91
  extra_rdoc_files: []
48
92
  files:
93
+ - ".gitignore"
94
+ - ".rspec"
95
+ - ".travis.yml"
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
49
101
  - bin/game_codebreaker
102
+ - bin/setup
103
+ - game_codebreaker.gemspec
50
104
  - lib/game_codebreaker.rb
51
105
  - lib/game_codebreaker/game.rb
52
- - lib/game_codebreaker/message.rb
53
- - lib/game_codebreaker/play.rb
54
- - lib/game_codebreaker/remember.rb
106
+ - lib/game_codebreaker/memory.rb
107
+ - lib/game_codebreaker/options.rb
55
108
  - lib/game_codebreaker/user.rb
56
109
  - lib/game_codebreaker/version.rb
110
+ - spec/game_codebreaker/fixtures/dump
111
+ - spec/game_codebreaker/game_spec.rb
112
+ - spec/game_codebreaker/interface_spec.rb
113
+ - spec/game_codebreaker/memory_spec.rb
114
+ - spec/game_codebreaker/user_spec.rb
115
+ - spec/spec_helper.rb
57
116
  homepage: https://github.com/woodcrust/Codebreaker
58
117
  licenses:
59
118
  - MIT
@@ -61,7 +120,7 @@ metadata: {}
61
120
  post_install_message:
62
121
  rdoc_options: []
63
122
  require_paths:
64
- - lib
123
+ - "./"
65
124
  required_ruby_version: !ruby/object:Gem::Requirement
66
125
  requirements:
67
126
  - - ">="
@@ -78,4 +137,10 @@ rubygems_version: 2.4.8
78
137
  signing_key:
79
138
  specification_version: 4
80
139
  summary: This is game codebreaker for your terminal
81
- test_files: []
140
+ test_files:
141
+ - spec/game_codebreaker/fixtures/dump
142
+ - spec/game_codebreaker/game_spec.rb
143
+ - spec/game_codebreaker/interface_spec.rb
144
+ - spec/game_codebreaker/memory_spec.rb
145
+ - spec/game_codebreaker/user_spec.rb
146
+ - spec/spec_helper.rb
@@ -1,47 +0,0 @@
1
- class Message
2
-
3
- attr_accessor :user, :turns, :number, :history, :win
4
-
5
- def question( mess )
6
- p mess
7
- value = gets.chomp
8
- end
9
-
10
- def user_data
11
- if question( mess 4 ).capitalize == "Y"
12
- user = question( mess 6 )
13
- user_data unless user =~ /^(.+)\s(.+)\s(.+)$/
14
- return array = [name = $1, surname = $2, age = $3]
15
- end
16
- false
17
- end
18
-
19
- def print_info( array )
20
- p "Name: " << array[0]
21
- p "Surname: " << array[1]
22
- p "Age " << array[2]
23
- p "Total games " << array[3].to_s
24
- p "Total wins " << array[4].to_s
25
- p "Total losses " << array[5].to_s
26
- p "Average amount of turns " << array[6].to_s
27
- p "Average amount of level " << array[7].to_s
28
- end
29
-
30
- def mess( value )
31
- case value
32
- when 1
33
- %Q^ Enter your integer and press 'Enter' ^
34
- when 2
35
- %Q^ Replay ? -- Y/N ^
36
- when 4
37
- %Q^ Save games? -- Y/N ^
38
- when 5
39
- %Q^ I am waiting the GARBAGE COLLECTOR ^
40
- when 6
41
- %Q^ Please enter your data separated by a space and press 'Enter'. Example: Name Surname Age ^
42
- else
43
- %Q^ читер ? ^
44
- end
45
- end
46
-
47
- end
@@ -1,57 +0,0 @@
1
- require "game_codebreaker/game"
2
- require "game_codebreaker/message"
3
-
4
- class Play < Message
5
-
6
- attr_accessor :code, :turns, :history, :win, :level
7
-
8
- def initialize
9
- @code = ""
10
- @turns = 0
11
- @history = []
12
- @level = 15
13
- end
14
-
15
- def dice
16
- 4.times{ @code << rand(1..6).to_s }
17
- @code.to_i
18
- end
19
-
20
- def start
21
- dice
22
- process
23
- Game.new( @code, @turns, @history, @win, @level )
24
- end
25
-
26
- def process
27
- @level.times do
28
- skynet = Array.new(@code.to_s.split(//))
29
- human = question( mess 1 ).split(//)
30
- list = [@code, human.join("")]
31
- p result = to_think( skynet, human )
32
- @turns += 1; @history << ( list << result )
33
- p @code
34
- return @win = true if game_over?( result )
35
- end
36
- @win = false
37
- end
38
-
39
- def to_think( skynet, human )
40
- string = ""
41
- (0..3).each do |i|
42
- if skynet[i] == human[i]
43
- string += "+"
44
- skynet[i] = nil
45
- human[i] = nil
46
- end
47
- end
48
- skynet.compact!; human.compact!;
49
- human.each { |val| string += "-" if skynet.include?( val ) }
50
- string
51
- end
52
-
53
- def game_over?( string )
54
- return true if string.count("+") == 4
55
- end
56
-
57
- end
@@ -1,47 +0,0 @@
1
- class Remember
2
-
3
- attr_accessor :users
4
-
5
- def initialize
6
- @users = []
7
- end
8
-
9
- def add_games( object )
10
- @users.each { |user| @user = user if user.hash == object.hash }
11
- object.games.each { |game| @user.games << game }
12
- end
13
-
14
- def exists?( object )
15
- @users.each { |user| return true if user.hash == object.hash }
16
- false
17
- end
18
-
19
- def get_user( object )
20
- @users.each { |user| @user = user if user.hash == object.hash }
21
- @user
22
- end
23
-
24
- def info( user )
25
- a = []
26
- a << name = user.name
27
- a << surname = user.surname
28
- a << age = user.age
29
- a << total_game = user.games.size
30
- array_win = user.games.select { |game| game.win == true }
31
- a << total_win = array_win.size
32
- a << total_losses = ( user.games.select { |game| game.win == false } ).size
33
- average_turns = 0 and array_win.each { |game| average_turns += game.turns }
34
- a << average_turns /= total_win
35
- average_level = 0 and user.games.select { |game| average_level += game.level }
36
- a << average_level /= total_game
37
- end
38
-
39
- def save(path)
40
- File.open(path, 'w') { |f| f.write Marshal.dump self }
41
- end
42
-
43
- def self.load(path)
44
- Marshal.load File.open(path, 'r').read if File.exists?(path)
45
- end
46
-
47
- end