-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
106 lines (95 loc) · 2.47 KB
/
player.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
import sys
from tkinter import Tk, Button
from tkinter.font import Font
from copy import deepcopy
class Player:
def __init__(self,other=None):
self.player = 'O'
self.ai = 'X'
self.empty = ' '
self.size = 3
self.fields = {}
self.score = 0
for y in range(self.size):
for x in range(self.size):
self.fields[x,y] = self.empty
if other:
self.__dict__ = deepcopy(other.__dict__)
def tied(self):
for (x,y) in self.fields:
if self.fields[x,y]==self.empty:
return False
return True
def won(self):
# horizontal
for y in range(self.size):
winning = []
for x in range(self.size):
if self.fields[x,y] == self.ai:
winning.append((x,y))
if len(winning) == self.size:
return winning
# vertical
for x in range(self.size):
winning = []
for y in range(self.size):
if self.fields[x,y] == self.ai:
winning.append((x,y))
if len(winning) == self.size:
return winning
# diagonal
winning = []
for y in range(self.size):
x = y
if self.fields[x,y] == self.ai:
winning.append((x,y))
if len(winning) == self.size:
return winning
# other diagonal
winning = []
for y in range(self.size):
x = self.size-1-y
if self.fields[x,y] == self.ai:
winning.append((x,y))
if len(winning) == self.size:
return winning
# default
return None
def move(self,x,y):
board = Player(self)
board.fields[x,y] = board.player
(board.player,board.ai) = (board.ai,board.player)
return board
def __minimax(self, O):
if self.won():
if O:
return (-5,None)
else:
return (+5,None)
elif self.tied():
return (0,None)
elif O:
best = (-10,None)
for x,y in self.fields:
if self.fields[x,y]==self.empty:
value = self.move(x,y).__minimax(not O)[0]
if value>best[0]:
best = (value,(x,y))
return best
else:
best = (+10,None)
for x,y in self.fields:
if self.fields[x,y]==self.empty:
value = self.move(x,y).__minimax(not O)[0]
if value<best[0]:
best = (value,(x,y))
return best
def best(self):
return self.__minimax(True)[0]
def __str__(self):
string = ''
for y in range(self.size):
for x in range(self.size):
string+=self.fields[x,y]
string+="\n"
return string