当前位置: 首页 > 知识库问答 >
问题:

我将如何跟踪连接4板和java中的JUnit测试方法中的光盘数量?

公孙棋
2023-03-14

我一直在用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测试代码?我一直无法解决如何使用JUnitTesting,因此应该会有所帮助。

共有1个答案

雷曜灿
2023-03-14

如果您想在项目中包括JUnit,我推荐本教程,因为它将逐步指导您如何为Java应用程序运行JUnit测试:从Java应用程序运行JUnit测试

为了找出程序无法按预期工作的原因,您应该尝试在调试模式下运行Java程序(因为您已经包含了eclipse标签,我假设您使用的是eclipse作为主IDE,所以本教程可能会有所帮助:快速启动调试初学者指南)

调试过程

  • 我在第60行()上为(int I=0;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;

变量i的循环内。

  • 如何修复:在开始运行我上面粘贴的代码块之前,您应该关闭'i'的方括号的for循环。正确关闭方括号后,第83行应该有一个额外的关闭方括号,您必须删除。

 类似资料:
  • 问题内容: 考虑下面的两个简单的Java类: 第一个例子 第二个例子 程序运行后,如何跟踪(1)哪个对象调用哪个方法(2)以及执行多少次? 稍微精确一点,我可能有100个类和1000个对象,每个对象都调用100多个方法。我希望能够跟踪(在运行程序之后)哪个对象调用了哪种方法以及调用了多少次。 感谢您的任何建议。 问题答案: 这将为所有线程中所有对象的每个方法调用打印一行: 和 您可以使用 hous

  • 大家好,我需要为我的方法编写单元测试。我有点麻烦,因为我是JUnit的新手。我需要为这个方法写一个测试。这是我的方法 我试过了,但我被挡了,这是我的结果 附言:测试通过了,但我不明白我的测试是不是真的

  • 问题内容: 在JUnit 4中,使用批注很容易在多个类中测试不变式。关键是要针对单个参数列表运行一组测试。 如何在不使用JUnit-vintage的情况下在JUnit 5中复制它? 不适用于测试课程。听起来似乎很合适,但是注释的目标也是一种方法。 此类JUnit 4测试的示例是: 问题答案: JUnit 5中的参数化测试功能所提供的 功能与JUnit 4所提供的功能完全不同。引入了具有更大灵活性的

  • 2014-12-20 15:35:52错误TestContextManager:334-允许TestExecutionListener[org.springframework.test.context.support.dependencyInjectionTestExecutionListener@5af97850]准备测试实例[com.amsb.bariz.base.test.usertest@

  • 问题内容: 我有一帮其扩展TestCase,并希望自动迁移他们是JUnit4测试与注释如JUnit 3班的,,等 任何工具都来在大批量运行做到这一点? 问题答案: 我认为这并不难。因此,让我们尝试一下: 0.进口 您需要导入三个注释: 完成接下来的几处更改后,您将不需要。 1.注释方法 以开头的所有方法都必须在注释之前。使用正则表达式可以轻松完成此任务。 2.注释SetUp和TearDown方法

  • 问题内容: 正确,我的junit测试看起来像是一个漫长的故事: 我创建了4位用户 我删除了1位用户 我尝试使用已删除的用户登录,并确保失败 我使用剩余的3个用户之一登录并确认我可以登录 我从一个用户向另一个用户发送一条消息,并验证该消息是否出现在发件人的发件箱和收件人的收件箱中。 我删除邮件 … … 优点 :测试非常有效(非常善于检测错误)并且非常稳定,因为它们仅使用API​​,如果我重构代码,那