So I have to make a battleship map that has 3 different terrains to the map. It has to have sea, land and mountains. I have the sea and land already working. I have to put mountains on only the land terrain but I have no idea how to do that? Thank you for the help. Heres my code:
import java.util.Random;
public class Map {
protected int numberOfRows; // number of rows in my map
protected int numberOfColumns; // number of columns in my map
protected int randomRow;
protected int randomColumn;
Terrain terrain [][]; // creates an instance of my terrain
public Map(int rows, int columns) { // creates a constructer
numberOfRows = rows;
numberOfColumns = columns;
terrain = new Terrain[rows][columns]; // puts my number of rows and columns in array
for(int i = 0; i < rows; i++) {
for(int k = 0; k < columns; k++) {
terrain[i][k] = new SeaTerrain(); // feels my map with all sea
}
}
addLand(); // runs my add land method
}
public void print() {
// nested for loop to print my map
for(int i =0; i < numberOfRows; i++) {
for(int j = 0; j < numberOfColumns; j++) {
System.out.print(terrain[i][j].getDisplayChar());
}
System.out.println("");
}
}
public void addLand() {
Random r = new Random();
int numberOfTimesToRun = r.nextInt(3)+2; // how many islands will be produced
int temp = 0;
while(temp <= numberOfTimesToRun) {
temp++;
try { // try catch just incase if the land exceeds my array limit
randomRow = r.nextInt(40); // picks a random coordinate for my island
randomColumn = r.nextInt(40); // picks a random coordiante for my island
int maxRowSize = 20; // max sizes for my islands
int maxColumnSize = 20;
int randomColumnSize = r.nextInt(maxColumnSize)+5;
int randomRowSize = r.nextInt(maxRowSize)+5;
terrain[randomRow][randomColumn] = new LandTerrain();
// feels my islands up with the dislay character of "+"
for(int i = 0; i < randomRowSize; i++) {
terrain[randomRow + i][randomColumn] = new LandTerrain();
for(int k = 0; k < randomColumnSize; k++) {
terrain[randomRow+i][randomColumn + k] = new LandTerrain();
}
}
} catch (Exception e) {
}
}
}
}
//heres my output
........................................
........................................
........................++++++++........
........................++++++++........
........................++++++++........
........................++++++++........
........................++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......................
..+++++++++++++++.......................
..+++++++++++++++.......................
..+++++++++++++++....++++++++...........
..+++++++++++++++....++++++++...........
..+++++++++++++++....++++++++...........
..+++++++++++++++.......................
..+++++++++++++++.......................
..+++++++++++++++.......................
..+++++++++++++++.......................
..+++++++++++++++.......................
........................................
........................................
........................................
instanceof.