Repository navigation
Expand file tree
/
Copy pathGame.py
More file actions
135 lines (131 loc) · 3.85 KB
/
Copy pathGame.py
File metadata and controls
135 lines (131 loc) · 3.85 KB
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
import AIClass
import sillyQ
def dropMarker(board, x):
temp = highestY(board,x)
board[x][temp] = "o"
return temp
def highestY(board, x):
i = 5
while(board[x][i] != "_" and i >= 0):
i -= 1
return i
def checkWin(board):
checkResult = None
for column in range (6):
for row in range(5):
if board[column][row] != '_':
checkResult = pieceCheck(board,board[column][row], 'up',column,row)
if checkResult != None:
return checkResult
checkResult = pieceCheck(board,board[column][row],'upRight',column,row)
if checkResult != None:
return checkResult
#right doesn't seem to be checking properly, could be left#
checkResult = pieceCheck(board,board[column][row],'right',column,row)
if checkResult != None:
return checkResult
checkResult = pieceCheck(board,board[column][row],'downRight',column,row)
if checkResult != None:
return checkResult
checkResult = pieceCheck(board,board[column][row],'down',column,row)
if checkResult != None:
return checkResult
checkResult = pieceCheck(board,board[column][row],'downLeft',column,row)
if checkResult != None:
return checkResult
checkResult = pieceCheck(board,board[column][row],'left',column,row)
if checkResult != None:
return checkResult
checkResult = pieceCheck(board,board[column][row],'upLeft',column,row)
if checkResult != None:
return checkResult
return checkResult
def pieceCheck(board, piece, direction,x,y):
xMod = 0
yMod = 0
distance = 1
checked = False
winnerFound = False
winner = None
if direction == 'up':
yMod = -1
elif direction == 'upRight':
yMod = -1
xMod = 1
elif direction == 'right':
xMod = 1
elif direction == 'downRight':
yMod = 1
xMod = 1
elif direction == 'down':
yMod = 1
elif direction == 'downLeft':
yMod = 1
xMod = -1
elif direction == 'left':
xMod = -1
elif direction == 'upLeft':
yMod = -1
xMod = -1
while not checked:
if y+(yMod*distance) < 0 or x+(xMod*distance) < 0 or x+(xMod*distance) > 6 or y+(yMod*distance) > 5:
checked = True
elif board[x+(xMod*distance)][y+(yMod*distance)] != piece:
checked = True
elif board[x+(xMod*distance)][y+(yMod*distance)] == piece:
distance+=1
if distance == 4:
winnerFound = True
checked = True
if winnerFound == True:
if piece == 'x':
winner = 'computer'
elif piece == 'o':
winner = 'player'
return winner
ai = sillyQ.sampleAI("Bill","x")
b = [["_","_","_","_","_","_"],["_","_","_","_","_","_"],["_","_","_","_","_","_"],["_","_","_","_","_","_"],["_","_","_","_","_","_",],["_","_","_","_","_","_"],["_","_","_","_","_","_"]]
print "Play Connect 4"
won = False
ai.printBoard(b)
while not won:
print "Player, Please make a move"
validInput = False
while not validInput:
#Add in something that prevents placing in full columns
tempInputt = raw_input()
try:
inputt = int(tempInputt)
if (inputt <= 6):
validInput = True
if highestY(b, inputt) == -1:
print "please place in a column that isn't full."
elif (inputt > 6):
print "That column is not in the board, please enter a number between 0 and 6."
except:
print "Invalid input, please enter a number between 0 and 6."
else:
if len(tempInputt) != 1:
print "please enter only 1 digit."
dropMarker(b, inputt)
ai.printBoard(b)
candidate = checkWin(b)
if candidate != None:
won = True
if candidate == 'computer':
print ai.AIname + " has won! Better luck next time."
break
elif candidate == 'player':
print "Congratulations! you have beaten " + ai.AIname + " in Connect 4."
break
print "Computer, Make a move"
ai.makeMove(b)
ai.printBoard(b)
candidate = checkWin(b)
if candidate != None:
won = True
if candidate == 'computer':
print ai.AIname + " has won! Better luck next time."
elif candidate == 'player':
print "Congratulations! you have beaten " + ai.AIname + " in Connect 4."
print "done"