简体   繁体   English

Android Studio 文件名必须以.xml结尾

[英]Android Studio File name must end with .xml

I'm following a tutorial for making a tic tac toe game and its giving me this error:我正在学习制作井字游戏的教程,它给了我这个错误:

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

I can't figure out why everything else checks out i saw a few other post but they were all in relation to fonts or pngs.我不明白为什么其他一切都检查出来我看到了其他一些帖子,但它们都与字体或 png 有关。 Any help would be appreciated as I'm fairly new to kotlin and android studio.任何帮助将不胜感激,因为我对 kotlin 和 android studio 还很陌生。

My code:我的代码:

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
    }

}

Please check the values folder inside res folder.请检查res文件夹中的values文件夹。 There may be .kt file, please remove it or place it on inside java folder where other .kt files are.可能有.kt文件,请将其删除或将其放在其他.kt文件所在的 java 文件夹中。 In res folder you can place xml or png,jpg files but not Java or kotlin files.在 res 文件夹中,您可以放置 xml 或 png、jpg 文件,但不能放置 Java 或 kotlin 文件。

You have placed the Board.kt file inside res/values/ folder.您已将Board.kt文件放在res/values/文件夹中。 It should be placed in java.com.YOUR_PACKAGE_NAME folder.它应该放在java.com.YOUR_PACKAGE_NAME文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM