-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayConnectFourTwoPlayer.py
114 lines (104 loc) · 2.78 KB
/
playConnectFourTwoPlayer.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
def printBoard(board):
for row in range(6):
output = "| "
for col in range(7):
pos = row * 7 + col
piece = board[pos]
if piece < 0:
output += "O "
elif piece > 0:
output += "X "
else:
output += " "
output += "| "
print(output)
print("-"*29)
print("")
return
def newMove(board,whosTurn):
if whosTurn == 1:
piece = "X"
else:
piece = "O"
while True:
column = input(f"Player {piece}, please choose a column (0-6): ")
try:
column = int(column)
except:
column = 99
if column >= 0 and column <= 6:
if board[column] != 0:
column = 99
if column >= 0 and column <= 6:
break
print(f'you selected a VALID COLUMN: {column}')
row = 0
while row < 5 and board[row*7 + 7 + column] == 0:
row += 1
pos = row * 7 + column
return pos
def playGame():
whosTurn = 1
board = [0] * 42
turnCount = 0
while True:
pos = newMove(board,whosTurn)
board[pos] = whosTurn
printBoard(board)
if didLastMoveWinGame(board,pos,whosTurn):
print("\nGame over, Brother!")
return whosTurn
whosTurn *= -1
turnCount += 1
if turnCount == 42:
print("Board is full")
return 0
def didLastMoveWinGame(board,pos,whosTurn):
# check vertical win
count = 0
checkPos = pos
while checkPos < 42:
if board[checkPos] == whosTurn:
count += 1
if count == 4:
print("\nVertical Win")
return True
checkPos += 7
else:
break
# check horizontal win
row = pos // 7
for rowStart in range(row*7,row*7+4):
if sum(board[rowStart:rowStart+4]) == 4 * whosTurn:
print("\nHorizontal Win")
return True
# check diagonal up-slope win
diagStart = pos
while diagStart % 7 > 0 and diagStart // 7 < 5:
diagStart += 6
count = 0
while diagStart >= 0:
if board[diagStart] == whosTurn:
count += 1
if count == 4:
print("\nDiagonal-Up Win")
return True
else:
count = 0
diagStart -= 6
# check diagonal down-slope win
diagStart = pos
while diagStart % 7 > 0 and diagStart // 7 > 0:
diagStart -= 8
count = 0
while diagStart < 42:
if board[diagStart] == whosTurn:
count += 1
if count == 4:
print("\nDiagonal-Down Win")
return True
else:
count = 0
diagStart += 8
return False
playGame()