java - 用 Java 制作四子棋(去掉检查器)

标签 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 列后,程序返回棋盘,但棋子并没有被删除。我在这里错过了什么/做错了什么?

最佳答案

您的代码存在一些问题

  1. 玩家的回合是相反的,所以你需要做的是在玩家放下棋子之后更换玩家,而不是之前

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

  3. 放置检查器的索引不正确,因此从底部的索引开始,然后在空间不“空”时递减(== ' ')

1.主游戏循环重构

while (gameEnd == false)
{
    DisplayBoard();
    dropColumn = GetDropColumn(turn, input);

     if (DropChecker(turn, dropColumn) == true)
     {
        turn = 'x' == turn ? 'o' : 'x'; //ternary operator works great
     }
}

2.Initialize方法重构

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方法重构

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 charemptySpace = ' '; 这样您就可以像 board[i][e] = emptySpace;,并检查是否有空白空间,例如 if (board[indexToPaceChecker][dropColumn] == emptySpace)。如果您想要不同的板填充,它会更干净、更容易进行更改。

关于java - 用 Java 制作四子棋(去掉检查器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48466903/

相关文章:

java - 使用流根据第二个列表中的值更新一个列表中的对象

java - 如何使用 XQuery 从 Java 使用 Saxon 输出 json

java - Google Play - 安装了应用内更新,但一次又一次显示更新可用

java - JodaTime 在不同环境中的奇怪行为

Java - 函数接口(interface) "lever"是否用于 lambda 表达式?

java - 在Android中使用restTemplate.getForObject反序列化JSON对象的正确方法

java - hibernate 性能

java - Eclipse RCP CSS 样式导出后不起作用

java - 列表java中的任意数量的列表

java - Android:启动画面实现不工作