Skip to content

Commit 87df204

Browse files
committed
Add wrap and neighbor access methods for grid logic
1 parent 73c72ee commit 87df204

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/Containers-Array2D-Tests/CTArray2DTest.class.st

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,34 @@ CTArray2DTest >> testAtColumnPut [
8787
1 to: foo height do: [ :y | self assert: (foo atColumn: 2 atRow: y) equals: 1 ]
8888
]
8989

90+
{ #category : 'tests-accessing' }
91+
CTArray2DTest >> testAtColumnWrapAtRowWrap [
92+
93+
| array |
94+
array := self arrayClass width2Height3.
95+
96+
self assert: (array atColumnWrap: 1 atRowWrap: 1) equals: 1.
97+
98+
self assert: (array atColumnWrap: 3 atRowWrap: 1) equals: 1.
99+
100+
self assert: (array atColumnWrap: 2 atRowWrap: 4) equals: 2.
101+
102+
self assert: (array atColumnWrap: 0 atRowWrap: 0) equals: 6.
103+
]
104+
105+
{ #category : 'tests-accessing' }
106+
CTArray2DTest >> testAtColumnWrapAtRowWrapPut [
107+
108+
| array |
109+
array := self arrayClass width2Height3.
110+
111+
array atColumnWrap: 3 atRowWrap: 2 put: 99.
112+
self assert: (array atColumn: 1 atRow: 2) equals: 99.
113+
114+
array atColumnWrap: 0 atRowWrap: 0 put: 88.
115+
self assert: (array atColumn: 2 atRow: 3) equals: 88.
116+
]
117+
90118
{ #category : 'tests-accessing' }
91119
CTArray2DTest >> testAtPut [
92120

@@ -236,6 +264,33 @@ CTArray2DTest >> testLeftToRightFromBottomToTopDo [
236264

237265
]
238266

267+
{ #category : 'tests-accessing' }
268+
CTArray2DTest >> testNeighborsAtColumnAtRow [
269+
270+
| array neighbors topCornerNeighbors |
271+
array := self arrayClass width2Height3.
272+
273+
neighbors := array neighborsAtColumn: 1 atRow: 2.
274+
self assert: neighbors size equals: 5.
275+
self assert: (neighbors includesAll: #(1 2 4 5 6)).
276+
277+
topCornerNeighbors := array neighborsAtColumn: 1 atRow: 1.
278+
self assert: topCornerNeighbors size equals: 3.
279+
self assert: (topCornerNeighbors includesAll: #(2 3 4)).
280+
]
281+
282+
{ #category : 'tests-accessing' }
283+
CTArray2DTest >> testNeighborsEight [
284+
285+
| array neighbors |
286+
array := self arrayClass fromArray: #(1 2 3 4 5 6 7 8 9) width: 3.
287+
288+
neighbors := array neighborsAtColumn: 2 atRow: 2.
289+
290+
self assert: neighbors size equals: 8.
291+
self assert: (neighbors includesAll: #(1 2 3 4 6 7 8 9)).
292+
]
293+
239294
{ #category : 'tests-printing' }
240295
CTArray2DTest >> testPrinting [
241296

src/Containers-Array2D/CTArray2D.class.st

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,26 @@ CTArray2D >> atColumn: x put: aCollection [
193193
^ aCollection
194194
]
195195

196+
{ #category : 'accessing' }
197+
CTArray2D >> atColumnWrap: col atRowWrap: row [
198+
199+
| wrappedCol wrappedRow |
200+
wrappedCol := ((col - 1) \\ self width) + 1.
201+
wrappedRow := ((row - 1) \\ self height) + 1.
202+
203+
^ self atColumn: wrappedCol atRow: wrappedRow
204+
]
205+
206+
{ #category : 'accessing' }
207+
CTArray2D >> atColumnWrap: col atRowWrap: row put: aValue [
208+
209+
| wrappedCol wrappedRow |
210+
wrappedCol := ((col - 1) \\ self width) + 1.
211+
wrappedRow := ((row - 1) \\ self height) + 1.
212+
213+
^ self atColumn: wrappedCol atRow: wrappedRow put: aValue
214+
]
215+
196216
{ #category : 'accessing rows/columns' }
197217
CTArray2D >> atRow: y [
198218
"Answer the content of the whole column at y"
@@ -301,6 +321,22 @@ CTArray2D >> leftToRightFromBottomToTopDo: aBlock [
301321
aBlock value: (self atColumn: col atRow: row)]]
302322
]
303323

324+
{ #category : 'enumerating' }
325+
CTArray2D >> neighborsAtColumn: col atRow: row [
326+
327+
| neighbors |
328+
neighbors := OrderedCollection new.
329+
330+
row - 1 to: row + 1 do: [ :r |
331+
col - 1 to: col + 1 do: [ :c |
332+
(r = row and: [ c = col ]) ifFalse: [
333+
(r between: 1 and: self height) ifTrue: [
334+
(c between: 1 and: self width) ifTrue: [
335+
neighbors add: (self atColumn: c atRow: r) ] ] ] ] ].
336+
337+
^ neighbors
338+
]
339+
304340
{ #category : 'accessing - compatibility' }
305341
CTArray2D >> numberOfColumns [
306342
"Answer the receiver's width, i.e., its number of x"

0 commit comments

Comments
 (0)