We Have Numbers Of Free Samples
For Each Subject To Make A Difference In Your Grade
Python Program: Journal Puzzle
Total Views 707
Introduction
For this assignment we write a Python program in JES that turns an image into a sliding- block puzzle and allows the user to solve that puzzle.
This work is organized on seven tasks as described in the next sections.
1. Solution Design
Using JES, we organize our code as illustrated in the figure below. Our python program is named puzzle.py which calls the puzzle() function that take in argument the selected image by the user using the function pickAFile()
2. Crop The Image to Make It Square
We propose the function crop_image to make the image in square format. This function takes the min of height and width then builds a square image based on this minimum and copy the source image to the new square image.
3. Draw Lines On the Image to Form The 16 Blocks
The function draw_grid draws lines to separate the puzzle blocks on the basis of the image dimension/4
4. Implement Block Slides Up, Down, Left, And Right into the Empty Space.
For this task, we start by implementing the function set_empty_block which draws an empty block in the top left of the image. The empty block takes the red color.
We define a list structure that memorizes each block index and position in the puzzle:
- list_blocks =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
To allow the empty block to move up, down, left and right, we propose the following functions:
- Block_xy: this function retrieves the x and y position of a given block index
- Copy_block: which copies a given block index and print in place an empty block (red color)
- paste_block: this function pastes a given block in argument to the empty block place. The empty block index is given in arguments
- move_block_horiz: which allows to move a block horizontally (left or right) then update the list_blocks list to save the new positions of each block of the puzzle
- move_block_vert: which allows to move a block vertically (up or down) then update the list_blocks list to save the new positions of each block of the puzzle.
5. Keep Track of Where Each Block Is.
The list structure list_blocks memorizes each block index and position in the puzzle:
- list_blocks =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
after a call of the move_block_horiz or move_block_vert, this list is updated by the new positions.
6. Allow User Input to Control the Block Slides.
The user has the possibility to test or to play the puzzle based on user input. To allow user to play, we propose a function named play_puzzle() that requests a user input (“Enter a single letter, u / d / l / r or tape q to quit”, based on this input, we call the adequate function as mentioned previously.
This function use a loop that tests if the puzzle is solved or not using the function is_solved(). If the function is_solved() return true, we make the image grey using the function grey_pic() then we display the message “Great the puzzle is solde.
7. Shuffle The Puzzle Arbitrarily.
To shuffle the blocks of the puzzle we use the shuffle() function. This last changes randomly 10 times the place of the empty block using the function getRandomMove() which returns randomly “up”, “down”, “left” or “right.
8. Check Whether the Puzzle Is Solved.
The function is_solved checks the position of each block and returns true if all blocks are in the initial positions or false if not.
BookMyEssay has gained A-star rating for offering best and attractive assignment writing help in Australia, USA and UK for all the professional courses such as Nursing, Law/Legal, Business, Taxation, Management, Engineering and so on. The team is multi-talented and boasts of unmatched technical and creative skills that favour them draft homework and assignment help with unfailing perfect..
Download
505
Size
140.91 KB
File Type