使用 Java 可定制的井字遊戲板 (Customizable TicTacToe game board with Java)


問題描述

使用 Java 可定制的井字遊戲板 (Customizable TicTacToe game board with Java)

我需要創建一個方法來檢查井字遊戲是 PLAYING、DRAW、XWIN 還是 OWIN。但是,我很難編寫代碼來檢查 X 或 O 是否獲勝,因為遊戲板的大小和獲勝所需的大小 (sizeWin) 會根據用戶的輸入而變化。而且我被迫為遊戲板使用一維數組。我根本不知道從這裡去哪裡。我最新的想法是使用嵌套的 for 循環來檢查行、列或對角線是否獲勝,但我不確定如何實現它。如果有人對如何解決此問題有任何提示或有任何其他解決方案,我將不勝感激

private void setGameState(int i) {

    // Check rows
    getLines();
    getColumns();
    getSizeWin();
    for (row = 0; row == lines; row++) {
        for (col = 0; col == columns; col++) {

        }
    }
}

public TicTacToeGame(int lines, int columns, int sizeWin) {

    // linesXcolumns game, starts with X, need sizeWin in a line/column/diag to win
    this.lines = lines;
    this.columns = columns;
    CellValue currentCellValue = CellValue.X;
    this.sizeWin = sizeWin;

    // Creating board according to given size
    int size = lines * columns;
    this.board = new CellValue[size];

    // Setting up board to be empty
    for (int i = 0; i < size; i++) {
        board[i] = CellValue.EMPTY;
    }
}

PS。如果有人調用運算符 TicTacToe(3,4,3),將打印 3 行 4 列的遊戲板。獲勝的 X 或 O 的數量將是 3。


參考解法

方法 1:

It is a little more complicated than it looks but after you get it it's simple. I've made a function that works just fine:

private static String checkGameState() {
    // Looking for errors.
    if (rowCount <= 0 || columnCount <= 0) {
        return "ERROR: Illegal board size: " + rowCount + "*" + columnCount;
    }
    if (boradContent.length != rowCount * columnCount) {
        return "ERROR: boradContent not compatible with rowSize and columnSize.";
    }
    if (sizeWin > rowCount && sizeWin > columnCount) {
        return "ERROR: Board is too small for this sizeWin: " + sizeWin + ".";
    }

    String gameState = "PLAYING";

    // Checking rows
    for (int i = 0; i < rowCount; i++) {
        char currentChar = getField(i, 0);
        int score = 1;
        for (int j = 1; j < columnCount; j++) {
            if (currentChar == getField(i, j)) {
                score++;
                if (score >= sizeWin) {
                    if (gameState.equals("PLAYING")) {
                        gameState = currentChar + "WIN";
                    } else if (!gameState.equals(currentChar + "WIN")) {
                        gameState = "DRAW";
                        return gameState;
                    }
                }
            } else {
                if (j > columnCount ‑ sizeWin) {
                    break;
                }
                score = 1;
                currentChar = getField(i, j);
            }
        }
    }

    // Checking columns
    for (int j = 0; j < columnCount; j++) {
        char currentChar = getField(0, j);
        int score = 1;
        for (int i = 1; i < rowCount; i++) {
            if (currentChar == getField(i, j)) {
                score++;
                if (score >= sizeWin) {
                    if (gameState.equals("PLAYING")) {
                        gameState = currentChar + "WIN";
                    } else if (!gameState.equals(currentChar + "WIN")) {
                        gameState = "DRAW";
                        return gameState;
                    }
                }
            } else {
                if (j > rowCount ‑ sizeWin) {
                    break;
                }
                score = 1;
                currentChar = getField(i, j);
            }
        }
    }

    // Checking diagonally
    // Checking diagonally ‑ from top‑left to bottom‑right
    for (int i = 0; i < rowCount ‑ sizeWin + 1; i++) {
        for (int j = 0; j < columnCount ‑ sizeWin + 1; j++) {
            char currentChar = getField(i, j);
            int score = 1;
            for (int k = 1; k < sizeWin; k++) {
                if (currentChar == getField(i + k, j + k)) {
                    score++;
                    if (score >= sizeWin) {
                        if (gameState.equals("PLAYING")) {
                            gameState = currentChar + "WIN";
                        } else if (!gameState.equals(currentChar + "WIN")) {
                            gameState = "DRAW";
                            return gameState;
                        }
                    }
                } else {
                    break;
                }
            }

        }
    }

    // Checking diagonally ‑ from top‑right to bottom‑left
    for (int i = 0; i < rowCount ‑ sizeWin + 1; i++) {
        for (int j = sizeWin ‑1; j < columnCount; j++) {
            char currentChar = getField(i, j);
            int score = 1;
            for (int k = 1; k < sizeWin; k++) {
                if (currentChar == getField(i + k, j ‑ k)) {
                    score++;
                    if (score >= sizeWin) {
                        if (gameState.equals("PLAYING")) {
                            gameState = currentChar + "WIN";
                        } else if (!gameState.equals(currentChar + "WIN")) {
                            gameState = "DRAW";
                            return gameState;
                        }
                    }
                } else {
                    break;
                }
            }

        }
    }

    return gameState;
}

It is worth to mention that the rowCount, columnCount, sizeWin and boradContent variables are class level variables and i used a getField(int X, int Y) method that is not a very complicated thing but is more useful. It just converts the given field coordinates to the place in a 1D array and returns it's content:

private static char getField(int X, int Y) {
    return boradContent[X * columnCount + Y];
}

(by CamKristóf Göncző)

參考文件

  1. Customizable TicTacToe game board with Java (CC BY‑SA 2.5/3.0/4.0)

#java #tic-tac-toe #arrays






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論