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:
- a1.py: You are required to implement your assignment solution entirely within this file. It includes some initial code to help you get started on the assignment. This is the only file that you will submit for this assignment, do not make changes to any other files.
- a1 support.py: This file contains code that you must use to assist you in implementing your assignment. The content of this file is described in Section 6.1.
- words.txt: This file contains a number of common English words.3 The file is used to generate new sliding puzzles, however, you are not to generate puzzles from this file yourself, instead, you will use functions available in a1 support.py to read the file contents.
In the context of this assignment:
- Sliding puzzle is a puzzle consisting of a square-shaped grid.
- The grid is evenly divided into tiles, with the same number of tiles in each row and column.
- Tile is a single cell in the grid containing a single letter, or is a blank tile.
- Blank tile represents an empty space in the physical puzzle on which this assignment is based.
- Puzzle is a grid in which every tile contains a single letter, except for a single tile that is blank.
- Solution is a grid which is filled so that every tile contains a single letter.
- The player’s goal is to solve the puzzle by sliding the non-blank tiles so that the grid corresponds to the solution, with the blank tile at the bottom right corner.
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:
“U” or “D” or “L” or “R”
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
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.
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”
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:
>>> check_win("abcdefgh ", "abcdefghi") True >>> check_win("dabecghf ", "abcdefghi") False
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:
>>> swap_position("care", 0, 2) ’race’ >>> swap_position("does", 3, 2) ’dose’
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:
>>> move("abcdefgh ", "U") ’abcde ghf’ >>> move("abcdefgh ", "R") >>>
本网站支持淘宝 支付宝 微信支付 paypal等等交易。如果不放心可以用淘宝交易！
E-mail: firstname.lastname@example.org 微信:itcsdx