scala - 将苹果放在用 Scala 编写的蛇游戏中
问题描述
要将苹果放在一个空的地方,我使用emptySpots()方法来定位空的或空闲的单元格的点。
当我实现下面的代码时,我的游戏运行良好:
def emptySpots() : Point = {
val emptyPointsList = new ArrayBuffer[Point]()
var emptyLocation : Point = new Point(0,0)
emptyLocation = Point(random.randomInt(upTo = 25), random.randomInt(upTo = 25))
emptyLocation
}
但是,当我尝试实现下面的代码时,它会显示错误:
def emptySpots() : Point = {
val emptyPointsList = new ArrayBuffer[Point]()
var emptyLocation : Point = new Point(0,0)
for(i <- 0 until gridDims.height) {
for(j <- 0 until gridDims.width) {
if(getCellType(Point(i, j)) == Empty()) {
emptyPointsList += Point(i, j)
}
}
}
if(!emptyPointsList.isEmpty) {
emptyLocation = emptyPointsList(random.randomInt(emptyPointsList.size))
}
emptyLocation
}
如果有人能指出我的第二个实现中有什么问题,向我展示我的第一个实现可以正常工作的错误,我将不胜感激。
PS这里的randomInt()没有错,因为它被导入的包和库支持
解决方案
推荐阅读
- arrays - 如何将单元格数组分配给嵌套结构中的字段?
- vb6 - 如何使用按钮将数据从复选框值保存到数据库
- apache - 如何使用 Apache 代理转发 ttyd
- python - 在 pandas 中提取并组合街道地址和公寓号码
- reactjs - 如何通过更改 url 来阻止用户访问网页
- node.js - 如何在 AWS Lambda 中设置环境变量或使用即时客户端库
- javascript - 检查客户端浏览器是否为 en-US、en-EG、en-AU、en-GB、en-CA 然后显示英文文件?
- android - Android - 如何使用一些 URL 深度链接到应用程序,然后在不使用 WebView 的情况下打开相同的 URL 应用程序内本机浏览器?
- java - 如何编写在三种颜色之间进行选择以生成特定图像的条件语句
- java - 打开项目时,intellij 中的构建选项将灰显