-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.rb
More file actions
executable file
·106 lines (96 loc) · 2.01 KB
/
Copy pathgame.rb
File metadata and controls
executable file
·106 lines (96 loc) · 2.01 KB
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
#!/usr/bin/env ruby
require "player"
require "board"
require "phrase"
class Game
def initialize
@board = Board.new
get_player
game_loop
end
# Set up the player
def get_player
introduction
@player = Player.new(gets.chomp, @board)
refresh_screen
@board.flash("Hey, #{@player.name}. I've selected a movie title. Let's begin.")
end
# Intro message
def introduction
#puts "#{@board.correct.inspect} #{@board.correct.length}"
#puts "#{@board.phrase.letters.inspect} #{@board.phrase.letters.length}"
refresh_screen
puts "Welcome to hangman!"
puts "What is your name?"
end
# Pick a letter
def pick_letter
puts "What letter would you like to guess?"
@board.man(@player.wrong)
puts @board.spaces
return gets.chomp.upcase
end
# Check the letter
def check_letter(letter)
# Quick validation
if letter.length > 1 || (letter =~ /^[a-zA-Z]/).nil?
puts "Please enter a letter."
return false
end
if @player.guess?(letter)
return "You guessed correctly!"
else
return "WRONG GUESS!"
end
end
# Status of the game
def game_status(response)
if @player.lost?
@board.flash "Sorry, you lose! The answer was #{@board.phrase.show.upcase}"
return play_again?
elsif @player.won?
@board.flash "You win! Awesome!"
return play_again?
else
return true
end
end
# Game over
# Ask the player if they want to play again
def play_again?
puts "Want to play again? [Y/N]"
while true
response = gets.chomp.upcase
case response
when "Y"
refresh_screen
@board = Board.new
@player = Player.new(@player.name, @board)
return true
break
when "N"
refresh_screen
puts "Goodbye!"
return false
break
else
puts "Press 'Y' to play again or 'N' to quit."
end
end
end
# Game loop
def game_loop
running = true
while (running)
guess = pick_letter
response = check_letter(guess)
refresh_screen
running = game_status(response)
end
end
private
def refresh_screen
system("clear")
end
end
game = Game.new