Starting from:
$29.99

$23.99

Computing and Programming Concepts Homework #3 Version 1 Solution

Purpose: This homework will require you to understand each problem, then design and implement a solution. You will use all of the different Python control structures for sequence, selection, and iteration. You will have the opportunity to use different Python constructs for implementing definite and indefinite iteration (the for and while loops for the former and the while loop for the latter) in your solutions. The homework will also require you to use Python’s list and potentially other “sequence-based” data structures and operations.

 

This is not a collaborative assignment; you must design, implement and test the solution(s) on your own. You may not consult or discuss the solution with anyone other than the course instructor or TAs.  In addition, you may not include solutions or portions of solutions obtained from any source other than those provided in class, Moodle, or this assignment  (i.e., do not refer to any internet sources other than those specified below, in class, or on Moodle).  Obtaining or sharing solutions to any homework problems for this class is considered academic misconduct.  If you are not sure what this means, consult the class syllabus or discuss it with the course instructor.

 

The total point value for programming assignments will be awarded for solutions that are complete, correct, and well-constructed.  A "well-constructed" program entails good design, appropriate comments and general readability (descriptive names for variables and procedures, appropriate use of blank space, etc.).  The following will result in a score reduction equal to a percentage of the total possible points:

 

●    Incorrectly named source file (100%)

●    Constraints not followed (up to 100%)

●    Failure to execute due to syntax errors (75%)

 

Note that your work will be graded using, and must function correctly with, the current version of Python

3 on CSE Labs UNIX machines. If you complete your programming assignment using a different system, it is your responsibility to ensure your programs work on CSE Labs machines prior to submitting them.

Finally, this assignment contains 4  problems (A,B,C, D) worth a total of 50 points, and is 7 pages long.

 

Introduction to Problems 3A, 3B, and 3C.

 

You are developing a text-based adventure game in which the player character is lost in the woods.  On each turn, the player chooses to move North ('N'), East ('E'), South ('S'), or West ('W') by one square.  The exit is three squares north of the player’s starting location, regardless of how far east or west the player moves.  The forest extends infinitely in all other directions (see figure on the top of the next page).

Figure 1: Starting State of a “Game World” for Problems 3A, 3B, and 3C, where north is up. Note that

the exit is three squares north from the player’s initial position (represented by the letter P).

 

Problem 3A (10 points).

 

For this problem, you must write a function called processMove which takes a single parameter, move (represented by an uppercase single character string, 'N', 'S', 'E', or 'W'), and returns an integer representing how many squares farther from the exit the move will take the player.

 

Specifically, processMove should return:

 

-1 if move is 'N'

1 if move is 'S'

0 if move is 'E' or 'W'

0 if move is anything other than 'N', 'S', 'E', or 'W'. In addition, the function must print the string 'Invalid move' before returning in this case.

 

Do not include any code in your file outside of the processMove function (though comments are fine).

 

Examples (user input to the Python Interpreter in bold):

 

processMove('N')

-1

processMove('S')

1

processMove('E')

0

processMove('Down')

Invalid move

0

 

 

Problem 3B (10 points).

 

Write a Python program which uses the function processMove to ask the user for moves until they exit the woods. Please do not import your module from Problem 3A into your solution for Problem 3B; instead, you can copy your function from your Problem A solution file and paste the function into your Problem 3B solution file. Your program should:

 

❖  Use a variable called toExit to track how far the player is from the exit. As described in the introduction, the initial value of this variable should be  3.

❖  Use an indefinite loop and input() to ask the user for a move until toExit reaches 0.

❖  Call the processMove function from Problem 3A within the indefinite loop to determine how many squares each move entered by the user will leave the player from the exit and update the value of toExit accordingly.

❖  Use print() to tell the user how many squares away the exit is at the beginning of each iteration.

❖  Use print() at the end of your script to tell the user that they have escaped.

 

Example (user input in bold):

 

You are 3 squares away from the exit

You are lost in the woods. Enter N/S/E/W: N

You are 2 squares away from the exit

You are lost in the woods. Enter N/S/E/W: E

You are 2 squares away from the exit

You are lost in the woods. Enter N/S/E/W: Burn everything

Invalid move

You are 2 squares away from the exit

You are lost in the woods. Enter N/S/E/W: W

You are 2 squares away from the exit

You are lost in the woods. Enter N/S/E/W: S

You are 3 squares away from the exit

You are lost in the woods. Enter N/S/E/W: N

You are 2 squares away from the exit

You are lost in the woods. Enter N/S/E/W: N

You are 1 squares away from the exit

You are lost in the woods. Enter N/S/E/W: N

You escaped the forest!

 

 

Problem 3C (10 points).

 

Some of your players want to have an option to process a large list of moves at once rather than having to enter them one at a time.  Again copy-pasting in and using your processMove function from 3A (do not include your 3B code), write a function called escape that takes as input a single parameter ls, which is a list of moves (i.e., a list full of single-string characters 'N', 'S', 'E', or 'W') . You may assume that the list of characters only contains valid moves. You should determine the player’s path by taking each move in the list ls in the given order, assuming once again that the exit is three spaces north of the player’s initial condition.  Your escape function should return True if the player would reach

the exit of the forest at any point during their journey and should return False otherwise. This means that if the player reaches the exit, the function should return True, even if the player then goes back into the forest in a subsequent move.

 

Do not include any code in your file outside of the processMove and escape functions (again, comments are fine).

 

Examples (user input to the Python Interpreter in bold):

 

 

escape(['N','N','N']) True

escape(['N','N']) False

escape(['N','N','S','N']) False

escape(['S','N','E','N','W','N','E','N','E']) True

escape(['N','N','N','S','S','S'])

True

 

 

D.  Drawing Shapes Made of Triangles, Squares, or Hexagons (20 points)

 

Create a program that draws shapes made out of squares, triangles, or hexagons in a 360 degree arc around a point in the center of the window on a turtle-graphics canvas.  The type of shape (triangle, square, or hexagon) and number of squares, triangles, or hexagons drawn around the point will depend on input from the user.

 

For example, if the user enters the input triangle and 5, five triangles will be drawn in a 360 degree arc around the point at the center of the turtle-graphics canvas window. A triangle will be drawn at 0 degrees,

72 degrees, 144 degrees, 216 degrees, and 288 degrees (remember 360 degrees and 0 degrees are equivalent).  If the user enters the input SQUARE and 8, eight squares will be drawn in a 360 arc around the point (at 0, 45, 90,135,180, 225, 270 and 315 degrees).  Finally, if the user enters the input HexaGON

and 6, six hexagons will be drawn in a 360 degree arc around the center point (at 0, 60, 120, 180, and 240 degrees).

 

Constraints:

 1)   Your solution should include a continuation loop that asks the user if they would like to continue after a drawing is successfully completed.

2)   You must check for, and recover from, all user input errors illustrated in the example run below.

3)   Your program should accept any combination of upper and lower case input for the words: Triangle, Square, and Hexagon. For example, TRIANGLE, SqUaRe, and hexaGON are all valid entries.

4)   If a user enters an incorrect input value for the shape type or the number of shapes to draw, your program should print an error and re-prompt the user for a value. This process should continue

until the user enters a valid input value. You may assume that the number of shapes entered by the user will be an integer, but should handle the case where the number is zero or negative (see example output).

5)   You should define and use at least one procedure to draw each of the different shapes (for

example, one procedure to draw a triangle, another procedure to draw a square, and another procedure to draw a hexagon).

6)   You should define a main procedure that obtains the user input and calls the procedures to draw

the shape pattern specified by the user input.

7)   Each shape (triangle, square, and hexagon) should be drawn in a different color on the turtle canvas. For example, in the output below, all triangles are drawn in blue while all squares are drawn in red.

8)   Your program should not use global variables (though global constants are acceptable, see (9)).

9)   Each individual shape you draw should be equilateral, and the side length used should be the same regardless of the type of shape selected.. You can choose any value for the side length, but the figure your program draws should be entirely visible on the turtle graphics canvas window. Also, you can use a global constant to set the value of the length of each side.

10) Ensure you clear the canvas before drawing a new shape.

 

An example of a run of the program is as follows (user input is in bold courier font):

 

 

Welcome to the Program to draw shapes

 

Enter the shape that will be used to draw the figure (Triangle, Square):

triSquare

ERROR: choice:  trisquare is not recognized, please enter a valid choice

 

Enter the shape that will be used to draw the figure (Triangle, Square, or

Hexagon): triangle

Enter the number of shapes you would like to draw: -2

ERROR:  -2 is not a positive number- please enter a positive integer value

 

Enter the number of shapes you would like to draw: 5

Would you like to continue ('Y' or 'y'): Y

 

Enter the shape that will be used to draw the figure (Triangle, Square, or

Hexagon): SQUARE

Enter the number of shapes you would like to draw: 8

Would you like to continue ('Y' or 'y'): y

 

Enter the shape that will be used to draw the figure (Triangle, Square, or

Hexagon): HEXAgon

Enter the number of shapes you would like to draw: 6

Would you like to continue ('Y' or 'y'): n

 

 

For the input above, the pictures in Figure 1 ( Drawn from Triangles),  Figure 2 (Drawn from squares), and Figure 3 (Drawn from Hexagons) depicted below are rendered in the turtle-graphics canvas window.

 

Figure 1 (below) – Shape Made from 5 Triangles.

 

 

 

 

Figure 2 (below) – Shape Made from 8 Squares

 

 

 

Figure 3 (below) Shape made from 6 Hexagons

 

Notes:

 

Test your programs thoroughly on a variety of values other than those included in the examples above (that is, all error cases, average and special/extreme cases). Also, include comments in your program that specify your name, your lab section, what the program does, a description of  each element in the input data, and a description of each element in the output data.

 

Finally, include comments in your program that specify your name, what the program does, a description of each element in the input data  and a description of each element in the output data.

 

Submit your Homework Problems via Moodle

 

You must follow the correct naming conventions specified below for each of the programs, or you may not receive credit for your homework.

 

When you are done with each of the problems submit the files containing the source code via the appropriate link in Moodle as follows:

 

For problem A, name the source code file:  <username_3A.py. For problem B, name the source code file:  <username_3B.py. For problem C, name the source code file:  <username_3C.py. For problem D, name the source code file:  <username_3D.py.

 

For each problem, replace <username with your U of M email address.

 

For example, if your email address is smithx1234@umn.edu, for Problem A,  your file should be named smithx1234_3A.py.  Then submit your program using the HW 3 Problem A submission link in Moodle. There will be a similar link on Moodle for programming Homework problems 3B, 3C, and 3D.

 

Make sure that each of your programs runs correctly on the CSE Lab machines or you may not receive credit.

 

 

Also, make sure to submit something for each problem before the deadline so you get some points.  Some points are better than no points, and a program that runs without error s and doessomething related to the homework problem will receive more credit than a program that does not run without errors.

 

Finally, remember that you can always re-submit a newer, better version of your program via Moodle before the deadline, even if you have already submitted something.

More products