首页 » IT辅导 » Python代写 | CSSE1001/CSSE7030 Assignment 1

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:

  • 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.

    3 Terminology

    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:

Input

“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.

Input

“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

d

o

g

c

a

t

p

i

g

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:

    >>> 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")
    >>>

f

h

g

b

c

a

d

e

3

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.

4

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

Choose your difficulty. The larger the puzzle, the harder it is to solve.
How big do you want the puzzle to be? 2
Solution:
+---+---+

|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| | +—+—+

Please input a direction (enter "H" for instructions): C
That’s not a valid input, try again.

程序辅导定制C/C++/JAVA/安卓/PYTHON/留学生/PHP/APP开发/MATLAB


本网站支持 Alipay WeChatPay PayPal等支付方式

E-mail: vipdue@outlook.com  微信号:vipnxx


如果您使用手机请先保存二维码,微信识别。如果用电脑,直接掏出手机果断扫描。

blank