Connect 4 (ish)
Assignment 1
Semester 1, 2024
CSSE1001
Due date: 28 March 2024, 16:00 GMT+10
1 Introduction
In this assignment, you will implement a text-based version of Connect 4, with some rule modifi- cations inspired by this version developed by Hasbro Inc. The rules of this game are very similar to regular connect four: Two players each have a set of pieces (In our text-based version player 1’s pieces are represented by X, and player 2’s pieces are represented by O). Players take turns to place pieces in one of 8 columns. These pieces are affected by gravity and fall into the lowest empty space out of 8 rows within each column. The objective for each player is to be the first to form an unbroken line with 4 of their own pieces. These lines can occur either vertically, horizontally, or diagonally. The twist with this version of the game is that, on their turn, instead of placing a piece at the top of a column, a player may choose to ’pop out’ a piece from the bottom of a column. All pieces above the removed piece within the chosen column will then ’fall down’ one row. This interrupts the opponents plans, and potentially forms an unbroken line of 4 pieces of one kind. A nifty feature of this ’pop out’ mechanic is that it also prevents stalemates from occuring. As such, the game is only over when either:
1. One player wins by creating an unbroken line (horizontal, vertical, or diagonal) of at least 4 of their own pieces at the end of a turn, while not creating an unbroken line of 4 of the other player’s pieces at the end of the same turn.
2. The players draw because at the end of a turn both players posess an unbroken line (hor- izontal, vertical, or diagonal) of at least 4 of their own pieces (This can happen when a player pops out a piece).
2 Getting Started
Download a1. zip from Blackboard — this archive contains the necessary files to start this as- signment. Once extracted, the a1. zip archive will provide the following files:
a1. py This is the only file you will submit and is where you write your code. Do not make changes to any other files.
a1 support. py Do not modify or submit this file, it contains pre-defined constants to use in your assignment. In addition to these, you are encouraged to create your own constants in a1. py where possible.
gameplay/ This folder contains a number of example outputs generated by playing the game using a fully-functional completed solution to this assignment. The purpose of the files in this folder is to help you understand how the game works, and how output should be formatted.
NOTE: You are not permitted to add any additional import statements to a1.py. Doing so will result in a deduction of up to 100% of your mark. You must not modify or remove the two import statements already provided to you in a1. py. Removing or modifying these existing import statements may result in your code not functioning, and in some cases will result in a deduction of up to 100% of your mark.
This section provides an overview of gameplay. Where prompts and outputs are not explicitly mentioned in this section, please see Section 4 and the example games in the gameplay/ folder provided with this assignment.
The game begins with an empty board of 8 rows separated into 8 columns. Player 1 ( X ) gets to make the first move. Until the end of the game, the following steps occur:
1. The current game board state is displayed.
2. The user is informed whose turn it is to move.
3. The user is prompted to enter a command, and then enters one. See Table 1 for the set of valid commands and the actions performed when they are entered. The gameplay/ folder provided with this assignment presents specific examples for what to do on each command.
4. If the move is invalid for any reason, the user is shown a message to inform them of why their move was invalid (see Table 2 for all required validity checking and messages for this step), and then the program returns to step 3. If the move is valid, the program progresses to the next step.
5. The board is updated according to the requested move, and the updated board state is displayed.
6. If the game is over (Due to either a win or a draw), the program continues to the next step. Otherwise, the program returns to step 2.
7. When the game is over, the users are informed of the outcome.
8. The users are prompted as to whether they would like to play again. At this prompt, if they enter either ‘y’ or ‘Y’, a new game is created (i.e. an empty board is set up and the game returns to player 1’s turn) and the program returns to step 1. If they enter anything other than ‘y’ or ‘Y’, the program should terminate gracefully (that is, the program should end without causing any errors or exiting the test suite).
Action to take |
|
"{action}{column} ", where {action} may be "a", "A", "r" or "R" and {column} is an integer |
If {action} is "a" or "A", add a piece to the top of the column with number given by {column}. If {action} is "r" or "R", remove a piece from the bottom of the column with number given by {column}. |
"h" or "H" |
The user is shown a help message, and then prompted for another command |
"q" or "Q" |
gameplay does not continue and the program skips to step 8 |
Table 1: Valid commands and the actions that should be taken. If the command entered by the user does not exactly match one of the commands in this table then no action should be taken for step 3 and the program should move directly to step 4
Constant in a1 |
support. py |
|
The move entered does not begin with a valid character, the first character is not followed by a single digit integer, or the command contains any superflous characters. |
INVALID FORMAT MESSAGE |
|
The integer is not a valid column on the board. |
INVALID COLUMN MESSAGE |
|
The move is requesting to add a piece to a full column. |
FULL COLUMN MESSAGE |
|
The move is requesting to remove a piece from an empty column. |
EMPTY COLUMN MESSAGE |
Table 2: Constants containing the messages to display when invalid user input is entered. Prece- dence is top down (i.e. if there are multiple issues with user input, only display the message for the one which occurs first in this table).
Permitted Techniques:
This assesment has been designed to allow you to practice what you have learnt in this course so far. As such, you must only use the functions, operators and data types presented to you in lectures up to (and including) Topic 4B (Lists). Namely, the following techniques are permitted for use in this assignment:
. Functions (def,return)
. Basic control structures (for, while, if, break)
. Primitive data types (int, str, bool etc.)
. Variable assignment (=)
. Arithmetic (+,-,*,\,\\, ,% etc.)
. Comparison (==,<=,>=,<,>, != etc.)
. Basic Logic (not, and, or etc.)
. lists and tuples
. range and enumerate
. input and print
Using any functions, operators and data types that have not been presented to you in lectures up to (and including) Topic 4B (Lists) will result in a deduction of up to 100% of your mark.
A pinned thread will be maintained on the Ed discussion boardwith alist of permitted techniques. If you would like clarification on whether you are permitted to use a specific technique, please first check this list. If the technique has not been mentioned, please ask about permission to use the technique in a comment on this pinned thread.
Required Functions
This section outlines the functions you are required to implement in your solution (in a1. py only). You are awarded marks for the number of tests passed by your functions when they are tested independently of one another. Thus an incomplete assignment with some working functions may well be awarded more marks than a complete assignment with faulty functions. Your pro- gram must operate exactly as specified. In particular, your program’s output must match exactly with the expected output. Your program will be marked automatically so minor differences in output (such as whitespace or casing) will cause tests to fail resulting in a zero mark for that test.
Each function is accompanied with some examples for usage to help you start your own testing. You should also test your functions with other values to ensure they operate according to the descriptions.
The following functions must be implemented in a1. py. They have been listed in a rough order of increasing difficulty. This does not mean that earlier functions are necessarily worth less marks than later functions. It is highly recommended that you do not begin work on a later function until each of the preceding functions can at least behave as per the shown examples. You may implement additional functions if you think they will help with your logic or make your code easier to understand.
4.1 num hours() -> float
This function should return the number of hours you estimate you spent (or have spent so far) on the assignment, as a float. Ensure this function passes the relevant test on Gradescope as soon as possible. The test will only ensure you have created a function with the correct name and number of arguments, which returns a float and does not prompt for input. You will not be marked incorrect for returning the ‘wrong’ number of hours. The purpose of this function is to enable you to verify that you understand how to submit to Gradescope as soon as possible, and to allow us to gauge difficulty level of this assignment in order to provide the best possible assistance. You will not be marked differently for spending more or less time on the assignment.
If the Gradescope tests have been released, you must ensure this function passes the relevant test before seeking help regarding Gradescope issues for any of the later functions. See Section 5.3 for instructions on how to submit your assignment to Gradescope.
4.2 generate initial board() -> list[str]
Returns the initial board state (i.e. an empty board state). The board is represented by a list of strings. Each column is represented by a string of characters. The first string in the list represents the leftmost column of the game board, and the last string in the list represents the rightmost column of the game board. The first character of each string represents the top of the associated column, and the last character of the string represents the bottom of the associated column.
Example:
>>> generate_initial_board()
['--------', '--------', '--------', '--------', '--------', '--------', ' --------', '--------']
4.3 is column full(column: str) -> bool
Returns True if the given column is full, and False otherwise. You may assume that column will represent a valid column state (i.e. no blank spaces between pieces).
Example:
>>> column = "---XOXXX"
>>> is_column_full(column)
False
>>> column = "OXXOOXOO"
>>> is_column_full(column)
True
4.4 is column empty(column: str) -> bool
Returns True if the given column is empty, and False otherwise. You may assume that column will represent a valid column state (i.e. no blank spaces between pieces).
Example:
>>> column = "-------- "
>>> is_column_empty(column)
True
>>> column = "-----XXO"
>>> is_column_empty(column)
False
4.5 display board(board: list[str]) -> None
Prints the game board to the terminal with columns separated by pipe characters (—) and num- bered below. The printed output must exactly match the format as presented in examples. Note that different system fonts may cause spacing to appear different on your machine.
A precondition to this function is that the input board will contain exactly 8 strings each with exactly 8 characters. You should not perform any additional validity checking (that is, do not check that the board represents a valid game state).
Example:
>>> board = generate_initial_board()
>>> display_board(board)
|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|-|
1 2 3 4 5 6 7 8
>>> board = ['--------', '----OOOO', 'XXXXXXXX', '--------', '------XO',
' --------', '---XXOXO', '--------']
>>> display_board(board)
|-|-|X|-|-|-|-|-|
|-|-|X|-|-|-|-|-|
|-|-|X|-|-|-|-|-|
|-|-|X|-|-|-|X|-|
|-|O|X|-|-|-|X|-|
|-|O|X|-|-|-|O|-|
|-|O|X|-|X|-|X|-|
|-|O|X|-|O|-|O|-|
1 2 3 4 5 6 7 8
>>> board = ['Ashleigh', ' ', '-----W--', 'B----i--', '-r---l--',
' --a--s--', '---e-o--', '-----n--']
>>> display_board(board)
|A| |-|B|-|-|-|-|
|s| |-|-|r|-|-|-|
|h| |-|-|-|a|-|-|
|l| |-|-|-|-|e|-|
|e| |-|-|-|-|-|-|
|i| |W|i|l|s|o|n|
|g| |-|-|-|-|-|-|
|h| |-|-|-|-|-|-|
1 2 3 4 5 6 7 8
4.6 check input(command: str) -> bool
Returns True if command is a well formatted command that is not invalid as described in the first two rows of Table 2, and False otherwise.
Note that user inputs will be 1-indexed (That is, users will enter numbers corresponding to the columns as numbered in the printout by display board). In the event that command is ill-formed, this function should also display the relevant error message to the user before returning False. This function should not check whether the command violates any game rules. It is sufficient to assume that the user entered column must be a single digit number.
Example:
>>> command = "a1"
>>> check_input(command)
True
>>> command = "r1"
>>> check_input(command)
True
>>> command = "a3"
>>> check_input(command)
True
>>> command = "h"
>>> check_input(command)
True
>>> command = "1r"
>>> check_input(command)
Invalid command . Enter 'h' for valid command format
False
>>> command = "a3 "
>>> check_input(command)
Invalid command . Enter 'h' for valid command format
False
>>> command = "a9"
>>> check_input(command)
Invalid column, please enter a number between 1 and 8 inclusive
False
>>> command = ""
>>> check_input(command)
Invalid command . Enter 'h' for valid command format
False
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。