-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectFour.py
More file actions
144 lines (117 loc) · 4 KB
/
connectFour.py
File metadata and controls
144 lines (117 loc) · 4 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
136
137
138
139
140
141
142
143
144
import os
import time
import random
import numpy
import re
from colorama import init
from colorama import Fore, Style
# Initializes a 6 x 7 matrix of zeroes
def initializeBoard():
return numpy.zeros((6,7))
def printScreen(board):
printGameTitle()
printBoard(board)
def printGameTitle():
os.system("clear")
print(" _____ _ ___")
print("/ __ \ | | / |")
print("| / \/ ___ _ __ _ __ ___ ___| |_ / /| |")
print("| | / _ \| '_ \| '_ \ / _ \/ __| __| / /_| |")
print("| \__/\ (_) | | | | | | | __/ (__| |_ \___ |")
print(" \____/\___/|_| |_|_| |_|\___|\___|\__| |_/")
print(" ")
# Prints the game board
def printBoard(board):
for row in board:
print(" -----------------------------")
print(" |", end="")
for element in row:
if element == 1:
print(Fore.RED + ' 0 ', end="")
print(Style.RESET_ALL, end="")
print('|', end="")
elif element == -1:
print(Fore.YELLOW + ' 0 ', end="")
print(Style.RESET_ALL, end="")
print('|', end="")
else:
print(" |", end="")
print ('')
print(" -----------------------------")
print(" | | | |")
print(" | | | |")
print(" | | | |")
print(" /___\\ /___\\")
print(" ")
# Randomly picks the first and second player
def chooseFirstPlayer():
return ("Player", "Computer") if random.randrange(2) == 1 else ("Computer", "Player")
# Player picks a spot to play
def playerTurn(board):
playerFlag = 1
while True:
column = input('What\'s your move? ')
if re.match('^[1-7]$', column): #Makes sure it's between 1 and 7 and only one digit
column = int(column)
if board[0][column - 1] == 1 or board[0][column - 1] == -1:
print("That column is full")
continue
insertPiece(board, column, playerFlag)
break
else:
print ('Not a column')
# Computer chooses best place to play
def computerTurn(board, turn):
computerFlag = -1
print ('Choosing move...')
time.sleep(0) #Set "thinking" time to 0 while we're testing
column = random.randrange(8)
while board[0][column - 1] == 1 or board[0][column - 1] == -1:
column = random.randrange(8)
insertPiece(board, column, computerFlag)
# Inserts a piece at bottom of specified column
def insertPiece(board, column, flag):
column -= 1
nextRowsIndex = 1 #Used to track the row under the current row
#
for row in board:
if nextRowsIndex< 6 and (board[nextRowsIndex][column] == 1 or board[nextRowsIndex][column] == -1):
row[column] = flag
break
elif nextRowsIndex == 6 and row[column] == 0: # Special case for when on the bottom row
row[column] = flag
break
nextRowsIndex += 1
# Checks if there is a tie
def gameIsTie(board):
printScreen(board)
print("Draw!")
# Main game function
def game():
init() #colorama required init function
board = initializeBoard()
turn = 1
# Pick first player and begin loop.
# The player who played first will always
# play first at the beginning of each round.
firstPlayer, secondPlayer = chooseFirstPlayer()
while True:
printScreen(board)
print(firstPlayer + "'s turn")
if firstPlayer == "Computer":
computerTurn(board, turn)
else:
playerTurn(board)
turn += 1
printScreen(board)
print(secondPlayer + "'s turn")
if firstPlayer == "Computer":
playerTurn(board)
else:
computerTurn(board, turn)
turn += 1
if turn == 43:
gameIsTie(board)
break
# Start of the game
game()