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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +32 -23
- data/lib/.idea/.name +1 -0
- data/lib/.idea/encodings.xml +6 -0
- data/lib/.idea/lib.iml +9 -0
- data/lib/.idea/misc.xml +14 -0
- data/lib/.idea/modules.xml +8 -0
- data/lib/.idea/workspace.xml +215 -0
- data/lib/paitin_hangman.rb +17 -2
- data/lib/paitin_hangman/computer.rb +24 -0
- data/lib/paitin_hangman/game_data.rb +17 -15
- data/lib/paitin_hangman/game_engine.rb +132 -56
- data/lib/paitin_hangman/game_resumption.rb +77 -0
- data/lib/paitin_hangman/levels.rb +36 -168
- data/lib/paitin_hangman/messages.rb +59 -59
- data/lib/paitin_hangman/player.rb +46 -0
- data/lib/paitin_hangman/simple_methods.rb +70 -0
- data/lib/paitin_hangman/version.rb +1 -1
- data/paitin_hangman.gemspec +3 -2
- metadata +30 -14
- data/.DS_Store +0 -0
- data/.travis.yml +0 -4
- data/bin/games.yml +0 -17
- data/games.yml +0 -0
- data/lib/paitin_hangman/.DS_Store +0 -0
- data/lib/paitin_hangman/dictionary.txt +0 -41211
- data/lib/paitin_hangman/extra_methods.rb +0 -80
- data/lib/paitin_hangman/saved_games.rb +0 -89
|
@@ -1,73 +1,149 @@
|
|
|
1
1
|
require_relative "messages"
|
|
2
2
|
require_relative "levels"
|
|
3
|
-
|
|
4
|
-
# => This class only ensures the smooth transfer of control to the levels
|
|
3
|
+
|
|
5
4
|
module PaitinHangman
|
|
6
|
-
class
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
30
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
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 "
|
|
4
|
+
require_relative "simple_methods"
|
|
5
5
|
require_relative "messages"
|
|
6
6
|
require_relative "game_data"
|
|
7
7
|
module PaitinHangman
|
|
8
|
-
class Levels
|
|
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
|
-
|
|
37
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
96
|
-
|
|
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
|