-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdond_spec.rb
347 lines (342 loc) · 11.8 KB
/
dond_spec.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Ruby assignment
# Change the following tests with your own name and student ID.
# NB. Failure to do so will result in marks being deducted.
# IMPORTANT: Ensure you save the file after making the changes.
# DO notchange the names of the files. Just ensure you backup the files often.
# The file where you are to write code to pass the tests must be present in the same folder.
# See http://rspec.codeschool.com/levels/1 for help about RSpec
# https://en.wikipedia.org/wiki/Wheel_of_Fortune_(UK_game_show)
require "#{File.dirname(__FILE__)}/dond_gen"
# predefined method - NOT to be removed
def check_valid(secret)
@game.secret = secret
sarray = []
i = 0
secret.split('').each do|c|
sarray[i] = c
i=i+1
end
end
module DOND_Game
# RSpec Tests
describe Game do
describe "Deal or no deal game" do
before(:each) do
@input = double('input').as_null_object
@output = double('output').as_null_object
@game = Game.new(@input, @output)
end
it "should create a method called start" do
@game.start
end
it "should display a welcome message when method start called" do
@output.should_receive(:puts).with('Welcome to Deal or No Deal!')
@game.start
end
it "should contain a method created_by which returns the students name" do
studentname = "HuangMiao" # -----Change text to your own name-----
@game.created_by.should == studentname
end
it "should display a message showing who designed the game when the method start called" do
@output.should_receive(:puts).with("Designed by: #{@game.created_by}")
@game.start
end
it "should contain a method student_id which returns the students ID number" do
studentid = "51988466" # -----Change text to your own student ID-----
@game.student_id.should == studentid
end
it "should display a message showing the id of the student when the method start called" do
@output.should_receive(:puts).with("StudentID: #{@game.student_id}")
@game.start
end
it "should display a starting message when the method start called" do
@output.should_receive(:puts).with('Starting game...')
@game.start
end
it "should display menu when method displaymenu called" do
@output.should_receive(:puts).with("Menu: (1) Play | (2) New | (3) Analysis | (9) Exit")
@game.displaymenu
end
it "should create a method called resetgame" do
@game.resetgame
end
it "should display new game message when method resetgame called" do
@output.should_receive(:puts).with("New game...")
@game.resetgame
end
it "should set object variables to correct value when resetgame method called" do
@game.resetgame
@game.sequence.should == [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
@game.selectedboxes.should == []
@game.openedboxes.should == [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
@game.chosenbox.should == 0
@game.selectedbox.should == 0
@game.turn.should == 0
@game.winner.should == 0
@game.played.should == 0
@game.wins.should == 0
@game.losses.should == 0
@game.guess.should == ""
@game.values.should == [0.01,0.10,0.50,1.00,5.00,10.00,50.00,100.00,250.00,500.00,750.00,1000.00,3000.00,5000.00,10000.00,15000.00,20000.00,35000.00,50000.00,75000.00,100000.00,250000.00]
@game.amounts.should == @game.values
end
it "should create a method called assignvaluestoboxes" do
@game.resetgame
@game.assignvaluestoboxes
end
it "should provide 22 boxes (represented by the @sequence[] array) containing amounts between 0.01 and 250000.00 when method assignvaluestoboxes called" do
@game.resetgame
@game.assignvaluestoboxes
sequencelen = @game.sequence.length
sequencelen.should == 22
for i in (0..21) do
item = @game.sequence[i]
item.should > 0 and item.should <= 250000.00
end
end
it "should place each predefined amount (contained in the @values[] array) ramdomly into one empty box (represented by the @sequence[] array) when method assignvaluestoboxes called" do
@game.resetgame
@game.assignvaluestoboxes
for v in (0..21) do # for each value
f = 0 # reset frequency
for b in (0..21) do # for each box check value in box
if @game.sequence[b] == @game.values[v] # if box is same as value
f = f + 1 # increase frequency
end
end
f.should == 1 # frequency should only be 1 for each box
end
end
it "should create a method showboxes" do
@game.resetgame
@game.assignvaluestoboxes
@game.showboxes
end
it "should list each box and its status (|Opened|/[Closed]) when showboxes method called" do
@game.resetgame
@game.assignvaluestoboxes
for i in (0..21) do
s = "_"
g = "_"
b = i + 1
# @output.should_receive(:puts).with("Box #{b}: [#{b}] Status: #{@game.openedboxes[i]}")
if @game.openedboxes[i] == 0
s = "Closed"
g = "[#{b}]"
else
s = "Opened"
g = "|#{b}|"
end
@output.should_receive(:print).with("#{g} ")
end
@game.showboxes
end
it "should create a method showamounts" do
@game.resetgame
@game.assignvaluestoboxes
@game.showamounts
end
it "should show amounts in asending order as two columns when method showamounts called" do
@game.resetgame
@game.assignvaluestoboxes
col1 = 0
col2 = 11
for i in (0..10)
c1 = @game.amounts[col1 + i]
c2 = @game.amounts[col2 + i]
@output.should_receive(:puts).with("#{c1} #{c2}")
end
@game.showamounts
end
it "should create a method removeamount that accepts parameter value" do
@game.resetgame
@game.assignvaluestoboxes
r = rand(0..21)
value = @game.values[r]
@game.removeamount(value)
@game.amounts[r].should == " "
end
it "should create a method called setchosenbox that receives and stores a box number (contained in @chosenbox)" do
@game.resetgame
b = rand(1..22)
@game.setchosenbox(b)
box = @game.chosenbox
box.should > 0 and box.should <= 22
end
it "should create a method called getchosenbox that returns the box number (contained in @chosenbox)" do
@game.resetgame
b = rand(1..22)
@game.setchosenbox(b)
box = @game.getchosenbox
box.should == b
end
it "should display a message representing the chosen box when method displaychosenbox called" do
@game.resetgame
b = rand(1..22)
@game.setchosenbox(b)
box = @game.getchosenbox
@output.should_receive(:puts).with("Chosen box: [#{box}]")
@game.displaychosenbox
end
it "should display a message containing the value stored in a chosen box when method displaychosenboxvalue called" do
@game.resetgame
@game.assignvaluestoboxes
b = rand(1..22)
@game.setchosenbox(b)
box = @game.getchosenbox
value = @game.sequence[box-1]
@output.should_receive(:puts).with("Chosen box: [#{box}] contains: #{value}")
@game.displaychosenboxvalue
end
it "should display a prompt requesting the user choose a box to keep until the end when method displaychosenboxprompt called" do
@output.should_receive(:puts).with("Enter the number of the box you wish to keep.")
@game.displaychosenboxprompt
end
it "should display chosen box error when method displaychosenboxerror called" do
@output.should_receive(:puts).with("Error: Box number must be 1 to 22.")
@game.displaychosenboxerror
end
# it "should display an analysis message within method displayanalysis" do
# @output.should_receive(:puts).with("Game analysis")
# @game.displayanalysis
# end
it "should display a message and show the status (Opened or Closed) of each box when method displayanalysis called" do
@output.should_receive(:puts).with("Game analysis...")
@game.resetgame
@game.assignvaluestoboxes
for i in (0..21) do
s = "_"
g = "_"
b = i + 1
if @game.openedboxes[i] == 0
s = "Closed"
g = "[#{b}]"
else
s = "Opened"
g = "|#{b}|"
end
@output.should_receive(:puts).with("#{g} Status: #{s}")
# puts "\ni: #{i} Status: #{@game.openedboxes[i]} s: #{s}"
end
@game.displayanalysis
end
it "should check that entered box number is between 1..22 when guess received by method boxvalid" do
@game.resetgame
@game.assignvaluestoboxes
for i in (0..22) do
guess = i
valid = @game.boxvalid ("#{guess.to_s}")
if guess > 0 && guess <= 22
valid.should == 0
else
valid.should == 1
end
end
end
it "should display log of boxes selected when method showboxesselected called" do
@game.resetgame
@game.assignvaluestoboxes
@output.should_receive(:puts).with("Log: #{@game.selectedboxes.inspect}")
@game.showboxesselected
end
it "should display the status of a box (as Opened) when method openbox receives its associated box number" do
@game.resetgame
@game.assignvaluestoboxes
guess = 1
@game.openbox(guess)
for i in (0..21) do
s = "_"
g = "_"
b = i + 1
if @game.openedboxes[i] == 0
s = "Closed"
g = "[#{b}]"
else
s = "Opened"
g = "|#{b}|"
end
@output.should_receive(:puts).with("#{g} Status: #{s}")
# puts "\ni: #{i} Status: #{@game.openedboxes[i]} Status: #{s}"
end
@game.displayanalysis
# @game.showboxes
end
it "should display the status of a box (as Opened) when method openbox receives its associated box number" do
@game.resetgame
@game.assignvaluestoboxes
guess = 1
@game.openbox(guess)
for i in (0..21) do
s = "_"
g = "_"
b = i + 1
if @game.openedboxes[i] == 0
s = "Closed"
g = "[#{b}]"
else
s = "Opened"
g = "|#{b}|"
end
@output.should_receive(:print).with("#{g} ")
# puts "\ni: #{i} Status: #{@game.openedboxes[i]} Status: #{s}"
end
# @game.displayanalysis
@game.showboxes
end
it "should show chosen box when method showboxes called" do
@game.resetgame
@game.assignvaluestoboxes
c = rand(1..22)
for i in (0..21) do
b = i + 1
g = "_"
@game.setchosenbox(c)
if @game.chosenbox == c
g = "*#{c}*"
@output.should_receive(:print).with("#{g} ")
else
g = "[#{b}]"
@output.should_receive(:print).with("#{g} ")
end
@game.showboxes
end
end
it "should display offer from banker when value received by method bankerphoneswithvalue" do
offer = rand(0..100000)
@output.should_receive(:puts).with("Banker offers you for your chosen box: #{offer}")
@game.bankerphoneswithvalue(offer)
end
it "should return the number of boxes still closed when method numberofboxesclosed called" do
@game.resetgame
@game.assignvaluestoboxes
o = rand(0..21) # number of boxes to open
c = 21 - o # number of boxes to remain closed
for i in (0..o) do
@game.openedboxes[i] = 1
end
num = @game.numberofboxesclosed
num.should == c
end
it "should create a method incrementturn which increases @turn by 1" do
@game.resetgame
@game.incrementturn
@game.turn.should == 1
@game.incrementturn
@game.turn.should == 2
end
it "should create a method getturnsleft which returns @turnsleft containing goes left" do
@game.resetgame
@game.incrementturn
turnsleft = @game.getturnsleft
turnsleft.should == GOES - 1
@game.incrementturn
turnsleft = @game.getturnsleft
turnsleft.should == GOES - 2
end
it "should display an exit message when method finish called" do
@output.should_receive(:puts).with("... game finished.")
@game.finish
end
end
end
end