我几乎完成了这项作业,但我的输出有一些打印错误。
游戏是功能性的,但方法printOBoard
和printXBoard
存在问题,在移动时它会弄乱游戏板。游戏进行的时间越长,问题就越严重。谢谢你的帮助。
import java.util.Scanner;
public class TicTacToe {
private enum Tiles {
X, O, EMPTY;
}
public static void main(String[] args) {
int i;
int j;
//initialize 2d array of enum types called "board"
Tiles[][] board = new Tiles[3][3];
for (i=0; i<board.length; i++)
for (j=0; j<board.length; j++)
board[i][j] = Tiles.EMPTY;
//print out an empty board as a 2d array, with each tile set as "EMPTY"
printBoard(board);
int row, col;
int countEmpty=0;
//initial countEmpty count, if it's less than 1, the board is full and the game is over.
for (i=0; i<board.length; i++)
for (j=0; j<board.length; j++)
if (board[i][j] == Tiles.EMPTY)
countEmpty++;
while (countEmpty > 0) {
//Player O enters the row coordinate
System.out.println("Player O's turn.");
System.out.println("Player O: Enter row (0, 1, or 2):");
Scanner stdin1 = new Scanner(System.in);
row = stdin1.nextInt();
//Player O enters the column coordinate
System.out.println("Player O: Enter column (0, 1, or 2):");
Scanner stdin2 = new Scanner(System.in);
col = stdin2.nextInt();
//If the tile is empty, it was a valid move, and an 'O' is placed on the spot.
if (board[row][col] == Tiles.EMPTY) {
board[row][col] = Tiles.O;
//MOVE FOR O ********************
printOBoard(board, row, col);
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
//If the tile is not empty, it was not a valid move, and Player O is prompted to try again.
else {
System.out.println("Space already occupied. Please choose another.");
System.out.println("Player O's turn.");
//Player 0 enters the row coordinate
System.out.println("Player O: Enter row (0, 1, or 2):");
stdin1 = new Scanner(System.in);
row = stdin1.nextInt();
//Player O enters the column coordinate
System.out.println("Player O: Enter column (0, 1, or 2):");
stdin2 = new Scanner(System.in);
col = stdin2.nextInt();
//ERROR MOVE FOR O********************
board[row][col] = Tiles.O;
printOBoard(board, row, col);
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
//Player X enters the row coordinate
System.out.println("Player X's turn.");
System.out.println("Player X: Enter row (0, 1, or 2):");
Scanner stdin3 = new Scanner(System.in);
row = stdin3.nextInt();
//Player X enters the column coordinate
System.out.println("Player X: Enter column (0, 1, or 2):");
Scanner stdin4 = new Scanner(System.in);
col = stdin4.nextInt();
if (board[row][col] == Tiles.EMPTY) {
board[row][col] = Tiles.X;
printXBoard(board, row, col);
//MOVE FOR X *************************************************
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
else {
System.out.println("Space already occupied. Please choose another.");
System.out.println("Player O's turn.");
System.out.println("Player O: Enter row (0, 1, or 2):");
stdin3 = new Scanner(System.in);
row = stdin3.nextInt();
//Player O enters the column coordinate
System.out.println("Player O: Enter column (0, 1, or 2):");
stdin4 = new Scanner(System.in);
col = stdin4.nextInt();
board[row][col] = Tiles.O;
//ERROR MOVE FOR X ****************************************
printXBoard(board, row, col);
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
//After both players move, we check to see if the board is full.
countEmpty = 0;
for (i=0; i<board.length; i++)
for (j=0; j<board.length; j++)
if (board[i][j] == Tiles.EMPTY)
countEmpty++;
}
}
//method printBoard prints out a grid of EMPTY's and returns nothing
private static void printBoard(Tiles board[][]) {
int i, j;
System.out.println(" -----------------------------");
System.out.println("| | | |");
for (i=0; i<board.length; i++){
for (j=0; j<board.length; j++){
System.out.printf("| " + board[i][j] + " ");
}
System.out.println("|");
System.out.println("| | | |");
System.out.println(" -----------------------------");
if (i<2)
System.out.println("| | | |");
}
return;
}
//method printXBoard prints out the grid modified with the addition of an X after Player X's turn
private static void printXBoard(Tiles board[][], int curRow, int curCol) {
int i, j;
System.out.println(" -----------------------------");
System.out.println("| | | |");
for (i=0; i<board.length; i++){
for (j=0; j<board.length; j++){
if (i == curRow && j == curCol)
board[i][j] = Tiles.X;
else
;
System.out.printf("| " + board[i][j] + " ");
}
System.out.println("|");
System.out.println("| | | |");
System.out.println(" -----------------------------");
if (i<2)
System.out.println("| | | |");
}
return;
}
//method printOBoard prints out the grid modified with the addition of an X after Player X's turn
private static void printOBoard(Tiles board[][], int curRow, int curCol) {
int i, j;
System.out.println(" -----------------------------");
System.out.println("| | | |");
for (i=0; i<board.length; i++){
for (j=0; j<board.length; j++){
if (i == curRow && j == curCol)
board[i][j] = Tiles.O;
else
;
System.out.printf("| " + board[i][j] + " ");
}
System.out.println("|");
System.out.println("| | | |");
System.out.println(" -----------------------------");
if (i<2)
System.out.println("| | | |");
}
return;
}
//method checkWin checks all possible winning combinations for both players.
private static int checkWin(Tiles board[][]) {
if (board[0][0] == Tiles.X && board[0][1] == Tiles.X && board[0][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[0][0] == Tiles.X && board[1][1] == Tiles.X && board[2][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[0][0] == Tiles.X && board[1][0] == Tiles.X && board[2][0] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[1][0] == Tiles.X && board[1][1] == Tiles.X && board[1][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[2][0] == Tiles.X && board[2][1] == Tiles.X && board[2][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[0][1] == Tiles.X && board[1][1] == Tiles.X && board[2][1] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[0][2] == Tiles.X && board[1][2] == Tiles.X && board[2][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
else if (board[2][0] == Tiles.X && board[1][1] == Tiles.X && board[0][2] == Tiles.X){
System.out.println("Player X wins!");
return 1;
}
//check if player O wins
else if (board[0][0] == Tiles.O && board[0][1] == Tiles.O && board[0][2] == Tiles.O){
System.out.println("Player O wins!");
return 0;
}
else if (board[0][0] == Tiles.O && board[1][1] == Tiles.O && board[2][2] == Tiles.O){
System.out.println("Player O wins!");
return 0;
}
else if (board[0][0] == Tiles.O && board[1][0] == Tiles.O && board[2][0] == Tiles.O){
System.out.println("Player O wins!");
return 0;
}
else if (board[1][0] == Tiles.O && board[1][1] == Tiles.O && board[1][2] == Tiles.O){
System.out.println("Player O wins!");
return 0;
}
else if (board[2][0] == Tiles.O && board[2][1] == Tiles.O && board[2][2] == Tiles.O) {
System.out.println("Player O wins!");
return 0;
}
else if (board[0][1] == Tiles.O && board[1][1] == Tiles.O && board[2][1] == Tiles.O) {
System.out.println("Player O wins!");
return 0;
}
else if (board[0][2] == Tiles.O && board[1][2] == Tiles.O && board[2][2] == Tiles.O){
System.out.println("Player O wins!");
return 0;
}
else if (board[2][0] == Tiles.O && board[1][1] == Tiles.O && board[0][2] == Tiles.O) {
System.out.println("Player O wins!");
return 0;
}
else
return 2;
}
}
答案 0 :(得分:0)
第二次尝试放置X时出现问题(如果空间被O占用),您从O复制粘贴的代码并且没有更改播放器&amp;瓦。
System.out.println("Player O's turn.");
board[row][col] = Tiles.O;
阅读这段代码很痛苦 - _ - 但最终你会变得更好;)。
答案 1 :(得分:0)
为什么在将信件打印到你的主板上时(我在我的Mac上运行)发生这种情况的原因是当你打印这些字母时,你正在使用println ...因为你'如果每个块都有一定的空间,你应该使用printf ...例如:
System.out.printf("%10s", LetterVariableForTurn);
这将为LetterVariableForTurn提供10个文本空间。
这是一个可以为您提供更多帮助的链接:printf
希望这有帮助!