Java 如何防止二维数组上的字符串重叠?

Java 如何防止二维数组上的字符串重叠?,java,Java,我正在制造战舰,目前我遇到了一个问题,我的战舰重叠。我试着加入一个if语句来判断它是否可以放置。这里有一个例子,我放置了两艘长度为3的船 public static void PlaceCruiser(String[][] board) { ThreadLocalRandom random = ThreadLocalRandom.current(); int timesplaced = 0; int size = 3; while (timesplaced < 2) { in

我正在制造战舰,目前我遇到了一个问题,我的战舰重叠。我试着加入一个if语句来判断它是否可以放置。这里有一个例子,我放置了两艘长度为3的船

public static void PlaceCruiser(String[][] board) {
ThreadLocalRandom random = ThreadLocalRandom.current();

int timesplaced = 0;
int size = 3;


while (timesplaced < 2) {
    int randomcruisercheck =(int)(Math.random()*2);
if (randomcruisercheck == 0) {
    int column = random.nextInt(0,9);
    int row = random.nextInt(0,7);
    if (row + 2 < 11 && board[row][column] == "." && board[row + 1][column] == "." && board[row + 2][column] == ".") {

        for(int i = 0; i<size; i++)
        {

            board[row+i][column] = "#";

            }
        System.out.println(board[row][column]);
        }

    timesplaced++;
    }
    else if (randomcruisercheck == 1) {
        int column = random.nextInt(0,9);
        int row = random.nextInt(0,7);
        if (column + 2 < 11 && board[row][column] == "." && board[row][column + 1] == "." && board[row][column + 2] == ".") {

            for (int i = 0; i<size; i++)
            {
                board[row][column + i] = "#";
                }
            System.out.println(board[row][column]);
        }

        timesplaced++;
    }
}
publicstaticvoidplacecruiser(字符串[][]板){
ThreadLocalRandom=ThreadLocalRandom.current();
int timesplaced=0;
int size=3;
同时(放置时间<2){
int randomcruisercheck=(int)(Math.random()*2);
如果(随机巡航检查==0){
int column=random.nextInt(0,9);
int row=random.nextInt(0,7);
如果(第2行<第11行和第2行[第2行][第列]==”&&board[第1行][第列]==”&&board[第2行][第列]=>){

对于(int i=0;i您的代码运行良好,您只需处理索引并初始化电路板:

公共类主{
公共静态字符串[][]板;
公共静态void main(字符串[]args){
PlaceCruiser pc=新PlaceCruiser();
board=新字符串[10][10];
//初始化电路板

对于(int i=0;i您的代码运行良好,您只需处理索引并初始化电路板:

公共类主{
公共静态字符串[][]板;
公共静态void main(字符串[]args){
PlaceCruiser pc=新PlaceCruiser();
board=新字符串[10][10];
//初始化电路板
对于(int i=0;i文体注释:

  • 如果您使用
    ThreadLocalRandom
    来生成位置,您还应该使用它来生成其他随机性(换句话说:
    (int)(Math.random()*2)
    更可能是
    random.nextBoolean()
    ,因为实际上布尔值可以决定船舶是水平还是垂直)
  • nextInt(0,x)
    只是
    nextInt(x)
    的一个较长变体
实际错误:

  • 由于可能存在复制粘贴问题,
    (0-“9”)和
    (0-“7”)在这两种情况下以相同的方式生成,使得放置垂直船舶时可以从阵列中索引
  • 您似乎已经注意到了,但通过
    行+2<11
    检查修复了它,该检查本身有两个问题:
  • 行+2
    最后变成
    10
    (这是
    风格备注:

    • 如果您使用
      ThreadLocalRandom
      来生成位置,您还应该使用它来生成其他随机性(换句话说:
      (int)(Math.random()*2)
      更可能是
      random.nextBoolean()
      ,因为实际上布尔值可以决定船舶是水平还是垂直)
    • nextInt(0,x)
      只是
      nextInt(x)
      的一个较长变体
    实际错误:

  • 由于可能存在复制粘贴问题,
    (0-“9”)和
    (0-“7”)在这两种情况下以相同的方式生成,使得放置垂直船舶时可以从阵列中索引
  • 您似乎已经注意到了,但通过
    行+2<11
    检查修复了它,该检查本身有两个问题:
  • 行+2
    最后变成
    10
    (即
    每次访问电路板之前,请尝试打印索引。您可能会发现这样做。您的意思是什么?除了代码之外,在比较字符串时,您应该使用
    .equals()
    Reference:@Nedward add System.out.println语句在代码中打印i和j,然后再使用它们引用数组。这是调试的最基本技巧,也是一项绝对关键的技能。这样做将帮助您发现问题。在每次访问电路板之前,请尝试打印索引。您可能会发现顺便问一下。你是什么意思?除了代码,当你比较字符串时,你应该使用
    .equals()
    Reference:@Nedward在代码中添加System.out.println语句,以便在使用i和j引用数组之前打印它们。这是调试的最基本技巧,也是一项绝对关键的技能。这样做可以帮助您发现问题。
    ..........
    ..###.....
    ..........
    ..........
    ....#.....
    ....#.....
    ....#.....
    ..........
    ..........
    ..........
    
    boolean tryPlace(int x,int y,int width,int height) {
      for(int i=0;i<height;i++) {
        for(int j=0;j<width;j++) {
          if(board[y+i][x+j]!='.') {
            return false; // ship can not be placed
          }
        }
      }
      // if we reach here, ship can be placed
      for(int i=0;i<height;i++) {
        for(int j=0;j<width;j++) {
          board[y+i][x+j]='#';
        }
      }
      return true; // ship placed successfully
    }
    
    board=new char[10][10];
    for(int i=0;i<10;i++)
      for(int j=0;j<10;j++)
        board[i][j]='.';
    
    int size=3;
    int amount=2;
    while(amount>0) {
      if(random.nextBoolean()) {
        // horizontal
        if(tryPlace(random.nextInt(10-size+1),random.nextInt(10),size,1)){
          amount--; // one placed
        }
      } else {
        // vertical
        if(tryPlace(random.nextInt(10),random.nextInt(10-size+1),1,size)){
          amount--; // one placed
        }
      }
    }
    
    // and a 4x2 mothership
    while(!(random.nextBoolean()
      ?tryPlace(random.nextInt(7),random.nextInt(9),4,2)
      :tryPlace(random.nextInt(9),random.nextInt(7),2,4)
    ));
    
    for(int i=0;i<10;i++)
      System.out.println(board[i]); // char[] has special overload for print/ln()
    
    boolean tryPlaceWithBorder(int x,int y,int width,int height) {
      for(int i=0;i<height;i++)
        for(int j=0;j<width;j++)
          if(board[y+i][x+j]!='.')
            return false; // ship can not be placed
      // if we reach here, ship can be placed
      for(int i=1;i<height-1;i++)
        for(int j=1;j<width-1;j++)
          board[y+i][x+j]='#';
      return true; // ship placed successfully
    }
    
    board=new char[12][12];
    for(int i=0;i<12;i++)
      for(int j=0;j<12;j++)
        board[i][j]='.';
    
    int size=3;
    int amount=2;
    while(amount>0) {
      if(random.nextBoolean()) {
        // horizontal
        if(tryPlaceWithBorder(random.nextInt(12-size-1),random.nextInt(10),size+2,3))
          amount--; // one placed
      } else {
        // vertical
        if(tryPlaceWithBorder(random.nextInt(10),random.nextInt(12-size-1),3,size+2)){
          amount--; // one placed
        }
      }
    }
    
    // and a 4x2 mothership
    while(!(random.nextBoolean()
      ?tryPlaceWithBorder(random.nextInt(7),random.nextInt(9),6,4)
      :tryPlaceWithBorder(random.nextInt(9),random.nextInt(7),4,6)
    ));
    
    for(int i=1;i<11;i++)
      System.out.println(String.valueOf(board[i],1,10));