我正在编写一个创建游戏tic tac toe的程序。现在的应用程序有两个人互相攻击,轮流输入行和列号。我在这个程序中遇到的主要问题是我无法显示一个人是赢还是输两个球员的结果。如果有人有任何关于如何解决这个问题的建议会很棒。感谢你的宝贵时间。
import java.util.Scanner;
public class ticTacToeApp {
// 1. GAME CONSTANTS
static final int WIN = 1;
static final int LOSE = 0;
static final int TIE = 2;
static final int GAME_IN_PROGRESS = 3;
static final String PLAYER = "X";
static final String OPPONENT = "O";
static final String EMPTY = " ";
// 2. INPUT STREAM
static Scanner in;
public static void main(String[] args) {
// 3. BUILD THE TIC TAC TOE 3X3 BOARD OF STRINGS
String[][] board = { { EMPTY, EMPTY, EMPTY }, { EMPTY, EMPTY, EMPTY },{EMPTY, EMPTY, EMPTY } };
// 4. INSTANTIATE THE SCANNER FOR INPUT
in = new Scanner(System.in);
// 5. GAME ENGINE
drawBoard(board);
int moveResult = GAME_IN_PROGRESS; // STATUS OF THE GAME AND AFTER A
// MOVE
while (moveResult == GAME_IN_PROGRESS) {
// PLAYER MOVES AND BOARD IS CHECKED FOR A RESULT
getMove(board, PLAYER, in);
drawBoard(board);
if (moveResult != GAME_IN_PROGRESS)
break;
// OPPONENT MOVES AND THE BOARD IS CHECKED FOR A RESULT
getMove(board, OPPONENT, in);
drawBoard(board);
moveResult = boardResults(board);
}
// 6. ONCE THE GAME HAS ENDED, DISPLAY IT IS AS A WIN, LOSE, OR TIE.
if (moveResult == WIN)
System.out.println("You win.");
else if (moveResult == LOSE)
System.out.println("You lost.");
else
System.out.println("You tied.");
}
public static int boardResults(String[][] board) {
// TASK 1: BUILD AN ARRAY CONTAINING ALL THE ROW, COLUMN, AND
// DIAGONAL STRING ARRANGEMENTS ON THE CURRENT
// TIC TAC TOE BOARD.
String[] waysToWin = new String[8];
int i = 0; // INDEX TO wayToWin
for (int r = 0; r < 3; r++) {
String str = " ", stc = " ";
for (int c = 0; c < 3; c++) {
str += board[r][c];
stc += board[c][r];
}
waysToWin[i++] = str;
waysToWin[i++] = stc;
// ADD 2 DIAGONALS
waysToWin[i++] = board[0][0] + board[1][1] + board[2][2];
waysToWin[i++] = board[0][2] + board[1][1] + board[2][0];
// TASK 2. CHECK IF ANY OF THESE ARRANGEMENTS CONTAIN A WINNING
// "XXX" OR
// "OOO"
// NOTE: AN "XXX" IS WIN AND AN "OOO" IS LOSE.
for (int p = 0; p < 8; p++) {
if (waysToWin[p].equals("XXX"))
return WIN;
if (waysToWin[p].equals("OOO"))
return LOSE;
// TASK 3. CHECK IF THE BOARD IS FULL (TIE) OR IF THE GAME IS
// STILL IN
// PROGRESS
if (board[0][0] == EMPTY || board[0][1] == EMPTY || board[0][2] == EMPTY || board[1][0] == EMPTY
|| board[1][1] == EMPTY || board[1][2] == EMPTY || board[2][0] == EMPTY || board[2][1] == EMPTY
|| board[2][2] == EMPTY)
return GAME_IN_PROGRESS;
}
}
return i;
}
public static void drawBoard(String[][] board) {
for (int row = 0; row < board.length; row++) {
System.out.println("___________");
for (int col = 0; col < board[row].length; col++) {
System.out.print("|" + board[row][col] + " ");
}
System.out.println("| ");
}
System.out.println("___________");
}
public static void getMove(String[][] board, String whoseMove, Scanner in) {
int[] xy = new int[2];
for (;;) {
System.out.print("You are " + whoseMove + ". ");
System.out.println("Enter the row and column of your move: ");
xy[0] = in.nextInt();
xy[1] = in.nextInt();
if (board[xy[0]][xy[1]].equals(EMPTY))
break;
System.out.println("You must choose an empty space.");
}
board[xy[0]][xy[1]] = whoseMove;
}
}
答案 0 :(得分:2)
我会在电路板上使用单维数组,并创建一系列获胜组合,并将其编入电路板。然后循环获胜组合并计算X和O.如果你有3,那么宣布获胜者。这是一个片段演示:
public class ticTacToeApp {
static final String X = "X";
static final String O = "O";
static final String N = " ";
public static void main(String[] args) {
String[] board = { X,X,O,
N,O,N,
O,N,N };
// combinations to win:
int[][] wins = { {0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,6,8}, {0,4,8}, {2,4,6} };
// count x's and o's to win
int x=0, o=0;
// find the winner by indexing the board array from the combinations to win array
// In each combination to win, if all 3 are X's or O's, declare a winner
for(int i=0; i<8; i++) {
x=0; o=0;
for(int j=0;j<3;j++) {
if (board[wins[i][j]] == X) x++;
if (board[wins[i][j]] == O) o++;
}
if (o==3 || x==3)
System.out.println(((x==3) ? "X" : "O") + " wins");
}
}
}