简体   繁体   English

我正在尝试制作基于控制台的战舰游戏,但我不知道如何设置船只

[英]I'm trying to make a console based battleship game but I don't know how to set the ships

int randRow = rand.nextInt(rows-4);
int randColumn = rand.nextInt(columns); 

Here I check if my new ship doesn't interfere with my first ship. 在这里,我检查我的新船是否不会干扰我的第一艘船。 The entire array is filled with objects which contain a '.' 整个数组都填充了包含'。'的对象。 char except for where the first ship is char除了第一艘船的位置

if(field[randRow][randColumn].value == '.' &&
    field[randRow][randColumn+1].value == '.' &&
    field[randRow][randColumn+2].value == '.' &&
    field[randRow][randColumn+3].value == '.'){
        field[randRow][randColumn] = new Square('b');
        field[randRow+1][randColumn] = new Square('b');
        field[randRow+2][randColumn] = new Square('b');
        field[randRow+3][randColumn] = new Square('b');

} else{

If my new ship doesn't fit, I want to retry with different random values but I don't know how. 如果我的新船不合适,我想用不同的随机值重试,但我不知道如何。

Basically you are not sure how to generate the random number again. 基本上你不知道如何再次生成随机数。 Just use something like below 只需使用下面的内容

while(true) {
     //generate random number here
     if (can place battleship) {
          place battleship
          break //placed battleship so no need to retry. stop retrying
     }
     //else we will loop till we can place the battleship
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 战舰游戏 - 舰船重叠 - Battleship game - ships overlapping 我正在创建一个“猜数字”游戏,但我不知道如何重新启动部分代码 - I'm creating a “Guess the number” game, and I don't know how to restart a part of code 我正在尝试使用android界面登录php网站,但我不知道如何发送数据 - I'm trying to login in a php site with an android interface, but I don't know how to send data 我正在尝试获得组合,但我不知道该怎么做 - I'm trying to get combinations but I don't know how to do it 我正在尝试计算并返回用户更改,但我不知道如何 - I'm trying to calculate and return the user change and I don't know how 我不知道如何使我的游戏响应系统正常工作 - I don't know how to make my game response system work 我正在尝试制作一个简单的 java 处理游戏,我可以在其中从某个形状射击子弹,但它们似乎没有射击 - I'm trying to make a simple java processing game where I can shoot bullets from a shape but they don't seem to be shooting 这行得通吗? 我正在尝试制作一个多维数组,我很确定它可以在Java中工作,但是我不了解C ++ - Would this work? I'm trying to make a multi-dimensional array, and I'm pretty sure it would work in Java, but I don't know about C++ 我正在尝试用Java创建基于文本的GUI游戏。 如何使消息框更新以显示下一个结果? - I'm trying to make a text based GUI game in Java. How do I make the message boxes update to show the next outcomes? 我正在尝试从word文件中读取..我不知道我错过了哪个jar文件: - I'm trying to read from a word file .. I don't know which jar file I missed :
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM