-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.rb
166 lines (145 loc) · 4 KB
/
hangman.rb
1
2
3
4
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require 'json'
class String
def red; "\e[31m#{self}\e[0m" end
def green; "\e[32m#{self}\e[0m" end
end
class Serialization
def save(name, word, player_choice_arr, correct_guesses, wrong_guesses)
to_serialize = ({
name: name,
word: word,
player_choice_arr: player_choice_arr,
correct_guesses: correct_guesses,
wrong_guesses: wrong_guesses
})
File.open('save_file.json', 'w+') do |f|
JSON.dump(to_serialize, f)
end
end
def deserialize(str)
JSON.parse(str)
end
def load(name, word, player_choice_arr, correct_guesses, wrong_guesses)
json_file = File.read('save_file.json')
json_file.kind_of?(String)
loaded_data = deserialize(json_file)
@name = loaded_data['name']
@word = loaded_data['word']
@player_choice_arr = loaded_data['player_choice_arr']
@correct_guesses = loaded_data['correct_guesses']
@wrong_guesses = loaded_data['wrong_guesses']
end
end
class Player < Serialization
attr_reader :name, :word, :letter
def initialize(name)
@name = name
@player_choice_arr = []
@correct_guesses = []
@wrong_guesses = 0
end
def file_reader(txt)
file = File.open(txt, 'r')
@lines = file.readlines
end
#Below method chooses a random word from given dictionary that is opened by the method above(file_reader)
def choose_word
@word = @lines[rand(0..(@lines.length - 1))].chomp
choose_word if @word.length < 5 || @word.length > 12
end
def player_choice
@letter = gets.chomp.downcase
if @letter.match?(/[[:alpha:]]/) && @letter.length == 1 && !@player_choice_arr.include?(@letter)
@player_choice_arr.push(@letter)
if @word.split("").include?(@letter)
puts "Correctamento, mi amigo!!!".green
@correct_guesses.push(@letter)
else
puts "No correctamento, try again.".red
@wrong_guesses += 1
end
elsif @letter == 'save'
save(@name, @word, @player_choice_arr, @correct_guesses, @wrong_guesses)
else
player_choice
end
end
def showing_all_guesses
@player_choice_arr.each do |e|
if @correct_guesses.include?(e)
print e.green
else
print e.red
end
end
puts " "
end
def display
unless 8 - @wrong_guesses == 1
puts "You have #{8 - @wrong_guesses} attempts."
else
puts "You have 1 attempt left. Do your best!"
end
showing_all_guesses
@word.split("").each do |l|
if @correct_guesses.include?(l)
print l
else
print ' _ '
end
end
puts " "
end
def start_of_game
puts "Welcome to the hangman game, you know the rules so do I. Let's start!"
puts "Press and enter 1 to play a new game, 2 to load a save file."
answer = gets.chomp
if answer == '1'
choose_word
elsif answer == '2'
if File.exists?('save_file.json')
load(@name, @word, @player_choice_arr, @correct_guesses, @wrong_guesses)
else
puts "You haven't saved any progress so far. Starting new game..."
choose_word
end
else
puts "Enter 1 or 2"
start_of_game
end
end
def game
file_reader("dictionary.txt")
start_of_game
puts "You can enter 'save' at any point duing game, this will save your progress."
while true
display
player_choice
if @word.split("").all?{ |e| @correct_guesses.include?(e)}
puts "You win! Congratulations!"
break
elsif @wrong_guesses == 8
puts "You're out of guesses. You lose..."
puts "The word you had to guess was - #{@word.green}."
break
end
end
replay
end
def replay
@correct_guesses = []
@wrong_guesses = 0
@player_choice_arr = []
puts "Do you want to play again? Enter 'y' for yes or 'n' for no: "
answer = gets.chomp.downcase
if answer == 'y'
game
elsif answer == 'n'
puts "Nice seeing you. Have a nice day!Byeee!!!"
else
replay
end
end
end
samir = Player.new("Samir")
samir.game