Skip to content

Latest commit

 

History

History

Connect Four

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Connect Four

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.

board = [["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6, ["-"] * 6]

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 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 in the table below:

Note: The coordinate is [col, row]

Positive Slope Possible Starting Points

1.1 2.1 3.1 4.1 5.1 6.1 7.1
1.2 2.2 3.2 4.2 5.2 6.2 7.2
1.3 2.3 3.3 4.3 5.3 6.3 7.3
1.4 2.4 3.4 4.4 5.4 6.4 7.4
1.5 2.5 3.5 4.5 5.5 6.5 7.5
1.6 2.6 3.6 4.6 5.6 6.6 7.6

Negative Slope Possible Starting Points

1.1 2.1 3.1 4.1 5.1 6.1 7.1
1.2 2.2 3.2 4.2 5.2 6.2 7.2
1.3 2.3 3.3 4.3 5.3 6.3 7.3
1.4 2.4 3.4 4.4 5.4 6.4 7.4
1.5 2.5 3.5 4.5 5.5 6.5 7.5
1.6 2.6 3.6 4.6 5.6 6.6 7.6

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 function and nested for loop that iterates through only the possible starting points.