-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwad_wof_gen_01.rb
executable file
·214 lines (177 loc) · 4.03 KB
/
wad_wof_gen_01.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Ruby code file - All your code should be located between the comments provided.
# Main class module
module WOF_Game
# Input and output constants processed by subprocesses. MUST NOT change.
GOES = 5
class Game
attr_reader :template, :wordtable, :input, :output, :turn, :turnsleft, :winner, :secretword, :played, :score, :resulta, :resultb, :guess
attr_writer :template, :wordtable, :input, :output, :turn, :turnsleft, :winner, :secretword, :played, :score, :resulta, :resultb, :guess
def initialize(input, output)
@input = input
@output = output
@played = 0
@score = 0
end
# Any code/methods aimed at passing the RSpect tests should be added below.
def start
@output.puts('Welcome to Hangman!')
@output.puts("Created by: #{created_by} (#{student_id})")
@output.puts('Starting game...')
@output.puts('Enter 1 to run the game in the command-line window or 2 to run it in a web browser')
end
def created_by
return "HuangMiao"
end
def student_id
return 51988466
end
def displaymenu
@output.puts("Menu: (1) Play | (2) New | (3) Analysis | (9) Exit")
end
def resetgame
@wordtable = []
@secretword = ""
@turn = 0
@resulta = []
@resultb = []
@winner = 0
@guess = ""
@template = "[]"
end
def readwordfile(filename)
if !@wordtable
@wordtable = []
end
file = File.open(filename)
cnt = 0
file.each do |line|
if line
cnt += 1
@wordtable.push(line.strip)
end
end
file.close
return cnt
end
def gensecretword
return @wordtable[rand([email protected])].upcase
end
def ReGenSecretWord
return @resultb[rand([email protected])]
end
def setsecretword(secret)
@secretword = secret
end
def getsecretword
return @secretword
end
def createtemplate
strlen = @secretword.length
str = '['
for i in 1..strlen
str += '_'
end
str += ']'
@template =str
return @template
end
def displaytemplate
puts @template
return @template
end
def charinword(char)
check = CheckIfExistChar(char)
strlen = @secretword.length
if(check >= 0)
for i in 0..strlen
if(char == @secretword[i])
@template[i+1] = @secretword[i].upcase
end
end
end
return check
end
def CheckIfExistChar(char)
check = @secretword.index(char)
if !check
return -1
end
return check
end
def GotOneWordRight
rightword = @secretword.to_s
puts "Congratulations! You got #{rightword} right."
end
def CheckifWin(a,b)
if a == b
return 1
end
return 0
end
def addplayed
@played += 1
end
def incrementturn
@turn += 1
end
def addscore
@score += 100
end
def getturnsleft
@turnsleft = GOES - @turn
end
def DisplayWinner(result)
if result == true
return "Well done. You win."
end
if result == false
return "Sorry, you failed."
end
end
def getguess
guess = @input.gets.chomp.upcase
end
# def storeguess(guess)
# if guess!= ""
# @guess = @guess.to_a.push "#{guess}"
# end
# end
def InvalidInput
@output.puts('Invalid input.')
end
def WordRevealed
if @secretword.upcase.count("A-Z") == @template.count("A-Z")
return 1
else
return 0
end
end
def UpdateResulta
return @resulta.push(@secretword)
end
def UpdateResultb
temp = @wordtable
for i in 0..(temp.length-1)
temp[i] = temp[i].upcase
end
@resultb = temp - @resulta
return @resultb
end
def DisplayResulta
puts @resulta
puts
return @resulta
end
def DisplayResultb
print @resultb
puts
return @resultb
end
def ShowWordtable
print @wordtable
puts
return @wordtable
end
# Any code/methods aimed at passing the RSpect tests should be added above.
end
end