首页 > 解决方案 > Android Studio 文件名必须以 .xml 结尾

问题描述

我正在关注制作井字游戏的教程,它给了我这个错误:

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
    }

}

标签: androidxml

解决方案


请检查res文件夹中的values文件夹。可能有.kt文件,请将其删除或放在其他.kt文件所在的 java 文件夹中。在 res 文件夹中,您可以放置​​ xml 或 png、jpg 文件,但不能放置 Java 或 kotlin 文件。


推荐阅读