Skip to content

Commit 2b48175

Browse files
authored
Brady| Connect Four| Added connect four (#243)
Add Connect Four game
1 parent a74d3fe commit 2b48175

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

Connect Four/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Connect Four Game
2+
A Python console implementation of the classic game Connect Four.
3+
4+
Execute the following command in terminal/shell to play:
5+
python "./Connect Four/main.py"

Connect Four/main.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
board = [[' ', ' ', ' ', ' ', ' ', ' ', ' '],
2+
[' ', ' ', ' ', ' ', ' ', ' ', ' '],
3+
[' ', ' ', ' ', ' ', ' ', ' ', ' '],
4+
[' ', ' ', ' ', ' ', ' ', ' ', ' '],
5+
[' ', ' ', ' ', ' ', ' ', ' ', ' '],
6+
[' ', ' ', ' ', ' ', ' ', ' ', ' ']]
7+
game = True
8+
player = 'X'
9+
choice = 0
10+
tie = False
11+
def printBoard():
12+
print('-' * 35)
13+
for i in range(6):
14+
for j in range(7):
15+
print("|", board[i][j], "|", end='')
16+
if (j % 7 == 6):
17+
print()
18+
print('-' * 35)
19+
while (game):
20+
printBoard()
21+
while (choice <= 0 or choice > 7):
22+
try:
23+
choice = int(input("Enter a column to put your chip (1-7) (Player " + player + "): "))
24+
except:
25+
print("Enter a valid number")
26+
if (board[0][choice - 1] != ' '):
27+
print("\nRow is full. Try again.\n")
28+
choice = 0
29+
continue
30+
for i in range(5, -1, -1):
31+
if (board[i][choice - 1] == ' '):
32+
board[i][choice - 1] = player
33+
break
34+
for i in range(6):
35+
for j in range(4):
36+
if (board[i][j] == player and board[i][j + 1] == player
37+
and board[i][j + 2] == player and board[i][j + 3] == player):
38+
game = False
39+
for i in range(3):
40+
for j in range(7):
41+
if (board[i][j] == player and board[i + 1][j] == player
42+
and board[i + 2][j] == player and board[i + 3][j] == player):
43+
game = False
44+
for i in range(5, 2, -1):
45+
for j in range(4):
46+
if (board[i][j] == player and board[i - 1][j + 1] == player
47+
and board[i - 2][j + 2] == player and board[i - 3][j + 3] == player):
48+
game = False
49+
for i in range(3):
50+
for j in range(4):
51+
if (board[i][j] == player and board[i + 1][j + 1] == player
52+
and board[i + 2][j + 2] == player and board[i + 3][j + 3] == player):
53+
game = False
54+
blankCount = 0
55+
for i in range(6):
56+
for j in range(7):
57+
if(board[i][j] == ' '): blankCount+=1
58+
if(blankCount == 0):
59+
game = False;
60+
tie = True;
61+
if (game == False): break
62+
if (player == 'X'): player = 'O'
63+
else: player = "X"
64+
choice = 0
65+
printBoard()
66+
if(tie == False):
67+
print("Player " + player + " Wins.")
68+
else:
69+
print("Tie game.")

0 commit comments

Comments
 (0)