Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d77563f

Browse files
Merge pull request #403 from ArjaynP/ConnectFour
Connect Four
2 parents b18953f + f3a607f commit d77563f

File tree

3 files changed

+282
-2
lines changed

3 files changed

+282
-2
lines changed

‎Connect Four/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Connect Four
2+
I have created the board game, "Connect Four", using the programming language Python. The entire project was created all on one file. This was a project I chose to develop during my spare time as this was not a common project constructed by other developers. The program follows all rules of the game, where players can win by either having 4 in a row horizontally, vertically, or diagonally. The dimensions for this particular game is a 6x7 board. In other words, the board is designed as 6 rows and 7 columns. Therefore, I initialized a nested list called board that contains the 7 columns as elements, and the 6 rows within each element.
3+
4+
```
5+
board = [["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6]
6+
```
7+
8+
9+
Determining the logic for a player to have 4 in a row diagonally proved to be the most challenging in my opinion. The best solution was to view this as positive and negative slopes since these diagonals were
10+
either from the bottom left to top right, or the top left to bottom right. Afterwards, I created two separate boards to determine all possible coordinates that can be starting points to create 4 in a row as shown
11+
in the table below:
12+
13+
Note: The coordinate is [col, row]
14+
15+
### Positive Slope Possible Starting Points
16+
| | | | | | | |
17+
|------------|------------|------------|------------|------------|------------|------------|
18+
| <s>1.1</s> | <s>2.1</s> | <s>3.1</s> | <s>4.1</s> | <s>5.1</s> | <s>6.1</s> | <s>7.1</s> |
19+
| <s>1.2</s> | <s>2.2</s> | <s>3.2</s> | <s>4.2</s> | <s>5.2</s> | <s>6.2</s> | <s>7.2</s> |
20+
| <s>1.3</s> | <s>2.3</s> | <s>3.3</s> | <s>4.3</s> | <s>5.3</s> | <s>6.3</s> | <s>7.3</s> |
21+
| 1.4 | 2.4 | 3.4 | 4.4 | <s>5.4</s> | <s>6.4</s> | <s>7.4</s> |
22+
| 1.5 | 2.5 | 3.5 | 4.5 | <s>5.5</s> | <s>6.5</s> | <s>7.5</s> |
23+
| 1.6 | 2.6 | 3.6 | 4.6 | <s>5.6</s> | <s>6.6</s> | <s>7.6</s> |
24+
25+
26+
### Negative Slope Possible Starting Points
27+
| | | | | | | |
28+
|------------|------------|------------|------------|------------|------------|------------|
29+
| 1.1 | 2.1 | 3.1 | 4.1 | <s>5.1</s> | <s>6.1</s> | <s>7.1</s> |
30+
| 1.2 | 2.2 | 3.2 | 4.2 | <s>5.2</s> | <s>6.2</s> | <s>7.2</s> |
31+
| 1.3 | 2.3 | 3.3 | 4.3 | <s>5.3</s> | <s>6.3</s> | <s>7.3</s> |
32+
| <s>1.4</s> | <s>2.4</s> | <s>3.4</s> | <s>4.4</s> | <s>5.4</s> | <s>6.4</s> | <s>7.4</s> |
33+
| <s>1.5</s> | <s>2.5</s> | <s>3.5</s> | <s>4.5</s> | <s>5.5</s> | <s>6.5</s> | <s>7.5</s> |
34+
| <s>1.6</s> | <s>2.6</s> | <s>3.6</s> | <s>4.6</s> | <s>5.6</s> | <s>6.6</s> | <s>7.6</s> |
35+
36+
What I noticed is the positive slope starting points are from rows 4-6 and columns 1-4, while the negatiive slope starting points are from rows 1-3 and columns 1-4. Therefore, each type of slope has its own
37+
function and nested for loop that iterates through only the possible starting points.

‎Connect Four/connectfour.py‎

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
import random
2+
3+
board = [["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6]
4+
5+
6+
def gamePlayTwoPlayers(p1, p2):
7+
win = ""
8+
while win == "":
9+
column = int(input("{}'s turn. Enter which column you would like to drop your piece into: ".format(p1)))
10+
connectFourBoard(column, "X")
11+
12+
if check_vertical_win(p1, p2, column):
13+
print()
14+
print(check_vertical_win(p1, p2, column))
15+
break
16+
if check_horizontal_win(p1, p2):
17+
print()
18+
print(check_horizontal_win(p1, p2))
19+
break
20+
if check_positive_diagonal_win(p1, p2):
21+
print()
22+
print(check_positive_diagonal_win(p1, p2))
23+
break
24+
if check_negative_diagonal_win(p1, p2):
25+
print()
26+
print(check_negative_diagonal_win(p1, p2))
27+
break
28+
29+
column = int(input("{}'s turn. Enter which column you would like to drop your piece into: ".format(p2)))
30+
connectFourBoard(column, "O")
31+
32+
if check_vertical_win(p1, p2, column):
33+
print()
34+
print(check_vertical_win(p1, p2, column))
35+
break
36+
if check_horizontal_win(p1, p2):
37+
print()
38+
print(check_horizontal_win("x", "O"))
39+
break
40+
if check_positive_diagonal_win(p1, p2):
41+
print()
42+
print(check_positive_diagonal_win(p1, p2))
43+
break
44+
if check_negative_diagonal_win(p1, p2):
45+
print()
46+
print(check_negative_diagonal_win(p1, p2))
47+
break
48+
49+
50+
def gamePlayOnePlayer(p1, p2):
51+
win = ""
52+
while win == "":
53+
column = int(input("{}'s turn. Enter which column you would like to drop your piece into: ".format(p1)))
54+
connectFourBoard(column, "X")
55+
56+
if check_vertical_win(p1, p2, column):
57+
print()
58+
print(check_vertical_win(p1, p2, column))
59+
break
60+
if check_horizontal_win(p1, p2):
61+
print()
62+
print(check_horizontal_win(p1, p2))
63+
break
64+
if check_positive_diagonal_win(p1, p2):
65+
print()
66+
print(check_positive_diagonal_win(p1, p2))
67+
break
68+
if check_negative_diagonal_win(p1, p2):
69+
print()
70+
print(check_negative_diagonal_win(p1, p2))
71+
break
72+
73+
print()
74+
75+
column = random.randint(1, 7)
76+
connectFourBoard(column, "O")
77+
78+
if check_vertical_win(p1, p2, column):
79+
print()
80+
print(check_vertical_win(p1, p2, column))
81+
break
82+
if check_horizontal_win(p1, p2):
83+
print()
84+
print(check_horizontal_win(p1, p2))
85+
break
86+
if check_positive_diagonal_win(p1, p2):
87+
print()
88+
print(check_positive_diagonal_win(p1, p2))
89+
break
90+
if check_negative_diagonal_win(p1, p2):
91+
print()
92+
print(check_negative_diagonal_win(p1, p2))
93+
break
94+
95+
96+
def connectFourBoard(col, playerIcon):
97+
col -= 1
98+
coordinate = []
99+
100+
for row in range(len(board[col])-1, -1, -1):
101+
if board[col][row] == "-":
102+
board[col][row] = playerIcon
103+
coordinate.append([row, col])
104+
break
105+
for row in range(len(board)):
106+
for col in range(len(board[row])):
107+
print("|", board[row][col], "|", board[row+1][col], "|", board[row+2][col], "|", board[row+3][col], "|",
108+
board[row+4][col], "|", board[row+5][col], "|", board[row+6][col], "|")
109+
break
110+
111+
112+
def check_vertical_win(p1, p2, col):
113+
playerCount1 = 0
114+
playerCount2 = 0
115+
col -= 1
116+
117+
for row in range(len(board[col])-1, -1, -1):
118+
if board[col][row] == "X":
119+
playerCount1 += 1
120+
playerCount2 = 0
121+
elif board[col][row] == "O":
122+
playerCount2 += 1
123+
playerCount1 = 0
124+
125+
if playerCount1 == 4:
126+
return "{} Wins".format(p1)
127+
elif playerCount2 == 4:
128+
return "{} Wins".format(p2)
129+
130+
131+
def check_horizontal_win(p1, p2):
132+
for row in range(len(board[0])-1, -1, -1):
133+
playerCount1 = 0
134+
playerCount2 = 0
135+
for col in range(len(board)):
136+
if board[col][row] == "X":
137+
playerCount1 += 1
138+
playerCount2 = 0
139+
elif board[col][row] == "O":
140+
playerCount2 += 1
141+
playerCount1 = 0
142+
elif board[col][row] == "-":
143+
playerCount1 = 0
144+
playerCount2 = 0
145+
146+
if playerCount1 == 4:
147+
return "{} Wins".format(p1)
148+
elif playerCount2 == 4:
149+
return "{} Wins".format(p2)
150+
151+
152+
def check_positive_diagonal_win(p1, p2):
153+
playerCount1 = 0
154+
playerCount2 = 0
155+
156+
for row in range(len(board[0])-1, -1, -1):
157+
for col in range(len(board)-3):
158+
if board[col][row] == "X":
159+
playerCount1 += 1
160+
while playerCount1 < 4:
161+
col += 1
162+
row -= 1
163+
if board[col][row] == "X":
164+
playerCount1 += 1
165+
else:
166+
playerCount1 = 0
167+
break
168+
elif board[col][row] == "O":
169+
playerCount1 += 1
170+
while playerCount1 < 4:
171+
col += 1
172+
row -= 1
173+
if board[col][row] == "O":
174+
playerCount1 += 1
175+
else:
176+
playerCount1 = 0
177+
break
178+
179+
if playerCount1 == 4:
180+
return "{} Wins".format(p1)
181+
elif playerCount2 == 4:
182+
return "{} Wins".format(p2)
183+
else:
184+
playerCount1 = 0
185+
playerCount2 = 0
186+
187+
188+
def check_negative_diagonal_win(p1, p2):
189+
playerCount1 = 0
190+
playerCount2 = 0
191+
192+
for row in range(len(board[0])-3):
193+
for col in range(len(board)-3):
194+
if board[col][row] == "X":
195+
playerCount1 += 1
196+
while playerCount1 < 4:
197+
col += 1
198+
row -= 1
199+
if board[col][row] == "X":
200+
playerCount1 += 1
201+
else:
202+
playerCount1 = 0
203+
break
204+
elif board[col][row] == "O":
205+
playerCount1 += 1
206+
while playerCount1 < 4:
207+
col += 1
208+
row += 1
209+
if board[col][row] == "O":
210+
playerCount1 += 1
211+
else:
212+
playerCount1 = 0
213+
break
214+
215+
if playerCount1 == 4:
216+
return "{} Wins".format(p1)
217+
elif playerCount2 == 4:
218+
return "{} Wins".format(p2)
219+
else:
220+
playerCount1 = 0
221+
playerCount2 = 0
222+
223+
224+
def main():
225+
print("Welcome to Connect Four! Connect 4 of your pieces in either horizontal, vertical, or diagonal to win.")
226+
227+
numPlayers = int(input("Choose how many players: "))
228+
229+
while numPlayers not in [1,2]:
230+
numPlayers = int(input("Sorry for the value you entered was invalid. Are you playing with 1 or 2 players: "))
231+
232+
if numPlayers == 1:
233+
player1 = input("Player 1, please enter your name: ")
234+
player2 = "CPU"
235+
gamePlayTwoPlayers(player1, player2)
236+
elif numPlayers == 2:
237+
player1 = input("Player 1, please enter your name: ")
238+
player2 = input("Player 2, please enter your name: ")
239+
gamePlayTwoPlayers(player1, player2)
240+
241+
242+
main()

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ More information on contributing and the general code of conduct for discussion
4949
| Blackjack | [Blackjack](https://github.com/DhanushNehru/Python-Scripts/tree/master/Blackjack) | A game of Blackjack - let's get a 21. |
5050
| Chessboard | [Chessboard](https://github.com/DhanushNehru/Python-Scripts/tree/master/Chess%20Board) | Creates a chessboard using matplotlib. |
5151
| Compound Interest Calculator | [Compound Interest Calculator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Calculate%20Compound%20Interest) | A Python script to calculate compound interest. |
52-
| Countdown Timer | [Countdown Timer](https://github.com/DhanushNehru/Python-Scripts/tree/master/Countdown%20Timer) | Displays a message when the Input time elapses. |
5352
| Convert Temperature | [Convert Temperature](https://github.com/DhanushNehru/Python-Scripts/tree/master/Convert%20Temperature) | A python script to convert temperature between Fahreheit, Celsius and Kelvin |
53+
| Connect Four | [Connect Four](https://github.com/DhanushNehru/Python-Scripts/tree/master/Connect%20Four) | A Python script for the board game Connect Four, which can be played by 1-2 players |
54+
| Countdown Timer | [Countdown Timer](https://github.com/DhanushNehru/Python-Scripts/tree/master/Countdown%20Timer) | Displays a message when the Input time elapses. |
5455
| Crop Images | [Crop Images](https://github.com/DhanushNehru/Python-Scripts/tree/master/Crop%20Images) | A Python script to crop a given image. |
5556
| CSV to Excel | [CSV to Excel](https://github.com/DhanushNehru/Python-Scripts/tree/master/CSV%20to%20Excel) | A Python script to convert a CSV to an Excel file. |
5657
| Currency Script | [Currency Script](https://github.com/DhanushNehru/Python-Scripts/tree/master/Currency%20Script) | A Python script to convert the currency of one country to that of another. |
@@ -110,7 +111,7 @@ More information on contributing and the general code of conduct for discussion
110111
| PDF to Audio | [PDF to Audio](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20Audio) | Converts PDF to audio. |
111112
| PDF to Text | [PDF to text](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20text) | Converts PDF to text. |
112113
| PDF merger and splitter | [PDF Merger and Splitter](https://github.com/AbhijitMotekar99/Python-Scripts/blob/master/PDF%20Merger%20and%20Splitter/PDF%20Merger%20and%20Splitter.py) | Create a tool that can merge multiple PDF files into one or split a single PDF into separate pages. |
113-
| Pizza Order | [Pizza Order](https://github.com/DhanushNehru/Python-Scripts/tree/master/Pizza%20Order) | An algorithm designed to handle pizza orders from customers with accurate receipts and calculations. |
114+
| Pizza Order | [Pizza Order](https://github.com/DhanushNehru/Python-Scripts/tree/master/Pizza%20Order) | An algorithm designed to handle pizza orders from customers with accurate receipts and calculations. |
114115
| Planet Simulation | [Planet Simulation](https://github.com/DhanushNehru/Python-Scripts/tree/master/Planet%20Simulation) | A simulation of several planets rotating around the sun. |
115116
| Playlist Exchange | [Playlist Exchange](https://github.com/DhanushNehru/Python-Scripts/tree/master/Playlist%20Exchange) | A Python script to exchange songs and playlists between Spotify and Python. |
116117
| Pigeonhole Sort | [Algorithm](https://github.com/DhanushNehru/Python-Scripts/tree/master/PigeonHole) | The pigeonhole sort algorithm to sort your arrays efficiently! |

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /