+1443 776-2705 panelessays@gmail.com

Are you looking for Reliable Online Writing Services? We are Here For you. Our Professional Tutors Can Handle Any Kind of Assignment. Don’t Use Plagiarized Sources. Get Your Perfect Paper Today! Click Here To Order.

PYTHON4 Program 1 – twine.py In this program, you are wandering through the forest; you are lost! Happily, you’ve brought with you a ball of twine, that you unroll as you go. That way, you can always get back to where you started. (Sadly, there’s no way out of the forest. Sorry about that!) In this program, you will model this as a small interactive game. You will prompt the user with your current state, and then they will give a command; based on that command, you will update the state, print out some sort of response, and then prompt them for another command. The commands that you must support include: n,s,e,w (move one square in that direction), back (retrace your steps by one space), crossings (tell the user how many times they’ve been at the current location), map (print a map of the space you’ve travelled), and ranges (give the x and y bounds of your path so far). Note that none of these commands have any parameters. 4.1 The History You will model the twine using a stack, and the stack must be implemented as a Python list; each element in the stack must be an (x,y) tuple, giving your position at that point in the path. If the user gives the back command, you must pop the most recent value off of the stack, effectively moving you backwards in time. Your start position is always (0,0), and it must always be included, in the first element of the stack; you must not pop it off. 4.2 Prompt The prompt, before every command, must be exactly three lines, like this: Current position: (3,2) Your history: [(0,0), (1,0), (2,0), (3,0), (3,1), (3,2)] What is your next command?and after you have printed out the response to any command, you must add a blank line before the next prompt. (To make the output from GradeScope look a little nicer, please print out the third line of the prompt using print() – and then use input(), with no argument, to read the command.) 4.3 Obstacles You can get most of the credit for this assignment without this feature. So, for your first version of this program, simply print out the prompt shown below, but ignore the first line typed by the user, and ignore the existence of obstacles. Add this feature later. Before the first prompt to the user, you must get the name of an “obstacles” file and read it. It gives a list of locations which are blocked, and the player cannot enter. First, give the user a prompt: Please give the name of the obstacles filename, or – for none: and then, on the next line, read the filename. If the user gives you “-“, then you must not attempt to open any file; instead, just record that there are no obstacles to worry about in your variables, and start the game. If the user gives you anything else, then attempt to open the file given – if it works, then read the file to get the obstacles list. If it fails, then report an error message to the user (see below), give them the prompt again, and then ask again – loop until either the user gives you “-“, or a file that you can read. 4.3.1 The Format of the Obstacles File The obstacles file simply contains a set of (x,y) pairs, one pair per line. Blank lines are allowed, although this time comments are not. The obstacles file can be of any size (including zero valid lines). A valid line in the obstacles file is simply two integers, separated by whites- pace, like this: 12 -3 10 01 So long as the values are both integers (and there’s nothing else on the line), you don’t have to do any more error checking. In particular, you may ignore strange things like having duplicate entries, having a (0,0) entry, or having exceptionally large entries – I won’t test any of these. Spec continues on next page. 4.4 Errors If you encounter any sort of input error (an invalid command, invalid line in the obstacles file, etc.), print out an error message. For this program, the error messages must begin with ERROR: and the autograder will ignore the rest of your error message – but TAs will inspect your code to see if you are printing out useful error messages, which would help the user (or help with debugging!). You must report a single error line any time that you encounter any of the following conditions: The user gives an “obstacles file” name which is not “-“, but also isn’t a valid filename (meaning that you can’t open it). Any line in the “obstacles file” is invalid. Any command typed by the user is invalid – either because it isn’t recog- nized, or because it has parameters. 4.5 EOF Your program must terminate without an exception, if stdin is connected to a file (like it will be in GradeScope). This means that it is not safe to call input() without a try-except block. So you have a choice: you can use a for loop, or you can use input() with a try-except block. In this case, because you have to print out a prompt before you ask the user for their next command, I think that using a try-except block actually makes for simpler and clearer code. But either option is OK! 5 twine.py – Command Details 5.1 Blank Lines If, when you’re expecting a command, the input instead is a blank line, then don’t treat this as an error. Instead, print out the following message, then go back and prompt for another input. You do nothing. 5.2 n,s,e,w The four directional commands are fairly self-explanatory; move in that direc- tion, pushing your new location onto the stack. (In this map, North is in the +y direction, and East is +x.) It’s perfectly OK if the user overlaps a place they’ve been before; this just means that the twine crosses over an older piece. In your program, this means that multiple entries in the stack will have the same value. You have only one thing to check for: if the user attempts to move into a location that has an obstacle, then print You could not move in that direction, because there is an obstacle in the way. You stay where you are. and then don’t push a new location onto the stack. If you succeed in moving, however, you don’t have to print anything; just go ahead and print the blank line, and then the new prompt. 5.3 back This command simply pops one value off of the stack – unless the stack only has one element. Normally, you would simply print You retrace your steps by one spacebut, if there is only one remaining value on the stack, then print Cannot move back, as you’re at the start!5.4 crossings Count how many times your current position is in the stack (including your current position as 1), and print out the result, like this: There have been 3 times in the history when you were at this point. NOTE: The crossings, map, ranges command should all print out in- formation only based on the current stack – to not try to keep additional information. If you move backwards, your program should completely forget where you previously had been. Spec continues on next page. 5.5 map Print out a grid of characters. The size of the grid is entirely determined by how far you’ve walked in each direction, so scan your stack and find the maximum and minimum values for both x and y. (I gave you the ranges command so that you could debug this easily.) For instance, if you find that the min and max x values are -2,3 then the left column of your map must represent -2 and the right column 3. Similarly, if the min and max y values are -1 and 1, then there will be three rows: the top row will represent y=1 and the bottom y=-1. Print out the following characters: Print a + (plus) at the player’s current location – even if it is also the origin. Print a * (asterisk) at the origin (that is, (0, 0)), unless the player is there. Print a X at any location where the player has been, but which is not the origin or the player’s current position. Print a blank space at any obstacle. Print a . (period) otherwise. Forexample,ifyourstackcontains[(0,0), (1,0), (2,0), (2,1), (2,2)], and the only obstacle is at (1,2), then print the following: .+ ..X *XX NOTE: See the NOTE in crossings above. 5.6 ranges This is really just a debug command; it helps you find the ranges that you will need for the map. Print it out like this: The furthest West you have ever walked is 0 The furthest East you have ever walked is 2 The furthest South you have ever walked is 0 The furthest North you have ever walked is 2NOTE: See the NOTE in crossings above.  Engineering & Technology Computer Science