File tree 5 files changed +105
-11
lines changed
chapter_13_creating_new_classes_changing_existing_ones
5 files changed +105
-11
lines changed Original file line number Diff line number Diff line change 1
1
1 - Chapter 9: Modern Roman Numerals
2
2
7 - Chapter 11 --> What is the difference between mixed up and random?
3
- 8 - Chapter 12: Section 12.6
4
3
9 - Chapter 13: Orange Tree
5
4
10 - Chapter 13: Interactive Baby Dragon
Original file line number Diff line number Diff line change
1
+ # the point of this exercise is to create a method that takes an array and
2
+ # returns the items in the array in in a completely random way
3
+ #
4
+ # for this program, I am going to be using the rand function, which returns an
5
+ # integer greater than or equal to 0 but less than the number passed.
6
+ #
7
+ # I'm also going to use the Array#delete_at(index) function, which deletes the
8
+ # element at the given index, but also returns it
9
+
10
+ def shuffle(input)
11
+ randomized = Array.new
12
+
13
+ until input.length == 0
14
+ # initialize a random integer within the array's # of elements
15
+ randomized_index = rand(input.length)
16
+
17
+ # push the element at that integer to our output array and delete that
18
+ # element from our input array
19
+ randomized << input.delete_at(randomized_index)
20
+ end
21
+
22
+ return randomized
23
+ end
Original file line number Diff line number Diff line change 6
6
#
7
7
# I'm also going to use the Array#delete_at(index) function, which deletes the
8
8
# element at the given index, but also returns it
9
+ #
10
+ # In this method, we are extending the Array class to support our shuffle
11
+ # method. Notice the use of the keyword self to refer to the calling object
9
12
10
- def shuffle ( input )
11
- randomized = Array . new
13
+ class Array
14
+ def shuffle
15
+ original_array = Array . new ( self )
12
16
13
- until input . length == 0
14
- # initialize a random integer within the array's # of elements
15
- randomized_index = rand ( input . length )
17
+ randomized = Array . new
16
18
17
- # push the element at that integer to our output array and delete that
18
- # element from our input array
19
- randomized << input . delete_at ( randomized_index )
20
- end
19
+ until original_array . length == 0
20
+ # initialize a random integer within the array's # of elements
21
+ randomized_index = rand ( original_array . length )
21
22
22
- return randomized
23
+ # push the element at that integer to our output array and delete that
24
+ # element from our input array
25
+ randomized << original_array . delete_at ( randomized_index )
26
+ end
27
+
28
+ return randomized
29
+ end
23
30
end
Original file line number Diff line number Diff line change
1
+ # =============================================================================
2
+ # Orange Tree
3
+ # =============================================================================
4
+
5
+ class OrangeTree
6
+ def initialize ( name = nil )
7
+ @height = 0
8
+ @age = 0
9
+ @name = name
10
+ @oranges = 0
11
+ end
12
+
13
+ def height
14
+ one_year_passes
15
+ return @height
16
+ end
17
+
18
+ def name
19
+ one_year_passes
20
+ return @name
21
+ end
22
+
23
+ def count_the_oranges
24
+ one_year_passes
25
+ return @oranges
26
+ end
27
+
28
+ def pick_an_orange
29
+ if @oranges >= 1
30
+ puts "You have picked an orange."
31
+ @oranges -= 1
32
+ puts "The tree now has #{ @oranges } fruit left."
33
+ else
34
+ puts "You don't have any oranges to pick."
35
+ end
36
+
37
+ one_year_passes
38
+ end
39
+
40
+ def continue
41
+ one_year_passes
42
+ end
43
+
44
+ private
45
+ def one_year_passes
46
+ @height += 1
47
+ @age += 1
48
+
49
+ if @age >= 13
50
+ puts "Your orange tree #{ @name } has withered away and died."
51
+ exit
52
+ elsif @age >= 3
53
+ random = 0
54
+ loop do
55
+ random = rand ( @age )
56
+ break if random > 0
57
+ end
58
+
59
+ @oranges = random * 2
60
+ end
61
+
62
+ print "\n Your tree has grown. It is now #{ @height } feet tall.\n It is has #{ @oranges } fruit.\n "
63
+ end
64
+ end
Original file line number Diff line number Diff line change
1
+ ../chapter_10_recursion/shuffle . rb
You can’t perform that action at this time.
0 commit comments