2014-04-17 65 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

建議。生成所有座標的列表。將列表隨機排序。現在從他們的洗牌順序中選擇列表中的座標。將其從列表中拉出後,檢查主柵格上每個座標的「〜」。

混洗列表保證每個座標只被選取一次。