Question: The objective of the project is to write in Python a working Connect4 (pop out version) program. The program must allow the user to congure:
The objective of the project is to write in Python a working Connect4 (pop out version) program. The program must allow the user to congure: the size of the board (i.e. the number of rows r and the number of columns c of the board, the default values must be r = 6 and c = 7). You can set minimum and maximum values if you want. the number N of consecutive same-color discs required for a player to win (by default the value must be N = 4). Again, you can set minimum and maximum values if you want. the type of the two players (human or computer), and the diculty level in case of a computer player (two levels). You have to implement this Connect4 game following the three steps given below. Moreover, note that everytime you add a feature to your program, you should test it thoroughly before continuing. Testing your program only at the very end is the best way to render the bug hunting close to impossible !
3 Three steps to complete the project
3.1
1st step: implementing the skeleton of the project and the user interface The rst step in a programming project is perhaps the most important one: before writing any Python code, you should think about the functions you will need to implement, their input/output, their goal, the overall structure of the entire program. Once this done, you should write a skeleton of the project that only handles the interface with the user (i.e. how the user will choose which column to play), the display of the board, and the initialization of the variables.
2 3.1.1 Data Structure for the game. In order to represent the board in Python, you can use a simple data structure: a rc matrix of integers, where 0 represents no disc (i.e. empty), 1 stands for a disc belonging to player 1, and 2 for a disc belonging to player 2 (row 0 being the bottom row). In order to represent that matrix, use a Numpy two-dimensional array: rst import the Numpy module using: import numpy as np You can create an empty board (all elements lled to 0) of r rows and c columns using: game_board = np.zeros((r, c)) Finally, the element of the matrix located at row r and column c can be accessed using game board[r,c]. We will use as convention that the row 0 is the bottom row (the one in which will end up the very rst disk inserted). You must create a Game object that will represent a Connect4 game. For that, simply copy/paste the following code in the beginning of your script: class
Game: mat = None # this represents the board matrix
rows = 0 # this represents the number of rows of the board
cols = 0 # this represents the number of columns of the board
turn = 0 # this represents whose turn it is (1 for player 1, 2 for player 2)
wins = 0 # this represents the number of consecutive disks you need to force in order to win This will allow you to create a game instance like this: my_game = Game() You can later change the values of the attributes of that game instance by using the dot operator. For example, if you would like to access the rows attribute of the game, simply write my game.rows
3.1.2 Skeleton of the project. You will have to implement the following functions for your project.
check victory(game). This function's role is to check if a victory situation has been reached. It will take a game as input and will return: { 0 is no winning/draw situation is present for this game { 1 if player 1 wins { 2 if player 2 wins { 3 if it is a draw
apply move(game,col,pop). This function's role is to apply a certain move to a game. It will take a game as input, as well a column value col and a boolean value pop that will represent the move. It returns a game with an updated board according to that move.
check move(game,col,pop). This function's role is to check if a certain move is valid for a certain game. It will take a game as input, as well a column value col and a boolean value pop that will represent the move. It returns a boolean value False if the move is impossible, and it return True if the move is possible.
computer move(game,level). This function's role is to ask for the computer to make a move for a certain game. It will take a game as input, as well a level that will represent the level of the computer opponent. It will return a column and a boolean value (for the pop) that represent the move chosen by the computer. display board(game). This function's role is to display the board in the console. It takes a game as input and does not return anything.
3 menu(). This function's role is to handle the menu interface with the user via the console. It is basically the director function that will interact with the user and distribute the work to all functions. It should be the main function that is called in your Python script. It takes no input and doesn't output anything. Note that these functions must be implemented to work with dierent board size, dierent number of consecutive same-color disks to win, etc.
3.2 2nd step: implementing the rules of the Connect 4 game Once the skeleton ready, you can start writing the internals of the functions that will implement the game. Display. The display should be handled in a separate function display board, that can be called every time a new move was made by a player. A simple way to implement that function is to simply print the board matrix (optionally, you can create a more advanced display function, using a graphical display, or using the SenseHat module of a Raspberry Pi). Making a move. Note that a move from a player can be described by a column index (in order to specify which column will be played) and a boolean value (in order to specify if the player would like to insert a disk or pop out a disk). Your menu should error-check that the user did not try to enter a column beyond board limits. Once the move chosen, your program must check if the move is valid and apply it only if it is indeed a valid move. Then, if the move is valid, it must check if a victory situation is reached. This entire sequence repeats until the game is over, or the user would like to quit the game. Checking victory. In order to check if a victory situation is reached, you must check if N consecutive discs of the same color are present in the board. Be careful, it might happen a situation where more than N consecutive discs of the same color are present, which obviously also leads to a victory of this player. Besides, if a pop out move was done, it might happen that both players get N consecutive discs of the same color aligned. In that case, the player who did the move will win. Finally, if the board is full, the game is a draw (even though a pop out move could be done in theory). Once this entire second step is fully implemented, you should be able to play human versus human with your program. Do not start the third step before this second step is fully functional (test several games, try unusual situations to make sure there is no bug in your program).
3.3rd step: implementing the computer player The last step of this project is to implement a computer player with two levels of quality.
Level 1: random computer player. To start, program a trivial strategy: each time he will have to play, the computer player will randomly choose a move among all the possible random moves (column and pop out choices). You can test that this player is easy to beat.
Level 2: medium computer player. Program a computer player that will necessarily play a move that leads to a direct win for him if such a move exists. If no such move exist, it will avoid to play a move that leads to a direct win for his adversary in the next round (if such a move exist for the adversary). If again no such move exist, the computer player will simply pick a random valid move.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
