Python代写:IFB104 Jigsaw Pullze Part1

Introduction

这次代写的作业,需要用Python的turtle库来画拼图,要求画的拼图可以全成一个完整的图案。第一部分拼图是完整的,第二部分部分拼图会缺失。

Overview

This is the first part of a two-part assignment. Part B is intended as a last-minute extension to the assignment, thereby testing the maintainability of your code, and the instructions for completing it will not be released until Week 7. Whether or not you complete Part B you will submit only one file, and receive only one mark, for the whole 25% assignment.

Motivation

One of the most basic functions of any IT system is to process a given data set to produce some form of human-readable output. This task requires you to produce a visual image based on data stored in a list. It tests your abilities to:

  • Process sequences of data values;
  • Perform arithmetic operations;
  • Display information in a visual form; and
  • Produce reusable code.

Goal

Jigsaw puzzles are a familiar, traditional pastime. In this assignment you are required to develop a Python program which processes data stored in a list to display an attempt to solve a jigsaw puzzle consisting of four distinct pieces. To do so you will need to use basic Python features and the Turtle graphics module.
You must design four interlocking puzzle pieces which, when assembled in the correct order, produce a single picture. The picture must be non-trivial, and must span all four pieces, but otherwise you have a free choice of what to draw, e.g., cartoon, game or science fiction characters, household objects, corporate or sporting logos, buildings or vehicles, animals or pets, landscapes, etc.
To position the pieces you must develop your code so that they can be drawn in different locations on the screen. A skeletal Python program, jigsaw_puzzle.py, is provided with these instructions which draws:

  • A four-place template for putting pieces whose position has been chosen; and
  • A box to contain unused pieces.

It also contains several data sets, in the form of lists, to guide your drawing of the attempted solution to the puzzle. The lists contain instructions in two or three parts:

  • The identity of the jigsaw puzzle piece to draw, from ‘Piece A’ to ‘Piece D’.
  • The place where the piece must be drawn, either in one of the four template locations, ‘Top left’, ‘Top right’, ‘Bottom left’ or ‘Bottom right’, or in the box of unused pieces, as indicated by ‘In box’.
  • An optional mystery value, ‘X’, whose purpose will be revealed only in the second part of the assigment.

All four jigsaw pieces must have a different shape, with protruding “tabs” that interlock into corresponding “blanks” in other pieces, just like a physical jigsaw puzzle. When assembled correctly the four pieces must form a perfect square. The picture produced by assembling the puzzle correctly must be non-trivial, and must span all four pieces. For instance, four separate images, one per piece, would be unacceptable. Although it’s difficult to generalise the artistic requirements for this assignment, given the wide range of pictures that could be chosen, it’s expected the assembled image would involve several different shapes of several different colours and the resulting picture must be immediately recognisable.
You are required to use Turtle graphics to draw the pieces in the places specified by any of the given data sets, and your code should work for any other similar data sets in the same format. Furthermore, you must provide your own data set for the correct solution to your particular puzzle.

Illustrative example

To get you started, you will find a Python file, jigsaw_puzzle.py, accompanying these instructions. When you run this program it will produce a drawing canvas as shown below. There is a neutral grey background and five squares. The four squares on the left mark the template where the four-piece jigsaw puzzle will be assembled. The bigger square on the right is the “box” in which unused jigsaw pieces are stored.
To help you develop your solution, the centre coordinates of each square are also marked. Each of the squares in the template on the left is a 300 pixel square, so the pieces of your jigsaw puzzle must all be of this basic size, excluding tabs. The box allows for tabs to protrude from pieces as far as 100 pixels in any direction, so your pieces should not have tabs larger than this.
Your first challenge is to develop code that will use Turtle graphics to draw four distinct jigsaw puzzle pieces with these dimensions. As an illustrative example, we have developed code that draws the following four pieces.
Excluding the protruding tabs and indented blanks, each of these pieces is a 300 × 300 pixel square. When assembled correctly they produce a single image which forms a perfect 600 × 600 pixel square. Notice that each piece has a different shape as well as a different image.
The pieces are “fully interlocking”, meaning that each piece shares at least one tab/blank with both of its immediate neighbours.
In the Python template file you will find several data sets in the form of lists.
These lists represent attempts to solve the puzzle. Each element of the list is itself a list which specifies which piece is to be drawn and where. For instance, attempt_27 above requires us to draw our ‘Piece C’ centred in the bottom left, ‘Piece D’ centred in the box, and so on. Unfortunately this particular attempted solution is unsuccessful for the pieces we have designed and produces the following image.
Not only are the pieces misplaced, but the uppermost tab on Piece C (at the bottom left) has been entirely obscured by Piece A (at the top left). Nonetheless, we are required to draw the pieces wherever instructed, even if their tabs overlap. Similarly, some of the data sets require several pieces to be left in the box, in which case they should simply be drawn on top of each other. Also, some data sets don’t mention all of the pieces, in which case only the pieces listed should be drawn. Finally, some lists contain a third value, ‘X’, associated with certain pieces. For now you can ignore these optional values. Their purpose will be revealed in Part B of this assignment.
Your final task is to provide instructions for correctly assembling your particular collection of jigsaw pieces, using the list “solution” in the provided Python file. In our case the correct solution is to put Piece B in the top left, Piece D in the top right, Piece C in the bottom right and Piece A in the bottom left. Applying these instructions finally reveals our overall picture to be a portrait of Fred Flintstone’s next door neighbour and best friend, Barney Rubble, as shown overleaf.

Requirements and marking guide

To complete this task you are required to extend the provided jigsaw_puzzle.py Python file by completing function draw_attempt so that it can draw jigsaw puzzle pieces at the places specified by a data set provided as its single parameter. Your code must work for all the supplied “attempt” data sets and any other data set in the same format. You must also provide your own “solution” data set that completes your puzzle correctly.
Your submitted solution will consist of a single Python file, and must satisfy the following criteria. Percentage marks available are as shown.

  1. Drawing four distinct puzzle pieces (4%). Your program must be able to draw four distinct puzzle pieces, each of a different shape. The basic shape of each piece must be a 300 × 300 pixel square, but they can have any number of “tabs” that protrude by up to 100 pixels (and corresponding indented “blanks”). When assembled correctly, as per your provided solution, all of the pieces must fit together precisely as a 600 × 600 pixel square in the provided template, with no parts overlapping or sticking out.
    Also your puzzle must be fully interlocking, meaning that when assembled correctly each piece shares at least one tab/blank with each of its immediate neighbours.
  2. Drawing a picture in four parts (4%). Each of your puzzle pieces must contain one piece of a single complete picture. Each piece must contain a different, non-trivial part of the overall picture. The whole picture must span all four pieces. When assembled correctly, as per your provided solution, the parts of the complete picture must align correctly. The picture should be clearly recognisable and of a reasonable degree of complexity, involving multiple shapes and colours.
  3. Relocating puzzle pieces (5%). Your code must be capable of drawing each of the four puzzle pieces at any of the five marked places, either in the four-place jigsaw template on the left or in the unused pieces box on the right. The pieces must preserve their appearance no matter where they are drawn and must fit perfectly into the marked places (although “tabs” may stick out, of course). Your solution for relocating the pieces must work for all of the provided data sets and any other data sets in the same format. (You cannot “hardwire” your code for specific data sets and you may not change the data sets provided.)
  4. Providing a solution to the puzzle (2%). You must provide a “solution” list, using the same data format as the “attempt” data sets, whose contents tell us how to solve your particular puzzle correctly. When your draw_attempt function is called with this list as its argument your program should draw a perfect solution to the puzzle. Your solution list should not contain the optional third value, ‘X’, in any of its elements. NB: If you do not provide a solution list we will not be able to assess how well your pieces fit together and you cannot receive full marks for Criteria 1 and 2 above.
  5. Code quality and presentation (2%). Your program code must be presented in a professional manner. See the coding guidelines in the IFB104 Code Presentation Guide (on Blackboard under Assessment) for suggestions on how to achieve this. In particular, given the obscure and repetitive nature of the code needed to draw complex images using Turtle graphics, each significant code segment must be clearly commented to say what it does, e.g., “Draw Barney’s right eye”, “Draw Barney’s left ear”, etc.
  6. Extra feature (8%). Part B of this assignment will require you to make a ‘last-minute extension’ to your solution. The instructions for Part B will not be released until just before the final deadline for Assignment 1.

You must complete the task using basic Turtle graphics and maths functions only. You may not import any additional modules or files into your program other than those already included in the given jigsaw_puzzle.py file. In particular, you may not import any image files for use in creating your puzzle pieces.
Most importantly, you are not required to copy the puzzle piece shapes shown in this document. Instead you are strongly encouraged to be creative and to choose your own shapes and an overall image that interests you personally.

Development hints

  • This is not a difficult task, but due to the need to create puzzle pieces that fit together properly it can be a time-consuming one, so you are strongly encouraged to design your puzzle pieces carefully before developing any program code.
  • The hardest part of this assignment is the need to allow the pieces to be drawn in different locations. You therefore need to devise a way of drawing each piece so that you can control its position either by (a) making all drawing moves relative to the starting position (e.g., using Turtle’s forward, left and right commands) or (b) by calculating absolute positions for each drawing move (e.g., using Turtle’s goto command) in terms of a given position.
  • If you are unable to complete the whole task, just submit whatever parts you can get working. You will receive partial marks for incomplete solutions.
  • To help you debug your code we have provided several data sets, numbered 1 to 12, which draw just one piece at a time. You can use these to help create the code for each puzzle piece separately.
  • Part B of this assignment will require you to add an extra feature to your solution in a short space of time. You are therefore encouraged to keep code maintainability in mind while developing your solution to Part A. Make sure your code is neat and well-commented so that you will find it easy to extend when the instructions for Part B are released.