我正在为一堂课写一艘战列舰游戏,而我现在正在为AI工作。它有时会很完美,但有时当它猜测一个随机数时,它会无限地猜测它,我不知道为什么。我目前根据最小的活船的大小猜测坐标。 我还要检查坐标是否已经猜到了,我假设这就是问题所在。 g2是一个二维数组,包含猜测是否已经命中或未命中。 〜代表没有问题。
当我的网格陷入循环时,这就是我的网格。
\ 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是第一次命中此命中模式的坐标。
另外,当我从主游戏循环中调用它们时,我使用:
p1.fireResult(p2.fireUpon(p1.fire()));
其中p1和p2是Player对象。
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 :(得分: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();
}
答案 1 :(得分:0)
一个建议。生成所有坐标的列表。将列表随机顺序洗牌。现在以洗牌顺序从列表中选择坐标。将其从列表中拉出后,检查主网格上每个坐标是否为“〜”。
混洗列表保证每个坐标只被选中一次。