-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTicTacToe.py
124 lines (114 loc) · 3.32 KB
/
TicTacToe.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
#!/usr/bin/env python
import os #for making folders and links
os.mkdir("Moves")
for x in range(0,(19684)): #should be 3^9 possibilities 3, possibilities for 9 squares
os.mkdir("Moves/" + str(x)) #Make directories for gamestate so they can be moved into and linked to
os.symlink("Moves/0", "START") #link's to first state
os.chdir("Moves") #yeah, way better then borking your system
os.mkdir("Invalid_Move") #folder for invalid moves
open("Invalid_Move/Invalid_Move", 'w').close()
for gamestate in range(19684):
if (gamestate % 703) == 0:
print(str(gamestate/703) + " / 28 done")#create gamestate's
board = [] #gamestate as a list
os.chdir(str(gamestate)) #Get ready to make gamestate
for square in range(0,9):
board.append((gamestate/(3**square))%3) #gamestate is in ternary
numo = 0 #num of O's
numx = 0 #num of X's
numun = 0 #num of un-used squares
for x in board: #count to figure out who's turn it is
if x == 2:
numo += 1
elif x == 1:
numx += 1
else:
numun += 1
#1 = player1
#2 = player2
#3 = Invalid State
#4 = no moves left
#5 = winnerx
#6 = winnero
if numx == numo: #Checks who's turn it is/for invalid states
PlayersTurn = 1
elif (numun == 0):
PlayersTurn = 4
elif numx == (numo + 1):
PlayersTurn = 2
else:
PlayersTurn = 3
#Screw it, I'll do 8 checks by hand checks for winning
if (PlayersTurn != 3):
if (board[0] == board[1] == board[2] != 0):
if (numx == numo):
PlayersTurn=6
else:
PlayersTurn=5
elif (board[3] == board[4] == board[5] != 0):
if (numx == numo):
PlayersTurn=6
else:
PlayersTurn=5
elif (board[6] == board[7] == board[8] != 0):
if (numx == numo):
PlayersTurn=6
else:
PlayersTurn=5
elif (board[0] == board[3] == board[6] != 0):
if (numx == numo):
PlayersTurn=6
else:
PlayersTurn=5
elif (board[1] == board[4] == board[7] != 0):
if (numx == numo):
PlayersTurn=6
else:
PlayersTurn=5
elif (board[2] == board[5] == board[8] != 0):
if (numx == numo):
PlayersTurn=6
else:
PlayersTurn=5
elif (board[0] == board[4] == board[8] != 0):
if (numx == numo):
PlayersTurn=6
else:
PlayersTurn=5
elif (board[2] == board[4] == board[6] != 0):
if (numx == numo):
PlayersTurn=6
else:
PlayersTurn=5
if PlayersTurn < 3:
for makelinks in range(0,9): #make the board if not winner
filename = ""
if board[makelinks] == 0:
filename = str(makelinks) + "--"
linknum = 0
for x in range(0,9):
if x == makelinks:
linknum += PlayersTurn * 3**x
else:
linknum += board[x] * 3**x
os.symlink("../" + str(linknum), filename)
elif board[makelinks] == 1:
filename = str(makelinks) + "X"
os.symlink("../Invalid_Move", filename)
else:
filename = str(makelinks) + "O"
os.symlink("../Invalid_Move", filename)
else:
if (PlayersTurn == 3): #makes end game files and a link to initial state
open("cheater", 'w').close()
os.symlink("../0", "Play_Again?")
elif (PlayersTurn == 4):
open("cats game", 'w').close()
os.symlink("../0", "Play_Again?")
elif (PlayersTurn == 5):
open("X wins!!", 'w').close()
os.symlink("../0", "Play_Again?")
elif (PlayersTurn == 6):
open("O wins!!", 'w').close()
os.symlink("../0", "Play_Again?")
os.chdir("..") #gets ready for making next game state