-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathships.py
139 lines (125 loc) · 5.29 KB
/
ships.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from random import randint
from random import choice
from oreol import Oreol
def rand_coordinate():
return randint(0, 9)
def random_sign(fcoord, scoord):
if fcoord == 0 and scoord not in (0, 9):
sign = choice([[fcoord + 1, scoord], [fcoord, scoord + 1], [fcoord, scoord - 1]])
elif fcoord == 9 and scoord not in (0, 9):
sign = choice([[fcoord - 1, scoord], [fcoord, scoord + 1], [fcoord, scoord - 1]])
elif scoord == 0 and fcoord not in (0, 9):
sign = choice([[fcoord, scoord + 1], [fcoord + 1, scoord], [fcoord - 1, scoord]])
elif scoord == 9 and fcoord not in (0, 9):
sign = choice([[fcoord, scoord - 1], [fcoord + 1, scoord], [fcoord - 1, scoord]])
elif fcoord == 9 and scoord == 0:
sign = choice([[fcoord - 1, scoord], [fcoord, scoord + 1]])
elif fcoord == 0 and scoord == 9:
sign = choice([[fcoord + 1, scoord], [fcoord, scoord - 1]])
elif fcoord == 9 and scoord == 9:
sign = choice([[fcoord - 1, scoord], [fcoord, scoord - 1]])
elif fcoord == 0 and scoord == 0:
sign = choice([[fcoord + 1, scoord], [fcoord, scoord + 1]])
else:
sign = choice([[fcoord + 1, scoord], [fcoord - 1, scoord], [fcoord, scoord + 1], [fcoord, scoord - 1]])
return sign
class Ships:
def __init__(self):
orl = Oreol()
self._oreol = orl
self.ships = list()
# 1P = 4
def desk1(self):
while len(self.ships) <= 3:
fcoord = rand_coordinate()
scoord = rand_coordinate()
if [fcoord, scoord] not in self._oreol.oreol:
self.ships.append([fcoord, scoord])
self._oreol.add_oreol(fcoord, scoord)
return self.ships
# 2P = 3
def desk2(self):
self.ships.clear()
while len(self.ships) <= 4:
fcoord = rand_coordinate()
scoord = rand_coordinate()
sign = random_sign(fcoord, scoord)
if [fcoord, scoord] not in self._oreol.oreol and sign not in self._oreol.oreol:
self.ships.append([fcoord, scoord])
self._oreol.add_oreol(fcoord, scoord)
self.ships.append(sign)
self._oreol.add_oreol(sign[0], sign[1])
return self.ships
# 3P = 2
def desk3(self):
self.ships.clear()
while len(self.ships) <= 3:
fcoord = rand_coordinate()
scoord = rand_coordinate()
sign = random_sign(fcoord, scoord)
if fcoord == sign[0]:
if max(scoord, sign[1]) + 1 > 9:
res = min(scoord, sign[1]) - 1
else:
res = max(scoord, sign[1]) + 1
sign2 = [fcoord, res]
else:
if max(fcoord, sign[0]) + 1 > 9:
res = min(fcoord, sign[0]) - 1
else:
res = max(fcoord, sign[0]) + 1
sign2 = [res, scoord]
if [fcoord, scoord] not in self._oreol.oreol and sign not in self._oreol.oreol and sign2 not in self._oreol.oreol:
self.ships.append([fcoord, scoord])
self._oreol.add_oreol(fcoord, scoord)
self.ships.append(sign)
self._oreol.add_oreol(sign[0], sign[1])
self.ships.append(sign2)
self._oreol.add_oreol(sign2[0], sign2[1])
return self.ships
# 4P = 1
def desk4(self):
self.ships.clear()
while len(self.ships) <= 1:
fcoord = rand_coordinate()
scoord = rand_coordinate()
sign = random_sign(fcoord, scoord)
if fcoord == sign[0]:
if max(scoord, sign[1]) + 1 > 9:
res = min(scoord, sign[1]) - 1
else:
res = max(scoord, sign[1]) + 1
sign2 = [fcoord, res]
else:
if max(fcoord, sign[0]) + 1 > 9:
res = min(fcoord, sign[0]) - 1
else:
res = max(fcoord, sign[0]) + 1
sign2 = [res, scoord]
if fcoord == sign[0] == sign2[0]:
if max(scoord, sign[1], sign2[1]) + 1 > 9 and min(scoord, sign[1], sign2[1]) != 0:
res = min(scoord, sign[1], sign2[1]) - 1
else:
res = max(scoord, sign[1], sign2[1]) + 1
sign3 = [fcoord, res]
else:
if max(fcoord, sign[0], sign2[0]) + 1 > 9 and min(fcoord, sign[0], sign2[0]) != 0:
res = min(fcoord, sign[0], sign2[0]) - 1
else:
res = max(fcoord, sign[0], sign2[0]) + 1
sign3 = [res, scoord]
if (
[fcoord, scoord] not in self._oreol.oreol
and sign not in self._oreol.oreol
and sign2 not in self._oreol.oreol
and sign3 not in self._oreol.oreol
):
self.ships.append([fcoord, scoord])
self._oreol.add_oreol(fcoord, scoord)
self.ships.append(sign)
self._oreol.add_oreol(sign[0], sign[1])
self.ships.append(sign2)
self._oreol.add_oreol(sign2[0], sign2[1])
self.ships.append(sign3)
self._oreol.add_oreol(sign3[0], sign3[1])
return self.ships