mastermind-konr 1.0.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: 888281df669c72c3c9484217afe330b6647954fa
4
+ data.tar.gz: 375f40b12263734df5dd03cdd9682294905a62c6
5
+ SHA512:
6
+ metadata.gz: bf35e68bd47ff6410d8083477ee83ecfc7b7dccb81889a869071139f4790850d023946f0fd6d12f888716309e35ae7e3fb6fe44d6e1d403a8100d0578f213c86
7
+ data.tar.gz: fb076ddc8c976a84ef66c2cc9411e52a828b6c2f1860ab491515c972e50480f670711c370f8900c4b9af9725a4e3980cfeaf0ee6f504c2f92d5f392b02ba6927
data/lib/cli.rb ADDED
@@ -0,0 +1,40 @@
1
+ class CLI
2
+ attr_reader :command, :printer
3
+
4
+ def initialize
5
+ @command = ""
6
+ @printer = Printer.new
7
+ end
8
+
9
+ def start
10
+ printer.welcome
11
+ until finished?
12
+ printer.command
13
+ @command = gets.strip
14
+ process_initial_commands
15
+ end
16
+ printer.ending
17
+ end
18
+
19
+ def process_initial_commands
20
+ case
21
+ when play?
22
+ game = Game.new(printer)
23
+ game.play
24
+ when instructions?
25
+ printer.game_instructions
26
+ end
27
+ end
28
+
29
+ def play?
30
+ command == "p"
31
+ end
32
+
33
+ def instructions?
34
+ command == "i"
35
+ end
36
+
37
+ def finished?
38
+ command == "q" || command == "quit"
39
+ end
40
+ end
data/lib/printer.rb ADDED
@@ -0,0 +1,101 @@
1
+ class Printer
2
+ def welcome
3
+ print "\e[2J\e[f"
4
+ puts"\n"
5
+ puts' Welcome to '.yellow
6
+ puts' _____ _____ ______________________________________ _____ .___ _______ ________ '.blue
7
+ puts' / \ / _ \ / _____\__ ___\_ _____\______ \ / \ | |\ \ \______ \ '.green
8
+ puts' / \ / \ / /_\ \ \_____ \ | | | __)_ | _// \ / \| |/ | \ | | \ '.red
9
+ puts' / Y / | \/ \ | | | \| | / Y | / | \| | \ '.yellow
10
+ puts' \____|__ \____|__ /_______ / |____| /_______ /|____|_ \____|__ |___\____|__ /_______ / '.blue
11
+ puts' \/ \/ \/ \/ \/ \/ \/ \/ '.green
12
+ puts' And may the odds be ever in your favor.'.red
13
+ puts "\n"
14
+ instruction
15
+ end
16
+
17
+ def instruction
18
+ puts "Enter 'p' to play, 'i' for instructions, or 'q' to quit."
19
+ end
20
+
21
+ def command
22
+ puts "\n"
23
+ print "Enter your command: "
24
+ end
25
+
26
+ def game_instructions
27
+ puts "The codebreaker tries to guess the pattern, in both order and color, within ten turns. \nEach guess is made by placing a guess of code colors on the decoding board. Once placed, \nthe computer provides feedback with how many are correct colors and how many are in the right location"
28
+ end
29
+
30
+ def ending
31
+ puts "Good bye. Come again"
32
+ end
33
+
34
+ def game_intro
35
+ puts "\n"
36
+ puts "Game initialized."
37
+ color_instructions
38
+ end
39
+
40
+ def turn(turns)
41
+ puts "\n"
42
+ puts "This is turn #{turns} of 10.".yellow
43
+ end
44
+
45
+ def command_request
46
+ puts "\n"
47
+ print "Enter your guess: "
48
+ end
49
+
50
+ def quit
51
+ puts "Exiting the game."
52
+ instruction
53
+ end
54
+
55
+ def win
56
+ puts ' _____.___. __ __.__ '.blue
57
+ puts ' \__ | | ____ __ __ / \ / \__| ____ '.blue
58
+ puts ' / | |/ _ \| | \ \ \/\/ / |/ \ '.blue
59
+ puts ' \____ ( <_> ) | / \ /| | | \ '.blue
60
+ puts ' / ______|\____/|____/ \__/\ / |__|___| / '.blue
61
+ puts ' \/ \/ \/ '.blue
62
+ instruction
63
+ end
64
+
65
+ def lose
66
+ puts' .____ __ '.blue
67
+ puts' | | ____ _______/ |_ '.blue
68
+ puts' | | / _ \/ ___/\ __\ '.blue
69
+ puts' | |__( <_> )___ \ | | '.blue
70
+ puts' |_______ \____/____ > |__| '.blue
71
+ puts' \/ \/ '.blue
72
+ puts "I'm sorry, you have lost. Would you like to try again"
73
+ puts "\n"
74
+ instruction
75
+ end
76
+
77
+ def invalid_command
78
+ puts "\n"
79
+ puts 'You have entered an invalid command please try again'.green
80
+ end
81
+
82
+ def invalid_guess_not_enough
83
+ puts "\n"
84
+ puts 'You have entered not enough letters please try again'.green
85
+ puts 'Format: rbgy or q to quit'.green
86
+ end
87
+ def invalid_guess_too_many
88
+ puts "\n"
89
+ puts 'You have entered too many lettters please try again'.green
90
+ puts 'Format: rbgy or q to quit'.green
91
+ end
92
+ def invalid_guess
93
+ puts "\n"
94
+ puts 'You have entered an invalid guess please try again'.green
95
+ puts 'Format: rbgy or q to quit'.green
96
+ end
97
+
98
+ def color_instructions
99
+ puts "Combine 'r' 'g' 'b' 'y' to make a 4 color code"
100
+ end
101
+ end
data/lib/sequence.rb ADDED
@@ -0,0 +1,16 @@
1
+ class Sequence
2
+
3
+ attr_reader :sequence
4
+
5
+ def initialize(sequence)
6
+ @sequence = sequence
7
+ end
8
+
9
+ def colors
10
+ counts = { 'r' => 0, 'b' => 0, 'y' => 0, 'g' => 0 }
11
+ @sequence.each do |color|
12
+ counts[color] += 1
13
+ end
14
+ counts
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ class SequenceGenerator
2
+ attr_accessor :sequence, :new_code
3
+
4
+ def self.colors
5
+ ["r","b","g","y"]
6
+ end
7
+
8
+ def self.sequence(length = 4)
9
+ (1..length).collect { colors.sample }
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ require_relative './sequence'
2
+
3
+ class SequenceValidator
4
+
5
+ attr_accessor :guess, :sequence
6
+
7
+ def initialize(guess, sequence)
8
+ @guess = guess
9
+ @sequence = sequence
10
+ end
11
+
12
+ def correct_colors
13
+ correct = 0
14
+ dup_sequence = sequence.dup
15
+
16
+ guess.each do |color|
17
+ if dup_sequence.include?(color)
18
+ index = dup_sequence.index(color)
19
+ dup_sequence.delete_at(index)
20
+ correct += 1
21
+ end
22
+ end
23
+ correct
24
+ end
25
+
26
+ def correct_positions
27
+ guess.each_with_index.count do |element, index_num|
28
+ element == sequence[index_num]
29
+ end
30
+ end
31
+ end
data/mastermind.rb ADDED
@@ -0,0 +1,5 @@
1
+ Dir["./lib/*.rb"].each { |file| require file }
2
+ require "colorize"
3
+ require "pry"
4
+
5
+ CLI.new.start
data/readme.md ADDED
@@ -0,0 +1 @@
1
+ Mastermind game in Ruby
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mastermind-konr
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Konr Larson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: mastermind game made in ruby
14
+ email: larsonkonr@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/cli.rb
20
+ - lib/printer.rb
21
+ - lib/sequence.rb
22
+ - lib/sequence_generator.rb
23
+ - lib/sequence_validator.rb
24
+ - mastermind.rb
25
+ - readme.md
26
+ homepage: https://rubygems.org/gems/mastermind-larsonkonr
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.4.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: mastermind
50
+ test_files: []