用Java创建Connect Four(删除检查程序)

时间:2018-01-26 17:53:29

标签: java

现在我正在用Java创建Connect Four。我正在努力放弃检查器,但我遇到了一些问题。到目前为止,这是我的代码:

private static char board[][];
private static final int BOARD_WIDTH = 7;
private static final int BOARD_HEIGHT = 6;
private static boolean gameEnd = false;

public static void main(String[] args)
{
    // Element #0 in the two-dimensional 'board' array is not used
    // so that the column numbers selected by the players match
    // with the actual internal element numbers of the array

    board = new char[BOARD_HEIGHT + 1][BOARD_WIDTH + 1];
    Scanner input = new Scanner(System.in);
    char turn = 'x';
    int dropColumn = 0;

    System.out.println("TWO PLAYER CONNECT FOUR GAME!");
    InitializeBoard();
    while (gameEnd == false)
    {

        DisplayBoard();
        dropColumn = GetDropColumn(turn, input);
        if (turn == 'x')
            turn = 'o';
        else
            turn = 'x';
         if (DropChecker(turn, dropColumn) == true)
            board[BOARD_HEIGHT][dropColumn] = turn;
    }

}

// Set all elements of the two-dimensional 'board' array to spaces
private static void InitializeBoard()
{
    char a = ' ';
    for (int i = 0; i < board.length; i++)
    {
        board[i][0] = a;
    }
    for (int e = 0; e < board.length; e++)
    {
        board[0][e] = a;
    }
}

// Display the game board (the board itself, along with the elements of
// the two-dimensional 'board' array); note that the lines of the game
// board are NOT stored in the two-dimensional 'board' array
private static void DisplayBoard()
{
    for (int row = 0; row < board.length; row++)
    {
        for (int col = 0; col < board.length; col++)
        {
            System.out.print("|");
            System.out.printf("%3c", board[row][col]);
        }
        System.out.println();
    }
}

// Get (from the appropriate player) the number (1 – 'BOARD_WIDTH') of
// the column in which a checker should be dropped, and then return that
// number; if the player does not enter an integer, report the error and
// keep asking for a column number until an integer is entered; note that
// the check for an invalid column number (< 1 or > 'BOARD_WIDTH') can be
// performed in this method or in 'main', from where this method is called
private static int GetDropColumn(char turn, Scanner input)
{
    int numInput = 0;
    int realInput = 0;
    while (realInput == 0)
    {
        try
        {
            System.out.println("Player " + turn + "'s turn: In which column would you like to place a checker? (1-7)");
            numInput = input.nextInt();
            if (numInput < 1 || numInput > BOARD_WIDTH)
            {
                numInput = 0;
                System.out.println("The number was out of bounds. Please try again.");
            }
        }
        catch (NumberFormatException e)
        {
            System.out.println("Invalid input. Please try again.");
        }
        realInput = numInput;
    }
    return realInput;
}

// "Drop" a checker into the designated column by setting the
// appropriate element of the two-dimensional 'board' array to
// either an 'x' or an 'o'; if the "drop" was successful, this
// method returns "true"; an attempt to "drop" the checker into
// a full column results in "false" being returned
private static boolean DropChecker(char turn, int dropColumn)
{
    int count = 0;
    while (count < BOARD_HEIGHT)
    {
        if (board[BOARD_HEIGHT - count][dropColumn] == ' ')
        {
            return true;
        }
        count++;
    }
    return false;
}

我目前的问题是程序不会丢弃棋盘上的棋盘格。在我输入第6列之后,程序返回板但是检查器没有被丢弃。我在这里错过了什么/做错了什么?

2 个答案:

答案 0 :(得分:1)

您的代码存在一些问题

  1. 玩家的转弯是相反的,所以你需要做的就是在玩家放弃之后更换玩家,而不是之前

  2. 初始化板方法已关闭,只需使用两个for循环来初始化它

  3. 放置检查器的索引不正确,所以从底部的索引开始,然后每当空格不是“空”时递减(=='')

  4. 1.Main Game Loop Refactor

    while (gameEnd == false)
    {
        DisplayBoard();
        dropColumn = GetDropColumn(turn, input);
    
         if (DropChecker(turn, dropColumn) == true)
         {
            turn = 'x' == turn ? 'o' : 'x'; //ternary operator works great
         }
    }
    

    2.Initialize Method refactor

    private static void InitializeBoard()
    {
        char a = ' ';
        for (int i = 0; i < board.length; i++)
        {
            for (int e = 0; e < board[i].length; e++)
                board[i][e] = a;
        }
    }
    

    3.Drop Checker Method Refactor

    private static boolean DropChecker(char turn, int dropColumn)
    {
        int indexToPaceChecker = BOARD_HEIGHT;
        while (indexToPaceChecker >= 0)
        {
            if (board[indexToPaceChecker][dropColumn] == ' ')
            {
                //drop the checker, that's what this method is supposed to do anyways :)
                board[indexToPaceChecker][dropColumn] = turn;
                return true;
            }
            indexToPaceChecker--;
        }
        return false;
    }
    

    我还建议您在代码中使用private static final char emptySpace = ' ';这样的方法,您可以像board[i][e] = emptySpace;一样对董事会进行初始化,并检查if (board[indexToPaceChecker][dropColumn] == emptySpace)这样的空白区域。如果你想要一个不同的板填充,它更干净,更容易进行更改。

答案 1 :(得分:0)

DropChecker()只检查列中是否有空位。相反,你应该返回哪一行是最低的那一行(索引最高的那一行),如果没有你可以返回-1。 您使用以下行设置错误的行:board[BOARD_HEIGHT][dropColumn] = turn这将转入最后一行。

您也没有初始化您的电路板阵列(如果您没有初始化它,它与''不同,它也是一个字符)

这应该适合你:

private static int DropChecker(char turn, int dropColumn) {
    int count = 0;
    while (count < BOARD_HEIGHT) {
        if (board[BOARD_HEIGHT - count][dropColumn] == ' ') {
            count++;
        }
        else {
            return --count;
        }
    }
}

您可以使用以下方法在电路板中设置位置:

board[DropChecker(turn, dropColumn)][dropColumn]

在主页的开头添加此行,在board = new char [] []:

之后
for(int i = 0; i < BOARD_HEIGHT; i++) {
    for(int j = 0; j < BOARD_WIDTH; j++) {
        board[i][j] = ' ';
    }
}