-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject1.py
More file actions
99 lines (83 loc) · 2.62 KB
/
Project1.py
File metadata and controls
99 lines (83 loc) · 2.62 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
import time
winner = False
height = 6
width = 7
wturn = 2
last = 0
column = 0
def gboard( height, width):
for i in range(8):
if i < height:
board.append(["_"] * width)
elif i < width:
board.append(["^"] * width)
else:
for h in range(width):
xaxis.append(str(h+1))
board.append(xaxis)
def playground(board):
print ("\n")
for row in board:
print(" ".join(row))
print ("\n")
def main():
print ("Welcome to Connect Four!")
def start():
while True:
column = int(input("Pick a column (1-7): ")) -1
row = int(input("Pick a row (1-6):")) -1
if column >= 0 and column <= width:
if row >=0 and row <= height:
for i in range(7):
if board[row][column] == ("_"):
last = row
score(last, column)
else:
print("Not a valid number! Please try again...")
break
def score(last, column):
if wturn == 2:
board[last][column] = ("X")
else:
board[last][column] = ("O")
playground(board)
def player(turn):
if turn == 2 :
turn= 1
print ("It is player %s's turn." % turn)
return turn
elif turn == 1:
turn= 2
print ("It is player %s's turn." % turn)
return turn
def check_winner(board, player):
#check horizontal spaces
for y in range(height):
for x in range(width - 3):
if board[x][y] == player and board[x+1][y] == player and board[x+2][y] == player and board[x+3][y] == player:
return True
#check vertical spaces
for x in range(width):
for y in range(height - 3):
if board[x][y] == player and board[x][y+1] == player and board[x][y+2] == player and board[x][y+3] == player:
return True
#check / diagonal spaces
for x in range(width - 3):
for y in range(3, height):
if board[x][y] == player and board[x+1][y-1] == player and board[x+2][y-2] == player and board[x+3][y-3] == player:
return True
#check \ diagonal spaces
for x in range(width - 3):
for y in range(height - 3):
if board[x][y] == player and board[x+1][y+1] == player and board[x+2][y+2] == player and board[x+3][y+3] == player:
return True
return False
xaxis=[]
board=[]
main()
gboard(height,width)
playground(board)
while winner == False:
wturn = player(wturn)
start()
winner = check_winner(board, str(wturn))