我正在学习制作井字游戏的教程,并给了我这个错误:
build fail Caused by: C:\Users\zavie\Downloads\Final Project\app\src\main\res\values\Board.kt: Error: The file name must end with .xml
at com.android.ide.common.resources.MergingException.throwIfNonEmpty(MergingException.java:166)
at com.android.ide.common.resources.DataSet.loadFromFiles(DataSet.java:262)
at com.android.build.gradle.tasks.MergeResources.lambda$doFullTaskAction$0(MergeResources.java:238)
at com.android.build.gradle.internal.tasks.Blocks.recordSpan(Blocks.java:58)
at com.android.build.gradle.tasks.MergeResources.doFullTaskAction(MergeResources.java:232)
... 92 more
我不知道为什么其他所有内容都可以查看,我看到了其他几篇文章,但它们都与字体或png相关。任何帮助将不胜感激,因为我是kotlin和android studio的新手。
我的代码:
package values
class Board {
//Strings for PLAYER and COMPUTER
companion object {
const val PLAYER = "O"
const val COMPUTER = "X"
}
// internal board
//and for this we used a 3 by 3 array of Strings
val board = Array(3) { arrayOfNulls<String>(3) }
//a list of all the empty cells
val availableCells: List<Cell>
get() {
val cells = mutableListOf<Cell>()
for (i in board.indices) {
for (j in board.indices) {
if (board[i][j].isNullOrEmpty()) {
cells.add(Cell(i, j))
}
}
}
return cells
}
//if the game is over or not
val isGameOver: Boolean
get() = hasComputerWon() || hasPlayerWon() || availableCells.isEmpty()
//Weather the computer or player has won or not
fun hasComputerWon(): Boolean {
if (board[0][0] == board[1][1] &&
board[0][0] == board[2][2] &&
board[0][0] == COMPUTER ||
board[0][2] == board[1][1] &&
board[0][2] == board[2][0] &&
board[0][2] == COMPUTER
) {
return true
}
for (i in board.indices) {
if (
board[i][0] == board[i][1] &&
board[i][0] == board[i][2] &&
board[i][0] == COMPUTER ||
board[0][i] == board[1][i] &&
board[0][i] == board[2][i] &&
board[0][i] == COMPUTER
) {
return true
}
}
return false
}
fun hasPlayerWon(): Boolean {
if (board[0][0] == board[1][1] &&
board[0][0] == board[2][2] &&
board[0][0] == PLAYER ||
board[0][2] == board[1][1] &&
board[0][2] == board[2][0] &&
board[0][2] == PLAYER
) {
return true
}
for (i in board.indices) {
if (
board[i][0] == board[i][1] &&
board[i][0] == board[i][2] &&
board[i][0] == PLAYER ||
board[0][i] == board[1][i] &&
board[0][i] == board[2][i] &&
board[0][i] == PLAYER
) {
return true
}
}
return false
}
//in this var we will store the computersMove
var computersMove: Cell? = null
//this is our minimax function to calculate
//the best move for the computer
fun minimax(depth: Int, player: String): Int {
if (hasComputerWon()) return +1
if (hasPlayerWon()) return -1
if (availableCells.isEmpty()) return 0
var min = Integer.MAX_VALUE
var max = Integer.MIN_VALUE
for (i in availableCells.indices) {
val cell = availableCells[i]
if (player == COMPUTER) {
placeMove(cell, COMPUTER)
val currentScore = minimax(depth + 1, PLAYER)
max = Math.max(currentScore, max)
if (currentScore >= 0) {
if (depth == 0) computersMove = cell
}
if (currentScore == 1) {
board[cell.i][cell.j] = ""
break
}
if (i == availableCells.size - 1 && max < 0) {
if (depth == 0) computersMove = cell
}
} else if (player == PLAYER) {
placeMove(cell, PLAYER)
val currentScore = minimax(depth + 1, COMPUTER)
min = Math.min(currentScore, min)
if (min == -1) {
board[cell.i][cell.j] = ""
break
}
}
board[cell.i][cell.j] = ""
}
return if (player == COMPUTER) max else min
}
//this function is placing a move in the given cell
fun placeMove(cell: Cell, player: String) {
board[cell.i][cell.j] = player
}
}
答案 0 :(得分:2)
请检查 res 文件夹中的 values 文件夹。可能存在 .kt 文件,请删除该文件或将其放在其他 .kt 文件所在的Java文件夹中。 在res文件夹中,您可以放置xml或png,jpg文件,但不能放置Java或kotlin文件。
答案 1 :(得分:1)
您已将Board.kt
文件放在res/values/
文件夹中。它应该放在java.com.YOUR_PACKAGE_NAME
文件夹中。