Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1 You need to write a game. THE GOAL IS TO TRAVERSE THE FIELD (represented in MATLAB with an array) IN AS FEW MOVES

image text in transcribedimage text in transcribed

Problem 1 You need to write a game. THE GOAL IS TO TRAVERSE THE FIELD (represented in MATLAB with an array) IN AS FEW MOVES AS POSSIBLE WITHOUT HITTING A HIDDEN MINE. You start at the upper left corner, and move either up/down or left/right until you get to the lower right corner. If you hit a "mine", you lose. As you go, you are given some information about nearby mines. The field is 10 blocks by 10 blocks. You are allowed only 40 moves. Your script should work as follows: 1. PLACE MINES: A random (decimal) array of dimensions 10x10 should be created and stored in the variable array mine-probabilities. This array describes the probability of putting a mine on each position. Also create a new 10x10 array called mines. Populate each (m,n)th element of mines as follows: If mine_probabilities (m,n) has a probability greater than 85% (or 0.85), put a 1 in the corresponding position of mines (this means that there IS a mine on the (m,n)th block). Otherwise, put a 0 in the corresponding position of mines (this means there is NOT a mine on the (m,n)th block). 2. MAKE BEGINNING/END SAFE: We need to ensure that the beginning is "safe" enough to start the game. Therefore, the upper-left 2x2 block (so, rows 1 to 2, columns 1 to 2) must NOT contain mines. If they do, remove them now (set the (m,n)th element in mines equal to zero). The end of the game must also be safe. Therefore, the lower-right 2x2 block (so, rows 9 to 10, columns 9 to 10) must NOT contain mines either. 3. INITIALIZE GAME WINDOW: Create a new 10x10 array called game_window. This is the "field" that will be displayed while you are playing your game. Every element in game_window should be -1, EXCEPT for where your "player" is located: Create a new variable my-position = [1, 1]; to describe your player's current position in terms of row, column). Set the corresponding element element (1,1)) of game_window equal to 100. This is how you know where your player currently is. 4. COUNT NUMBER OF MOVES AND TRACK GAME STATUS: You will need to keep track of how many moves you have used, AND whether or not you have hit a mine, AND whether or not you have won. You can do this by initializing three variables - moves_used, you_lost, and you_won - and setting them all equal to zero. (continued on the next page...) 5. START THE GAME: As long as you have not used all of your moves (remember, you are limited to 40 moves) AND you have not lost hit a mine), AND you have not yet won (landed in the bottom right corner), repeatedly execute the following code: (a) CLEAR GAME WINDOW: You should erase the command window, display the value of game_window, and display the number of moves used already. (b) COUNT SURROUNDING MINES: You should count the number of surrounding mines - how many mines are immediately adjacent to your position? This count should count every mine that is directly above, below, left, right, and diagonal to you. Store this count in a variable called nearby_mines. Display the value of nearby_mines so the user is aware of how many mines are nearby. . Note that, if you are on the border of the field, you need to make sure your code does not return an error when it tries to check if there are mines "outside of the array. (c) MAKE A MOVE: Prompt the user to enter a "move" - does the user want to move up, down, left, or right? There is more than one way to do this (you could prompt the user to enter a number, or a string, or a vector, etc). However, the code should make sure the user enters a valid "move" option. Keep the following guidelines in mind: The user can only move up/down/left right. NO DIAGONAL MOVEMENT. The user can only move one space at a time. The user cannot go "off the edge of the field. They must stay inside the field. (d) UPDATE GAME WINDOW: Your player now needs to "move to its new position. You should update the value of my-position. Then, update the value of game_window as follows: Replace your OLD (m,n)th position in game_window with the value of nearby_mines. This allows the user to keep track of how many mines are near each position. Replace your NEW (m,n)th position in game_window with the number 100. This is how you know where your player is currently located. (c) UPDATE MOVES AND GAME STATUS: Keep track of your number of moves by updating moves_used. Also, check to see if your new position is on a mine - if so, you'll need to update the value of you_lost. Also, check to see if you've arrived at the "end" (the bottom right corner) - if so, you'll need to update the value of you_won. 6. DISPLAY FINAL GAME WINDOW: You should erase the command window, display the value of game_windou, and display the number of moves used already. 7. DISPLAY GAME STATUS: If the player won, display You won! or something like that. If they loet, display You lost. or something like that. When turning in your code, only submit a "snapshot" of the output - just include a sample output showing what the command window looks like when you are roughly halfway through the game. PROFESSOR'S COMMENTS: PLEASE use the template provided in the "Misc. Info folder on Blackboard. Don't forget - when doing test runs of your code, you can enter ctrl-c to exit your program at any time (if you want to stop it before it finishes). I recommend starting with a "simplified" version of the game first. By that I mean: - Maybe start with a smaller field (say, 5x5 instead of 10x10). This will make it easier to test. - Don't worry about checking for mines at first. Worry only about moving around the field. In other words, don't try to solve step 5 part b UNTIL the rest of your code works. - Don't worry about trying to handle invalid "move" options at first. In other words, UNTIL the rest of your code works, assume the user always enters a valid "move

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual C# .NET

Authors: Ying Bai

1st Edition

0470467274, 978-0470467275

More Books

Students also viewed these Databases questions

Question

Describe a goal attainment scale.

Answered: 1 week ago