【问题标题】:Java Applet GameJava小程序游戏
【发布时间】:2017-03-10 07:21:44
【问题描述】:

我已经开始为战舰之类的游戏制作 Java 小程序。这是我想要做的:

构建一个 10x10 的棋盘,随机选择 10 个“船”所在的坐标。用户通过点击板来猜测船舶位置,放置白色钉子代表“未命中”,红色钉子代表“命中”。 一旦用户点击了最后一个“船”,程序就会写入一条获胜消息,其中包括它所猜测的次数并退出。

我已经制作了 10x10 板,并将 reg 钉的随机位置存储在 ArrayList 中。现在,它允许输出隐藏红点的坐标供我测试,并在它们被击中时放置它们。如果没有击中,它可以让我放一个白点。我如何让它计算它需要多少次命中,最后如果全部填满或找到所有 10 艘船,让它输出游戏结束和命中数?任何帮助表示赞赏,谢谢。这是我的代码的一部分:

 Boolean isHit = false;

 while(unWon && totalClicks <= 100) {
 isHit = false; // reset isHit
 Coordinate currentClick = board.getClick(); // Get the current click

 //Check the ship coordinates to see whether it is hit
 for(Coordinate c: ships) {
   if(c.getRow() == currentClick.getRow() && c.getCol() == currentClick.getCol()) {
     board.putPeg("red", currentClick.getRow(), currentClick.getCol());
     isHit = true;
     break;          
   }
 }

 // If it didn't hit, mark it with a white peg
 if (!isHit) {
   board.putPeg("white", currentClick.getRow(), currentClick.getCol());
    } 
  }
 }
}

【问题讨论】:

  • 请不要破坏您的内容。一旦发布到网站,它就被授权给该网站并“属于社区”。

标签: java if-statement


【解决方案1】:

由于某种原因,您在这段代码中有两个独立的循环 ships

for(Coordinate c: ships){

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

摆脱外部的。它没有做任何有用的事情,如果currentClick 不在ships 中的某个位置,则与之相关的if 语句会使循环“内部”的所有代码都被跳过。

【讨论】:

  • 那段代码在编辑后会是什么样子?我需要对 if 语句进行哪些更改?
【解决方案2】:

您似乎只需要稍微清理一下,就可以更轻松地进行整理:

// This will be used to track whether any of the ship coordinates is a match for currentClick
Boolean isHit = false;

// It looks like you can combine these two conditions, but if that changes, just put `totalClicks <= 100` in its own `if` statement
while(unWon && totalClicks <= 100) {
  // reset isHit
  isHit = false;
  // Get the current click
  Coordinate currentClick = board.getClick();
  // Check the ship coordinates to see whether we hit
  for(Coordinate c: ships) {
    if(c.getRow() == currentClick.getRow() && c.getCol() == currentClick.getCol()) {
      board.putPeg("red", currentClick.getRow(), currentClick.getCol());
      isHit = true;
      break;          
    }
  }
  // If we didn't hit, mark it with white
  if (!isHit) {
    board.putPeg("white", currentClick.getRow(), currentClick.getCol());
  } 
}

为了提高清晰度,您可以将红色检查放入它自己的函数中:

Boolean isHit(Coordinate currentClick, ArrayList<Coordinate> ships) {
  for(Coordinate c: ships) {
    if(c.getRow() == currentClick.getRow() && c.getCol() ==  currentClick.getCol()) {
    return = true;
  }
  return false;
}

然后您可以摆脱 isHit 布尔值并重写您的 while:

while(unWon && totalClicks <= 100) {
  Coordinate currentClick = board.getClick();
  if (isHit(currentClick, ships)) {
    board.putPeg("red", currentClick.getRow(), currentClick.getCol());
  } else {
    board.putPeg("white", currentClick.getRow(), currentClick.getCol());
  }
}

【讨论】:

  • 第一部分有效,但提高清晰度是什么意思。我不太明白你在第二和第三部分做了什么。感谢您的帮助,因为它现在允许我单击白点。
  • 第二部分将当前点击是否命中的测试拆分为它自己的函数,它允许您将 while 循环作为第三部分。
  • 好的,知道了。谢谢你的帮助! @adamdc78
猜你喜欢
  • 2015-11-25
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-07
  • 2012-05-21
  • 2014-10-29
相关资源
最近更新 更多