所以我必须制作一张有3种不同地形的战舰地图。它必须有海,陆和山。我有大海和陆地已经在工作。我只能把山脉放在陆地上,但我不知道怎么做?感谢您的帮助。 继承我的代码:
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) {
}
}
}
}
//继承我的输出
........................................
........................................
........................++++++++........
........................++++++++........
........................++++++++........
........................++++++++........
........................++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......++++++++........
..+++++++++++++++.......................
..+++++++++++++++.......................
..+++++++++++++++.......................
..+++++++++++++++....++++++++...........
..+++++++++++++++....++++++++...........
..+++++++++++++++....++++++++...........
..+++++++++++++++.......................
..+++++++++++++++.......................
..+++++++++++++++.......................
..+++++++++++++++.......................
..+++++++++++++++.......................
........................................
........................................
........................................
答案 0 :(得分:2)
首先,你要为每张地图分配水,
第二,你在水面上创造了随机大小的岛屿。
现在你想要做几乎相同的事情,在陆地上添加山脉(不像以前那样在水面上)......对吗?
我有两个想法,第一个应该更快
方法1 每次你创建一个岛屿时,当你添加landTerrains时,你会抛出一个骰子(random.nextInt()),如果它在某个值范围内,你可以改为使用MountainTerrain。
// 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++) {
if(r.nextInt(10)>8){ //10% probability
terrain[randomRow+i][randomColumn + k] = new MountainTerrain();
}
}
}
方法2 你应该做的事情与添加土地时一样,但这次每次你要添加一座山时,只要问一下那块特定的地图(或矩阵元素)是否已落地。
boolean addedMountain=false;
int i = r.nextInt(numberOfRows);
int j = r.nextInt(numberOfColumns);
while(addedMountain==false){
if(terrain[i][k].getType()=="Land"){ //just to give you an idea
terrain[i][k] = new MountainTerrain();
addedMountain=true;
}
else{
//try again?
i = r.nextInt(limit);
j = r.nextInt(limit);
}
}
你需要为你想要添加的每座山都这样做。
我认为一座山可能只是一个矩阵元素,对吧?但如果您的山脉需要是块(多个矩阵元素),您可以从随机选择的像素中生长它们,直到您遇到水。