-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom_bag.e
91 lines (76 loc) · 1.87 KB
/
random_bag.e
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
note
description : "A bag that is used to randomize {TETROMINO} `pick' order."
author : "Louis Marchand"
date : "July 19 2012"
revision : "1.0"
class
RANDOM_BAG
create
make
feature {NONE} -- Initialization
make(rnd_ctrl:GAME_RANDOM_CONTROLLER;min,max:INTEGER)
-- Initialization for `Current' using `rnd_ctrl' as `random_ctrl' and
-- `min' and `max' as possible value in the `bag'.
require
rnd_ctrl/=Void
do
min_value:=min
max_value:=max
random_ctrl:=rnd_ctrl
create bag.make (max-min+1)
end
feature -- Access
pick
-- Assign the next element of `Current' to `last_pick' and remove it.
do
if bag.count<1 then
fill_bag
end
last_pick:=bag.item
bag.remove
ensure
Random_Bag_Pick_Value_Valid: last_pick>=min_value and last_pick<=max_value
end
last_pick:INTEGER
-- The last element that has been assign by `pick'
max_value:INTEGER
-- The maximal value that `Current' can `pick'
min_value:INTEGER
-- The minimal value that `Current' can `pick'
feature {NONE} -- Implementation - Routines
fill_bag
-- Add every possible element between `min_value'
-- and `max_value' in the `bag' with random order.
local
value_list:LINKED_LIST[INTEGER]
i:INTEGER
index:INTEGER
do
create value_list.make
from
i:=min_value
until
i>max_value
loop
value_list.extend (i)
i:=i+1
end
from
i:=max_value-min_value+1
until
i<1
loop
random_ctrl.generate_new_random
index:=random_ctrl.last_random_integer_between (1, i)
value_list.go_i_th (index)
bag.put (value_list.item)
value_list.remove
i:=i-1
end
end
feature {NONE} -- Implementation - Variables
bag:ARRAYED_STACK[INTEGER]
-- {STACK} that contain every element to `pick' in random order.
random_ctrl:GAME_RANDOM_CONTROLLER
-- Random manager used to randomize the elements of `bag'
end