MasterMind是github上一个开源项目(https://github.com/Squirrelbear/MasterMind/tree/main),为了练习GUI按钮,标签和面板,体验MVC模式,我在开源的项目上做了很多有趣的尝试,巩固学习的成果:
1.将游戏背景颜色改为淡棕色;
2.不同尝试编号的文本颜色应该不同。 同一次尝试面板中的两个提示字符串应该有不同的文本样式;
3.将游戏结束面板的实现从在游戏面板上绘制改为在弹出面板上绘制,点击重新开始按钮后,游戏将重置并使用新解决方案。 原有的机制(按下键盘上的“R”键)不再起作用;
4.对SimpleButton.java进行单元测试,使用3A原则用于测试方法isClicked(int,int)和getActionCode();
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import java.awt.*;
/**
* Name:ZHANG AIWEI ID:1155215891
* 对SimpleButton.java进行JUnit测试
*/
public class SimpleButtonTest {
private SimpleButton button;
//在每个测试之前初始化 SimpleButton 对象
@Before
public void setUp() {
button = new SimpleButton(100, 100, 50, 50, Color.RED,true,1);
}
// 测试 isClicked(int, int) 方法
@Test
public void testIsClicked_WithinBounds_ReturnsTrue() {
//准备测试数据,点击位置在按钮范围内
int clickX = 120;
int clickY = 120;
//调用 isClicked 方法
boolean result = button.isClicked(clickX, clickY);
// Assert: 验证结果是否为 true,,如果为true则测试通过
assertTrue("点击位置在按钮范围内应返回 true", result);
}
// 测试 isClicked(int, int) 方法
@Test
public void testIsClicked_OutOfBounds_ReturnsFalse() {
//准备测试数据,点击位置在按钮范围外
int clickX = 200;
int clickY = 200;
//调用 isClicked 方法
boolean result = button.isClicked(clickX, clickY);
// Assert: 验证结果是否为 false,如果为false则测试通过
assertFalse("点击位置在按钮范围外应返回 false", result);
}
// 测试 getActionCode() 方法
@Test
public void testGetActionCode_ReturnsCorrectActionCode() {
//不需要配置,因为 actionCode 在构造函数中已设置
//调用 getActionCode 方法
int actionCode = button.getActionCode();
// Assert: 验证返回的 actionCode 是否为预期的 1 为1则测试通过
assertEquals("返回的动作码应为 1", 1, actionCode);
}
}
5.使用水果图标来替代颜色按钮,例如,苹果、橙子、香蕉等