我将如何跟踪connect4板中的磁盘数量和java中的JUnit测试方法?

hwamh0ep  于 2022-11-11  发布在  Java
关注(0)|答案(1)|浏览(120)

我一直在用java编写一个connect 4程序,并且我一直在检查board的有效性,但是在开发代码时遇到了一些问题。

public class Connect4 {
     public static void main(String args[]) {
            char[][] board = new char[][]{///2d array for board
                    {'.', '.', '.', 'Y', 'R', '.','.'},
                    {'.', '.', '.', 'Y', 'R', '.','.'},
                    {'.', '.', '.', 'Y', 'R', 'Y','.'},
                    {'.', '.', '.', 'Y', 'R', 'R','.'},
                    {'.', '.', '.', 'Y', 'R', 'Y','.'},
                    {'.', '.', '.', 'Y', 'R', 'R','.'},             
                };
            System.out.println("\n1.2/ Is this Board Valid?");
            System.out.println(Q2_ValidBoard(board) + " this (is/is not) a valid board.");
            System.out.println();
     }

            public static boolean Q2_ValidBoard(char[][] board) {
                int allowedRows = 6;///allowed row size for board
                int allowedColumns = 7;///allowed size for columns
                if ((board == null) || (board.length == 0)) { //checks if array is empty
                    System.out.println("Array is Empty");
                    return false;
                }

                // Verify Board Size and Each Square of the Board
                // Condition 1 - Check if The Size Of Rows is 6 / If Height is 6
                if(board.length != allowedRows) {System.out.println("Number of Rows is Not " + allowedRows); return false;}

                for (int i = 0; i < board.length; i++) { // For Each Row in Board..
                    if (board[i].length != allowedColumns) { // Check if Size of Column is 6 / If Width is 7
                        System.out.println("Number of Column's is Not " + allowedColumns + " on ROW " + (i + 1));///Declares if the 
                        return false;
                    }

                    // Condition 2 - Check whether Each Square has a Valid Value
                    for (int j = 0; j < board[i].length; j++) { // For Each Column in Board..
                        if(((board[i ][j] != 'R')&&(board[i][j] != 'Y')&&(board[i][j] != '.'))) {
                            System.out.println("Invalid Board Square At " + (i + 1) + " x " + (j + 1));
                            return false; // If The Square isn't R, Y or . it's not Valid
                        }
                    }

                }

                // Condition 3 - Loop Backwards and Check if a Disc is Floating
                for (int i = board.length - 1; i >= 0; i--) {
                    for (int j = (board[i].length - 1); j >= 0; j--) {
                        if ((board[i][j] == 'R') || (board[i][j] == 'Y')) {
                            if (i != allowedRows - 1) { // Make sure to check it's not the deepest row level or it will call an error as it looks OutOfBounds of the List (-1)
                                if(board[i + 1][j] == '.') { // You want to increment to go deeper in the 2D array not decrease; you're already going backwards.
                                    System.out.println("Disc is Floating at " + (i + 1) + " x " + (j + 1));
                                    return false;
                                }
                            }
                        }
                    }
                }
                ///Condition 4 check if the amount of discs is fair
                int redCount = 0;
                int yellowCount = 0;
                for (int i = 0; i < board.length;i++) {
                    for (int j = 0 ;j < board[i].length; j++) {
                        if (board[i][j] == 'R'){
                            System.out.println("red disc found");
                            redCount++;
                        }else if (board[i][j] == 'Y') {
                        System.out.println("yellow disc found");
                        yellowCount++;
                    }
                 }

                if (redCount - yellowCount > 1){///If the difference between red and yellow discs is greater than one then the board is invalid
                    System.out.println("Amount of discs isn't fair. There are too many red discs and thus the board is invalid. There are " + redCount + " reds and " + yellowCount + " yellows.");
                    System.out.println(redCount);
                    return false;

                }
                else if (yellowCount > redCount) {///If there are more yellows than reds then the board is invalid since red is always meant to go first
                    System.out.println("Amount of discs isn't fair. There are too many yellow discs and thus the board is invalid. There are " + redCount + " reds and " + yellowCount + " yellows.");
                    return false;
                }

                    }
                System.out.println("The number of red discs is " + redCount + ". And the number of yellow discs is " + yellowCount );
                return true;

            }
}

我输入的棋盘应该是有效的,因为红色和黄色的数量是相等的。正确的圆盘比例的条件在代码注解中列出。
当我输入板卡时,我被告知板卡无效,返回false

1.2/ Is this Board Valid?
yellow disc found
red disc found
yellow disc found
red disc found
yellow disc found
red disc found
yellow disc found
Amount of discs isn't fair. There are too many yellow discs and thus the board is invalid. There are 3 reds and 4 yellows.
false this (is/is not) a valid board.

正如你所看到的董事会有超过3个黄色和4个红色(8个红色光盘和8个黄色光盘),但有一些在代码中是停止计数的嵌套循环内的光盘计数,计数的红色和黄色的数量。

///Condition 4 check if the amount of discs is fair
                int redCount = 0;
                int yellowCount = 0;
                for (int i = 0; i < board.length;i++) {
                    for (int j = 0 ;j < board[i].length; j++) {
                        if (board[i][j] == 'R'){
                            System.out.println("red disc found");
                            redCount++;
                        }else if (board[i][j] == 'Y') {
                        System.out.println("yellow disc found");
                        yellowCount++;
                    }
                 }

                if (redCount - yellowCount > 1){///If the difference between red and yellow discs is greater than one then the board is invalid
                    System.out.println("Amount of discs isn\'t fair. There are too many red discs and thus the board is invalid. There are " + redCount + " reds and " + yellowCount + " yellows.");
                    System.out.println(redCount);
                    return false;

                }
                else if (yellowCount > redCount) {///If there are more yellows than reds then the board is invalid since red is always meant to go first
                    System.out.println("Amount of discs isn't fair. There are too many yellow discs and thus the board is invalid. There are " + redCount + " reds and " + yellowCount + " yellows.");
                    return false;
                }

我该如何解决这个问题?我该如何使用JUnitTesting来测试我的代码?我一直不知道如何使用JUnitesting,所以应该会有所帮助。

2eafrhcq

2eafrhcq1#

在Eclipse中运行JUnit测试和调试

如果您希望在项目中包含JUnit,我推荐您使用本教程,因为它将逐步指导您如何为Java应用程序运行JUnit测试:Running JUnit Tests from a Java Application
要找出程序不能按预期运行的原因,您应该尝试在调试模式下运行Java程序(因为您已经包含了eclipse标记,我假设您使用Eclipse作为主IDE,因此本教程可能会有所帮助:(x、x、e、f、x)

调试过程

  • 我在第60行**(for (int i = 0; i < board.length;i++) {)上放置了一个断点,然后对循环进行了一些迭代。
  • 出现此错误的原因是您没有正确关闭大括号(因为您正在运行以下代码块
if (redCount - yellowCount > 1){///If the difference between red and yellow discs is greater than one then the board is invalid
                   System.out.println("Amount of discs isn't fair. There are too many red discs and thus the board is invalid. There are " + redCount + " reds and " + yellowCount + " yellows.");
                   System.out.println(redCount);
                   return false;

               }
               else if (yellowCount > redCount) {///If there are more yellows than reds then the board is invalid since red is always meant to go first
                   System.out.println("Amount of discs isn't fair. There are too many yellow discs and thus the board is invalid. There are " + redCount + " reds and " + yellowCount + " yellows.");
                   return false;
               }

                   }
               System.out.println("The number of red discs is " + redCount + ". And the number of yellow discs is " + yellowCount );
               return true;

for-loop of variable i中)。

*如何修复:在运行我粘贴的代码块之前,你应该关闭'i'的花括号的for循环。2在正确关闭花括号之后,在第83行**应该有一个额外的关闭花括号,你必须删除它。

编辑后的代码片段

public class Connect4 {
    public static void main(String args[]) {
        char[][] board = new char[][]{///2d array for board
                {'.', '.', '.', 'Y', 'R', '.', '.'},
                {'.', '.', '.', 'Y', 'R', '.', '.'},
                {'.', '.', '.', 'Y', 'R', 'Y', '.'},
                {'.', '.', '.', 'Y', 'R', 'R', '.'},
                {'.', '.', '.', 'Y', 'R', 'Y', '.'},
                {'.', '.', '.', 'Y', 'R', 'R', '.'},
        };
        System.out.println("\n1.2/ Is this Board Valid?");
        System.out.println(Q2_ValidBoard(board) + " this (is/is not) a valid board.");
        System.out.println();
    }

    public static boolean Q2_ValidBoard(char[][] board) {
        int allowedRows = 6;///allowed row size for board
        int allowedColumns = 7;///allowed size for columns
        if ((board == null) || (board.length == 0)) { //checks if array is empty
            System.out.println("Array is Empty");
            return false;
        }

        // Verify Board Size and Each Square of the Board
        // Condition 1 - Check if The Size Of Rows is 6 / If Height is 6
        if (board.length != allowedRows) {
            System.out.println("Number of Rows is Not " + allowedRows);
            return false;
        }

        for (int i = 0; i < board.length; i++) { // For Each Row in Board..
            if (board[i].length != allowedColumns) { // Check if Size of Column is 6 / If Width is 7
                System.out.println("Number of Column's is Not " + allowedColumns + " on ROW " + (i + 1));///Declares if the
                return false;
            }

            // Condition 2 - Check whether Each Square has a Valid Value
            for (int j = 0; j < board[i].length; j++) { // For Each Column in Board..
                if (((board[i][j] != 'R') && (board[i][j] != 'Y') && (board[i][j] != '.'))) {
                    System.out.println("Invalid Board Square At " + (i + 1) + " x " + (j + 1));
                    return false; // If The Square isn't R, Y or . it's not Valid
                }
            }

        }

        // Condition 3 - Loop Backwards and Check if a Disc is Floating
        for (int i = board.length - 1; i >= 0; i--) {
            for (int j = (board[i].length - 1); j >= 0; j--) {
                if ((board[i][j] == 'R') || (board[i][j] == 'Y')) {
                    if (i != allowedRows - 1) { // Make sure to check it's not the deepest row level or it will call an error as it looks OutOfBounds of the List (-1)
                        if (board[i + 1][j] == '.') { // You want to increment to go deeper in the 2D array not decrease; you're already going backwards.
                            System.out.println("Disc is Floating at " + (i + 1) + " x " + (j + 1));
                            return false;
                        }
                    }
                }
            }
        }
        ///Condition 4 check if the amount of discs is fair
        int redCount = 0;
        int yellowCount = 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == 'R') {
                    System.out.println("red disc found");
                    redCount++;
                } else if (board[i][j] == 'Y') {
                    System.out.println("yellow disc found");
                    yellowCount++;
                }
            }

        }
            if (redCount - yellowCount > 1){///If the difference between red and yellow discs is greater than one then the board is invalid
                System.out.println("Amount of discs isn't fair. There are too many red discs and thus the board is invalid. There are " + redCount + " reds and " + yellowCount + " yellows.");
                System.out.println(redCount);
                return false;

            }
            else if (yellowCount > redCount) {///If there are more yellows than reds then the board is invalid since red is always meant to go first
                System.out.println("Amount of discs isn't fair. There are too many yellow discs and thus the board is invalid. There are " + redCount + " reds and " + yellowCount + " yellows.");
                return false;
            }

        System.out.println("The number of red discs is " + redCount + ". And the number of yellow discs is " + yellowCount );
        return true;

    }
}
  • 在正确关闭括号并运行下面的代码后,您应该看到以下输出:
1.2/ Is this Board Valid?
yellow disc found
red disc found
yellow disc found
red disc found
yellow disc found
red disc found
yellow disc found
yellow disc found
red disc found
red disc found
yellow disc found
red disc found
yellow disc found
yellow disc found
red disc found
red disc found
The number of red discs is 8. And the number of yellow discs is 8
true this (is/is not) a valid board.

我希望这有助于修复您的错误!

相关问题