mastermind_sname 1.0 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ module MastermindSname
2
+ class GameColour
3
+ attr_accessor :level
4
+ def initialize(player)
5
+ @level = player[:level]
6
+ @colours = { b: %w(r y g b), i: %w(r y g b o), a: %w(r y g b o v) }
7
+ end
8
+
9
+ def get_colours
10
+ final_colour = []
11
+ initial_colour = @colours[@level.to_sym]
12
+ code_length = set_length_of_colours
13
+ until final_colour.length == code_length
14
+ final_colour << initial_colour.sample
15
+ end
16
+ final_colour
17
+ end
18
+
19
+ def set_length_of_colours
20
+ case @level
21
+ when "b" then return 4
22
+ when "i" then return 6
23
+ when "a" then return 8
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,54 @@
1
+ module MastermindSname
2
+ class GameLogic
3
+ def initialize(guess, game_colour)
4
+ @guess = guess
5
+ @game_colour = game_colour
6
+ end
7
+
8
+ def length_feedback
9
+ return "guess is too long" if @guess.length > @game_colour.length
10
+ "guess is too short"
11
+ end
12
+
13
+ def input_command?
14
+ commands = [:cheat, :exit, :h, :c, :q, :quit]
15
+ true if commands.include?(@guess.to_sym)
16
+ end
17
+
18
+ def valid_length?
19
+ @game_colour.length == @guess.length
20
+ end
21
+
22
+ def get_no_correct_elements
23
+ no_of_elements = 0
24
+ game_colour = @game_colour.sort
25
+ guess = @guess.split("").sort
26
+ game_colour.each_with_index do |val, index|
27
+ no_of_elements += 1 if val == guess[index]
28
+ end
29
+ no_of_elements
30
+ end
31
+
32
+ def get_no_correct_positions
33
+ correct_positions = 0
34
+ @game_colour.each_with_index do |val, index|
35
+ correct_positions += 1 if val == @guess[index]
36
+ end
37
+ correct_positions
38
+ end
39
+
40
+ def get_feedback
41
+ "#{@guess} has #{get_no_correct_elements} of"\
42
+ " the correct elements with #{get_no_correct_positions} in"\
43
+ " the correct positions"
44
+ end
45
+ end
46
+ end
47
+
48
+ class Integer
49
+ def time_format
50
+ mins = self / 60
51
+ secs = self % 60
52
+ "#{mins}m#{secs}s"
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+
2
+ module MastermindSname
3
+ module Input
4
+ def get_input
5
+ gets.chomp.downcase
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,131 @@
1
+ module MastermindSname
2
+ class Message
3
+ def invalid_level
4
+ puts "Enter valid level, \"b\" for"\
5
+ " Beginner, \"i\" for Intermediate, \"a\" for Advanced"
6
+ end
7
+
8
+ def get_level_message
9
+ puts "Select your level: (b)eginner, (i)ntermediate, (a)dvanced"
10
+ end
11
+
12
+ def congratulations_screen(player)
13
+ @player = player
14
+ horizontal_stars
15
+ vertical_stars
16
+ body_message
17
+ vertical_stars
18
+ horizontal_stars
19
+ end
20
+
21
+ def start(player, game_colours)
22
+ colours = "(r)ed, (g)reen, (y)ellow, (b)lue" if game_colours.length == 4
23
+ colours = "(r)ed, (g)reen, (y)ellow, (b)lue, (o)"\
24
+ "range" if game_colours.length == 6
25
+ colours = "(r)ed, (g)reen, (y)ellow, (b)lue, (o)"\
26
+ "range, (v)iolet" if game_colours.length == 8
27
+ "Hello #{player[:name]} I have generated a code with"\
28
+ " #{game_colours.length} elements made up of a combination of"\
29
+ " any of: #{colours}... Can you guess the colour"
30
+ end
31
+
32
+ def horizontal_stars
33
+ 30.times do
34
+ print "* "
35
+ end
36
+ print "\n"
37
+ end
38
+
39
+ def cheat(game_colours)
40
+ "The sequence is #{game_colours.join}"
41
+ end
42
+
43
+ def vertical_stars
44
+ 3.times do
45
+ print "*"
46
+ blank_space(59)
47
+ print "*"
48
+ print "\n"
49
+ end
50
+ end
51
+
52
+ def body_message
53
+ blank_space(10)
54
+ puts "CONGRATS! You got the sequence #{@player[:game_colours].join}"
55
+ blank_space(10)
56
+ puts "LEVEL: #{@player[:full_level]}"
57
+ blank_space(10)
58
+ puts "GUESSES: #{@player[:guesses_count]}"
59
+ end
60
+
61
+ def blank_space(n)
62
+ n.times do
63
+ print " "
64
+ end
65
+ end
66
+
67
+ def message_border
68
+ blank_space(10)
69
+ 56.times do
70
+ print "="
71
+ end
72
+ print "\n"
73
+ blank_space(20)
74
+ end
75
+
76
+ def goodbye_message
77
+ puts "Thank you for playing MASTERMIND"
78
+ end
79
+
80
+ def game_over
81
+ puts "Game Over, You are out of Guesses"
82
+ end
83
+
84
+ def splash_screen
85
+ blank_space(10)
86
+ 56.times do
87
+ print "="
88
+ end
89
+ print "\n"
90
+ blank_space(10)
91
+ puts "Welcome to MASTERMIND"
92
+ blank_space(10)
93
+ puts "Would you like to (p)lay, (r)ead instructions or (q)uit"
94
+ end
95
+
96
+ def border
97
+ 59.times do
98
+ print "="
99
+ end
100
+ print "\n"
101
+ end
102
+
103
+ def top_ten_head
104
+ 20.times do
105
+ print "="
106
+ end
107
+ print "Top Players"
108
+ 20.times do
109
+ print "="
110
+ end
111
+ print "\n"
112
+ end
113
+
114
+ def get_instructions
115
+ print "Instructions\nEnter p to start
116
+ \nEnter your name and level
117
+ \nb - beginner, i - intermediate, a - advanced
118
+ \nRandom code of elements. You have 10 guesses
119
+ \nTo view entry history, enter h at any time.
120
+ \nTo view sequence, enter c or cheat at any time
121
+ \nTo quit the game enter q or quit.
122
+ \nThe game is timed"
123
+ print "\n"
124
+ Sname.new.start_choice("p")
125
+ end
126
+
127
+ def play_again
128
+ puts "Do you want to play again? (y for yes/ press any other key to quit)"
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,44 @@
1
+ module MastermindSname
2
+ class Player
3
+ include Input
4
+ attr_accessor :level, :name
5
+
6
+ def initialize
7
+ @message = Message.new
8
+ end
9
+
10
+ def set_player
11
+ { name: set_player_name, level: set_level, full_level: set_full_level }
12
+ end
13
+
14
+ def set_player_name
15
+ puts "Enter first name"
16
+ @name = get_input
17
+ @name.capitalize!
18
+ end
19
+
20
+ def set_level
21
+ puts @message.get_level_message
22
+ @level = get_input
23
+ loop do
24
+ break if valid_level?
25
+ puts @message.invalid_level
26
+ @level = get_input
27
+ end
28
+ @level
29
+ end
30
+
31
+ def set_full_level
32
+ case @level
33
+ when "i" then "intermediate"
34
+ when "b" then "beginner"
35
+ when "a" then "advanced"
36
+ end
37
+ end
38
+
39
+ def valid_level?
40
+ valid_level = [:a, :b, :i]
41
+ valid_level.include?(@level.to_sym)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,40 @@
1
+ module MastermindSname
2
+ class Record
3
+ require "json"
4
+
5
+ def initialize(player)
6
+ @player = player
7
+ end
8
+
9
+ def set_new
10
+ @record = get_record
11
+ @record[@player[:full_level]] << {
12
+ name: @player[:name], game_colours: @player[:game_colours].join,
13
+ guesses: @player[:guesses_count], time: @player[:time] }
14
+ File.open("game_records.json", "w") { |f| f.write(@record.to_json) }
15
+ end
16
+
17
+ def get_record
18
+ json = File.read("game_records.json")
19
+ obj = JSON.parse(json)
20
+ obj
21
+ end
22
+
23
+ def display_top_ten
24
+ Message.new.top_ten_head
25
+ level = @player[:full_level]
26
+ record = get_record[level].sort_by { |hsh| hsh["guesses"] }
27
+ record.first(10).each do |val|
28
+ puts val.stringify
29
+ end
30
+ print "\n"
31
+ end
32
+ end
33
+ end
34
+
35
+ class Hash
36
+ def stringify
37
+ "#{self['name']} Guessed #{self['game_colours']} in "\
38
+ " #{self['guesses']} guesses within #{self['time']} "
39
+ end
40
+ end
@@ -1,25 +1,24 @@
1
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path("../lib", __FILE__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require 'mastermind_sname/version'
3
+ require "mastermind_sname/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "mastermind_sname"
7
- spec.version = "1.0"
7
+ spec.version = "2.0"
8
8
  spec.authors = ["Olumuyiwa Osiname"]
9
9
  spec.email = ["olumuyiwa.osiname@andela.com"]
10
-
11
- spec.summary = %q{This installs mastermind on the pc.}
12
- spec.description = %q{Mastermind is a game where a user tries to guess a sequence of colour codes.}
10
+ spec.summary = ["This installs mastermind on the pc."]
11
+ spec.description = ["Mastermind is a game where a user tries to"\
12
+ " guess a sequence of colour codes."]
13
13
  spec.homepage = "https://github.com/andela-oosiname/mastermind_sname"
14
14
  spec.license = "MIT"
15
-
16
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
17
18
  spec.bindir = "bin"
18
19
  spec.executables = ["mastermind"]
19
20
  spec.require_paths = ["lib"]
20
-
21
21
  spec.add_development_dependency "bundler"
22
22
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency 'rspec'
23
+ spec.add_development_dependency "rspec"
24
24
  end
25
-
@@ -0,0 +1,166 @@
1
+ {
2
+ "auto_complete":
3
+ {
4
+ "selected_items":
5
+ [
6
+ ]
7
+ },
8
+ "buffers":
9
+ [
10
+ ],
11
+ "build_system": "",
12
+ "build_system_choices":
13
+ [
14
+ ],
15
+ "build_varint": "",
16
+ "command_palette":
17
+ {
18
+ "height": 0.0,
19
+ "last_filter": "",
20
+ "selected_items":
21
+ [
22
+ ],
23
+ "width": 0.0
24
+ },
25
+ "console":
26
+ {
27
+ "height": 0.0,
28
+ "history":
29
+ [
30
+ ]
31
+ },
32
+ "distraction_free":
33
+ {
34
+ "menu_visible": true,
35
+ "show_minimap": false,
36
+ "show_open_files": false,
37
+ "show_tabs": false,
38
+ "side_bar_visible": false,
39
+ "status_bar_visible": false
40
+ },
41
+ "file_history":
42
+ [
43
+ "/Users/USER/Library/Application Support/Sublime Text 3/Packages/User/Preferences.sublime-settings",
44
+ "/Users/USER/Library/Application Support/Sublime Text 3/Packages/Default/Preferences.sublime-settings"
45
+ ],
46
+ "find":
47
+ {
48
+ "height": 0.0
49
+ },
50
+ "find_in_files":
51
+ {
52
+ "height": 0.0,
53
+ "where_history":
54
+ [
55
+ ]
56
+ },
57
+ "find_state":
58
+ {
59
+ "case_sensitive": false,
60
+ "find_history":
61
+ [
62
+ ],
63
+ "highlight": true,
64
+ "in_selection": false,
65
+ "preserve_case": false,
66
+ "regex": false,
67
+ "replace_history":
68
+ [
69
+ ],
70
+ "reverse": false,
71
+ "show_context": true,
72
+ "use_buffer2": true,
73
+ "whole_word": false,
74
+ "wrap": true
75
+ },
76
+ "groups":
77
+ [
78
+ {
79
+ "sheets":
80
+ [
81
+ ]
82
+ }
83
+ ],
84
+ "incremental_find":
85
+ {
86
+ "height": 0.0
87
+ },
88
+ "input":
89
+ {
90
+ "height": 0.0
91
+ },
92
+ "layout":
93
+ {
94
+ "cells":
95
+ [
96
+ [
97
+ 0,
98
+ 0,
99
+ 1,
100
+ 1
101
+ ]
102
+ ],
103
+ "cols":
104
+ [
105
+ 0.0,
106
+ 1.0
107
+ ],
108
+ "rows":
109
+ [
110
+ 0.0,
111
+ 1.0
112
+ ]
113
+ },
114
+ "menu_visible": true,
115
+ "output.find_results":
116
+ {
117
+ "height": 0.0
118
+ },
119
+ "pinned_build_system": "",
120
+ "project": "mastermind_sname.sublime-project",
121
+ "replace":
122
+ {
123
+ "height": 0.0
124
+ },
125
+ "save_all_on_build": true,
126
+ "select_file":
127
+ {
128
+ "height": 0.0,
129
+ "last_filter": "",
130
+ "selected_items":
131
+ [
132
+ ],
133
+ "width": 0.0
134
+ },
135
+ "select_project":
136
+ {
137
+ "height": 0.0,
138
+ "last_filter": "",
139
+ "selected_items":
140
+ [
141
+ ],
142
+ "width": 0.0
143
+ },
144
+ "select_symbol":
145
+ {
146
+ "height": 0.0,
147
+ "last_filter": "",
148
+ "selected_items":
149
+ [
150
+ ],
151
+ "width": 0.0
152
+ },
153
+ "selected_group": 0,
154
+ "settings":
155
+ {
156
+ },
157
+ "show_minimap": true,
158
+ "show_open_files": false,
159
+ "show_tabs": true,
160
+ "side_bar_visible": true,
161
+ "side_bar_width": 150.0,
162
+ "status_bar_visible": true,
163
+ "template_settings":
164
+ {
165
+ }
166
+ }