svatok_codebreaker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 336d9787471d65cd7b30f8655bee09b6ec4b9423
4
+ data.tar.gz: 9bb1463eb29f9fad1f27c4fe8656bb5d2e14092c
5
+ SHA512:
6
+ metadata.gz: 94909b9acba5b45407558cba0c6d46d2eaa5a193612ed6f5abc096f87a62f78a2b8f66a59a06e7f27ac77023304d36b5c1efd11ceddffe98fd0c54ab5adf2046
7
+ data.tar.gz: cb3949e77c4cefba559fdc96665c831f13e223878d34d393a1e3e3eeb6fa088a538cf32b9bba3a48f8bf33129592c94ffd44b9896defa3a28e0c9c883e6a6169
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ lib/svatok_codebreaker/scores.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ svatok_codebreaker
@@ -0,0 +1 @@
1
+ ruby-2.3.0
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at s_s_s@ua.fm. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in svatok_codebreaker.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ # SvatokCodebreaker
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/svatok_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
+ gem 'svatok_codebreaker'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install svatok_codebreaker
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/svatok_codebreaker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
@@ -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
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require "bundler/setup"
3
+ require "svatok_codebreaker"
4
+
5
+ game_console = SvatokCodebreaker::ConsoleForGame.new
6
+ game_console.prepare_game
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ require 'svatok_codebreaker/version'
2
+ require 'svatok_codebreaker/console_game'
3
+ require 'svatok_codebreaker/game'
4
+ require 'svatok_codebreaker/marker'
5
+ require 'svatok_codebreaker/game_message'
@@ -0,0 +1,62 @@
1
+ module SvatokCodebreaker
2
+ class ConsoleForGame
3
+ def initialize
4
+ @exit_game = false
5
+ @menu_commands = %w(exit restart hint save)
6
+ @console_message = GameMessage.new
7
+ end
8
+
9
+ def prepare_game
10
+ puts @console_message.show(:about)
11
+ puts @console_message.show(:login)
12
+ @player_name = gets.chomp
13
+ puts @console_message.show(:start)
14
+ run_game
15
+ end
16
+
17
+ def run_game
18
+ @game = Game.new(@player_name)
19
+ until @exit_game
20
+ answer = gets.chomp
21
+ begin
22
+ raise puts @console_message.show(:not_valid_answer) unless answer_valid?(answer)
23
+ raise puts send(('command_' + answer.downcase).to_sym) if @menu_commands.include?(answer)
24
+ @game.submit_guess(answer)
25
+ puts @game.end_of_game? ? game_completition : @console_message.show(:next_step, @game.get_game_data)
26
+ rescue
27
+ next
28
+ end
29
+ end
30
+ end
31
+
32
+ def game_completition
33
+ return @console_message.show(:win) if @game.marking_guess == '++++'
34
+ @console_message.show(:lose, @game.get_game_data)
35
+ end
36
+
37
+ def answer_valid?(answer)
38
+ @game.valid_guess?(answer) || @menu_commands.include?(answer)
39
+ end
40
+
41
+ def command_exit
42
+ @exit_game = true
43
+ @console_message.show(:exit_game)
44
+ end
45
+
46
+ def command_restart
47
+ @game = Game.new(@player_name)
48
+ @console_message.show(:restart_game)
49
+ end
50
+
51
+ def command_hint
52
+ return @console_message.show(:no_hint) unless @game.hint
53
+ @game.show_hint
54
+ end
55
+
56
+ def command_save
57
+ return @console_message.show(:not_available) unless @game.end_of_game?
58
+ @game.save_game
59
+ @console_message.show(:saved)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,54 @@
1
+ module SvatokCodebreaker
2
+ ATTEMPTS = 10
3
+
4
+ class Game
5
+ attr_accessor :attempts, :codebreaker_name, :hint, :marking_guess
6
+
7
+ def initialize(codebreaker_name = 'Test')
8
+ @codebreaker_name = codebreaker_name
9
+ @secret_code = Array.new(4) { rand(1..6) }.join
10
+ @attempts = ATTEMPTS
11
+ @hint = true
12
+ @file_path = File.join(File.dirname(__FILE__), 'scores.txt')
13
+ end
14
+
15
+ def submit_guess(guess)
16
+ return if end_of_game?
17
+ @attempts -= 1
18
+ @marking_guess = Marker.new(@secret_code, guess).marking_guess
19
+ end
20
+
21
+ def valid_guess?(guess)
22
+ return false unless !!guess
23
+ return false unless guess.length == 4
24
+ return false unless /[1-6]+/=~guess
25
+ true
26
+ end
27
+
28
+ def end_of_game?
29
+ @marking_guess == '++++' || @attempts.zero?
30
+ end
31
+
32
+ def show_hint
33
+ @hint = false
34
+ @secret_code.chars.sample
35
+ end
36
+
37
+ def save_game
38
+ score_file = File.open(@file_path, 'a')
39
+ score_file.puts get_game_data.map { |k, v| "#{k}=#{v}" }.join(';')
40
+ score_file.close
41
+ end
42
+
43
+ def get_game_data
44
+ {
45
+ codebreaker_name: @codebreaker_name,
46
+ secret_code: @secret_code,
47
+ marking_guess: @marking_guess,
48
+ attempts: @attempts,
49
+ hint: @hint == true ? 'not used' : 'used',
50
+ game_date: Time.now.strftime('%d/%m/%Y')
51
+ }
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,98 @@
1
+ module SvatokCodebreaker
2
+ class GameMessage
3
+ def show(method, game_data = nil)
4
+ return send(method) if game_data.nil?
5
+ send(method, game_data)
6
+ end
7
+
8
+ def about
9
+ "\n--------------------CODEBREAKER--------------------\n" +
10
+ "\nCodebreaker is a logic game in which a player" +
11
+ "\ntries to break a secret code created by a computer. The" +
12
+ "\ncode-maker, which will be played by the application were" +
13
+ "\ngoing to write, creates a secret code of four numbers" +
14
+ "\nbetween 1 and 6.\n" +
15
+ "\nComputer creates a secret code of four numbers between 1" +
16
+ "\nand 6. You get " + ATTEMPTS.to_s + " chances to break the code." +
17
+ "\nIn each turn, you make a guess of four numbers. Computer" +
18
+ "\nthen marks the guess with up to four + and - signs." +
19
+ "\n A + indicates an exact match: one of the numbers in the guess" +
20
+ "\nis the same as one of the numbers in the secret code and" +
21
+ "\nin the same position." +
22
+ "\n A - indicates a number match: one of the numbers in the guess" +
23
+ "\nis the same as one of the numbers in the secret code but in a" +
24
+ "\ndifferent position." +
25
+ "\n At any time during a game, you can request a hint, at which" +
26
+ "\npoint the system reveals one of the numbers in the" +
27
+ "\nsecret code. After the game is won or lost, you can opt" +
28
+ "\nto save information about the game.\n" +
29
+ "\nDuring the game you can use the commands Exit, Restart," +
30
+ "\nHint, Save."
31
+ end
32
+
33
+ def login
34
+ "\nPlease, enter your name:"
35
+ end
36
+
37
+ def start
38
+ "\n----------Game started. Enter guess:----------"
39
+ end
40
+
41
+ def not_valid_answer
42
+ "\nYou entered is not valid answer. Enter the correct answer."
43
+ end
44
+
45
+ def next_step(game_data)
46
+ message = "\n" + message_guess(game_data[:marking_guess]) + "\n" +
47
+ 'You have ' + game_data[:attempts].to_s + " attempts.\n" +
48
+ 'Enter a guess:'
49
+ message += '(You can use Hint)' if game_data[:hint] == 'not used'
50
+ message
51
+ end
52
+
53
+ def win
54
+ "\n" + "----------Congratulations! You win!----------\n" +
55
+ commands
56
+ end
57
+
58
+ def lose(game_data)
59
+ "\n" + message_guess(game_data[:marking_guess]) + "\n" +
60
+ "----------Sorry, but you lose :(----------\n" +
61
+ 'Secret code is ' + game_data[:secret_code] + "\n" +
62
+ commands
63
+ end
64
+
65
+ def commands
66
+ 'You can Exit/Restart/Save. Enter the desired command:'
67
+ end
68
+
69
+ def exit_game
70
+ "\n----------Bye!----------"
71
+ end
72
+
73
+ def restart_game
74
+ "\n----------Game restarted. Enter guess:----------"
75
+ end
76
+
77
+ def not_available
78
+ "\nSorry, but command is not available at the moment."
79
+ end
80
+
81
+ def saved
82
+ "\n----------Score saved!---------"
83
+ end
84
+
85
+ def no_match
86
+ "\nSorry, but there is no match..."
87
+ end
88
+
89
+ def no_hint
90
+ "\nSorry, but you have used a hint."
91
+ end
92
+
93
+ def message_guess(guess)
94
+ return 'Sorry, but there is no match...' if guess == ''
95
+ guess
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,33 @@
1
+ module SvatokCodebreaker
2
+ class Marker
3
+ def initialize(secret_code, guess)
4
+ @secret_code = secret_code.split('')
5
+ @guess = guess.split('')
6
+ end
7
+
8
+ def marking_guess
9
+ return '++++' if @secret_code == @guess
10
+ mark = marking_exact_matches
11
+ mark += marking_inexact_matches
12
+ mark.join
13
+ end
14
+
15
+ def marking_exact_matches
16
+ @guess.each_with_index.map do |number, index|
17
+ next unless number == @secret_code[index]
18
+ @guess[index] = nil
19
+ @secret_code[index] = nil
20
+ '+'
21
+ end
22
+ end
23
+
24
+ def marking_inexact_matches
25
+ [@guess, @secret_code].each(&:compact!)
26
+ @guess.map do |number|
27
+ next unless @secret_code.include?(number)
28
+ @secret_code[@secret_code.find_index(number)] = nil
29
+ '-'
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module SvatokCodebreaker
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'svatok_codebreaker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "svatok_codebreaker"
8
+ spec.version = SvatokCodebreaker::VERSION
9
+ spec.authors = ["Svatok"]
10
+ spec.email = ["s_s_s@ua.fm"]
11
+
12
+ spec.summary = %q{Codebreaker is a logic game.}
13
+ spec.description = %q{Codebreaker is a logic game in which a code-breaker
14
+ tries to break a secret code created by a code-maker.
15
+ The code-maker, which will be played by the application
16
+ we’re going to write, creates a secret code of four
17
+ numbers between 1 and 6.}
18
+ spec.homepage = "https://github.com/Svatok/svatok_codebreaker"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "pry"
31
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svatok_codebreaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Svatok
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-19 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: |-
70
+ Codebreaker is a logic game in which a code-breaker
71
+ tries to break a secret code created by a code-maker.
72
+ The code-maker, which will be played by the application
73
+ we’re going to write, creates a secret code of four
74
+ numbers between 1 and 6.
75
+ email:
76
+ - s_s_s@ua.fm
77
+ executables: []
78
+ extensions: []
79
+ extra_rdoc_files: []
80
+ files:
81
+ - ".gitignore"
82
+ - ".rspec"
83
+ - ".ruby-gemset"
84
+ - ".ruby-version"
85
+ - ".travis.yml"
86
+ - CODE_OF_CONDUCT.md
87
+ - Gemfile
88
+ - README.md
89
+ - Rakefile
90
+ - bin/console
91
+ - bin/setup
92
+ - lib/svatok_codebreaker.rb
93
+ - lib/svatok_codebreaker/console_game.rb
94
+ - lib/svatok_codebreaker/game.rb
95
+ - lib/svatok_codebreaker/game_message.rb
96
+ - lib/svatok_codebreaker/marker.rb
97
+ - lib/svatok_codebreaker/scores.txt
98
+ - lib/svatok_codebreaker/version.rb
99
+ - svatok_codebreaker.gemspec
100
+ homepage: https://github.com/Svatok/svatok_codebreaker
101
+ licenses: []
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.5.1
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Codebreaker is a logic game.
123
+ test_files: []