/
seaposition.py
executable file
·65 lines (57 loc) · 1.82 KB
/
seaposition.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 15 17:06:04 2017
@author: michael
"""
class SeaPosition(object):
"""
Cartesian position (x,y) within the sea.
"""
def __init__(self, x, y, sea):
"""
Initialize position with given coordinates
"""
self.x = x
self.y = y
self.sea = sea
def getX(self):
return self.x
def getY(self):
return self.y
def getSeaPosition(self):
return(self.x, self.y)
def getAdjacent(self, traditional):
"""
Returns two arrays of tuples for the positions adjacent to the position.
The first contains location tuples for empty cells, while the second
contains cells with a creature in it.
The cells can either be those in the n, e, s, w postion, or all eight
surrounding cells - n, ne, e, se, s, sw, w, ne.
traditional:
(0,+1)
(-1, 0) Pos. (+1, 0)
(0,-1)
new:
(-1,+1) (0,+1) (+1,+1)
(-1, 0) Pos. (+1, 0)
(-1,-1) (0,-1) (+1,-1)
"""
empty = []
occupied = []
search = []
if traditional:
search = [(0,+1),(-1, 0),(+1, 0),(0,-1)]
else:
search = [(-1,+1),(0,+1),(+1,+1),(-1, 0),(+1, 0),(-1,-1),(0,-1),(+1,-1)]
for s in search:
deltaX, deltaY = s[0], s[1]
newX = (self.x + deltaX + self.sea.getMaxX()) % self.sea.getMaxX()
newY = (self.y + deltaY + self.sea.getMaxY()) % self.sea.getMaxY()
if self.sea.isCellEmpty(newX,newY):
empty.append((newX, newY))
else:
occupied.append((newX, newY))
return(empty,occupied)
def __str__(self):
return "(%d, %d)" % (self.x, self.y)