-
Notifications
You must be signed in to change notification settings - Fork 9
/
machine.py
62 lines (51 loc) · 2.28 KB
/
machine.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
import random
from itertools import combinations
from shapely.geometry import LineString, Point
class MACHINE():
"""
[ MACHINE ]
MinMax Algorithm을 통해 수를 선택하는 객체.
- 모든 Machine Turn마다 변수들이 업데이트 됨
** To Do **
MinMax Algorithm을 이용하여 최적의 수를 찾는 알고리즘 생성
- class 내에 함수를 추가할 수 있음
- 최종 결과는 find_best_selection을 통해 Line 형태로 도출
* Line: [(x1, y1), (x2, y2)] -> MACHINE class에서는 x값이 작은 점이 항상 왼쪽에 위치할 필요는 없음 (System이 organize 함)
"""
def __init__(self, score=[0, 0], drawn_lines=[], whole_lines=[], whole_points=[], location=[]):
self.id = "MACHINE"
self.score = [0, 0] # USER, MACHINE
self.drawn_lines = [] # Drawn Lines
self.board_size = 7 # 7 x 7 Matrix
self.num_dots = 0
self.whole_points = []
self.location = []
self.triangles = [] # [(a, b), (c, d), (e, f)]
def find_best_selection(self):
available = [[point1, point2] for (point1, point2) in list(combinations(self.whole_points, 2)) if self.check_availability([point1, point2])]
return random.choice(available)
def check_availability(self, line):
line_string = LineString(line)
# Must be one of the whole points
condition1 = (line[0] in self.whole_points) and (line[1] in self.whole_points)
# Must not skip a dot
condition2 = True
for point in self.whole_points:
if point==line[0] or point==line[1]:
continue
else:
if bool(line_string.intersection(Point(point))):
condition2 = False
# Must not cross another line
condition3 = True
for l in self.drawn_lines:
if len(list(set([line[0], line[1], l[0], l[1]]))) == 3:
continue
elif bool(line_string.intersection(LineString(l))):
condition3 = False
# Must be a new line
condition4 = (line not in self.drawn_lines)
if condition1 and condition2 and condition3 and condition4:
return True
else:
return False