tobi-mastermind 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/console +14 -14
- data/bin/mastermind +4 -4
- data/bin/setup +7 -7
- data/lib/mastermind/tobi/gamehelper.rb +87 -105
- data/lib/mastermind/tobi/gamemethods.rb +106 -90
- data/lib/mastermind/tobi/gameplay.rb +14 -14
- data/lib/mastermind/tobi/logic.rb +37 -37
- data/lib/mastermind/tobi/multi_game.rb +98 -97
- data/lib/mastermind/tobi/player.rb +20 -20
- data/lib/mastermind/tobi/single_game.rb +117 -116
- data/lib/mastermind/tobi/store.yaml +137 -137
- data/lib/mastermind/tobi/timehelper.rb +38 -30
- data/lib/mastermind/tobi/ui.rb +70 -70
- data/lib/mastermind_start.rb +16 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 371b62b6e9da0e85c74d914c8bb1d53f76f1be71
|
4
|
+
data.tar.gz: ca7fbd38b813e053ce1069a604a6ae8002a87dde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1bcb4b69fdcc746c3ebc4959fb0111348aa99d5ab2a71d6d8c61679f13695f927fc1757cc1813b3515dc411dcc4105596a6eb5b9479602fde8207903d86b0fa5
|
7
|
+
data.tar.gz: d3a217459fce55788ad12f776f0fcd9acf3de982794ff3c8893fc8f6c46e79ad70867b15d24b6e91e13f56ab1a9fa2303246be77b9b359ce1f931fade7a98b36
|
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "tobi/mastermind"
|
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
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tobi/mastermind"
|
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
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'mastermind_start'
|
4
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'mastermind_start'
|
4
|
+
|
5
5
|
MasterMind::Tobi.start
|
data/bin/setup
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
#!/bin/bash
|
2
|
-
set -euo pipefail
|
3
|
-
IFS=$'\n\t'
|
4
|
-
|
5
|
-
bundle install
|
6
|
-
|
7
|
-
# Do any other automated setup that you need to do here
|
1
|
+
#!/bin/bash
|
2
|
+
set -euo pipefail
|
3
|
+
IFS=$'\n\t'
|
4
|
+
|
5
|
+
bundle install
|
6
|
+
|
7
|
+
# Do any other automated setup that you need to do here
|
@@ -1,106 +1,88 @@
|
|
1
|
-
require 'ui'
|
2
|
-
require 'logic'
|
3
|
-
require 'single_game'
|
4
|
-
require 'multi_game'
|
5
|
-
|
6
|
-
module MasterMind
|
7
|
-
module Tobi
|
8
|
-
|
9
|
-
def user_choice
|
10
|
-
option_chosen = false
|
11
|
-
|
12
|
-
while !option_chosen
|
13
|
-
option_chosen = true # assume user selects valid option so as to quit loop
|
14
|
-
|
15
|
-
input = gets.chomp.downcase
|
16
|
-
option_chosen = validate_input?(input)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def validate_input?(input)
|
21
|
-
case input
|
22
|
-
when "p", "play" then play_game
|
23
|
-
when "r", "read"
|
24
|
-
print_help
|
25
|
-
return false # continue loop after displaying help message
|
26
|
-
when "q", "quit" then exit
|
27
|
-
else # user selects an invalid option
|
28
|
-
print UI::INVALID_MESSAGE
|
29
|
-
return false
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
input
|
50
|
-
|
51
|
-
when "
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
option_chosen = false
|
89
|
-
|
90
|
-
while !option_chosen
|
91
|
-
option_chosen = true # assume user selects valid level so as to quit loop
|
92
|
-
|
93
|
-
input = gets.chomp.downcase
|
94
|
-
case input
|
95
|
-
when "b", "beginner" then return GameLogic::BEGINNER
|
96
|
-
when "i", "intermediate" then return GameLogic::INTERMEDIATE
|
97
|
-
when "a", "advanced" then return GameLogic::ADVANCED
|
98
|
-
else # user selects an invalid level
|
99
|
-
print UI::INVALID_MESSAGE
|
100
|
-
option_chosen = false
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
1
|
+
require 'ui'
|
2
|
+
require 'logic'
|
3
|
+
require 'single_game'
|
4
|
+
require 'multi_game'
|
5
|
+
|
6
|
+
module MasterMind
|
7
|
+
module Tobi
|
8
|
+
class GameHelper
|
9
|
+
def user_choice
|
10
|
+
option_chosen = false
|
11
|
+
|
12
|
+
while !option_chosen
|
13
|
+
option_chosen = true # assume user selects valid option so as to quit loop
|
14
|
+
|
15
|
+
input = gets.chomp.downcase
|
16
|
+
option_chosen = validate_input?(input)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def validate_input?(input)
|
21
|
+
case input
|
22
|
+
when "p", "play" then play_game
|
23
|
+
when "r", "read"
|
24
|
+
print_help
|
25
|
+
return false # continue loop after displaying help message
|
26
|
+
when "q", "quit" then exit
|
27
|
+
else # user selects an invalid option
|
28
|
+
print UI::INVALID_MESSAGE
|
29
|
+
return false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def play_game
|
34
|
+
game_logic = GameLogic.new(ask_level); sequence = game_logic.generate_sequence
|
35
|
+
ask_mode(sequence, game_logic)
|
36
|
+
puts ""
|
37
|
+
print UI::OPTIONS_MESSAGE + UI::INPUT_PROMPT
|
38
|
+
user_choice
|
39
|
+
end
|
40
|
+
|
41
|
+
def ask_mode(sequence, game_logic)
|
42
|
+
print UI::MODE_SELECT
|
43
|
+
option_chosen = false
|
44
|
+
|
45
|
+
while !option_chosen
|
46
|
+
option_chosen = true # assume user selects valid option so as to quit loop
|
47
|
+
|
48
|
+
input = gets.chomp.downcase
|
49
|
+
case input
|
50
|
+
when "s", "single" then SinglePlayer.new(sequence, game_logic).start_game
|
51
|
+
when "m", "multi"
|
52
|
+
print UI::PASSWORD_MESSAGE
|
53
|
+
hide_guess = MasterMind::Tobi::GameMethods.new.yes_or_no?
|
54
|
+
MultiPlayer.new(sequence, game_logic, hide_guess).start_game
|
55
|
+
else
|
56
|
+
print UI::INVALID_MESSAGE
|
57
|
+
option_chosen = false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
def print_help
|
64
|
+
puts UI::HELP_MESSAGE
|
65
|
+
print UI::OPTIONS_MESSAGE + UI::INPUT_PROMPT
|
66
|
+
end
|
67
|
+
|
68
|
+
def ask_level
|
69
|
+
print UI::LEVEL_MESSAGE
|
70
|
+
option_chosen = false
|
71
|
+
|
72
|
+
while !option_chosen
|
73
|
+
option_chosen = true # assume user selects valid level so as to quit loop
|
74
|
+
|
75
|
+
input = gets.chomp.downcase
|
76
|
+
case input
|
77
|
+
when "b", "beginner" then return GameLogic::BEGINNER
|
78
|
+
when "i", "intermediate" then return GameLogic::INTERMEDIATE
|
79
|
+
when "a", "advanced" then return GameLogic::ADVANCED
|
80
|
+
else # user selects an invalid level
|
81
|
+
print UI::INVALID_MESSAGE
|
82
|
+
option_chosen = false
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
106
88
|
end
|
@@ -1,91 +1,107 @@
|
|
1
|
-
require 'ui'
|
2
|
-
require 'gamehelper'
|
3
|
-
module MasterMind
|
4
|
-
module Tobi
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
puts
|
37
|
-
puts
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
by_guess
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
print UI::
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
1
|
+
require 'ui'
|
2
|
+
require 'gamehelper'
|
3
|
+
module MasterMind
|
4
|
+
module Tobi
|
5
|
+
class GameMethods
|
6
|
+
def average_string(top_ten_list, current_player) # generates user's performance compared to average
|
7
|
+
time_diff, guess_diff = difference(top_ten_list, current_player)
|
8
|
+
|
9
|
+
"That's %s %s and %s %s %s the average\n" % [time_convert(time_diff.abs), time_diff < 0 ? "slower" : "faster",
|
10
|
+
guess_diff.abs, guess_diff.abs == 1 ? "guess" : "guesses", guess_diff < 0 ? "more" : "fewer"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def difference(top_ten_list, current_player)
|
14
|
+
total_time = 0
|
15
|
+
total_guess = 0
|
16
|
+
|
17
|
+
diff_hash = top_ten_list.each{ |player|
|
18
|
+
total_time += player.time
|
19
|
+
total_guess += player.guesses
|
20
|
+
}
|
21
|
+
|
22
|
+
average_time = (total_time.to_f/top_ten_list.length).round - current_player.time
|
23
|
+
average_guess = (total_guess.to_f/top_ten_list.length).round - current_player.guesses
|
24
|
+
|
25
|
+
return average_time, average_guess
|
26
|
+
end
|
27
|
+
|
28
|
+
def print_top_ten(current_player)
|
29
|
+
top_ten_list = get_top_ten
|
30
|
+
|
31
|
+
puts average_string(top_ten_list, current_player) if top_ten_list.length > 1 # print out user's performance compared to average
|
32
|
+
|
33
|
+
# print out top ten results
|
34
|
+
if !top_ten_list.nil?
|
35
|
+
puts ""
|
36
|
+
puts UI::TOP_TEN
|
37
|
+
top_ten_list.each_with_index{|player, index| puts "#{index+1}. " + player.to_s }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_top_ten
|
42
|
+
YAML.load_stream(File.open(UI::DB_STORE)).sort{|player1, player2| # load player objects from db and sort by guesses/time
|
43
|
+
by_guess = player1.guesses <=> player2.guesses # first sort by guesses
|
44
|
+
by_guess == 0 ? player1.time <=> player2.time : by_guess # then sort by time
|
45
|
+
}[0...10] if File.file?(UI::DB_STORE) # pick out top ten
|
46
|
+
end
|
47
|
+
|
48
|
+
def store_game(sequence, guesses, time) #get player name and store details to file
|
49
|
+
print UI::NAME_MESSAGE
|
50
|
+
name = get_name
|
51
|
+
current_player = Player.new(name, sequence, time, guesses) # create new player object
|
52
|
+
|
53
|
+
# write player object to file if file does not exist, or verify whether to add record from user, and write if it exists
|
54
|
+
File.open(UI::DB_STORE, 'a'){|file| file.write(YAML.dump(current_player))} if user_permits_store?
|
55
|
+
|
56
|
+
current_player
|
57
|
+
end
|
58
|
+
|
59
|
+
def user_permits_store? # confirm from user to add record to top-scores if file exists
|
60
|
+
return true if !File.exist?(UI::DB_STORE) # if file does not exist, return true
|
61
|
+
print UI::OVERWRITE_MESSAGE
|
62
|
+
print UI::INPUT_PROMPT
|
63
|
+
option_chosen = false
|
64
|
+
|
65
|
+
return yes_or_no?
|
66
|
+
end
|
67
|
+
|
68
|
+
def yes_or_no?
|
69
|
+
option_chosen = false
|
70
|
+
|
71
|
+
while !option_chosen
|
72
|
+
option_chosen = true # assume user selects valid option so as to quit loop
|
73
|
+
|
74
|
+
input = gets.chomp.downcase
|
75
|
+
case input
|
76
|
+
when "y", "yes" then return true
|
77
|
+
when "n", "no" then return false
|
78
|
+
else # user selects an invalid option
|
79
|
+
print UI::INVALID_MESSAGE
|
80
|
+
option_chosen = false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def get_name
|
86
|
+
name = ""
|
87
|
+
|
88
|
+
while name.eql?("")
|
89
|
+
name = gets.chomp.capitalize
|
90
|
+
print UI::INVALID_MESSAGE if name.eql?("")
|
91
|
+
end
|
92
|
+
|
93
|
+
name
|
94
|
+
end
|
95
|
+
|
96
|
+
def print_history(history)
|
97
|
+
if history.empty?
|
98
|
+
print "No history yet. Enter a guess" + UI::INPUT_PROMPT
|
99
|
+
else
|
100
|
+
puts ""
|
101
|
+
puts history
|
102
|
+
print UI::INPUT_PROMPT
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
91
107
|
end
|