mastermind_sname 1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +7 -0
- data/Gemfile +5 -0
- data/LICENSE +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/mastermind +5 -0
- data/bin/setup +7 -0
- data/game_records.json +1 -0
- data/lib/mastermind_sname/sname/checkers.rb +57 -0
- data/lib/mastermind_sname/sname/colour_generator.rb +53 -0
- data/lib/mastermind_sname/sname/commands.rb +15 -0
- data/lib/mastermind_sname/sname/dump_data.rb +75 -0
- data/lib/mastermind_sname/sname/game.rb +129 -0
- data/lib/mastermind_sname/sname/messages.rb +117 -0
- data/lib/mastermind_sname/sname/player_details.rb +33 -0
- data/lib/mastermind_sname/sname/test.rb +0 -0
- data/lib/mastermind_sname/sname/testjason.json +0 -0
- data/lib/mastermind_sname/sname/timer.rb +26 -0
- data/lib/mastermind_sname/version.rb +3 -0
- data/lib/mastermind_sname.rb +34 -0
- data/lib/records.json +0 -0
- data/mastermind_sname.gemspec +25 -0
- data/rspec.rake +0 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 67589eac9cd726e2ba4f78c089f239bf7dc55cb2
|
4
|
+
data.tar.gz: 7858ff6c00b0fac69d94fe26237cbd5d3e7d9f1b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a593657e9430e598931f869e48ba2def9a3bc22ac349d1682d935d7a528bdb69d196f96f1865a8131b886c96511979a9429417db53a964cb83d335ff79776aa7
|
7
|
+
data.tar.gz: f7d9ea3f0f6a9915bb82ea70640cb64bcdc259afc943c357af7c74b49cb36e4d10bfa54cbb590e960783fbb0cca0264566d4f88acc174c90c42cda5c19549532
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Olumuyiwa Osiname
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Olumuyiwa Osiname
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
<a href="https://codeclimate.com/github/andela-oosiname/mastermind_sname"><img src="https://codeclimate.com/github/andela-oosiname/mastermind_sname/badges/gpa.svg" /></a>
|
2
|
+
[](https://travis-ci.org/andela-oosiname/mastermind_sname)
|
3
|
+
<a href="https://codeclimate.com/github/andela-oosiname/mastermind_sname/coverage"><img src="https://codeclimate.com/github/andela-oosiname/mastermind_sname/badges/coverage.svg" /></a>
|
4
|
+
# MastermindSname
|
5
|
+
|
6
|
+
Welcome to Mastermind by sname. An interactive gem packaged and played in a ruby environment.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
The User should have ruby set on his pc
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```rub
|
15
|
+
gem 'mastermind_sname'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install mastermind_sname
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
A splash screen is showed once a game is started from where the user can start a game or read instructions.
|
29
|
+
|
30
|
+
A random code of varying legth is generated depending on the level selected by the user.
|
31
|
+
|
32
|
+
You have twelve guesses per game.
|
33
|
+
|
34
|
+
On every guess, you are presented with a message identifying the number of elements you got correctly, and in what positions.
|
35
|
+
|
36
|
+
To view entry history, enter h or history at any time.
|
37
|
+
To view sequence generated, enter c or cheat at any time
|
38
|
+
To quit the game at any point enter q or quit.
|
39
|
+
|
40
|
+
The game is timed and on successful completion, the user gets a detail of his performance and a top players list is displayed
|
41
|
+
|
42
|
+
|
43
|
+
## Development
|
44
|
+
|
45
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
46
|
+
|
47
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it ( https://github.com/andela-oosiname/mastermind_sname/fork )
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mastermind_sname"
|
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/mastermind
ADDED
data/bin/setup
ADDED
data/game_records.json
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"beginner":[{"name":"Aaaa","game_colours":"rgrr","guesses":5,"times":"25s"}],"intermediate":[],"advanced":[]}
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module SnameLogics
|
2
|
+
class Logics
|
3
|
+
def self.is_valid_level?(level)
|
4
|
+
@level = level
|
5
|
+
if @level == "a" || @level == "i" || @level == "b"
|
6
|
+
return true
|
7
|
+
else
|
8
|
+
return false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.check_guess_length?(guess,level)
|
13
|
+
result = case
|
14
|
+
when level == "b" && guess.length > 4 then return "guess is too long"
|
15
|
+
when level == "i" && guess.length > 6 then return "guess is too long"
|
16
|
+
when level == "a" && guess.length > 8 then return "guess is too long"
|
17
|
+
end
|
18
|
+
return "guess is too short"
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.is_input_command?(guess)
|
22
|
+
return true if guess == "cheat" || guess == "exit" || guess == "h" || guess == "c" || guess == "q"
|
23
|
+
return true if guess == "quit"
|
24
|
+
return false
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.check_guess?(guess,level)
|
28
|
+
return true if guess.length == 4 && level == "b"
|
29
|
+
return true if guess.length == 6 && level == "i"
|
30
|
+
return true if guess.length == 8 && level == "a"
|
31
|
+
return false
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.get_no_correct_elements
|
35
|
+
no_of_elements = 0
|
36
|
+
for i in (0...@user_guess.length) do
|
37
|
+
no_of_elements += 1 if @user_guess.split("").sort[i] == @colour_code.sort[i]
|
38
|
+
end
|
39
|
+
no_of_elements
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.get_no_correct_positions
|
43
|
+
correct_positions = 0
|
44
|
+
for i in (0... @user_guess.length) do
|
45
|
+
correct_positions += 1 if @user_guess[i] == @colour_code[i]
|
46
|
+
end
|
47
|
+
correct_positions
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.get_feedback(user_guess,colour_code)
|
51
|
+
@user_guess = user_guess
|
52
|
+
@colour_code = colour_code
|
53
|
+
feedback = "#{@user_guess} has #{get_no_correct_elements} of the correct elements with #{get_no_correct_positions} in the correct positions"
|
54
|
+
feedback
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module SnameGameColours
|
2
|
+
class GameColours
|
3
|
+
|
4
|
+
attr_accessor :beginner_colours, :intermediate_colours, :advanced_colours, :level
|
5
|
+
|
6
|
+
def self.beginner_colours
|
7
|
+
["r","y","g","b"]
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.intermediate_colours
|
11
|
+
beginner_colours << "o"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.advanced_colours
|
15
|
+
intermediate_colours << "v"
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def self.set_colours(level)
|
20
|
+
case level
|
21
|
+
when "b"
|
22
|
+
return beginner_colours
|
23
|
+
when "i"
|
24
|
+
return intermediate_colours
|
25
|
+
when "a"
|
26
|
+
return advanced_colours
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.get_colours(level)
|
31
|
+
@level = level
|
32
|
+
final_colour_array = []
|
33
|
+
initial_colour_array = set_colours(level)
|
34
|
+
code_length = set_length_of_colours
|
35
|
+
until final_colour_array.length == code_length
|
36
|
+
final_colour_array << initial_colour_array.sample
|
37
|
+
end
|
38
|
+
final_colour_array
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.set_length_of_colours
|
42
|
+
case @level
|
43
|
+
when "b"
|
44
|
+
return 4
|
45
|
+
when "i"
|
46
|
+
return 6
|
47
|
+
when "a"
|
48
|
+
return 8
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative "game.rb"
|
2
|
+
module SnameCommands
|
3
|
+
class Commands
|
4
|
+
def self.command_action(to_do,colours)
|
5
|
+
colours = colours
|
6
|
+
exit if to_do == "quit" || to_do == "q"
|
7
|
+
cheat_mode(colours) if to_do == "cheat" || to_do == "c"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.cheat_mode(game_colours)
|
11
|
+
"The sequence is #{game_colours.join}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module BuildRecord
|
2
|
+
class Sname
|
3
|
+
require 'json'
|
4
|
+
def set_new_record(player_hash)
|
5
|
+
@player_hash = player_hash
|
6
|
+
@record = get_record
|
7
|
+
@new_record = @record[player_hash["full_level"]] << {"name"=>player_hash["name"],"game_colours" => player_hash["game_colours"].join(""), "guesses" => player_hash["guesses_count"], "times" => player_hash["time"]}
|
8
|
+
select_level
|
9
|
+
end
|
10
|
+
def get_record
|
11
|
+
json = File.read("game_records.json")
|
12
|
+
obj = JSON.parse(json)
|
13
|
+
return obj
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def select_level
|
18
|
+
case @player_hash["level"]
|
19
|
+
when "b"
|
20
|
+
update_beginner_record
|
21
|
+
when "i"
|
22
|
+
update_intermediate_record
|
23
|
+
when "a"
|
24
|
+
update_advanced_record
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def update_beginner_record
|
29
|
+
tempHash = {
|
30
|
+
"beginner" => @new_record,
|
31
|
+
"intermediate" => @record["intermediate"],
|
32
|
+
"advanced" => @record["advanced"]
|
33
|
+
}
|
34
|
+
File.open("game_records.json","w") do |f|
|
35
|
+
f.write(tempHash.to_json)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def update_intermediate_record
|
40
|
+
tempHash = {
|
41
|
+
"beginner" => @record["beginner"],
|
42
|
+
"intermediate" => @new_record,
|
43
|
+
"advanced" => @record["advanced"]
|
44
|
+
}
|
45
|
+
File.open("game_records.json","w") do |f|
|
46
|
+
f.write(tempHash.to_json)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
def update_advanced_record
|
50
|
+
tempHash = {
|
51
|
+
"beginner" => @record["beginner"],
|
52
|
+
"intermediate" => @record["intermediate"],
|
53
|
+
"advanced" => @new_record
|
54
|
+
}
|
55
|
+
File.open("game_records.json","w") do |f|
|
56
|
+
f.write(tempHash.to_json)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def display_top_ten(player_hash)
|
61
|
+
SnameMessages::Messages.top_ten_head
|
62
|
+
level = player_hash["full_level"]
|
63
|
+
record = get_record
|
64
|
+
level_record = record[level].sort_by { |hsh| hsh["guesses"] }
|
65
|
+
n = 0
|
66
|
+
record[level].length.times do
|
67
|
+
print "#{level_record[n]["name"]} solved #{level_record[n]["game_colours"]} in #{level_record[n]["guesses"]} guesses over #{level_record[n]["times"]}"
|
68
|
+
n += 1
|
69
|
+
print "\n"
|
70
|
+
break if n == 10
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require_relative "commands.rb"
|
2
|
+
require_relative "dump_data.rb"
|
3
|
+
module SnameGame
|
4
|
+
class Game
|
5
|
+
attr_accessor :level,:game_colours,:user_colours,:number_of_guesses, :user_guess, :player_hash, :user_guess_array, :user_guess_count
|
6
|
+
|
7
|
+
def initialize_values
|
8
|
+
@user_guess_count = 0
|
9
|
+
@user_guess_array = []
|
10
|
+
@player_hash = SnamePlayer::Player.get_player
|
11
|
+
@game_colours = SnameGameColours::GameColours.get_colours(@player_hash["level"])
|
12
|
+
create_records_file unless File.file?("game_records.json")
|
13
|
+
File.file?("game_records.json")
|
14
|
+
end
|
15
|
+
|
16
|
+
def play
|
17
|
+
initialize_values
|
18
|
+
@player_hash["start_time"] = SnameTimer::Timer.set_time
|
19
|
+
SnameMessages::Messages.start_message(@player_hash,@game_colours)
|
20
|
+
start_guessing
|
21
|
+
play_again
|
22
|
+
end
|
23
|
+
|
24
|
+
def start_guessing
|
25
|
+
while @user_guess_count <= 11 && !is_correct? do
|
26
|
+
collect_user_guess
|
27
|
+
break if is_quit?
|
28
|
+
puts record_guess
|
29
|
+
end
|
30
|
+
unless SnameLogics::Logics.is_input_command?(@user_guess)
|
31
|
+
puts game_end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def is_quit?
|
36
|
+
arr = ["cheat","quit","q","c"]
|
37
|
+
arr.each do |x|
|
38
|
+
return true if @user_guess == x
|
39
|
+
end
|
40
|
+
return false
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def is_correct?
|
45
|
+
return true if @user_guess == @game_colours.join("")
|
46
|
+
return false
|
47
|
+
end
|
48
|
+
|
49
|
+
def create_records_file
|
50
|
+
system "echo '{\"beginner\":[],\"intermediate\":[],\"advanced\":[]}' > game_records.json"
|
51
|
+
File.file?("game_records.json")
|
52
|
+
end
|
53
|
+
|
54
|
+
def update_player
|
55
|
+
@player_hash["guesses_count"] = @user_guess_array.length
|
56
|
+
@player_hash["full_level"] = get_full_level_name
|
57
|
+
@player_hash["game_colours"] = @game_colours
|
58
|
+
@player_hash["end_time"] = SnameTimer::Timer.set_time
|
59
|
+
@player_hash["time"] = SnameTimer::Timer.get_time_taken(@player_hash["start_time"],@player_hash["end_time"])
|
60
|
+
@player_hash
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_full_level_name
|
64
|
+
case @player_hash["level"]
|
65
|
+
when "i" then return "intermediate"
|
66
|
+
when "b" then return "beginner"
|
67
|
+
when "a" then return "advanced"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def game_end
|
72
|
+
if @user_guess_count > 11
|
73
|
+
return "GAME OVER! Out of Guesses"
|
74
|
+
else
|
75
|
+
update_player
|
76
|
+
BuildRecord::Sname.new.set_new_record(@player_hash)
|
77
|
+
SnameMessages::Messages.congratulations_screen(@player_hash)
|
78
|
+
BuildRecord::Sname.new.display_top_ten(@player_hash)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def collect_user_guess
|
83
|
+
valid = false
|
84
|
+
puts "You've taken #{@user_guess_array.length} guess(es)" if @user_guess_array != []
|
85
|
+
@user_guess = gets.chomp.downcase
|
86
|
+
if @user_guess == "h"
|
87
|
+
valid = true
|
88
|
+
return get_guess_history
|
89
|
+
end
|
90
|
+
until valid == true do
|
91
|
+
valid = true if SnameLogics::Logics.is_input_command?(@user_guess)
|
92
|
+
puts SnameCommands::Commands.command_action(@user_guess,@game_colours)
|
93
|
+
valid = SnameLogics::Logics.check_guess?(@user_guess, @player_hash["level"]) if !valid
|
94
|
+
puts SnameLogics::Logics.check_guess_length?(@user_guess, @player_hash["level"]) if !valid
|
95
|
+
@user_guess = gets.chomp.downcase if !valid
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def user_guess_check
|
100
|
+
end
|
101
|
+
|
102
|
+
def play_again
|
103
|
+
puts "Do you want to play again? (y for yes/ press any other key to quit)"
|
104
|
+
choice = gets.chomp.downcase
|
105
|
+
if choice == "y"
|
106
|
+
MastermindSname::Sname.start
|
107
|
+
else
|
108
|
+
exit
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def get_guess_history
|
113
|
+
for i in (0...@user_guess_array.length) do print " #{i+1} ==> #{@user_guess_array[i]} \n" end
|
114
|
+
print "\n"
|
115
|
+
puts "Enter guess"
|
116
|
+
return @user_guess_array
|
117
|
+
end
|
118
|
+
|
119
|
+
def record_guess
|
120
|
+
if @user_guess != "h"
|
121
|
+
@user_guess_count += 1
|
122
|
+
@user_guess_array << @user_guess
|
123
|
+
is_correct?
|
124
|
+
SnameLogics::Logics.get_feedback(@user_guess,@game_colours)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module SnameMessages
|
2
|
+
class Messages
|
3
|
+
def self.invalid_level
|
4
|
+
puts "Please enter a valid level, 'b' for Beginner, 'i' for Intermediate, 'a' for Advanced"
|
5
|
+
end
|
6
|
+
def self.invalid_code
|
7
|
+
puts "Your colour code contains invalid letters. TIP: it should be one of r,g,y,b,o,v"
|
8
|
+
end
|
9
|
+
def self.get_level_message
|
10
|
+
puts "Select your level: (b)eginner, (i)ntermediate, (a)dvanced"
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.congratulations_screen(player_hash)
|
14
|
+
@player_hash = player_hash
|
15
|
+
horizontal_stars
|
16
|
+
vertical_stars
|
17
|
+
body_message(player_hash)
|
18
|
+
vertical_stars
|
19
|
+
horizontal_stars
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.start_message(player_hash,game_colours)
|
23
|
+
colours = SnameGameColours::GameColours.beginner_colours.join("") if game_colours.length == 4
|
24
|
+
colours = SnameGameColours::GameColours.intermediate_colours.join("") if game_colours.length == 6
|
25
|
+
colours = SnameGameColours::GameColours.advanced_colours.join("") if game_colours.length == 8
|
26
|
+
puts "Hello #{player_hash["name"]} I have generated a sequence with #{game_colours.length} elements made up of a combination of any of: #{colours}... Can you guess the colour"
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def self.horizontal_stars
|
31
|
+
30.times do
|
32
|
+
print "* "
|
33
|
+
end
|
34
|
+
print "\n"
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.vertical_stars
|
38
|
+
3.times do
|
39
|
+
print "*"
|
40
|
+
blank_space(59)
|
41
|
+
print "*"
|
42
|
+
print "\n"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.body_message(player_hash)
|
47
|
+
blank_space(10)
|
48
|
+
puts "CONGRATULATIONS!! You got the sequence '#{player_hash["game_colours"].join("")}'"
|
49
|
+
blank_space(10)
|
50
|
+
puts "LEVEL: #{player_hash["full_level"]}"
|
51
|
+
blank_space(10)
|
52
|
+
puts "GUESSES: #{player_hash["guesses_count"]}"
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.blank_space(n)
|
56
|
+
n.times do
|
57
|
+
print " "
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.message_border
|
62
|
+
blank_space(10)
|
63
|
+
56.times do
|
64
|
+
print "="
|
65
|
+
end
|
66
|
+
print "\n"
|
67
|
+
blank_space(20)
|
68
|
+
end
|
69
|
+
def self.goodbye_message
|
70
|
+
message_border
|
71
|
+
puts "Thank you for playing MASTERMIND"
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.splash_screen
|
75
|
+
blank_space(10)
|
76
|
+
56.times do
|
77
|
+
print "="
|
78
|
+
end
|
79
|
+
print "\n"
|
80
|
+
blank_space(10)
|
81
|
+
puts "Welcome to MASTERMIND"
|
82
|
+
blank_space(10)
|
83
|
+
puts "Would you like to (p)lay, (r)ead instructions or (q)uit"
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.border
|
87
|
+
59.times do
|
88
|
+
print "="
|
89
|
+
end
|
90
|
+
print "\n"
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.top_ten_head
|
94
|
+
20.times do
|
95
|
+
print "="
|
96
|
+
end
|
97
|
+
print "Top Players"
|
98
|
+
20.times do
|
99
|
+
print "="
|
100
|
+
end
|
101
|
+
print "\n"
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.get_instructions
|
105
|
+
print "Instructions\nEnter p from splash screen to start a new game\n\nEnter your name and select a level
|
106
|
+
\nb - beginner, code is 4 elements long\ni - intermediate, code is 6 elements long\na - advanced, code is 8 elements long
|
107
|
+
\nRandom code of elements is generated depending on the level. You have 12 guesses
|
108
|
+
\nafter every guess, you are presented with a message identifying the number of elements you got correctly, and in what positions.
|
109
|
+
\nTo view entry history, enter h or history at any time.
|
110
|
+
\nTo view sequence generated, enter c or cheat at any time
|
111
|
+
\nTo quit the game at any point enter q or quit.
|
112
|
+
\nThe game is timed and on successful completion, the user gets a detail of his performance and a top players list is displayed"
|
113
|
+
puts ""
|
114
|
+
MastermindSname::Sname.start
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module SnamePlayer
|
2
|
+
class Player
|
3
|
+
attr_accessor :level, :name
|
4
|
+
|
5
|
+
def self.set_player
|
6
|
+
set_player_name
|
7
|
+
set_level
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.set_player_name
|
11
|
+
puts "Enter first name"
|
12
|
+
@name = gets.chomp.downcase
|
13
|
+
@name.capitalize!
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.set_level
|
17
|
+
puts SnameMessages::Messages.get_level_message
|
18
|
+
level = gets.chomp.downcase
|
19
|
+
status = SnameLogics::Logics.is_valid_level?(level)
|
20
|
+
while status == false do
|
21
|
+
puts SnameMessages::Messages.invalid_level
|
22
|
+
level = gets.chomp.downcase
|
23
|
+
status = SnameLogics::Logics.is_valid_level?(level)
|
24
|
+
end
|
25
|
+
@level = level
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.get_player
|
29
|
+
{"name" => @name, "level" => @level}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
File without changes
|
File without changes
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module SnameTimer
|
2
|
+
class Timer
|
3
|
+
|
4
|
+
def self.set_time
|
5
|
+
time = Time.now
|
6
|
+
time_array = [time.hour, time.min, time.sec]
|
7
|
+
(time_array[0] * 3600) + (time_array[1] * 60) + time_array[2]
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.format_time(time_interval)
|
11
|
+
mins = 0
|
12
|
+
while time_interval > 59 do
|
13
|
+
mins = mins + time_interval/60
|
14
|
+
time_interval -= (mins * 60)
|
15
|
+
end
|
16
|
+
secs = time_interval
|
17
|
+
return "#{secs}s" if mins == 0
|
18
|
+
return "#{mins}m#{secs}s"
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.get_time_taken(start_time,end_time)
|
22
|
+
time_interval = end_time.to_i - start_time.to_i
|
23
|
+
format_time(time_interval)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative "mastermind_sname/sname/test.rb"
|
2
|
+
require_relative "mastermind_sname/sname/colour_generator.rb"
|
3
|
+
require_relative "mastermind_sname/sname/game.rb"
|
4
|
+
require_relative "mastermind_sname/sname/player_details.rb"
|
5
|
+
require_relative "mastermind_sname/sname/messages.rb"
|
6
|
+
require_relative "mastermind_sname/sname/checkers.rb"
|
7
|
+
require_relative "mastermind_sname/sname/commands.rb"
|
8
|
+
require_relative "mastermind_sname/sname/timer.rb"
|
9
|
+
|
10
|
+
module MastermindSname
|
11
|
+
class Sname
|
12
|
+
def self.start
|
13
|
+
SnameMessages::Messages.splash_screen
|
14
|
+
my_choice = get_choice
|
15
|
+
start_choice(my_choice)
|
16
|
+
end
|
17
|
+
def self.start_choice(user_choice)
|
18
|
+
case user_choice
|
19
|
+
when "p", "play"
|
20
|
+
SnamePlayer::Player.set_player
|
21
|
+
SnameGame::Game.new.play
|
22
|
+
when "q","quit" then exit
|
23
|
+
when "r", "read" then SnameMessages::Messages.get_instructions
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.get_choice
|
28
|
+
choice = gets.chomp.downcase
|
29
|
+
choice
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
data/lib/records.json
ADDED
File without changes
|
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'mastermind_sname/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "mastermind_sname"
|
7
|
+
spec.version = "1.0"
|
8
|
+
spec.authors = ["Olumuyiwa Osiname"]
|
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.}
|
13
|
+
spec.homepage = "https://github.com/andela-oosiname/mastermind_sname"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "bin"
|
18
|
+
spec.executables = ["mastermind"]
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
end
|
25
|
+
|
data/rspec.rake
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mastermind_sname
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Olumuyiwa Osiname
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-06 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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: 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: Mastermind is a game where a user tries to guess a sequence of colour
|
56
|
+
codes.
|
57
|
+
email:
|
58
|
+
- olumuyiwa.osiname@andela.com
|
59
|
+
executables:
|
60
|
+
- mastermind
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/mastermind
|
73
|
+
- bin/setup
|
74
|
+
- game_records.json
|
75
|
+
- lib/mastermind_sname.rb
|
76
|
+
- lib/mastermind_sname/sname/checkers.rb
|
77
|
+
- lib/mastermind_sname/sname/colour_generator.rb
|
78
|
+
- lib/mastermind_sname/sname/commands.rb
|
79
|
+
- lib/mastermind_sname/sname/dump_data.rb
|
80
|
+
- lib/mastermind_sname/sname/game.rb
|
81
|
+
- lib/mastermind_sname/sname/messages.rb
|
82
|
+
- lib/mastermind_sname/sname/player_details.rb
|
83
|
+
- lib/mastermind_sname/sname/test.rb
|
84
|
+
- lib/mastermind_sname/sname/testjason.json
|
85
|
+
- lib/mastermind_sname/sname/timer.rb
|
86
|
+
- lib/mastermind_sname/version.rb
|
87
|
+
- lib/records.json
|
88
|
+
- mastermind_sname.gemspec
|
89
|
+
- rspec.rake
|
90
|
+
homepage: https://github.com/andela-oosiname/mastermind_sname
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.8
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: This installs mastermind on the pc.
|
114
|
+
test_files: []
|