paitin_hangman 0.1.0 → 1.0.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.
@@ -1,73 +1,149 @@
1
1
  require_relative "messages"
2
2
  require_relative "levels"
3
- require_relative "extra_methods"
4
- # => This class only ensures the smooth transfer of control to the levels
3
+
5
4
  module PaitinHangman
6
- class Computer
7
- def initialize
8
- Message.level_choice
9
- end
5
+ class GameEngine
6
+ include SimpleMethods
7
+ attr_reader :count, :misses, :right_guesses, :guess, :player2
8
+ def initialize
9
+ @misses = []
10
+ @right_guesses = []
11
+ end
10
12
 
11
- def level_integrity(name)
12
- choice = gets.chomp
13
- until choice == "1" || choice == "2" || choice == "3"
14
- puts "Please press either '1', '2' or '3'"
15
- choice = STDIN.gets.chomp
13
+ def trials(chances, game_word, player2, player1)
14
+ setup(game_word, player2, player1)
15
+ chances.times do |counter|
16
+ @counter = counter
17
+ verify_guess(chances)
18
+ compare_guess
19
+ win_game(chances, counter)
20
+ end
21
+ end_game
16
22
  end
17
- level(choice, name)
18
- end
19
23
 
20
- def level(choice, name)
21
- case choice
22
- when "1" then Levels.new(name, 4, 8, 10)
23
- when "2" then Levels.new(name, 9, 12, 12)
24
- when "3" then Levels.new(name, 13, 25, 15)
24
+ def compare_guess
25
+ if @game_word.include?(@guess) == false
26
+ wrong_guess
27
+ else
28
+ correct_guess
29
+ end
25
30
  end
26
- end
27
- end
28
31
 
29
- class Player
30
- include SimpleMethods
32
+ def setup(game_word, player2, player1)
33
+ @player2 = player2
34
+ @player1 = player1
35
+ @game_word = game_word
36
+ @word_control = ""
37
+ @game_word.length.times { @word_control << "_" }
38
+ @count = 0
39
+ puts @word_control.gsub("_", "__ ")
40
+ end
31
41
 
32
- def first_player_name(name)
33
- player1 = name
34
- get_friend_name(player1)
35
- end
42
+ def wrong_guess
43
+ @misses << @guess
44
+ puts "Bad guess, the noose tightens".red
45
+ puts "Try again"
46
+ puts @word_control.gsub("_", " __ ").upcase
47
+ end
36
48
 
37
- def get_friend_name(player1)
38
- puts "Hi #{player1}, what is the name of your friend"
39
- player2 = verify_name_integrity
40
- puts "Hello, #{player1} and #{player2}, who will like to challenge"
41
- game(player1, player2)
42
- end
49
+ def correct_guess
50
+ @game_word.each_char.with_index do |letter, index|
51
+ right_guess(letter, index)
52
+ end
53
+ @right_guesses << @guess
54
+ puts @word_control.gsub("_", " __ ").upcase
55
+ end
43
56
 
44
- def game(player1, player2)
45
- begin
46
- puts "Please enter one of your names: #{player1} or #{player2}"
47
- name = verify_name_integrity
48
- end until name == player1 || name == player2
49
- other_player = player2 if name == player1
50
- other_player = player1 if name == player2
51
- Message.level_choice
52
- level_integrity(name, other_player)
53
- end
57
+ def right_guess(letter, index)
58
+ if letter == @guess
59
+ if @word_control.include?(@guess) == false
60
+ @count += 1
61
+ puts "Nice one!".green
62
+ end
63
+ @word_control[index] = @guess
64
+ end
65
+ end
54
66
 
55
- def level_integrity(name, other_player)
56
- choice = gets.chomp
57
- until choice == "1" || choice == "2" || choice == "3"
58
- puts "Please press either '1', '2' or '3'"
59
- choice = STDIN.gets.chomp
67
+ def verify_guess(chances)
68
+ puts "Enter a guess"
69
+ @guess = gets.chomp.downcase
70
+ cheat_or_quit_or_history(chances)
71
+ until length_one?(@guess) && unique?(@guess) && number?(@guess)
72
+ @guess = STDIN.gets.chomp.downcase
73
+ puts @game_word if @guess == ":c"
74
+ puts "Your missed guesses: #{@misses.join(', ')}" if @guess == ":h"
75
+ end
60
76
  end
61
- select_level(choice, name, other_player)
62
- end
63
77
 
64
- def select_level(choice, name, other_player)
65
- case choice
66
- when "1" then Levels.new(other_player, 4, 8, 10, name)
67
- when "2" then Levels.new(other_player, 9, 12, 12, name)
68
- when "3" then Levels.new(other_player, 13, 25, 15, name)
78
+ def cheat_or_quit_or_history(chances)
79
+ if @guess == ":c"
80
+ puts @game_word
81
+ verify_guess(chances)
82
+ elsif @guess == "quit" || @guess == ":q"
83
+ quit_or_save(chances)
84
+ elsif @guess == ":h" || @guess == "history"
85
+ history_verify_guess(chances)
86
+ end
69
87
  end
70
- end
71
88
 
72
- end
89
+ def history_verify_guess(chances)
90
+ history
91
+ verify_guess(chances)
92
+ end
93
+
94
+ def quit_or_save(chances)
95
+ exit if @player1
96
+ puts "Do you want to save your game? type 'Yes' or 'No'"
97
+ choice = STDIN.gets.chomp.downcase
98
+ until choice == "yes" || choice == "no"
99
+ puts "Please type a 'Yes' or a 'No'"
100
+ choice = STDIN.gets.chomp.downcase
101
+ end
102
+ choice == "no" ? exit : save_game(chances)
103
+ end
104
+
105
+ def save_game(chances)
106
+ game_data = GameData.new(@player2, @misses, @right_guesses,
107
+ (chances - @counter), @word_control,
108
+ @game_word, @count)
109
+ file_name = File.join(File.dirname(File.expand_path(__FILE__)), '../../games.yml')
110
+ File.open(file_name, "a") { |data| YAML.dump(game_data, data) }
111
+ puts "Goodbye, your game has been saved successfully".green
112
+ exit
113
+ end
114
+
115
+ def history
116
+ if @misses.empty? && @right_guesses.empty?
117
+ puts "you have not entered any guesses"
118
+ else
119
+ print_history
120
+ end
121
+ end
122
+
123
+ def print_history
124
+ if @misses.empty? && @right_guesses.empty? == false
125
+ puts "your right guesses are #{@right_guesses.join(', ')} but no misses"
126
+ elsif @misses.empty? == false && @right_guesses.empty?
127
+ puts "your misses are #{@misses.join(', ')} & you have no right_guesses"
128
+ else
129
+ puts "\tyour misses are #{@misses.join(', ')} and your
130
+ right guesses are #{@right_guesses.join(', ')}"
131
+ end
132
+ end
133
+
134
+ def win_game(chances, counter, player = @player2)
135
+ if @count == @game_word.chars.uniq.length
136
+ puts "Congratulations #{player}! You have won the game".green
137
+ exit
138
+ end
139
+ puts "You have #{chances - 1 - counter} chance(s) left"
140
+ end
141
+
142
+ def end_game(player = @player2)
143
+ puts "Game over! You have been HANGED! Sorry #{player}".red
144
+ puts "The word is #{@game_word.upcase}"
145
+ Message.end_games
146
+ choice_integrity
147
+ end
148
+ end
73
149
  end
@@ -0,0 +1,77 @@
1
+ require_relative "game_engine"
2
+ module PaitinHangman
3
+ class GameResumption < GameEngine
4
+ def initialize
5
+ file_name = File.join(File.dirname(File.expand_path(__FILE__)), '../../games.yml')
6
+ @all_games = YAML.load_stream(File.open(file_name, "a+"))
7
+ puts "Enter the name you used to store the game"
8
+ name = STDIN.gets.chomp.upcase
9
+ @saved_game = @all_games.select do |game|
10
+ game.player_name == name
11
+ end
12
+ initialize_cont(name)
13
+ end
14
+
15
+ def initialize_cont(name)
16
+ if @saved_game.empty?
17
+ puts "You have no saved game with that name".red
18
+ puts "START A NEW GAME INSTEAD".yellow
19
+ exit
20
+ end
21
+ puts "Here are the saved games found for #{name}\n\n"
22
+ print_function
23
+ choice_integrity
24
+ end
25
+
26
+ def choice_integrity
27
+ puts "Now enter the number for the game you want to play".blue
28
+ @choice = STDIN.gets.chomp.to_i
29
+ until @choice > 0 && @choice <= @saved_game.length
30
+ puts "Enter a number that has a game please"
31
+ @choice = STDIN.gets.chomp.to_i
32
+ end
33
+ @choice -= 1
34
+ load_properties
35
+ end
36
+
37
+ def load_properties
38
+ @game_object = @saved_game[@choice]
39
+ @name = @game_object.player_name
40
+ @misses = @game_object.misses
41
+ @right_guesses = @game_object.right_guesses
42
+ @chances = @game_object.chances
43
+ @word_control = @game_object.word_control
44
+ @game_word = @game_object.game_word
45
+ load_properties_cont
46
+ end
47
+
48
+ def load_properties_cont
49
+ @count = @game_object.count
50
+ trials(@chances)
51
+ end
52
+
53
+ def print_function
54
+ @saved_game.length.times do |index|
55
+ print "#{index + 1}.\t"
56
+ puts @saved_game[index], "\n"
57
+ end
58
+ end
59
+
60
+
61
+ def trials(chances)
62
+ setup
63
+ chances.times do |counter|
64
+ @counter = counter
65
+ verify_guess(chances)
66
+ compare_guess
67
+ win_game(chances, counter, @name)
68
+ end
69
+ end_game(@name)
70
+ end
71
+
72
+ def setup
73
+ puts "Now, continue playing..."
74
+ puts @word_control.gsub("_", "__ ").upcase
75
+ end
76
+ end
77
+ end
@@ -1,180 +1,48 @@
1
1
  require "colorize"
2
2
  require "io/console"
3
3
  require "yaml"
4
- require_relative "extra_methods"
4
+ require_relative "simple_methods"
5
5
  require_relative "messages"
6
6
  require_relative "game_data"
7
7
  module PaitinHangman
8
- class Levels
9
- attr_reader :player2, :min, :max, :chances, :player1, :game_word
10
- include SimpleMethods
11
- def initialize(player, min, max, chances, another_player = nil)
12
- @misses = []
13
- @right_guesses = []
14
- initialize_specs(player, min, max, chances, another_player)
15
- puts "\t\tWelcome #{@player2}, In this level, you will have #{@chances}
16
- chances to guess the word correctly"
17
- word_generator(@min, @max) if @player1.nil?
18
- challenge_word(@min, @max) if @player1.nil? == false
19
- trials(@chances)
20
- end
21
-
22
- def initialize_specs(player, min, max, chances, another_player)
23
- @player2 = player
24
- @min = min
25
- @max = max
26
- @chances = chances
27
- @player1 = another_player
28
- end
29
-
30
- def word_generator(min, max, path = "")
31
- words = File.open(path + "dictionary.txt", "r").readlines.map!(&:chomp)
32
- level_words = words.select { |i| i.length >= min && i.length <= max }
33
- random_index = rand(level_words.length)
34
- @game_word = level_words[random_index]
35
- end
36
-
37
- def challenge_word(min, max)
38
- puts "Now, enter your challenge_word #{@player1}, it will be hidden"
39
- @game_word = STDIN.noecho(&:gets).chomp.downcase.delete(" ")
40
- until @game_word.length >= min && @game_word.length <= max &&
41
- number?(@game_word)
42
- puts "Word length not correct!Make it between #{min} & #{max} characters"
8
+ class Levels
9
+ attr_reader :player2, :min, :max, :chances, :player1, :game_word
10
+ include SimpleMethods
11
+ def initialize(player, min, max, chances, another_player = nil)
12
+ initialize_specs(player, min, max, chances, another_player)
13
+ puts "\t\tWelcome #{@player2}, In this level, you will have #{@chances}
14
+ chances to guess the word correctly"
15
+ word_generator(min, max) if @player1.nil?
16
+ challenge_word(min, max) if @player1
17
+ end
18
+
19
+ def initialize_specs(player, min, max, chances, another_player)
20
+ @player2 = player
21
+ @min = min
22
+ @max = max
23
+ @chances = chances
24
+ @player1 = another_player
25
+ end
26
+
27
+ def word_generator(min, max)
28
+ file_name = File.join(File.dirname(File.expand_path(__FILE__)), '../../dictionary.txt')
29
+ words = File.open(file_name, "r").readlines.map!(&:chomp)
30
+ level_words = words.select { |i| i.length >= min && i.length <= max }
31
+ random_index = rand(level_words.length)
32
+ @game_word = level_words[random_index]
33
+ GameEngine.new.trials(@chances, @game_word, @player2, @player1)
34
+ end
35
+
36
+ def challenge_word(min, max)
37
+ puts "Now, enter your challenge_word #{@player1}, it will be hidden"
43
38
  @game_word = STDIN.noecho(&:gets).chomp.downcase.delete(" ")
44
- end
45
- puts "The stage is set #{@player2}"
46
- end
47
-
48
- def trials(chances)
49
- setup
50
- chances.times do |counter|
51
- @counter = counter
52
- verify_guess
53
- compare_guess
54
- win_game(chances, counter)
55
- end
56
- end_game
57
- end
58
-
59
- def compare_guess
60
- if @game_word.include?(@guess) == false
61
- wrong_guess
62
- else
63
- correct_guess
64
- end
65
- end
66
-
67
- def setup
68
- @word_control = ""
69
- @game_word.length.times { @word_control << "_" }
70
- @count = 0
71
- puts @word_control.gsub("_", "__ ")
72
- end
73
-
74
- def wrong_guess
75
- @misses << @guess
76
- puts "Bad guess, the noose tightens".red
77
- puts "Try again"
78
- puts @word_control.gsub("_", " __ ").upcase
79
- end
80
-
81
- def correct_guess
82
- @game_word.each_char.with_index do |letter, index|
83
- right_guess(letter, index)
84
- end
85
- @right_guesses << @guess
86
- puts @word_control.gsub("_", " __ ").upcase
87
- end
88
-
89
- def right_guess(letter, index)
90
- if letter == @guess
91
- if @word_control.include?(@guess) == false
92
- @count += 1
93
- puts "Nice one!".green
39
+ until @game_word.length >= min && @game_word.length <= max && number?(@game_word)
40
+ puts "Word length not correct! Make it between #{min} & #{max} characters"
41
+ @game_word = STDIN.noecho(&:gets).chomp.downcase.delete(" ")
94
42
  end
95
- @word_control[index] = @guess
96
- end
97
- end
98
-
99
- def verify_guess
100
- puts "Enter a guess"
101
- @guess = gets.chomp.downcase
102
- cheat_or_quit_or_history
103
- until length_one?(@guess) && unique?(@guess) && number?(@guess)
104
- @guess = STDIN.gets.chomp.downcase
105
- puts @game_word if @guess == ":c"
106
- puts "Your missed guesses: #{@misses.join(', ')}" if @guess == ":h"
107
- end
108
- end
109
-
110
- def cheat_or_quit_or_history
111
- if @guess == ":c"
112
- puts @game_word
113
- verify_guess
114
- elsif @guess == "quit" || @guess == ":q"
115
- quit_or_save
116
- elsif @guess == ":h" || @guess == "history"
117
- history_verify_guess
118
- end
119
- end
120
-
121
- def history_verify_guess
122
- history
123
- verify_guess
124
- end
125
-
126
- def quit_or_save
127
- exit if @player1.nil? == false
128
- puts "Do you want to save your game? type 'Yes' or 'No'"
129
- choice = STDIN.gets.chomp.downcase
130
- until choice == "yes" || choice == "no"
131
- puts "Please type a 'Yes' or a 'No'"
132
- choice = STDIN.gets.chomp.downcase
43
+ puts "The stage is set #{@player2}"
44
+ GameEngine.new.trials(@chances, @game_word, @player2, @player1)
133
45
  end
134
- choice == "no" ? exit : save_game
135
- end
136
46
 
137
- def save_game
138
- game_data = GameData.new(@player2, @misses, @right_guesses,
139
- @chances - @counter, @word_control,
140
- @game_word, @count)
141
- File.open("games.yml", "a") { |data| YAML.dump(game_data, data) }
142
- puts "Goodbye, your game has been saved successfully".green
143
- exit
144
47
  end
145
-
146
- def history
147
- if @misses.empty? && @right_guesses.empty?
148
- puts "you have not entered any guesses"
149
- else
150
- print_history
151
- end
152
- end
153
-
154
- def print_history
155
- if @misses.empty? && @right_guesses.empty? == false
156
- puts "your right guesses are #{@right_guesses.join(', ')} but no misses"
157
- elsif @misses.empty? == false && @right_guesses.empty?
158
- puts "your misses are #{@misses.join(', ')} & you have no right guesses"
159
- else
160
- puts "\tyour misses are #{@misses.join(', ')} and your
161
- right guesses are #{@right_guesses.join(', ')}"
162
- end
163
- end
164
-
165
- def win_game(chances, counter, player = @player2)
166
- if @count == @game_word.chars.uniq.length
167
- puts "Congratulations #{player}! You have won the game".green
168
- exit
169
- end
170
- puts "You have #{chances - 1 - counter} chance(s) left"
171
- end
172
-
173
- def end_game(player = @player2)
174
- puts "Game over! You have been HANGED! Sorry #{player}".red
175
- puts "The word is #{@game_word.upcase}"
176
- Message.end_games
177
- choice_integrity
178
- end
179
- end
180
48
  end