2014-04-17 63 views
0

我正在为一个班级编写一艘战舰游戏,现在我正在为它工作。它有时是完美的,但有时当它猜测一个随机数时,它会不断猜测它,我不知道为什么。我现在根据最小活着的船的大小猜测坐标。 我还在检查坐标是否已被猜测,并且我假设这是问题所在。 g2是一个二维数组,包含猜测是否已命中或未命中。 〜代表没有想到的。如何纠正此Java无限随机数生成?

这是我的网格当它卡在循环中时的样子。

\ 0 1 2 3 4 5 6 7 8 9 
------------------------------ 
a| M ~ M ~ H H H H M ~ 
b| ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 
c| M ~ M ~ M ~ M ~ M ~ 
d| ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 
e| M ~ M ~ M ~ M ~ M ~ 
f| ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 
g| M ~ M ~ M ~ M ~ M ~ 
h| ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 
i| M ~ M ~ M ~ M ~ M ~ 
j| ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 

这是我的循环选择它的坐标。

while (true) { 
      a = rn.nextInt(10); 
      while (a % parity != 0) { 
       a++; 
       if (a > 9) 
        a = 0; 
      } 
      n = rn.nextInt(10); 
      while (n % parity != 0) { 
       n++; 
       if (n > 9) 
        n = 0; 
      } 

       if (g2[a][n] == '~') { //ensures coordinate has not been guessed already 
        break; 
       } 
      System.out.println((let[a]) + "" + n); //displays coordinate if guessed already 
     } 

根据要求,这是一个更大的上下文代码片段。 lastHit是此命中模式中最后一次击中船的坐标。 firstHit是第一次击中此Hit模式的坐标。

此外,当我把这些从我的游戏主循环,我使用:

p1.fireResult(p2.fireUpon(p1.fire())); 

其中P1和P2播放器的对象。

import java.util.Random; 

public class AIPlayer_ndk22 implements Player { 
Random rn = new Random(); 
char[] let = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }; 
int n; 
int a; 
char[][] g1 = new char[10][10]; 
char[][] g2 = new char[10][10]; 
int[] shipHits = new int[5]; 
Coordinate last; 
Coordinate lastHit, firstHit; 
boolean hitModechan = false; 
int parity = 2, miss = 0; 

/** 
* This player is being fired upon. Given a coordinate, updates board(s) accordingly. 
* 
* @param x - the coordinate that is being fired upon 
* @return M for miss, otherwise the ship's char representation 
*/ 
public char fireUpon(Coordinate c) { 
    if (g1[c.x][c.y] == '~') { 
     return 'M'; 
    } else { 
     return g1[c.x][c.y]; 
    } 
} 

/** 
* Returns a coordinate that this player wishes to guess. 
* 
* 
* @return A coordinate object 
*/ 
public Coordinate fire() { 
    Coordinate coords; 
    if (!hitModechan) { 
     while (true) { 
      a = rn.nextInt(10); 
      try { 
       Thread.sleep(1); 
      } catch(Exception e){} 
      while (a % parity != 0) { 
       a++; 
       if (a > 9) 
        a = 0; 
      } 
      try { 
       Thread.sleep(1); 
      } catch(Exception e){} 
      n = rn.nextInt(10); 
      while (n % parity != 0) { 
       n++; 
       if (n > 9) 
        n = 0; 
      } 

       if (g2[a][n] == '~') { //ensures coordinate has not been guessed already 
        break; 
       } 
      System.out.print((let[a]) + "" + n + " "); 
      System.out.println(g2[a][n]); 
     } 
    } else { 
     if (miss == 0) { 
      if (lastHit.x > 0 && g2[(lastHit.x) - 1][lastHit.y] == '~') { 
       a = (lastHit.x) - 1; 
       n = lastHit.y; 
      } else { 
       miss++; 
      } 
     } 
     if (miss == 1) { 
      if (lastHit.y < 9 && g2[(lastHit.x)][lastHit.y + 1] == '~') { 
       a = lastHit.x; 
       n = (lastHit.y) + 1; 
      } else 
       miss++; 
     } 
     if (miss == 2) { 
      if (lastHit.y > 0 && g2[(lastHit.x)][lastHit.y - 1] == '~') { 
       a = lastHit.x; 
       n = (lastHit.y) - 1; 
      } else 
       miss++; 
     } 
     if (miss == 3) { 
      if (lastHit.x < 9 && g2[(lastHit.x) + 1][lastHit.y] == '~') { 
       a = (lastHit.x) + 1; 
       n = lastHit.y; 
      } else 
       miss++; 
     } 
     if (miss == 4) { 
      miss = 0; 
      if (miss == 0) { 
       if (firstHit.x > 0 && g2[(firstHit.x) - 1][firstHit.y] == '~') { 
        a = (firstHit.x) - 1; 
        n = firstHit.y; 
       } else { 
        miss++; 
       } 
      } 
      if (miss == 1) { 
       if (firstHit.y < 9 && g2[(firstHit.x)][firstHit.y + 1] == '~') { 
        a = firstHit.x; 
        n = (firstHit.y) + 1; 
       } else 
        miss++; 
      } 
      if (miss == 2) { 
       if (firstHit.y > 0 && g2[(firstHit.x)][firstHit.y - 1] == '~') { 
        a = firstHit.x; 
        n = (firstHit.y) - 1; 
       } else 
        miss++; 
      } 
      if (miss == 3) { 
       if (firstHit.x < 9 && g2[(firstHit.x) + 1][firstHit.y] == '~') { 
        a = (firstHit.x) + 1; 
        n = firstHit.y; 
       } 
      } else { 
       hitModechan = false; 
       while (true) { 
        a = rn.nextInt(10); 
        while (a % parity != 0) { 
         a++; 
         if (a > 9) 
          a = 0; 
        } 
        n = rn.nextInt(10); 
        while (n % parity != 0) { 
         n++; 
         if (n > 9) 
          n = 0; 
        } 
        if (g2[a][n] == '~') { //ensures coordinate has not been guessed already 
         break; 
        } 
       } 
      } 
     } 
    } 
    System.out.printf("AI guesses at: %c%d\n", let[a], n); 
    coords = new Coordinate(a, n); 
    last = coords; 
    return coords; 
} 

/** 
* Callback method to notify player whether last fire() attempt was successful or not. 
* 
* @param result 'M' if the last fire() resulted in a miss, otherwise the character code of the ship 
*/ 
public void fireResult(char result) { 
    if (result == 'M') { 
     g2[last.x][last.y] = 'M'; 
     System.out.println("AI Miss"); 
     if (hitModechan) { 
      miss++; 
      if (miss == 4) { 
       lastHit = firstHit; 
       miss = 0; 
      } 
     } 
    } else { 
     if (!hitModechan) { 
      firstHit = new Coordinate(last.x, last.y); 
      lastHit = firstHit; 
      hitModechan = true; 
     } else { 
      miss = 0; 
      lastHit = new Coordinate(last.x, last.y); 
     } 
     g2[last.x][last.y] = 'H'; 
     System.out.println("AI Hit"); 
     if (result == 'P') { 
      shipHits[0]++; 
      if (shipHits[0] == 2) { 
       System.out.println("Patrol Boat Sunk."); 
       hitModechan = false; 
       parity = 3; 
      } 
     } else if (result == 'S') { 
      shipHits[1]++; 
      if (shipHits[1] == 3) { 
       System.out.println("Submarine Sunk."); 
       hitModechan = false; 
       if (shipHits[2] == 3 && shipHits[0] == 2) { 
        parity = 4; 
       } 
      } 
     } else if (result == 'D') { 
      shipHits[2]++; 
      if (shipHits[2] == 3) { 
       System.out.println("Destroyer Sunk."); 
       hitModechan = false; 
      } 
     } else if (result == 'B') { 
      shipHits[3]++; 
      if (shipHits[3] == 4) { 
       System.out.println("Battleship Sunk."); 
       hitModechan = false; 
      } 
     } else { 
      shipHits[4]++; 
      if (shipHits[4] == 5) { 
       System.out.println("Carrier Sunk."); 
       hitModechan = false; 
      } 
     } 
    } 
} 
+0

也许g2 [a] [n] =='〜'永远不会是真的。核实。 –

+0

或者你没有正确更新g2,所以即使在猜测了一个坐标后,该坐标的g2仍然是'〜'。 – matt

+0

你试图通过用'parity'进行修改来将猜测与船的长度相匹配? – ooga

回答

0

这是我实施平价的问题。 固定代码:

while (true) { 
      a = rn.nextInt(10); 
      /*while (a % parity != 0) { 
       a++; 
       if (a > 9) 
        a = 0; 
      }*/ 
      n = rn.nextInt(10); 
      while ((n + a)% parity != 0) { 
       n++; 
       if (n > 9) 
        n = 0; 
      } 

       if (g2[a][n] == '~') { //ensures coordinate has not been guessed already 
        break; 
       } 
      System.out.print((let[a]) + "" + n + " "); 
      System.out.println(g2[a][n]); 
      printBoard(); 
     } 
0

建议。生成所有坐标的列表。将列表随机排序。现在从他们的洗牌顺序中选择列表中的坐标。将其从列表中拉出后,检查主栅格上每个坐标的“〜”。

混洗列表保证每个坐标只被选取一次。