Python代写:CSC108H Wordlock

实现文字游戏Wordlock, 练习Python的基础语法。


Goals of this Assignment

  • Use the Function Design Recipe to plan, implement, and test functions.
  • Write function bodies using variables, different types (numeric, boolean, string), and conditional statements. You can do this whole assignment with only the concepts from Weeks 1, 2, and 3 of the course.
  • Learn to use Python 3, Wing 101, provided starter code, a checker module, and other tools.

Wordlock: A Word Game

In this assignment, you will write functions to implement a puzzle game that requires the player to use simple moves to reconstruct a scrambled word.
Start by watching this video about the game, in which a player gets a scrambled word and they have to rearrange the letters in the word following a set of rules. Once you’ve watched a couple turns, pause the video and come back here to read about the game mechanics. Then back to the video to finish.

Game Mechanics

These game mechanics describe the operations that you will solve using functions, many of which contain if statements. Read these mechanics carefully and review this section when you try to figure out example function calls what information does each function get passed, what information does it return, and how do you gather that information?

The original string has been scrambled by first splitting it into a number of sections, each with the same length, and then rearranging each section in a random fashion, as in Figure 1. The player’s objective is to unscramble the word using as few moves as they can.

The player’s objective is to unscramble the word using as few moves as they can. The player has three moves available: Rotate, Swap, and Check. The moves can only be applied to the string one section at a time.

The sections in the string are numbered 1 to N, where N is the number of sections. The section length is fixed for the game, and each section will have the same length.

For example, given the string ‘wordlockgame’ and a section length of 4 , section 2 would be ‘lock’ .

You can assume that the answer length is a multiple of the section length.

The Rotate move is a circular shift of the string to the right, while the Swap move exchanges the positions of the first and last characters of the string. Both these moves are illustrated in Figure 2 below:

The Check move is the only move that does not modify the game’s state. It is used to check if a given section is correctly arranged.

The game allows for 3 modes: Easy, Hard, and Test. Hard mode involves playing the game as we have described it above, while Easy mode allows the player to receive hints on what sections and moves to choose each round. Enabling Test mode causes the program to provide the final answer before the game begins.


When the game begins, the player is first asked to choose a game mode. Then, the player is repeatedly asked to provide:

  1. The section number corresponding to the section they want to manipulate. A string with N sections will have its sections numbered 1 to N.
  2. The move they would like to apply to the section.

If the player is playing in mode, they will be offered a hint for a section to choose, or for a move on a particular section. Getting a hint also counts towards the player’s total number of moves.

The game continues until the player has unscrambled the string. We will use the phrase game state to refer to the current state of the string the player is trying to unscramble. When the game is over, the player’s total number of moves is reported.

Starter code

You will write code in a file called It is part of a set of files. Please download the Assignment 1 Files and extract the zip archive. Leave the files where they are, they need to be in the same directory to work properly on your computer.

Here are short descriptions of the files:

    This the file in which you will do all your work!
    This file contains some constants and a complete function header and docstring (but not body!) for the first function you are to write. You will edit this file to design and write a half dozen functions, described below. For each function, you will include a function header (the def line, including the type contract), a function docstring containing a description and two examples, and the body of the function.
    Tip: When you have written all of the functions, you might try playing the game with more challenging puzzles by changing the constants ANSWER and SECTION_LENGTH and making sure your game still works.
    This is the main program. Open this in Wing and run it to start the game.
    This program calls your functions that you wrote in . Do not make any changes to the file.
    We provide a checker program that you should use to check your code. See below for more information about .
    This is the part of the checker that is reusable across all the assignments. uses this to do some of its work, much like the main program,, uses wordlock_functions to do some of its work.
  • pyta
    This is a directory of a large program that examines your code for style and other issues. uses this to do the bulk of its work.


Constants are special variables whose values do not change once assigned. A different naming convention (uppercase pothole) is used for constants, so that programmers know to not change their values. For example, in the starter code, the constant SECTION_LENGTH is assigned the value 3 at the beginning of the module. That value should never change in your code. When writing your code, if you need to use the value of the section length, you should use SECTION_LENGTH . The same goes for the other constant values.

Using constants simplifies code modifications and improves readability. If we later decide to use a different tweet length, we would only have to change the length in one place (the SECTION_LENGTH assignment statement), rather than throughout the program.

What to do

In file , complete the following function definitions. Use the Function Design Recipe, including writing complete docstrings for each function. We have included the type contracts in the following table; please read through the table to understand how the functions will be used.

You will note that we give you the function names and type contracts.

Remember to come up with your example function calls first. Use that step to make sure you understand what you’re being asked to do.

We will be evaluating your docstrings in addition to your code. Please include two examples in your docstrings. You will need to paraphrase the full descriptions of the functions to get an appropriate docstring description.

To understand how these functions might map onto the game, be sure to watch and review the video side by side with thinking of your example function calls.

A1 Checker

We are providing a checker module ( ) that tests two things:

  • whether your code follows the Python Style Guidelines, and
  • whether your functions are named correctly, have the correct number of parameters, and return the correct types.

To run the checker, open and run it. Note: the checker file should be in the same directory as your , as provided in the starter code zip file. Here is a video using the checker in last semester’s assignment the process is the same. a demo of the checker being run (

Be sure to scroll up to the top of the output and read all messages.

If the checker passes for both style and types:

  • Your code follows the style guidelines.
  • Your function names, number of parameters, and return types match the assignment specification. This does not mean that your code works correctly in all situations. We will run a different set of tests on your code once you hand it in, so be sure to thoroughly test your code yourself before submitting.

If the checker fails, carefully read the message provided:

  • It may have failed because your code did not follow the style guidelines. Review the error description(s) and fix the code style. Please see the PyTA documentation for more information about errors.
  • It may have failed because:
    • you are missing one or more function,
    • one or more of your functions is misnamed,
    • one or more of your functions has the incorrect number or type of parameters, or
    • one of more of your function return types does not match the assignment specification.

Read the error message to identify the problematic function, review the function specification in the handout, and fix your code.

Make sure the checker passes before submitting.