Class: PaitinHangman::GameEngine
- Inherits:
-
Object
- Object
- PaitinHangman::GameEngine
show all
- Includes:
- SimpleMethods
- Defined in:
- lib/paitin_hangman/game_engine.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#choice_integrity, #decide, #length_one?, #number?, #option_integrity, #unique?, #verify_name_integrity
Constructor Details
Returns a new instance of GameEngine.
8
9
10
11
|
# File 'lib/paitin_hangman/game_engine.rb', line 8
def initialize
@misses = []
@right_guesses = []
end
|
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
7
8
9
|
# File 'lib/paitin_hangman/game_engine.rb', line 7
def count
@count
end
|
#guess ⇒ Object
Returns the value of attribute guess.
7
8
9
|
# File 'lib/paitin_hangman/game_engine.rb', line 7
def guess
@guess
end
|
#misses ⇒ Object
Returns the value of attribute misses.
7
8
9
|
# File 'lib/paitin_hangman/game_engine.rb', line 7
def misses
@misses
end
|
#player2 ⇒ Object
Returns the value of attribute player2.
7
8
9
|
# File 'lib/paitin_hangman/game_engine.rb', line 7
def player2
@player2
end
|
#right_guesses ⇒ Object
Returns the value of attribute right_guesses.
7
8
9
|
# File 'lib/paitin_hangman/game_engine.rb', line 7
def right_guesses
@right_guesses
end
|
Instance Method Details
#cheat_or_quit_or_history(chances) ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/paitin_hangman/game_engine.rb', line 78
def cheat_or_quit_or_history(chances)
if @guess == ":c"
puts @game_word
verify_guess(chances)
elsif @guess == "quit" || @guess == ":q"
quit_or_save(chances)
elsif @guess == ":h" || @guess == "history"
history_verify_guess(chances)
end
end
|
#compare_guess ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/paitin_hangman/game_engine.rb', line 24
def compare_guess
if @game_word.include?(@guess) == false
wrong_guess
else
correct_guess
end
end
|
#correct_guess ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/paitin_hangman/game_engine.rb', line 49
def correct_guess
@game_word.each_char.with_index do |letter, index|
right_guess(letter, index)
end
@right_guesses << @guess
puts @word_control.gsub("_", " __ ").upcase
end
|
#end_game(player = @player2) ⇒ Object
142
143
144
145
146
147
|
# File 'lib/paitin_hangman/game_engine.rb', line 142
def end_game(player = @player2)
puts "Game over! You have been HANGED! Sorry #{player}".red
puts "The word is #{@game_word.upcase}"
Message.end_games
choice_integrity
end
|
#history ⇒ Object
115
116
117
118
119
120
121
|
# File 'lib/paitin_hangman/game_engine.rb', line 115
def history
if @misses.empty? && @right_guesses.empty?
puts "you have not entered any guesses"
else
print_history
end
end
|
#history_verify_guess(chances) ⇒ Object
89
90
91
92
|
# File 'lib/paitin_hangman/game_engine.rb', line 89
def history_verify_guess(chances)
history
verify_guess(chances)
end
|
#print_history ⇒ Object
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/paitin_hangman/game_engine.rb', line 123
def print_history
if @misses.empty? && @right_guesses.empty? == false
puts "your right guesses are #{@right_guesses.join(', ')} but no misses"
elsif @misses.empty? == false && @right_guesses.empty?
puts "your misses are #{@misses.join(', ')} & you have no right_guesses"
else
puts "\tyour misses are #{@misses.join(', ')} and your
right guesses are #{@right_guesses.join(', ')}"
end
end
|
#quit_or_save(chances) ⇒ Object
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/paitin_hangman/game_engine.rb', line 94
def quit_or_save(chances)
exit if @player1
puts "Do you want to save your game? type 'Yes' or 'No'"
choice = STDIN.gets.chomp.downcase
until choice == "yes" || choice == "no"
puts "Please type a 'Yes' or a 'No'"
choice = STDIN.gets.chomp.downcase
end
choice == "no" ? exit : save_game(chances)
end
|
#right_guess(letter, index) ⇒ Object
57
58
59
60
61
62
63
64
65
|
# File 'lib/paitin_hangman/game_engine.rb', line 57
def right_guess(letter, index)
if letter == @guess
if @word_control.include?(@guess) == false
@count += 1
puts "Nice one!".green
end
@word_control[index] = @guess
end
end
|
#save_game(chances) ⇒ Object
105
106
107
108
109
110
111
112
113
|
# File 'lib/paitin_hangman/game_engine.rb', line 105
def save_game(chances)
game_data = GameData.new(@player2, @misses, @right_guesses,
(chances - @counter), @word_control,
@game_word, @count)
file_name = File.join(File.dirname(File.expand_path(__FILE__)), '../../games.yml')
File.open(file_name, "a") { |data| YAML.dump(game_data, data) }
puts "Goodbye, your game has been saved successfully".green
exit
end
|
#setup(game_word, player2, player1) ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/paitin_hangman/game_engine.rb', line 32
def setup(game_word, player2, player1)
@player2 = player2
@player1 = player1
@game_word = game_word
@word_control = ""
@game_word.length.times { @word_control << "_" }
@count = 0
puts @word_control.gsub("_", "__ ")
end
|
#trials(chances, game_word, player2, player1) ⇒ Object
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/paitin_hangman/game_engine.rb', line 13
def trials(chances, game_word, player2, player1)
setup(game_word, player2, player1)
chances.times do |counter|
@counter = counter
verify_guess(chances)
compare_guess
win_game(chances, counter)
end
end_game
end
|
#verify_guess(chances) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/paitin_hangman/game_engine.rb', line 67
def verify_guess(chances)
puts "Enter a guess"
@guess = gets.chomp.downcase
cheat_or_quit_or_history(chances)
until length_one?(@guess) && unique?(@guess) && number?(@guess)
@guess = STDIN.gets.chomp.downcase
puts @game_word if @guess == ":c"
puts "Your missed guesses: #{@misses.join(', ')}" if @guess == ":h"
end
end
|
#win_game(chances, counter, player = @player2) ⇒ Object
134
135
136
137
138
139
140
|
# File 'lib/paitin_hangman/game_engine.rb', line 134
def win_game(chances, counter, player = @player2)
if @count == @game_word.chars.uniq.length
puts "Congratulations #{player}! You have won the game".green
exit
end
puts "You have #{chances - 1 - counter} chance(s) left"
end
|
#wrong_guess ⇒ Object
42
43
44
45
46
47
|
# File 'lib/paitin_hangman/game_engine.rb', line 42
def wrong_guess
@misses << @guess
puts "Bad guess, the noose tightens".red
puts "Try again"
puts @word_control.gsub("_", " __ ").upcase
end
|