java - 定位在数组中不匹配
问题描述
我正在使用我在互联网上找到的文档中的二维数组制作一个简单的蛇应用程序。我已经通过了文件TestAsmt1.java中的所有测试。但是,我确信我已按照 PDF 文档中的所有说明进行操作。我错过了什么吗?谁能帮我这个?所以我可以正确使用PlayGame.java。谢谢
问题是,当我移动蛇时,它的头部会重复并在数组中移动一步 - 这没有写在代码中。无法确定位置在哪里
这是我到目前为止所做的
源代码 - https://drive.google.com/open?id=1-oxBFusUIjX9bIqG5HhdCNpmIgTEl6mc
pdf 文件 - https://drive.google.com/open?id=1QsyrmDSYhsvaUUkHoTl6jPNsgKfLdWPy
MainApp - PlayGame.java,TestAsmnt.java
public void moveSnake(String direction){
Position[] arr = new Position[this.snakeBody.length];
Position pos = this.newHeadPosition(direction);
arr[0] = new Position(pos.getRow(), pos.getCol());
for (int a = 0; a< this.snakeLength; a++){
arr[a+1] = new Position(this.snakeBody[a].getRow(), this.snakeBody[a].getCol());
}
for (int a = 0; a< this.snakeLength; a++){
this.snakeBody[a].setRow(arr[a].getRow());
this.snakeBody[a].setCol(arr[a].getCol());
}
}
解决方案
发现错误 - 在从 arr 数组恢复元素之前,我没有更新蛇身数组。下面更新方法
public void moveSnake(String direction){
Position[] arr = new Position[this.snakeBody.length];
Position pos = this.newHeadPosition(direction);
arr[0] = new Position(pos.getRow(), pos.getCol());
for (int a = 0; a< this.snakeLength; a++){
arr[a+1] = new Position(this.snakeBody[a].getRow(), this.snakeBody[a].getCol());
}
this.snakeBody = new Position[arr.length];
for (int a = 0; a< this.snakeLength; a++){
this.snakeBody[a].setRow(arr[a].getRow());
this.snakeBody[a].setCol(arr[a].getCol());
}
}
推荐阅读
- performance - Unity3D:动作捕捉动画在移动设备上的兼容性是否低很多?
- python - 根据其他数据框中提供的范围值创建新列
- python - 如何将使用“\r\n”作为换行符的字符串转换为熊猫数据框
- aws-lambda - AWS-Lambda 可以使用调用 C 的 Python 吗?
- c# - 如何在锁定屏幕下退出 UWP Kiosk 应用程序
- haskell - 如何使用 MonadReader 和 MonadIO 运行此方法?
- google-apps-script - 如何根据表单问题的字符串值有效地修改变量?
- php - 如何获取url参数
- android - 使用数据绑定时是否还需要使用 View Holder 模式?
- python-3.x - Python web App 多处理/多线程连接[swagger-ui]