Create a function with one matrix output board and no inputs

Assignment Help MATLAB Programming
Reference no: EM131891763

Homework-

For this assignment, turn in two m-files, a function for generating the board named hw6_yourinitials_board_gen, and a script for solving the board named hw6_yourinitials_solve.

It is a rainy day in Amherst, and of course, Eduroam is down, leaving you without any internet connection. To combat this, you and a friend decide to play a game of Battleship. In a traditional game of Battleship, two players place five different ships on a 10x10 board in the location of their choosing, and take turns guessing the locations of the ships on the board. In the real life game, the boards have letters A-J on the y-axis, and 1-10 on the x axis, but since this is MATLAB, each axis will be numerical. Let's set [1 1] to the upper left corner, [1 10] lower left, [10 1] upper right and [10 10] lower right. Note these are basically Cartesian coordinates. But be aware that when refereeing to an element in a Matlab matrix, the row comes first, then the column. So the A(10,1) in a 10x10 matrix is actually the lower left corner.

The sizes of the ships are as follows:

1 - Aircraft Carrier - 5 spaces

2 - Battleship - 4 spaces

3 - Submarine - 3 spaces

4 - Destroyer - 3 spaces

5 - Patrol Boat - 2 spaces

They can be put in any orientation (horizontal or vertical) and location that you want, as long as they do not overlap or go off the board.

If you correctly guess the coordinates of a space occupied by part of a ship, it is considered a "hit". An incorrect guess is considered a "miss". The game ends when one player hits every possible space occupied by a ship (17 total hits).

Board Generation

Create a function named hw6_yourinitials_board_gen with one matrix output Board and no inputs. This function will also have a subfunction called CheckSpaces, described below.

1. Initialize Board as a matrix of zeros that is size 10x10. You will use a 1-5 to signify that a space is occupied (1-5 correspond to the 5 boats listed above), and a 0 for open spaces. Also initialize the length of each ship in a 5x1 vector, in the order given above.

2. Loop over the five ships with a for loop in order to place them on the board.

3. For each ship, use a while loop that runs until the ship has been placed successfully, meaning it is on the board and not overlapping.

4. For each ship, start by picking a random starting location on the board. The starting location can be called initial_coord and is a 1x2 vector. Check if this is a viable starting location, i.e. one that is unoccupied.

5. If the initial location is unoccupied, call a sub-function called CheckSpaces. This function checks the spaces around the initial coordinates to decide if it is a viable location, and updates the Board matrix if it is viable. The inputs to the subfunction are Board, the length of the current ship, the initial coordinates, and the number of the current ship (1-5). The outputs are Board, and a scalar success that is equal to 0 if the ship could not be placed, and 1 if it could. Your subfunction should do the following

a. Call the function "get_limits" that I will provide you. This function tells you the minimum and maximum possible x and y coordinates that are open horizontally and vertically from the initial coordinate. So if the entire row and column is open, it will return 1 and 10 for the min and max in both directions. But, if a row or column is blocked by another ship, it will return an intermediate value that is the first open coordinate available in that direction. Open the function and read the comment that explains it more. You do not have to understand what is happening in the code, though feel free to check it out. But make sure you understand the outputs it gives.

b. Knowing the length of the ship, and the limits of the current row and column, you can now check if the ship can be placed either horizontally or vertically. If it can be placed, use the ship number (1-5) and update Board and success.

c. If both the horizontal and vertical directions are viable, pick this randomly (50% of either). If only one direction is viable, then pick that direction.

d. When placing the ship, at least one part of the ship must occupy the initial coordinate. Beyond that, it is up to you, i.e. the initial coordinate can be the first, last, or in between location on the ship.

6. Your function should output a viable Board, with all five ships placed in non-overlapping positions on the Board, and numbered 1-5. So there will be five 1's, four 2's, three 3's and 4's, and two 5's. Here is an example:

0

0

0

4

1

1

1

1

1

0

0

0

0

4

0

3

3

3

0

0

0

0

0

4

0

0

0

0

0

0

2

2

2

2

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

5

0

0

0

0

0

0

0

0

0

5

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

Script -

Create a script named hw6_yourinitials_script.

1. Call your board generation function to generate a random Board to play with.

2. Using a for loop, you will execute 100 iterations, each of which will make random coordinate guesses until all of your ships have sunk.

3. For each iteration, create a new variable Board2 which initially equals Board (Board never changes).

4. For each of the 100 iterations, use a while loop that runs until all the ships have sunk.

5. Within the while loop, guess a random coordinate (which can repeat). Check Board2, and if it is a hit (occupied by a ship, not hit already) change the value at that location to -999. Your loop should run until all entries in Board2 are either 0 or -999.

6. After the while loop ends, calculate the percentage of hits in that iteration (hits divided by total guesses) and save the value in a vector HitPercent. Determine the mean, max, min and standard deviation of HitPercent.

7. Plot a histogram of HitPercent using hist, label axes and use a title, and comment on the results in a few sentences.

Part 2 - create an algorithm that sinks all the ships more efficiently than the random,

brute force method we implemented. Use a lengthy comment to describe your strategy and implementation. Calculate a new variable HitPercent2 and compare it to the baseline value. If you do not attempt Part 2, you can get up to a 90. An additional 20 points are available for a well-executed strategy in part 2.

Reference no: EM131891763

Questions Cloud

Ideas on says laws of markets : How did T. Malthus try to articulate his ideas on Says Laws of Markets?
What is the average daily float : What is the average daily float? What is the weighted average delay? What are the average daily receipts?
Explain what is the point of view of the narrator : Notice the language. Do you think this story takes place in a modern time? Why or why not? Who is telling the story? What is the point of view of the narrator?
Enter the annual equivalent cost as positive number : Enter the Annual Equivalent Cost as a positive number of the preferred machine.?
Create a function with one matrix output board and no inputs : MIE 124 Homework. Create a function named hw6_yourinitials_board_gen with one matrix output Board and no inputs. Create a script named hw6_yourinitials_script
Are performance evaluations required by law : Care in this type of setting is delivered by physicians, physician assistants, nurse practitioners, and advanced practice professionals.
How your poem is connected to the theme you chose : Draw a conclusion about how your poem is connected to the theme you chose. Draw a conclusion about how this poem is connected to the theme.
How much money will analog need to borrow in order to end up : How much money will Analog need to borrow in order to end up with $830,000 spendable cash?
Discuss your response to the productions lights and sound : Discuss your response to the production's costumes, set, lights, sound, cast (actors), and the performance venue itself. What worked? Why? What didn't work?

Reviews

Write a Review

MATLAB Programming Questions & Answers

  Design the circuit and select the appropriate components

ENG318 Project - Design the circuit and select the appropriate components for that circuit to fulfill the requirements of the device and Verify your design by MATLAB simulations.

  Use matalb to plot the computed eigenvector

Write a Matlab code implementing the inverse power method to compute approximations of the smallest eigenvalue λ1 = π2 of L and corresponding eigenfunctions. Use matalb to plot the computed eigenvector

  Demonstrate the effect of zero-padding

Write a programs in Matlab to demonstrate the effect of zero-padding and filtering the DC-component in the frequency domain.

  Calculate the integration along the given path

define the integrand using the given definition as Integrand - Find a parametric representation of C using Matlab

  Write a matlab function that would simulate rolling a biased

Write a MATLAB function that would simulate rolling a biased ("lucky") die N times; i.e., the function must return face values of N rolls of a biased die (N is a function input).

  Determine the tap settings of a three-tap

48771 - Discrete Communications - Complete the problem below AND use MATLAB to confirm your sketches, ensure you submit your MATLAB code and the plots it generates and what should be the tap parameters ai to make the resulting transfer function.

  Dimensional plot of the free surface charge

Include a qualitative dispersion curve sketch thatdemonstrates the operating point at which your Matlab plot applies.

  Function that takes as input parameters a function

Define a function that takes as input parameters a function handle f, start, and end values a and b, and a number of steps n. The function should compute and return the average value of f over the range a to b.

  Jacobian needed for newtons method

Write a Sci/Matlab function that returns the function values and the Jacobian needed for Newton's method .

  Find the potential energy stored in each spring

The potential energy stored in a spring is kx2/2, where k is the spring constant and x is the compression in the spring. The force required to compress the spring is kx. The following table gives the data for five springs:

  Analysis of the changes to the external environment

Analysis of the changes to the external environment that may, or have, influenced the value chain for Australian wine producers over the last six (6) years.

  Calculates the required input torque and the magnitudes

Calculates the required input torque (T) and the magnitudes of the pin reaction forces (A, B, and C, along both x and y directions) for two full revolutions (0 ≤ θ ≤ 4Π) of the crank.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd