tobi-mastermind 1.0 → 1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90c82361b37324c107990abb8336d085795b1dfd
4
- data.tar.gz: 38cc4b58d57efcc9066588c0ee0e00e19aa27e36
3
+ metadata.gz: 371b62b6e9da0e85c74d914c8bb1d53f76f1be71
4
+ data.tar.gz: ca7fbd38b813e053ce1069a604a6ae8002a87dde
5
5
  SHA512:
6
- metadata.gz: bab88ba83c5ca30683d603a55b468b35d7587c4ab228287dbe05d92ec0b2d0064e26582d02add45693b1f826580f9c463e98b317061119ea6b00bf0325f22c3b
7
- data.tar.gz: f5f456ec4734be6503cc6441e64ef488b7cd1941588aa03dcfb3aff1fec1c85df3b247c531081033d2f3012a9ff44a089dadb955e571652e8e8f08856638a2b1
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
- module 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
- return true
32
- end
33
-
34
- def play_game
35
- game_logic = GameLogic.new(ask_level); sequence = game_logic.generate_sequence
36
- ask_mode(sequence, game_logic)
37
- puts ""
38
- print UI::OPTIONS_MESSAGE + UI::INPUT_PROMPT
39
- user_choice
40
- end
41
-
42
- def ask_mode(sequence, game_logic)
43
- print UI::MODE_SELECT
44
- option_chosen = false
45
-
46
- while !option_chosen
47
- option_chosen = true # assume user selects valid option so as to quit loop
48
-
49
- input = gets.chomp.downcase
50
- case input
51
- when "s", "single" then SinglePlayer.new(sequence, game_logic).start_game
52
- when "m", "multi"
53
- print UI::PASSWORD_MESSAGE
54
- hide_guess = MasterMind::Tobi::GameHelper.yes_or_no?
55
- MultiPlayer.new(sequence, game_logic, hide_guess).start_game
56
- else
57
- print UI::INVALID_MESSAGE
58
- option_chosen = false
59
- end
60
- end
61
-
62
- end
63
-
64
- def self.yes_or_no?
65
- option_chosen = false
66
-
67
- while !option_chosen
68
- option_chosen = true # assume user selects valid option so as to quit loop
69
-
70
- input = gets.chomp.downcase
71
- case input
72
- when "y", "yes" then return true
73
- when "n", "no" then return false
74
- else # user selects an invalid option
75
- print UI::INVALID_MESSAGE
76
- option_chosen = false
77
- end
78
- end
79
- end
80
-
81
- def print_help
82
- puts UI::HELP_MESSAGE
83
- print UI::OPTIONS_MESSAGE + UI::INPUT_PROMPT
84
- end
85
-
86
- def ask_level
87
- print UI::LEVEL_MESSAGE
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
- module GameMethods
6
-
7
- def average_string(top_ten_list, current_player) # generates user's performance compared to average
8
- time_diff, guess_diff = difference(top_ten_list, current_player)
9
-
10
- "That's %s %s and %s %s %s the average\n" % [time_convert(time_diff.abs), time_diff < 0 ? "slower" : "faster",
11
- guess_diff.abs, guess_diff.abs == 1 ? "guess" : "guesses", guess_diff < 0 ? "more" : "fewer"]
12
- end
13
-
14
- def difference(top_ten_list, current_player)
15
- total_time = 0
16
- total_guess = 0
17
-
18
- diff_hash = top_ten_list.each{ |player|
19
- total_time += player.time
20
- total_guess += player.guesses
21
- }
22
-
23
- average_time = (total_time.to_f/top_ten_list.length).round - current_player.time
24
- average_guess = (total_guess.to_f/top_ten_list.length).round - current_player.guesses
25
-
26
- return average_time, average_guess
27
- end
28
-
29
- def print_top_ten(current_player)
30
- top_ten_list = get_top_ten
31
-
32
- puts average_string(top_ten_list, current_player) if top_ten_list.length > 1 # print out user's performance compared to average
33
-
34
- # print out top ten results
35
- if !top_ten_list.nil?
36
- puts ""
37
- puts UI::TOP_TEN
38
- top_ten_list.each_with_index{|player, index| puts "#{index+1}. " + player.to_s }
39
- end
40
- end
41
-
42
- def get_top_ten
43
- YAML.load_stream(File.open(UI::DB_STORE)).sort{|player1, player2| # load player objects from db and sort by guesses/time
44
- by_guess = player1.guesses <=> player2.guesses # first sort by guesses
45
- by_guess == 0 ? player1.time <=> player2.time : by_guess # then sort by time
46
- }[0...10] if File.file?(UI::DB_STORE) # pick out top ten
47
- end
48
-
49
- def store_game(sequence, guesses, time) #get player name and store details to file
50
- print UI::NAME_MESSAGE
51
- name = get_name
52
- current_player = Player.new(name, sequence, time, guesses) # create new player object
53
-
54
- # write player object to file if file does not exist, or verify whether to add record from user, and write if it exists
55
- File.open(UI::DB_STORE, 'a'){|file| file.write(YAML.dump(current_player))} if user_permits_store?
56
-
57
- current_player
58
- end
59
-
60
- def user_permits_store? # confirm from user to add record to top-scores if file exists
61
- return true if !File.exist?(UI::DB_STORE) # if file does not exist, return true
62
- print UI::OVERWRITE_MESSAGE
63
- print UI::INPUT_PROMPT
64
- option_chosen = false
65
-
66
- return MasterMind::Tobi::GameHelper.yes_or_no?
67
- end
68
-
69
- def get_name
70
- name = ""
71
-
72
- while name.eql?("")
73
- name = gets.chomp.capitalize
74
- print UI::INVALID_MESSAGE if name.eql?("")
75
- end
76
-
77
- name
78
- end
79
-
80
- def print_history(history)
81
- if history.empty?
82
- print "No history yet. Enter a guess" + UI::INPUT_PROMPT
83
- else
84
- puts ""
85
- puts history
86
- print UI::INPUT_PROMPT
87
- end
88
- end
89
- end
90
- end
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