1 Board m r computer will sim alate a rectangular mon board. You are required to
ID: 3911181 • Letter: 1
Question
1 Board m r computer will sim alate a rectangular mon board. You are required to use a 2-dimensional array to represent the board. The type of this array is up to the peogrammmer The wer will input the dimensions m and n at the beginning of the game, and the peogra shoud create a board of m rows ly n columax Ernoes such as noupositive lsput should be ehecedb you cam a wumethr input always two intern, nie minimum baard sine 3 3 and the maximum ?s 12 × 12. Assume that the points in the board range from (0.0) to (m-1.n-1) inclaive. Hint: Yos may find it usefal to make yr own BattleboatsBoard class that contains a ?estructor to set up the array and all the methods for setting up boats. CSCI 1933 PROJECT 2 2. BOATS 2 Boats Each boat is repeesented by a line of consecutive squares on the board. Boats may not overlap othrr boats, extend outside the game oard, or be placed dingoeally. They may be horincetal or vertical. A boat is considered "sunk" when all tbc squares of the boat have bec ??t" by the user. Examples: Valid coordinates for a boat of sie 3 ase (0,0),(0,1),(0,2)) and [( 1).21)I) Examples of ianvalid coordinates are [(0,0).(0, 1)), which is invalid because there are wot enough polnts. ((0.0),(0,2),(0,3)) is ialid becase the polnts are not consecative, (-1,0),(0.),(,0)alid becatase the first coordimate is out of bounds Finally, two boats cannot contain the same point becaase they cnot overlng. After the game board has been sized, the program should place boats randomly on the board. This requires randomly generating a cocedinate (s,y) where the boat will be placed as well as randomly choosing whether the boat should be horizontal or vertical The quaatity of boats is defined by the width and height of the game board. All boats should be of length 3 Recall the smallest boord is 3 x 3 and the largenst boand is 12 x 12 Smallest Dimension Boat Quantity vsat? “ 3 or height .. 3 width 5 or 3Explanation / Answer
/*Board*/
import java.io.*;
import java.util.Scanner;
public class Num_1
{
/*main method */
public static void main (String[] args)
{
Scanner scanner = new Scanner (System.in);
int m;
int n;
System.out.println("Enter number of rows: ");
m= scanner.nextInt();
System.out.println ("Now enter the number of columns: ");
n= scanner.nextInt();
if(m<0||n<0)
{
System.Out.Println("please enter positive values");
}
else
{
if(m<3||n<3||m>12||n>12)
{
System.Out.Println("minimum rows and columns are 3 maximum are 12");
}
}
int [][] matrix = new int [m] [n];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println("Enter the number for row " + i + " and column " + j + " : ");
matrix [i][j] = scanner.nextInt();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.