-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathConnect4FINAL.py
More file actions
133 lines (119 loc) Β· 4.23 KB
/
Connect4FINAL.py
File metadata and controls
133 lines (119 loc) Β· 4.23 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
import random
print("Welcome to Connect Four")
print("-----------------------")
possibleLetters = ["A","B","C","D","E","F","G"]
gameBoard = [["","","","","","",""], ["","","","","","",""], ["","","","","","",""], ["","","","","","",""], ["","","","","","",""], ["","","","","","",""]]
rows = 6
cols = 7
def printGameBoard():
print("\n A B C D E F G ", end="")
for x in range(rows):
print("\n +----+----+----+----+----+----+----+")
print(x, " |", end="")
for y in range(cols):
if(gameBoard[x][y] == "π΅"):
print("",gameBoard[x][y], end=" |")
elif(gameBoard[x][y] == "π΄"):
print("", gameBoard[x][y], end=" |")
else:
print(" ", gameBoard[x][y], end=" |")
print("\n +----+----+----+----+----+----+----+")
def modifyArray(spacePicked, turn):
gameBoard[spacePicked[0]][spacePicked[1]] = turn
def checkForWinner(chip):
### Check horizontal spaces
for y in range(rows):
for x in range(cols - 3):
if gameBoard[x][y] == chip and gameBoard[x+1][y] == chip and gameBoard[x+2][y] == chip and gameBoard[x+3][y] == chip:
print("\nGame over", chip, "wins! Thank you for playing :)")
return True
### Check vertical spaces
for x in range(rows):
for y in range(cols - 3):
if gameBoard[x][y] == chip and gameBoard[x][y+1] == chip and gameBoard[x][y+2] == chip and gameBoard[x][y+3] == chip:
print("\nGame over", chip, "wins! Thank you for playing :)")
return True
### Check upper right to bottom left diagonal spaces
for x in range(rows - 3):
for y in range(3, cols):
if gameBoard[x][y] == chip and gameBoard[x+1][y-1] == chip and gameBoard[x+2][y-2] == chip and gameBoard[x+3][y-3] == chip:
print("\nGame over", chip, "wins! Thank you for playing :)")
return True
### Check upper left to bottom right diagonal spaces
for x in range(rows - 3):
for y in range(cols - 3):
if gameBoard[x][y] == chip and gameBoard[x+1][y+1] == chip and gameBoard[x+2][y+2] == chip and gameBoard[x+3][y+3] == chip:
print("\nGame over", chip, "wins! Thank you for playing :)")
return True
return False
def coordinateParser(inputString):
coordinate = [None] * 2
if(inputString[0] == "A"):
coordinate[1] = 0
elif(inputString[0] == "B"):
coordinate[1] = 1
elif(inputString[0] == "C"):
coordinate[1] = 2
elif(inputString[0] == "D"):
coordinate[1] = 3
elif(inputString[0] == "E"):
coordinate[1] = 4
elif(inputString[0] == "F"):
coordinate[1] = 5
elif(inputString[0] == "G"):
coordinate[1] = 6
else:
print("Invalid")
coordinate[0] = int(inputString[1])
return coordinate
def isSpaceAvailable(intendedCoordinate):
if(gameBoard[intendedCoordinate[0]][intendedCoordinate[1]] == 'π΄'):
return False
elif(gameBoard[intendedCoordinate[0]][intendedCoordinate[1]] == 'π΅'):
return False
else:
return True
def gravityChecker(intendedCoordinate):
### Calculate space below
spaceBelow = [None] * 2
spaceBelow[0] = intendedCoordinate[0] + 1
spaceBelow[1] = intendedCoordinate[1]
### Is the coordinate at ground level
if(spaceBelow[0] == 6):
return True
### Check if there's a token below
if(isSpaceAvailable(spaceBelow) == False):
return True
return False
leaveLoop = False
turnCounter = 0
while(leaveLoop == False):
if(turnCounter % 2 == 0):
printGameBoard()
while True:
spacePicked = input("\nChoose a space: ")
coordinate = coordinateParser(spacePicked)
try:
### Check if the space is available
if(isSpaceAvailable(coordinate) and gravityChecker(coordinate)):
modifyArray(coordinate, 'π΅')
break
else:
print("Not a valid coordinate")
except:
print("Error occured. Please try again.")
winner = checkForWinner('π΅')
turnCounter += 1
### It's the computers turn
else:
while True:
cpuChoice = [random.choice(possibleLetters), random.randint(0,5)]
cpuCoordinate = coordinateParser(cpuChoice)
if(isSpaceAvailable(cpuCoordinate) and gravityChecker(cpuCoordinate)):
modifyArray(cpuCoordinate, 'π΄')
break
turnCounter += 1
winner = checkForWinner('π΄')
if(winner):
printGameBoard()
break