This repository was archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch10-continent.rb
More file actions
99 lines (89 loc) · 2.97 KB
/
ch10-continent.rb
File metadata and controls
99 lines (89 loc) · 2.97 KB
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
# program to determine the size of a continent from a 'map' in an array
o = 'water'
M = 'land'
world1 = [[o,o,o,o,o,o,o,o,o,o,o],
[o,o,o,o,M,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,M,M,o],
[o,o,o,M,o,o,o,o,o,M,o],
[o,o,o,M,o,M,M,o,o,o,o],
[o,o,o,o,M,M,M,M,o,o,o],
[o,o,o,M,M,M,M,M,M,M,o],
[o,o,o,M,M,o,M,M,M,o,o],
[o,o,o,o,o,o,M,M,o,o,o],
[o,M,o,o,o,M,o,o,o,o,o],
[o,o,o,o,o,o,o,o,o,o,o]]
world2 = [[o,o,M,o,o,o,o,o,o,o,o],
[o,o,M,o,M,M,o,o,o,o,o],
[o,o,M,o,o,o,o,o,M,M,o],
[o,o,o,M,o,o,o,o,o,M,o],
[o,M,M,M,o,M,M,o,o,o,o],
[M,o,o,o,M,o,M,M,o,o,o],
[o,o,o,M,M,M,M,M,M,M,M],
[o,o,o,M,M,o,M,M,M,o,o],
[o,o,o,o,o,o,M,M,o,o,o],
[o,M,o,o,o,M,o,o,o,o,o],
[o,o,o,o,o,M,o,o,o,o,o]]
world3 = [[o,o,M,o,o,o,o,o,o,o,o,o],
[o,o,M,o,M,M,o,o,o,o,o,o],
[o,o,M,o,o,o,o,o,M,M,o,o],
[o,o,o,M,o,o,o,o,o,M,o,o],
[o,M,M,M,o,M,M,o,o,o,o,o],
[M,o,o,o,M,o,M,M,o,o,o,o],
[o,o,o,M,M,M,M,M,M,M,M,o],
[o,o,o,M,M,o,M,M,M,o,o,M],
[o,o,o,o,o,o,M,M,o,o,o,o],
[o,M,o,o,o,M,o,o,o,o,o,o],
[o,o,o,o,o,M,o,o,o,o,o,o]]
world4 = [[o,o,M,o,o,o,o,o,o,o,o],
[o,o,M,o,M,M,o,o,o,o,o],
[o,o,M,o,o,o,o,o,M,M,o],
[o,o,o,M,o,o,o,o,o,M,o],
[o,M,M,M,o,M,M,o,o,o,o],
[M,o,o,o,M,o,M,M,o,o,o],
[o,o,o,M,M,M,M,M,M,M,M],
[o,o,o,M,M,o,M,M,M,o,o],
[o,o,o,o,o,o,M,M,o,o,o],
[o,M,o,o,o,M,o,o,o,o,o],
[o,o,o,o,o,M,o,o,o,o,o],
[o,o,o,o,o,M,o,o,o,o,o]]
def continent_size world, x, y # x and y are the starting co-ordinates
if (x < 0)||(x>=world[0].size)||(y<0)||(y>=world.size)
# falling off the edge of the map
return 0
end
if world [y][x] != 'land'
# water or already checked
return 0
end
size = 1 # count current tile
world [y][x] = 'counted land' #change to counted
#count all neighbouring tiles recursively
size = size + continent_size(world,x-1,y-1)
size = size + continent_size(world,x,y-1)
size = size + continent_size(world,x+1,y-1)
size = size + continent_size(world,x-1,y)
size = size + continent_size(world,x+1,y)
size = size + continent_size(world,x-1,y+1)
size = size + continent_size(world,x,y+1)
size = size + continent_size(world,x+1,y+1)
size
end
puts continent_size(world1,5,5)
puts continent_size(world1,9,2)
puts continent_size(world1,4,1)
puts continent_size(world1,1,9)
puts continent_size(world1,8,9)
puts continent_size(world1,10,10)
puts continent_size(world1,0,0)
puts
puts continent_size(world2,6,5)
puts world2.size.to_s
puts world2[0].size.to_s
puts
puts continent_size(world3,6,5)
puts world3.size.to_s
puts world3[0].size.to_s
puts
puts continent_size(world4,6,5)
puts world4.size.to_s
puts world4[0].size.to_s