Welcome to the start of our Connect Four project! We'll lay the foundation for our game, setting up the basic structure, initializing the board, and beginning the core gameplay loop. Let's dive in and bring our game to life, step by step.
The hardest part of any project is getting started. Luckily we have spen some time already breaking down the problem and thinking through the specifications. From here we will attempt to follow our structure chart, making changes as necessary.
Main Method
Let's start by writing a 'main' method.
Now, if we look at our structure chart we can begin to define and call some of the functions we have planned for.
def main():
board = initialiseBoard()
displayBoard(board)
This is the equivalent mainline.
Initialise Board
def initialiseBoard():
board = []
for _ in range(6):
row = []
for _ in range(7):
row.append('-')
board.append(row)
return board
The initialiseBoard function is designed to set up the initial state of the Connect Four game board, which consists of a grid with 6 rows and 7 columns. Here's the thought process broken down:
We start by creating an empty list named board. This list will eventually contain all the rows of our game board.
The first for loop iterates 6 times, corresponding to the 6 rows in the game. Each iteration represents the creation of a new row.
Inside this loop, we create another empty list named row for each individual row.
The second for loop inside the first one iterates 7 times, reflecting the 7 columns in each row. In each iteration, we append a hyphen ('-') to the row list. The hyphen is used as a placeholder to signify that a particular spot in the game board is empty and available for a player's move.
After filling the row list with 7 hyphens, we append this row to the board. This process is repeated 6 times, resulting in a 6x7 grid filled with hyphens.
Finally, the function returns the board, which is now a list of lists representing our game board, ready for the players to make their moves.
Display Board
def displayBoard(board):
for row in board:
for cell in row:
print(cell, end=' ')
print()
print()
The displayBoard function is designed to visually represent the Connect Four board in the console.
It starts with a for loop that goes through each row in the board list.
Within this loop, there's another for loop that iterates over each cell in the current row.
The print(cell, end=' ') command prints each cell followed by a space but doesn't immediately move to a new line. This way, all cells in a row are printed on the same line, separated by spaces, which visually represents the row of the Connect Four board.
After all cells in a row are printed, print() is called without arguments, which simply moves the output to the next line, ready for the next row to be printed.
After all rows are printed, print() is called again to add an extra empty line for spacing, making the board easier to view and distinguish from any subsequent output or user input prompts.
Putting It Together
We can see the program below successfully initialises the board and displays it to the user.
Open this assignment on GitHub Classroom: Project 7 - Tic Tac Toe
Clone the repository to your computer using GitHub Desktop, or open it online using Codespaces
Open lab4.py
Break off into pairs and attempt to code the following:
A variable to track who is the current player
A function for the player to input which column they'd like to place their token
A function to check if this was a valid move
Think about ways in which you could test these functions
We will rejoin as a class to compare and discuss
def main():
board = initialiseBoard()
currentPlayer = 1
while True:
displayBoard(board)
column = playerInput(currentPlayer)
if isValidMove(board, column):
# make move
# switch player
To create the game loop we create a while loop and move much of the logic inside.
When this program is run, the current player should be able to keep inputting moves, over and over. The empty board will also show each time as we have not actually implemented a move function.
Many of the functions we write in the next lab will exist inside of this loop.
Go back to your program and try to implement the game loop