-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
123 lines (106 loc) · 3.55 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
# Defining board
board = [[' ', ' ', ' ', ' ', ' ', ' '],[' ', ' ', ' ', ' ', ' ', ' '],[' ', ' ', ' ', ' ', ' ', ' '],[' ', ' ', ' ', ' ', ' ', ' '],[' ', ' ', ' ', ' ', ' ', ' '],[' ', ' ', ' ', ' ', ' ', ' '],[' ', ' ', ' ', ' ', ' ', ' '],]
# Print board function
def printBoard():
columnHeader = ''
for i in range(7):
columnHeader += ' ' + str(i+1) + ' '
print(columnHeader)
print('+---' *7 + '+')
for row in range(len(board[0])):
items=''
for col in range(len(board)):
items += '| ' + board[col][row] + ' '
print(items + '|')
print('+---' *7 + '+')
print()
# Function to add a user defined piece
def addPiece(piece, column):
print("Trying to place an " + piece.upper() + ' in column ' + str(column))
error, msg = displayError(column)
if error:
print(msg)
print()
return False
for row in range(len(board[0]), 0, -1):
if board[column-1][row-1] == ' ':
board[column-1][row-1] = piece.upper()
break
else:
continue
print("Placed an " + piece + ' in column ' + str(column))
print()
printBoard()
def displayError(column):
msg=''
if column > 7 or column < 1 or board[column-1][0] != ' ':
msg = "Make sure to pick a column between 1 and 7 that is not full"
return True, msg
# elif piece.upper() not in 'XO':
# msg = "Make sure to use either 'X' or 'O' as your piece"
# return True, msg
else:
return False, 'no err'
def detectWin(piece):
# vertical
for row in range(len(board[0])-3):
for col in range(len(board)):
if board[col][row] == board[col][row+1] == board[col][row+2] == board[col][row+3] == piece:
return True
# horizontal
for row in range(len(board[0])):
for col in range(len(board)-3):
if board[col][row] == board[col+1][row] == board[col+2][row] == board[col+3][row] == piece:
return True
# diagonal \
for row in range(len(board[0])-3):
for col in range(len(board)-3):
if board[col][row] == board[col+1][row+1] == board[col+2][row+2] == board[col+3][row+3] == piece:
return True
# diagonal /
for row in range(len(board[0])):
for col in range(len(board)-3):
if board[col][row] == board[col+1][row-1] == board[col+2][row-2] == board[col+3][row-3] == piece:
return True
return False
def available_moves():
# Returns the columns that are open
moves = []
for i in range(1, len(board)+1):
error, msg = displayError(i)
if not error:
moves.append(i)
return moves
def detectGameOver(piece):
moves= available_moves()
win = detectWin(piece)
if win:
return True
elif len(moves) == 0:
return True
else:
return False
def play_game():
printBoard()
turn = 'X'
while(not detectGameOver(turn)):
col = 0
available = available_moves()
while (col not in available or col < 1 or col > 7):
col = int(input("It is " + turn + "'s turn. Please choose a column. "))
addPiece(turn, col)
if detectWin(turn):
print(turn + ' has won the game!')
print()
printBoard()
break
if turn == 'X':
turn = 'O'
elif turn == 'O':
turn = 'X'
if len(available) == 0:
print("It's a tie!")
print()
printBoard()
break
play_game()