This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTT.py
65 lines (58 loc) · 1.95 KB
/
TTT.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
import random
class game:
def __init__(self):
self.table = [0] * 9
self.currentPlayer = 1
self.HAS_WON = 9
self.HAS_LOST = -1
self.HAS_ENDED = -3
self.NO_MOVE = -2
def validMove(self, index):
return self.table[index] == 0
def hasWon(self):
c = 0
rows = []
rows.append(self.table[0:3])
rows.append(self.table[3:6])
rows.append(self.table[6:9])
rows.append(self.table[0:9:3])
rows.append(self.table[1:9:3])
rows.append(self.table[2:9:3])
rows.append(self.table[0:9:4])
rows.append(self.table[2:8:2])
for row in rows:
if row[0] == row[1] == row[2] != 0:
return row[0]
return 0
def getOpenSpaces(self):
counter = 0
openSpaces = []
while counter < len(self.table):
if self.validMove(counter): openSpaces.append(counter)
counter = counter + 1
return openSpaces
def nextMove(self, previousMove):
if previousMove != self.NO_MOVE:
if not self.validMove(previousMove):
print("Betrueger!")
return "Error: Betrug."
self.table[previousMove] = self.currentPlayer
self.currentPlayer = -self.currentPlayer
winner = self.hasWon()
if winner != 0:
if winner == self.currentPlayer: return self.HAS_WON
else: return self.HAS_LOST
openSpaces = self.getOpenSpaces()
if openSpaces == []: return self.HAS_ENDED
return openSpaces[random.randint(0, len(openSpaces) - 1)]
def printState(self):
print(self.table[0:3])
print(self.table[3:6])
print(self.table[6:9])
if __name__ == "__main__":
gm = game()
res = gm.NO_MOVE
while res != gm.HAS_WON and res != gm.HAS_LOST and res != gm.HAS_ENDED:
res = gm.nextMove(res)
gm.printState()
print(res)