-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
185 lines (148 loc) · 5.19 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from minimaxAlphaBeta import *
RED = '\033[1;31;40m'
YELLOW = '\033[1;33;40m'
BLUE = '\033[1;34;40m'
MAGENTA = '\033[1;35;40m'
CYAN = '\033[1;36;40m'
WHITE = '\033[1;37;40m'
dir_path = os.getcwd()
os.chdir(dir_path)
def saveParser(board, filename):
def parseRow(row):
return str(row).strip(']').strip('[').replace("' '",'.').replace(',','').replace("'",'')
if os.name == 'nt':
slash = '\\'
else:
slash = '/'
with open(dir_path +slash + "saved-games" + slash +filename + '.txt', 'w') as f:
for row in board:
f.write(parseRow(row)+'\n')
return True
def saveBoard(board):
saveFile = True if input(YELLOW + 'DO YOU WANT TO SAVE BOARD AND QUIT(y/n)? ' + WHITE).lower() == 'y' else False
if saveFile:
filename = input(CYAN + 'ENTER FILE NAME: ' + WHITE)
if saveParser(board,filename):
return True
return False
def loadParser(filename):
def parseBoard(board):
for row in range(BOARD_HEIGHT):
for col in range(BOARD_WIDTH):
if board[row][col]=='.':
board[row][col]=' '
return board
if os.name == 'nt':
slash = '\\'
else:
slash = '/'
f = open(dir_path +slash + "saved-games" + slash +filename + '.txt')
result = [[c.replace(' ',' ').lower()for c in l.strip('\n').split(' ')] for l in f.readlines()]
board = parseBoard(result)
return board
def loadBoard():
loadFlag = True if input(YELLOW + 'DO YOU WANT TO LOAD A BOARD(y/n)? ' + WHITE).lower() == 'y' else False
if loadFlag:
filename = input(CYAN + 'ENTER FILE NAME: ' + WHITE)
board = loadParser(filename)
return board, loadFlag
else:
return None, loadFlag
def playerTurn(board):
Col = input(YELLOW + 'Choose a Column between 1 and 7: ' + WHITE)
if not(Col.isdigit()):
print(MAGENTA + "Input must be integer!" + WHITE)
return playerTurn(board)
playerMove = int(Col) - 1
if playerMove < 0 or playerMove > 6:
print(MAGENTA + "Column must be between 1 and 7!" + WHITE)
return playerTurn(board)
if not(isColumnValid(board, playerMove)):
print(MAGENTA + "The Column you select is full!" + WHITE)
return playerTurn(board)
board = makeMove(board, playerMove, HUMAN_PLAYER)[0]
playerFourInRow = findFours(board)
return board, playerFourInRow
def playerWins(board):
printBoard(board)
print(' '+BLUE+"HUMAN WINS !!\n" +WHITE)
playagain = True if input(YELLOW +'DO YOU WANT TO PLAY AGAIN(y/n)?'+WHITE).lower() == 'y' else False
#saveBoard(board)
if playagain:
mainFucntion()
return 0
def aiTurn(board,depth):
aiMove = MiniMaxAlphaBeta(board, depth, AI_PLAYER)
board = makeMove(board, aiMove, AI_PLAYER)[0]
aiFourInRow = findFours(board)
return board, aiFourInRow
def aiWins(board):
printBoard(board)
print(' '+RED+"AI WINS !!!!\n" +'\033[1;37;40m')
playagain = True if input(YELLOW+'DO YOU WANT TO PLAY AGAIN(y/n)?'+WHITE).lower() == 'y' else False
#saveBoard(board)
if playagain:
mainFucntion()
return 0
def getDepth():
depth = input(YELLOW + 'ENTER DIFFICULTY(1-5): ' + WHITE)
if not(depth.isdigit()):
print(MAGENTA + 'Input must be integer!' + WHITE)
return getDepth()
depth = int(depth,10)
if depth < 1 or depth > 5:
print(MAGENTA + "Difficulty must be between 1 and 5!" + WHITE)
return getDepth()
return depth
def mainFucntion():
#board = initializeBoard()
os.system('cls' if os.name == 'nt' else 'clear')
board, loadFlag = loadBoard()
if board == None:
board = initializeBoard()
printBoard(board)
depth = getDepth()
whileCondition = 1
if loadFlag == True:
whomStart = True
else:
whomStart = True if input(YELLOW + 'DO YOU WANT TO START(y/n)? ' + WHITE).lower() == 'y' else False
if board == None:
board = initializeBoard()
while(whileCondition):
if isBoardFilled(board) :
print("GAME OVER\n")
break
if whomStart:
board, playerFourInRow = playerTurn(board)
if playerFourInRow:
whileCondition = playerWins(board)
if whileCondition ==0:
break
#AI
board, aiFourInRow = aiTurn(board,depth)
if aiFourInRow:
whileCondition = aiWins(board)
if whileCondition ==0:
break
printBoard(board)
if saveBoard(board):
break
else:
#AI
board, aiFourInRow = aiTurn(board,depth)
if aiFourInRow:
whileCondition = aiWins(board)
if whileCondition ==0:
break
printBoard(board)
if saveBoard(board):
break
#Human
board, playerFourInRow = playerTurn(board)
if playerFourInRow:
whileCondition = playerWins(board)
if whileCondition ==0:
break
printBoard(board)
mainFucntion()