Java Connect Four Bug

时间:2015-07-24 01:15:36

标签: java

我正在学习Java并遇到了一个我似乎无法修复的错误。它是一个基于控制台连接四个游戏。我遇到的问题是检查胜利的方法似乎没有正常工作。我已经确定这是方法本身的问题,而不是提交的参数。任何其他一般提示将不胜感激,因为这是我的第一个程序。谢谢!代码如下。

package com.coleriggle.java;

import java.util.Scanner;


public class Game {

private static boolean running;

public static void main(String[] args) {

    Game game = new Game();

    game.startGame();
}

private void startGame() {

    BoardHandler boardHandlerObject = new BoardHandler();
    Game gameObject = new Game();
    SelectionHandler selectionHandlerObject = new SelectionHandler();

    gameObject.startingDialogue();
    boardHandlerObject.setNewBoard();
    boardHandlerObject.refreshBoard();

    running = true;

    while (running) {

        selectionHandlerObject.startSelection();
    }

}

private void startingDialogue() {

    GraphicsHandler graphicsHandlerObject = new GraphicsHandler();

    graphicsHandlerObject.outputTextToScreen("Welcome to Connect Four!", 0);
    graphicsHandlerObject.outputTextToScreen("Player X will go first!", 0);
    graphicsHandlerObject.outputTextToScreen("", 3);
}

public void announceWin(char player) {

    GraphicsHandler graphicsHandlerObject = new GraphicsHandler();

    graphicsHandlerObject.outputTextToScreen("Player " + player
            + " has won!", 0);

    running = false;
}

}

class GraphicsHandler {

public int outputTextToScreen(String text, int newLineType) {

    if (newLineType == 0) {

        System.out.println(text);

        return 0;
    }

    else if (newLineType == 1) {

        System.out.print(text);

        return 1;
    }

    else if (newLineType == 2) {

        System.out.print("\n" + text);

        return 2;
    }

    else if (newLineType == 3) {

        System.out.println();

        return 3;
    }

    else {
        return 4;
    }
}

protected void drawBoardToScreen() {

    BoardHandler boardHandlerObject = new BoardHandler();

    char[][] tempGameBoard = boardHandlerObject.getGameBoard();

    System.out.println("|  1  |  2  |  3  |  4  |  5  |  6  |  7  |");
    System.out.println("|_____|_____|_____|_____|_____|_____|_____|");

    for (int i = 0; i < 6; i++) {
        System.out.println("|  " + tempGameBoard[i][0] + "  |  "
                + tempGameBoard[i][1] + "  |  " + tempGameBoard[i][2]
                + "  |  " + tempGameBoard[i][3] + "  |  "
                + tempGameBoard[i][4] + "  |  " + tempGameBoard[i][5]
                + "  |  " + tempGameBoard[i][6] + "  |  ");
    }

    System.out.println("|_____|_____|_____|_____|_____|_____|_____|");

}

}

class BoardHandler {

GraphicsHandler graphicsHandlerObject = new GraphicsHandler();

private static char[][] gameBoard = {
        { '?', '?', '?', '?', '?', '?', '?' },
        { '?', '?', '?', '?', '?', '?', '?' },
        { '?', '?', '?', '?', '?', '?', '?' },
        { '?', '?', '?', '?', '?', '?', '?' },
        { '?', '?', '?', '?', '?', '?', '?' },
        { '?', '?', '?', '?', '?', '?', '?' } };

public char[][] getGameBoard() {

    return gameBoard;
}

public void setNewBoard() {

    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 7; j++) {
            gameBoard[i][j] = '?';
        }
    }
}

public void refreshBoard() {

    graphicsHandlerObject.drawBoardToScreen();
}

public void replaceSelectionOfBoard(int row, int column, char player) {

    BoardHandler.gameBoard[row][column] = player;
}

public int checkSelectionValidity(int columnSubmitted, char currentPlayer) {

    BoardHandler boardHandlerObject = new BoardHandler();
    WinCheckHandler winCheckHandlerObject = new WinCheckHandler();
    Game gameObject = new Game();

    char[][] tempGameBoard = boardHandlerObject.getGameBoard();
    ;

    int column;

    column = columnSubmitted -= 1;

    if (tempGameBoard[5][columnSubmitted] == '?') {

        if (winCheckHandlerObject.checkForWin(5, column, currentPlayer) ==   1)     {

            gameObject.announceWin(currentPlayer);
        }

        boardHandlerObject
                .replaceSelectionOfBoard(5, column, currentPlayer);

        return 0;
    }

    else if (tempGameBoard[4][columnSubmitted] == '?') {

        if (winCheckHandlerObject.checkForWin(4, column, currentPlayer) == 1)     {

            gameObject.announceWin(currentPlayer);

        }

        boardHandlerObject
                .replaceSelectionOfBoard(4, column, currentPlayer);

        return 0;
    }

    else if (tempGameBoard[3][columnSubmitted] == '?') {

        if (winCheckHandlerObject.checkForWin(3, column, currentPlayer) == 1)     {

            gameObject.announceWin(currentPlayer);

        }

        boardHandlerObject
                .replaceSelectionOfBoard(3, column, currentPlayer);

        return 0;
    }

    else if (tempGameBoard[2][columnSubmitted] == '?') {

        if (winCheckHandlerObject.checkForWin(2, column, currentPlayer) == 1)     {

            gameObject.announceWin(currentPlayer);

        }

        boardHandlerObject
                .replaceSelectionOfBoard(2, column, currentPlayer);

        return 0;
    }

    else if (tempGameBoard[1][columnSubmitted] == '?') {

        if (winCheckHandlerObject.checkForWin(1, column, currentPlayer) == 1)     {

            gameObject.announceWin(currentPlayer);

        }

        boardHandlerObject
                .replaceSelectionOfBoard(1, column, currentPlayer);

        return 0;
    }

    else if (tempGameBoard[0][columnSubmitted] == '?') {

        if (winCheckHandlerObject.checkForWin(0, column, currentPlayer) == 1)     {

            gameObject.announceWin(currentPlayer);

        }

        boardHandlerObject
                .replaceSelectionOfBoard(0, column, currentPlayer);

        return 0;
    }

    else {

        return 1;
    }

}

}

class TurnHandler {

private char currentTurn = 'X';

public char getCurrentTurn() {

    return currentTurn;
}

public void changeCurrentTurn() {

    if (currentTurn == 'X') {

        currentTurn = 'O';
    }

    else if (currentTurn == 'O') {

        currentTurn = 'X';
    }
}

}

class SelectionHandler {

TurnHandler turnHandlerObject = new TurnHandler();
GraphicsHandler graphicsHandlerObject = new GraphicsHandler();
BoardHandler boardHandlerObject = new BoardHandler();

Scanner userInputHandler = new Scanner(System.in);

private char currentTurn;

private int columnSelected;

public void startSelection() {

    currentTurn = turnHandlerObject.getCurrentTurn();

    graphicsHandlerObject.outputTextToScreen("Player " + currentTurn
            + " please select a column", 0);

    columnSelected = userInputHandler.nextInt();

    if (boardHandlerObject.checkSelectionValidity(columnSelected,
            currentTurn) == 0) {

        graphicsHandlerObject.outputTextToScreen(
                "Symbol placed successfully", 0);
        graphicsHandlerObject.outputTextToScreen("", 3);

        turnHandlerObject.changeCurrentTurn();
        boardHandlerObject.refreshBoard();
    }

    else {

        graphicsHandlerObject.outputTextToScreen("Error. Column Full.", 0);
        graphicsHandlerObject.outputTextToScreen("", 3);

        startSelection();
    }
}

}

class WinCheckHandler {

private static char player;

public int checkForWin(int row, int column, char player) {

    BoardHandler boardHandlerObject = new BoardHandler();

    char[][] checkingBoard = boardHandlerObject.getGameBoard();

    WinCheckHandler.player = player;

    if (towerCheck(row, column, checkingBoard) == 1) {

        return 1;
    }

    else if (flatCheck(row, column, checkingBoard) == WinCheckHandler.player) {

        return player;
    }

    else if (diagnalCheck(row, column, checkingBoard) ==  WinCheckHandler.player) {

        return player;
    }


    return 0;
}

private int towerCheck(int row, int column, char[][] checkingBoard) {

    int row1 = row;

    int row2 = row;

    row2 -= 1;

    int row3 = row;

    row3 -= 2;

    int row4 = row;

    row4 -= 3;


    if (checkingBoard[row1][column] == player
            && checkingBoard[row2][column] == player
            && checkingBoard[row3][column] == player
            && checkingBoard[row4][column] == player) {

        return 1;
    }

    return 0;
}

private char flatCheck(int row, int column, char[][] checkingBoard) {

    if (checkingBoard[row][column] == player
            && checkingBoard[row][column += 1] == player
            && checkingBoard[row][column += 1] == player
            && checkingBoard[row][column += 1] == player) {

        return player;
    }

    else if (checkingBoard[row][column] == player
            && checkingBoard[row][column -= 1] == player
            && checkingBoard[row][column -= 1] == player
            && checkingBoard[row][column -= 1] == player) {

        return player;
    }

    return 0;
}

private char diagnalCheck(int row, int column, char[][] checkingBoard) {

    if (checkingBoard[row][column] == player
            && checkingBoard[row -= 1][column -= 1] == player
            && checkingBoard[row -= 1][column -= 1] == player
            && checkingBoard[row -= 1][column -= 1] == player) {

        return player;
    }

    else if (checkingBoard[row][column] == player
            && checkingBoard[row += 1][column -= 1] == player
            && checkingBoard[row += 1][column -= 1] == player
            && checkingBoard[row += 1][column -= 1] == player) {

        return player;
    }

    else if (checkingBoard[row][column] == player
            && checkingBoard[row += 1][column += 1] == player
            && checkingBoard[row += 1][column += 1] == player
            && checkingBoard[row += 1][column += 1] == player) {

        return player;
    }


    return 0;
    }
}

编辑:小故障在WinCheckHandler类中。它应该确定最近的位置是否连接了四个玩家的符号

1 个答案:

答案 0 :(得分:3)

flatCheckdiagnalCheck方法中,您在支票中使用-=+=。这将改变rowcolumn变量的值,这将使以下检查变得毫无意义。

例如:

if (checkingBoard[row][column] == player
        && checkingBoard[row][column += 1] == player
        && checkingBoard[row][column += 1] == player
        && checkingBoard[row][column += 1] == player) {

    return player;
}

else if (checkingBoard[row][column] == player
        && checkingBoard[row][column -= 1] == player
        && checkingBoard[row][column -= 1] == player
        && checkingBoard[row][column -= 1] == player) {

    return player;
}

else if表达式中,列不会传递给方法 - 它将根据前一个子句中传递的表达式多次递减。这不太可能是你想要的。

我建议您更改所有代码,使其更像以下内容:

private boolean playHasFourInRow(int row, int col, char[][] board, int player) {
    return row > 3
        && board[row][col] == player
        && board[row-1][col] == player
        && board[row-2][col] == player
        && board[row-3][col] == player;
}

如果您熟悉Java 8,那么表达式的更好格式可能是:

return IntStream.range(0, WIN_LENGTH)
    .allMatch(n -> n >= 0 && board[row - n][col] == player);