-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectfour.py
More file actions
97 lines (74 loc) · 2.11 KB
/
connectfour.py
File metadata and controls
97 lines (74 loc) · 2.11 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
import sys
board = None
RED_CHIP = "R"
YELLOW_CHIP = "Y"
def setup_board():
global board
board = [[0 for i in range(7)] for j in range(6)]
def print_board():
global board
for row in board:
print(row)
def game_loop():
while True:
red_column = input("Enter Column(0-6) for RED move: ")
process_move(int(red_column), RED_CHIP)
yellow_column = input("Enter Column(0-6) for Yellow move: ")
process_move(int(yellow_column), YELLOW_CHIP)
def process_move(column, player_colour):
#check valid list index
#check column for existing chips
#add chip
#Check column num is within valid range - to avoid use of -1
if(column>=0 and column<len(board[0])):
try:
for i in range(len(board)):
#check if top row of column is full
if (i==0 and board[i][column] != 0):
print("Column is full")
break
#check bottom row
elif (i==len(board)-1 and board[i][column]==0):
board[i][column] = player_colour
check_gamewon(i, column, player_colour)
break
#check rows between top (inclusive) and bottom
elif board[i][column]!=0:
board[i-1][column] = player_colour
check_gamewon(i-1, column, player_colour)
break
print_board()
except IndexError:
print("Invalid move")
print_board()
return
else:
print("Invalid move: invalid column number, please enter a number between 0 and 6")
def check_gamewon(row_num, column_num, player_colour):
won = False
if (check_horizontal(row_num, player_colour)):
won = True
elif (check_vertical(column_num, player_colour)):
won = True
# check diagnol
if(won):
print_board()
print("WIN WIN WIN WIN: congrats %s player!" % player_colour)
sys.exit(0)
else:
print("Next move...")
def check_vertical(column_num, player_colour):
winning_str = player_colour*4
column_str = ''
for i in range(len(board)):
column_str += str(board[i][column_num])
return winning_str in column_str
def check_horizontal(row_num, player_colour):
winning_str = player_colour*4
return winning_str in ''.join(str(x) for x in board[row_num])
def check_diagonal():
pass
if __name__ == '__main__':
setup_board()
print_board()
game_loop()