Python代写 | CSSE1001/CSSE7030 Assignment 1

本次Python代写主要内容是完成一个猜谜游戏

Sliding Puzzle Game

Assignment 1 Semester 1, 2021 CSSE1001/CSSE7030

1 Introduction

Sliding Puzzle Game

Assignment 1 Semester 1, 2021 CSSE1001/CSSE7030

Due date: 20:00 (AEST), 26 March, 2021

In this assignment, you will implement a text-based version of the popular sliding puzzle game.1 recommended that you try an online version of the game to familiarise yourself with the concepts and gameplay.2

2 Getting Started

To start, download a1.zip from Blackboard and extract the contents. The a1.zip archive contains all the necessary files to start this assignment. Some support code has been included to assist with implementing the program.

Within the a1.zip archive you will find the following:

3 Terminology

In the context of this assignment:

4 Game Stages 4.1 Setup

At the beginning of the game, the player is asked to choose a difficulty. Difficulty is an integer which is the number of rows and columns of the puzzle. This determines the size of words used in the puzzle, where each word fits into one row. You may assume the difficulty (size of the puzzle) given by the player is an integer between two and fourteen.

After the difficulty is specified, a solution is generated from the words provided in a word file. A shuffled puzzle is then generated by shuffling the tiles in the solution and replacing the tile at the bottom right corner with an empty tile.

4.2 Solving the Puzzle

The player tries to solve the puzzle by sliding the tiles to reach their goal. At each turn, the solution and current state of the game are displayed. The player is then prompted to enter an action. The action can be one of the following:

“H”
“GU”
“U” or “D” or “L” or “R”

Description

Display a help message.
Stop playing the current game.
Move the empty tile in the up/down/left/right direction, respectively.

The prompt will repeat until the player either wins the game, or gives up by entering the “GU” action.

4.3 End of Game

At the end of a game, the player is prompted to choose if they want to start a new game or to stop the program.

“Y” or “y” or “” Anything else

Description

Start a new game. Close the program

If they choose to start a new game, they need to set a new difficulty. A new solution and puzzle are generated, and the new game is started.

5 Conventions

This assignment follows the conventions specified below:

1. solution: A solution is stored in a string. This string contains all letters in the grid from left to right and then top to bottom, without any special characters and/or new lines. For example, the solution, “dogcatpig”, represents the grid below:

2. puzzle: The puzzle is a string that is a copy of the solution, except for the last character. All the characters in the puzzle are shuffled at the start of the game. A space character (i.e. ” “) is added to the end of the puzzle to represent the blank tile. For example, after several moves the puzzle, “fhg bcade”, is represented by the grid below:

To implement the logic of sliding a tile, you need to swap the position of the character in the string that is being slid with the position of the space character. Your logic needs to ensure that you only perform the swap if the character and the space are adjacent to each other in the grid.

3. direction: A direction can either be “U” or “D” or “L” or “R”, which correspond to up/down/left/right. 6 Implementation

The following functions must be implemented in a1.py. You may implement additional functions, if you think they will help with your logic or make your code easier to understand. These functions have been listed in order of increasing difficulty.

check win(puzzle: str, solution: str) -> bool
Returns True if the game is won, given the puzzle and the solution, and False otherwise. Examples of calling check win:

swap position(puzzle: str, from index: int, to index: int) -> str
Swaps the positions of the characters at from index and to index in the puzzle and returns the updated puzzle. Examples of calling swap position:

move(puzzle: str, direction: str) -> Optional[str]
Moves the empty tile in the given direction and returns the updated puzzle. If the move cannot be made (i.e. moving in the given direction results in the empty tile being outside the grid), it returns None. Examples of calling move:

The first line of code in the examples above moves the empty tile from the bottom corner up by one tile. It corresponds to the following graphical demonstration.

The second line of code returns nothing. This is because the empty tile is in the bottom right corner and cannot be moved to the right.

print grid(puzzle: str) -> None
Displays the puzzle in a user-friendly format. Examples of calling print grid:

>>> print_grid(“nevagonagiveu up”) +—+—+—+—+ |n|e|v|a| +—+—+—+—+ |g|o|n|a| +—+—+—+—+ |g|i|v|e| +—+—+—+—+

|u| |u|p|
+—+—+—+—+
>>> print_grid(“nevergonnalet udooooowwwn”) +—+—+—+—+—+
|n|e|v|e|r|
+—+—+—+—+—+
|g|o|n|n|a|
+—+—+—+—+—+
|l|e|t| |u|
+—+—+—+—+—+
|d|o|o|o|o|
+—+—+—+—+—+
|o|w|w|w|n|
+—+—+—+—+—+

main() -> None
Handles the main user interaction in the game, as specified in Section 4.2. You need to replace the print statement with the main logic of your program.

6.1 Support Code

The file a1.py includes an implementation of the shuffle puzzle function. You should not modify this function. You must call this function once and only once per game to create a shuffled puzzle.

shuffle puzzle(solution: str) -> str

Generates a solvable sliding game given the solution.
This function calls swap position, which is specified above. You must implement swap position before you can successfully call shuffle puzzle.

You must use the code provided in a1 support.py to implement your assignment. Do not make changes to a1 support.py as it could cause unexpected errors. In this file you will find some predefined named constants and functions. You are expected to use the provided named constants in your implementation of the game. Of the three functions, you only need to call:

get game solution(file name: str, grid size: int) -> str

This function returns a string, which is the solution to the puzzle. The first parameter is the name of the file from which words are to be loaded. The second parameter is the size of the grid.

7 Example Gameplay

|a|c| +—+—+ |f|c| +—+—+

Current position: +—+—+ |c|f| +—+—+

|a| | +—+—+

Please input a direction (enter “H” for instructions): R ‘R’ is not a possible move here. Please try again.

Solution: +—+—+ |a|c| +—+—+ |f|c| +—+—+

Current position: +—+—+ |c|f| +—+—+

|a| | +—+—+