首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java小程序游戏

Java小程序游戏
EN

Stack Overflow用户
提问于 2017-03-08 09:06:17
回答 2查看 114关注 0票数 0

我已经开始为像战舰这样的游戏制作一个java applet。这就是我想要做的:

建立一个10x10的棋盘,随机选取10个坐标作为“船”的位置。用户通过点击棋盘,放置白色的钉子表示“未命中”,红色的钉子表示“命中”,来猜测发货位置。一旦用户点击最后一条“船”,程序就会写一条获胜的消息,其中包括它进行的猜测和退出的次数。

我已经制作了10x10的电路板,并将注册表钉的随机位置存储在ArrayList中。现在,它允许输出隐藏红点的坐标供我测试,并在它们被击中时放置它们。如果它没有命中,它允许我放一个白点。我如何让它计算它需要多少点击,最后如果所有的船都装满了或者所有的10艘船都找到了,让它输出游戏结束和点击的数量?我们非常感谢您的帮助。下面是我的一段代码:

代码语言:javascript
复制
 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());
    } 
  }
 }
}
EN

回答 2

Stack Overflow用户

发布于 2017-03-08 09:17:12

由于某些原因,您在此代码中的ships上有两个单独的循环:

代码语言:javascript
复制
for(Coordinate c: ships){

代码语言:javascript
复制
for (int i = 0; i < ships.size(); i++) {

去掉外面的那个。它没有做任何有用的事情,如果currentClick不在ships中的某个位置,那么与它相关的if语句会跳过该循环中的所有代码。

票数 0
EN

Stack Overflow用户

发布于 2017-03-08 09:57:46

看起来你只需要稍微清理一下,让事情更容易分类:

代码语言:javascript
复制
// 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());
  } 
}

为了提高清晰度,您可以将红色复选标记放入自己的函数中:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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());
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42661185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档