game-codebreaker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c3703414c1c258f9e9ba146c7c3d9ecd7828cb36
4
+ data.tar.gz: c21d4d1d851098a7f201e9d59b8ab96b10bf7c21
5
+ SHA512:
6
+ metadata.gz: 230f417089b0c67be46c2504cd89f27dfe864adf9ee9242d4308087ce21d1d1f2e01d0191b959a8d78eae246cd407ad10482291a48f0ec5d5e0f47b71dbdbab8
7
+ data.tar.gz: 1e11407a11fad78123c1686647e54eb75a233f8b7587c0ee0ae0b81d1cc1eec2c9b73c65300c6e8b437b8ff8f6222be4107e154b4ea1c45b898030dc8ab75563
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
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.0.0
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 codebreaker.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Codebreaker
2
+
3
+ Gem for RG courses
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'game-codebreaker'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install game-codebreaker
20
+
21
+ ## Development
22
+
23
+ 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.
24
+
25
+ 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).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/codebreaker.
30
+
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 "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/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,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'codebreaker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "game-codebreaker"
8
+ spec.version = Codebreaker::VERSION
9
+ spec.authors = ["kolyastepanets"]
10
+ spec.email = ["kolyastepanets@gmail.com"]
11
+
12
+ spec.summary = %q{Gem for RG courses}
13
+ spec.description = %q{A game codebreaker to find out 4 numbers}
14
+ spec.homepage = "https://rubygems.org/profiles/kolyastepanets"
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
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ end
@@ -0,0 +1,2 @@
1
+ require "codebreaker/version"
2
+ require 'codebreaker/game'
@@ -0,0 +1,81 @@
1
+ require_relative 'game'
2
+
3
+ module Codebreaker
4
+
5
+ class Console
6
+
7
+ def start_game
8
+ @game = Codebreaker::Game.new
9
+ puts "Welcome, let's play"
10
+ puts "Enter 4 digits from 1 to 6 to guess the secret code"
11
+ puts "Type 'h' to find out 1 number of the secret code"
12
+ attempt
13
+ end
14
+
15
+ def attempt
16
+ answer = gets.chomp
17
+
18
+ if /^[1-6]*$/ === answer
19
+ guess(answer)
20
+ elsif answer == "h"
21
+ hint
22
+ else
23
+ puts "Enter please only 4 digits"
24
+ attempt
25
+ end
26
+ rescue
27
+ puts "Enter please only 4 digits size"
28
+ attempt
29
+ end
30
+
31
+ def guess(answer)
32
+ if @game.attempts > 1
33
+ res = @game.guess_code(answer)
34
+ if res == ["+", "+", "+", "+"]
35
+ p res
36
+ puts "Huraaah"
37
+ game_over
38
+ else
39
+ p res
40
+ puts "Left attempts: #{@game.attempts}"
41
+ puts "-------------------"
42
+ attempt
43
+ end
44
+ else
45
+ res = @game.guess_code(answer)
46
+ if res == ["+", "+", "+", "+"]
47
+ p res
48
+ puts "Huraaah"
49
+ game_over
50
+ else
51
+ p res
52
+ game_over
53
+ end
54
+ end
55
+ end
56
+
57
+ def hint
58
+ if @game.hints > 0
59
+ hint = @game.hint
60
+ p hint
61
+ attempt
62
+ else
63
+ puts "There is no hints avaliable"
64
+ attempt
65
+ end
66
+ end
67
+
68
+ def game_over
69
+ puts "Game over"
70
+ play_again
71
+ end
72
+
73
+ def play_again
74
+ puts "Do you want to play again? Press y/n"
75
+ answer = gets.chomp
76
+ start_game if answer == "y"
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,84 @@
1
+ module Codebreaker
2
+ class Game
3
+
4
+ attr_reader :hints, :attempts
5
+
6
+ def initialize(hints = 1, attempts = 10)
7
+ @hints = hints
8
+ @attempts = attempts
9
+ @secret_code = (1..4).collect{ rand(1..6) }
10
+ @rand_index = rand(0..3)
11
+ @win = false
12
+ end
13
+
14
+ def guess_code(value)
15
+ raise ArgumentError, "wrong size of values" unless value.length == 4
16
+ raise ArgumentError, "wrong entered characters" unless value.match(/^[1-6]+$/)
17
+ return nil unless @attempts > 0
18
+
19
+ @attempts -= 1
20
+
21
+ result = []
22
+
23
+ secret_code = @secret_code.dup
24
+ value = value.split("").map!{ |i| i.to_i }
25
+
26
+ value.each_with_index do |val, index|
27
+ if secret_code[index] == val
28
+ secret_code[index] = " "
29
+ value[index] = " "
30
+ result << "+"
31
+ end
32
+ end
33
+
34
+ secret_code.delete(" ")
35
+ value.delete(" ")
36
+
37
+ value.each_with_index do |val, index|
38
+ if secret_code.include?(val)
39
+ secret_code[secret_code.index(val)] = " "
40
+ value[index] = " "
41
+ result << "-"
42
+ end
43
+ end
44
+
45
+ # if result == ["+", "+", "+", "+"]
46
+ # @win = true
47
+ # else
48
+ # @win
49
+ # end
50
+
51
+ result
52
+ end
53
+
54
+ def hint
55
+ return nil unless @hints > 0
56
+ @hints -= 1
57
+ secret_code = @secret_code
58
+ result = ["*","*","*","*"]
59
+ result[@rand_index] = secret_code[@rand_index]
60
+ result
61
+ end
62
+
63
+ def save_to_file(user)
64
+ raise TypeError, "user is not a string" unless user.is_a?(String)
65
+ raise ArgumentError, "it is allowed to use a-z, A-Z, 0-9" unless user.match(/^[a-zA-Z0-9]+$/)
66
+
67
+ file = "#{__dir__}/../../bin/saved_files/#{user}_file.mar"
68
+
69
+ result = { "name" => user, "hints_left" => @hints, "secret_code" => @secret_code, "attempts_left" => @attempts }
70
+ complete_result = Marshal.dump(result)
71
+ File.open(file, 'w') { |file| file.write(complete_result) }
72
+ end
73
+
74
+ def read_from_file(user)
75
+ raise TypeError, "user is not a string" unless user.is_a?(String)
76
+ raise ArgumentError, "it is allowed to use a-z, A-Z, 0-9" unless user.match(/^[a-zA-Z0-9]+$/)
77
+
78
+ file = "#{__dir__}/../../bin/saved_files/#{user}_file.mar"
79
+ result = File.read(file)
80
+ Marshal.load(result)
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,4 @@
1
+ require_relative '../codebreaker/console'
2
+
3
+ console = Codebreaker::Console.new
4
+ console.start_game
@@ -0,0 +1,3 @@
1
+ module Codebreaker
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: game-codebreaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kolyastepanets
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A game codebreaker to find out 4 numbers
56
+ email:
57
+ - kolyastepanets@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - codebreaker.gemspec
71
+ - lib/codebreaker.rb
72
+ - lib/codebreaker/console.rb
73
+ - lib/codebreaker/game.rb
74
+ - lib/codebreaker/run.rb
75
+ - lib/codebreaker/version.rb
76
+ homepage: https://rubygems.org/profiles/kolyastepanets
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.8
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Gem for RG courses
100
+ test_files: []