dsa_codebreaker_game 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/dsa_codebreaker_game +5 -0
- data/dsa_codebreaker_game.gemspec +25 -0
- data/lib/dsa_codebreaker_game.rb +9 -0
- data/lib/dsa_codebreaker_game/code.rb +20 -0
- data/lib/dsa_codebreaker_game/console.rb +63 -0
- data/lib/dsa_codebreaker_game/game.rb +96 -0
- data/lib/dsa_codebreaker_game/gamer.rb +32 -0
- data/lib/dsa_codebreaker_game/version.rb +3 -0
- data/spec/lib/code_spec.rb +16 -0
- data/spec/lib/gamer_spec.rb +24 -0
- data/spec/spec_helper.rb +11 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8152b7b19865cfc5c3e048fb3b6db86df5d326e5
|
4
|
+
data.tar.gz: b7acc0066caed1422154982772c3a9644b1db284
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 098f85be1b302339ea31f41537d39b44ed7d580cb1ecf2c768cbad40905e615644e2b11a3fd7423c73f9fbbda5f2dd04cc3d642972890e060bfc205d28d7096e
|
7
|
+
data.tar.gz: 6a07a82bdf705b249c0eec1df1e9f64a46444b60d43370d60e2d4fa2b6d1ca5148764511d5f12aa5b6b2e5b45910d6c6fd2dbc86aa5e917ddfa9b179c731d71c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 szines
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# DsaCodebreakerGame
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
$ gem install dsa_codebreaker_game
|
6
|
+
|
7
|
+
#A variation on the mastermind game
|
8
|
+
|
9
|
+
##The Star Jam Challenge
|
10
|
+
|
11
|
+
###STAGE 1: Build it as per spec below with text based display
|
12
|
+
>Build a game of code breaker
|
13
|
+
|
14
|
+
The game starts by choosing the code patch which is a sequence of four colours from the following available colours red(R), orange (O), yellow (Y), green (G), blue (B), Indigo (I), and violet (V). The code patch is not displayed to the user only 4 lines are displayed ( _ _ _ _ ) and lives = 8.
|
15
|
+
|
16
|
+
The user enters a string of four characters which is their guess at the sequence of 4 letters choosen by the computer.
|
17
|
+
|
18
|
+
The user input is compared to the code patch and the following feedback is provided:
|
19
|
+
|
20
|
+
- If the two code patches match, the game says YOU WIN, do you want to play again (Y/N)?
|
21
|
+
|
22
|
+
- If one or more colours between the two colour patches match, display the positions where the colours match.
|
23
|
+
|
24
|
+
- If the colour is correct but the position is wrong for one or more colours give the user a clue as to how many colours there are in the in the users patch that are not in the correct position.
|
25
|
+
|
26
|
+
- If the same colour is used twice in the users patch and twice in the computers patch but the positions are wrong the clue will have a value of 2.
|
27
|
+
|
28
|
+
- If the colour patch has not been guessed then a life is lost and the user is asked : Enter a sequence a 4 character sequence from ROYGBIV or 0 to exit:
|
29
|
+
|
30
|
+
- If the number of lives is zero following this guess and the user has not won, then display. YOU LOOSE, do you want to play again (Y/N)?
|
31
|
+
|
32
|
+
- If instead of entering in a code sequence the user enters 0, or there are 0 in the code patch entered, exit the game (boss kill switch)
|
33
|
+
|
34
|
+
- If the same sequence is entered twice or more, inform the user that duplicate patches are not allowed and ask then to re enter a new code patch. No life is lose for a duplicate entry. Display the game as per the example screens on the next page. STAGE 2: If you have time before the deadline and stage 1 is built Create a graphical version of the game
|
35
|
+
|
36
|
+
© Copyright Digital Skills Academy 2011-12. All rights reserved.
|
37
|
+
|
38
|
+
Conor O’Reilly
|
39
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'dsa_codebreaker_game/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "dsa_codebreaker_game"
|
8
|
+
spec.version = DsaCodebreakerGame::VERSION
|
9
|
+
spec.authors = ["Zoltan Debre"]
|
10
|
+
spec.email = [""]
|
11
|
+
spec.description = %q{A variation on MasterMind game}
|
12
|
+
spec.summary = %q{This is a code breaker game with simplified rules. A coding challenge from Digital Skills Academy.}
|
13
|
+
spec.homepage = "https://github.com/szines/dsa_codebreaker_game"
|
14
|
+
spec.license = "MIT/DIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency 'rb-readline'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module DsaCodebreakerGame
|
2
|
+
|
3
|
+
class Code < Array
|
4
|
+
|
5
|
+
#Available colours red(R), orange (O), yellow (Y), green (G), blue (B), Indigo (I), and violet (V).
|
6
|
+
COLORS = ['R', 'O', 'Y', 'G', 'B', 'I', 'V']
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
4.times do
|
10
|
+
self << randomizer
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def randomizer
|
15
|
+
COLORS[rand(0..6)]
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'readline'
|
2
|
+
|
3
|
+
module DsaCodebreakerGame
|
4
|
+
class Console
|
5
|
+
|
6
|
+
def welcome(gamer)
|
7
|
+
puts ''
|
8
|
+
puts '***** Welcome in the Amazing DSA Codebreaker Game ****'
|
9
|
+
puts ''
|
10
|
+
puts 'This is a variation on the MasterMind game. You have to figure out all four colours in right sort for winning.'
|
11
|
+
puts 'Colours: R, O, Y, G, B, I, V ---- Exit from the game if your input contains 0 (zero).'
|
12
|
+
puts 'Only the first 4 characters will be analyzed.'
|
13
|
+
puts 'Example guess: OYGB'
|
14
|
+
puts "Let's start!"
|
15
|
+
puts ''
|
16
|
+
puts '-----------------------------------------------------------------------------'
|
17
|
+
puts ''
|
18
|
+
end
|
19
|
+
|
20
|
+
def input(q='')
|
21
|
+
str = 'Enter a sequence a 4 character sequence from ROYGBIV or 0 to exit > '
|
22
|
+
str = '> ' if q == "exit"
|
23
|
+
Readline::readline str, true
|
24
|
+
end
|
25
|
+
|
26
|
+
def printer(figured_out, clues, gamer)
|
27
|
+
puts "Lives: #{gamer.lives}"
|
28
|
+
puts "Code: #{figured_out.join(' ')}, Guessed: #{gamer.guess.join(' ')}, Clue: #{clues}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def exitmessage
|
32
|
+
puts "Thanks for your game. See you soon!"
|
33
|
+
end
|
34
|
+
|
35
|
+
def duplicatepatches
|
36
|
+
puts "Hey, duplicate patches are not allowed and please re enter a new code patch."
|
37
|
+
end
|
38
|
+
|
39
|
+
def lost(code)
|
40
|
+
puts "YOU LOOSE! The code was: #{code}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def win
|
44
|
+
puts "**** CONGRATULATION! YOU WIN! ****"
|
45
|
+
end
|
46
|
+
|
47
|
+
def want_to_play_again?
|
48
|
+
print "Do you want to play again (Y/N)?"
|
49
|
+
loop do
|
50
|
+
i = input("exit")
|
51
|
+
case i
|
52
|
+
when "Y"
|
53
|
+
return true
|
54
|
+
when "N"
|
55
|
+
return false
|
56
|
+
else
|
57
|
+
print "Sorry? (Y/N)"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module DsaCodebreakerGame
|
2
|
+
|
3
|
+
class Game
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@code = Code.new
|
7
|
+
@gamer = Gamer.new
|
8
|
+
@console = Console.new
|
9
|
+
@clues = 0
|
10
|
+
@figured_out = ['_','_','_','_']
|
11
|
+
end
|
12
|
+
|
13
|
+
def start
|
14
|
+
@console.welcome(@gamer)
|
15
|
+
looper
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def looper
|
21
|
+
|
22
|
+
loop do
|
23
|
+
@console.printer(@figured_out, @clues, @gamer)
|
24
|
+
|
25
|
+
g = @console.input[0..3]
|
26
|
+
|
27
|
+
if @gamer.history.include?g
|
28
|
+
@console.duplicatepatches
|
29
|
+
redo
|
30
|
+
else
|
31
|
+
@gamer.guess = g
|
32
|
+
end
|
33
|
+
|
34
|
+
if @gamer.guess.include?("0")
|
35
|
+
@console.exitmessage
|
36
|
+
exit
|
37
|
+
end
|
38
|
+
|
39
|
+
@clues = 0
|
40
|
+
@remained = 4
|
41
|
+
|
42
|
+
evaluate
|
43
|
+
|
44
|
+
if @gamer.died?
|
45
|
+
@console.lost(@code.join(" "))
|
46
|
+
again
|
47
|
+
end
|
48
|
+
|
49
|
+
if @remained == 0
|
50
|
+
@console.win
|
51
|
+
again
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
def again
|
61
|
+
if @console.want_to_play_again?
|
62
|
+
game = DsaCodebreakerGame::Game.new
|
63
|
+
game.start
|
64
|
+
else
|
65
|
+
@console.exitmessage
|
66
|
+
exit
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
#Inspiration from here: https://gist.github.com/mariozig/4512157
|
72
|
+
def evaluate
|
73
|
+
|
74
|
+
counter = @gamer.guess.length
|
75
|
+
@gamer.guess.each_with_index do |char, i|
|
76
|
+
|
77
|
+
|
78
|
+
if @code[i] == char
|
79
|
+
|
80
|
+
@figured_out[i] = char
|
81
|
+
@remained-=1
|
82
|
+
elsif @code.include?(char)
|
83
|
+
|
84
|
+
@clues+=1
|
85
|
+
else
|
86
|
+
|
87
|
+
counter-=1
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
@gamer.lost_a_life if counter == 0
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module DsaCodebreakerGame
|
2
|
+
|
3
|
+
class Gamer
|
4
|
+
|
5
|
+
attr_accessor :lives, :guess, :history
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@lives = 8
|
9
|
+
@history = []
|
10
|
+
@guess = ['_','_','_','_']
|
11
|
+
end
|
12
|
+
|
13
|
+
def lost_a_life
|
14
|
+
@lives -= 1
|
15
|
+
end
|
16
|
+
|
17
|
+
def died?
|
18
|
+
@lives < 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def guess=(guess)
|
22
|
+
@history << guess
|
23
|
+
@guess = guess.chomp.split('')
|
24
|
+
end
|
25
|
+
|
26
|
+
def guess
|
27
|
+
@guess
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'dsa_codebreaker_game'
|
3
|
+
|
4
|
+
module DsaCodebreakerGame
|
5
|
+
|
6
|
+
|
7
|
+
describe Code do
|
8
|
+
|
9
|
+
let(:gamer) {Gamer.new}
|
10
|
+
|
11
|
+
it 'should lost a life' do
|
12
|
+
gamer.lost_a_life
|
13
|
+
gamer.lives.should == 7
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should be died' do
|
17
|
+
9.times {gamer.lost_a_life}
|
18
|
+
gamer.died?.should == true
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'dsa_codebreaker_game'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
6
|
+
config.run_all_when_everything_filtered = true
|
7
|
+
config.filter_run :focus
|
8
|
+
config.order = 'random'
|
9
|
+
config.color_enabled = true
|
10
|
+
config.formatter = 'documentation'
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dsa_codebreaker_game
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zoltan Debre
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-04 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rb-readline
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
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: A variation on MasterMind game
|
70
|
+
email:
|
71
|
+
- ''
|
72
|
+
executables:
|
73
|
+
- dsa_codebreaker_game
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/dsa_codebreaker_game
|
83
|
+
- dsa_codebreaker_game.gemspec
|
84
|
+
- lib/dsa_codebreaker_game.rb
|
85
|
+
- lib/dsa_codebreaker_game/code.rb
|
86
|
+
- lib/dsa_codebreaker_game/console.rb
|
87
|
+
- lib/dsa_codebreaker_game/game.rb
|
88
|
+
- lib/dsa_codebreaker_game/gamer.rb
|
89
|
+
- lib/dsa_codebreaker_game/version.rb
|
90
|
+
- spec/lib/code_spec.rb
|
91
|
+
- spec/lib/gamer_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
homepage: https://github.com/szines/dsa_codebreaker_game
|
94
|
+
licenses:
|
95
|
+
- MIT/DIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.0.3
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: This is a code breaker game with simplified rules. A coding challenge from
|
117
|
+
Digital Skills Academy.
|
118
|
+
test_files:
|
119
|
+
- spec/lib/code_spec.rb
|
120
|
+
- spec/lib/gamer_spec.rb
|
121
|
+
- spec/spec_helper.rb
|