我正在尝试用java编写一个tic tac toe游戏。在这个游戏中有两个玩家作为A和B.玩家A总是开始游戏(X), 而玩家B是(O)。我使用随机方法生成移动数字。
所以,我创建了一个while循环,但它总是从头开始,但我想打印一个带有最新移动的新板。
下面有代码,如果你看一下并说出我错过了什么,我会很高兴。
单数也属于玩家A,复数属于玩家B. 我必须为whoTurn添加+1才能正确移动(如果随机方法尝试将X或O写入已经采用的位置,则必须生成另一行和列编号,但我也无法弄清楚)< / p>
import java.util.Random;
public class TicTacToe {
final static int SIZE = 5;
private static int row;
private static int column;
static Random number = new Random();
static char [][] array = new char[SIZE][SIZE];
public static void createBoard(){
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array.length; j++) {
array[i][j] = '#';
}
}
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("--------------");
}
public static void move(){
int whoseTurn =1;
while(whoseTurn <= (SIZE * SIZE)) {
row = number.nextInt(6-1) + 1;
column = number.nextInt(6-1) + 1;
if ((whoseTurn % 2) != 0) {
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array.length; j++){
array[i][j] = '#';
array[row-1][column-1] = 'X';
}
}
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array.length; j++){
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println("--------------");
whoseTurn +=1;
} else if((whoseTurn % 2) == 0){
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array.length; j++){
array[i][j] = '#';
array[row-1][column-1] = 'O';
System.out.print(array[i][j] + " ");
}
System.out.println();
}
whoseTurn += 1;
}
}
}
答案 0 :(得分:0)
你用#和用户的一次移动(即0或X)循环重新填充用户的每次移动。只需更换板中的一个位置而不是修改整个板,所有之前的移动都是安全并将被打印。
import java.util.Random;
public class TicTacToe {
final static int SIZE = 5;
private static int row;
private static int column;
static Random number = new Random();
static char[][] array = new char[SIZE][SIZE];
public static void createBoard() {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
array[i][j] = '#';
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("--------------");
}
public static void move() {
int whoseTurn = 1;
while (whoseTurn <= (SIZE * SIZE)) {
row = number.nextInt(6 - 1) + 1;
column = number.nextInt(6 - 1) + 1;
if ((whoseTurn % 2) != 0) {
array[row - 1][column - 1] = 'X';
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println("--------------");
whoseTurn += 1;
} else if ((whoseTurn % 2) == 0) {
array[row - 1][column - 1] = 'O';
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println("--------------");
whoseTurn += 1;
}
}
}
}
答案 1 :(得分:0)
您需要将表状态保留在某处。
可以这样做:
您创建一个包含符号坐标的Pair类:
public class PairList extends ArrayList<Pair>{
/**
*
*/
private static final long serialVersionUID = -7047628156984387721L;
public boolean contains(int x, int y) {
for (Pair pair : this) {
if (pair.getX() == x && pair.getY() == y) {
return true;
}
}
return false;
}
}
然后,您可以创建自己的ArrayList实现,以便检查历史记录是否已经更容易包含坐标:
import java.util.Random;
public class TicTacToe {
final static int SIZE = 5;
private static int row;
private static int column;
static Random number = new Random();
static char[][] array = new char[SIZE][SIZE];
public static void createBoard() {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
array[i][j] = '#';
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("--------------");
}
public static void move() {
int whoseTurn = 1;
PairList boardStatusX = new PairList();
PairList boardStatusY = new PairList();
while (whoseTurn <= (SIZE * SIZE)) {
do {
row = number.nextInt(6 - 1) + 1;
column = number.nextInt(6 - 1) + 1;
} while (boardStatusX.contains(row, column) || boardStatusY.contains(row, column));
if ((whoseTurn % 2) != 0) {
boardStatusX.add(new Pair(row, column));
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (boardStatusX.contains(i, j)) {
array[i][j] = 'X';
}
else {
array[i][j] = '#';
}
}
}
//array[row - 1][column - 1] = 'X';
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println("--------------");
whoseTurn += 1;
}
else if ((whoseTurn % 2) == 0) {
boardStatusY.add(new Pair(row, column));
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (boardStatusY.contains(i, j)) {
array[i][j] = 'Y';
}
else {
array[i][j] = '#';
}
}
}
//array[row - 1][column - 1] = 'Y';
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println("--------------");
whoseTurn += 1;
}
}
}
}
最后,你的TicTacToe类看起来像这样:
private String ReadArabic() {
String words="";
try {
InputStream stream = getAssets().open("arabwords.txt");
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
words = new String(buffer, "cp1256");
words = words.replaceAll("(\\r|\\n)", "");
} catch (IOException e) {
// Handle exceptions here
}
return words;
}
答案 2 :(得分:0)
这是一个有效的简单解决方案,并检查该字段是否已被占用(放置&#39; X&#39;或&#39; Y&#39;)因此它不会覆盖现有的X和O.
它基于@ melanzane的答案:
import java.util.Random;
public class TicTacToe {
final static int SIZE = 5;
private static int row;
private static int column;
static Random number = new Random();
static char[][] array = new char[SIZE][SIZE];
public static void createBoard() {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
array[i][j] = '#';
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("--------------");
}
public static void move() {
int whoseTurn = 1;
while (whoseTurn <= (SIZE * SIZE)) {
do {
row = number.nextInt(6 - 1) + 1;
column = number.nextInt(6 - 1) + 1;
} while (array[row][column] == 'X' || array[row][column] == 'Y');
if ((whoseTurn % 2) != 0) {
array[row - 1][column - 1] = 'X';
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println("--------------");
whoseTurn += 1;
} else if ((whoseTurn % 2) == 0) {
array[row - 1][column - 1] = 'O';
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println("--------------");
whoseTurn += 1;
}
}
}
}