-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman_revised.rb
167 lines (133 loc) · 2.77 KB
/
hangman_revised.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
167
class Hangman
attr_accessor :word, :dictionary_array
def initialize
@word = ""
@dictionary_array = dictionary_array
@game_board = []
@game_mode = ""
end
def dictionary_array
dictionary = []
File.foreach("dictionary.txt") do |line|
dictionary << line.chomp
end
dictionary
end
def game_loop
game_mode
choose_word
blank_game_board(@word)
until game_won?
guess = human_guess
if take_guess(guess)
place_letters(guess)
end
show_board
end
puts "You've won!"
puts "The word was #{@game_board.join("")}"
end
def game_won?
@game_board.join("") == @word
end
def game_mode
puts "Press 1 to choose a word, press 2 to guess a word!"
command = gets.chomp
case command
when "1"
@game_mode = "computer guess"
# play_choosing_game
when "2"
@game_mode = "human guess"
# play_guessing_game
else
puts "invalid entry, choose 1 or 2"
game_mode
end
end
# def play_choosing_game
# player chooses word v
# store word v
# create board v
# evaluate guess v
# change board v
# store/show guesses
# store attempts
# end
# def play_guessing_game
# choose word v
# store word v
# create board v
# prompt user
# evaluate guess v
# change board v
# store/show guesses
# store attempts
# end
def choose_word
case @game_mode
when "computer guess"
puts "Choose a word! (It must be in the dictionary)"
@word = gets.chomp
when "human guess"
@word = computer_guess
end
end
def take_guess(letter)
if guess_correct?(letter)
puts "correct guess!"
true
else
puts "incorrect guess!"
false
end
end
def human_guess
puts "Make a guess!"
guess = gets.chomp
if guess.class != String && guess.length != 1
puts "Invalid entry! Try again."
human_guess
else
guess
end
end
def computer_guess
@dictionary_array.sample
end
def guess_correct?(letter)
@word.split("").include?(letter)
end
def place_letters(letter)
# word = "testing"
# game_board = ["_", "_", "_", "_", "_", "_", "_"]
@word.split("").each_with_index do |l, index|
if letter == l
@game_board[index] = letter
end
end
# game_board
end
def blank_game_board(word)
word_length = word.length
game_board = []
word_length.times do
game_board << "_"
end
@game_board = game_board
end
# Interface stuff
def show_board
puts "here is your game board"
puts @game_board.join(" ")
end
end
class Player
end
class HumanPlayer < Player
end
class ComputerPlayer < Player
end
game = Hangman.new
# p game.dictionary_array
p game.game_loop