zavier vaidya
zavier vaidya

Reputation: 13

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. Any help would be appreciated as I'm fairly new to kotlin and 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
    }

}

Upvotes: 0

Views: 621

Answers (2)

Qazi Fahim Farhan
Qazi Fahim Farhan

Reputation: 2176

You have placed the Board.kt file inside res/values/ folder. It should be placed in java.com.YOUR_PACKAGE_NAME folder.

Upvotes: 1

Avinash tiwari
Avinash tiwari

Reputation: 269

Please check the values folder inside res folder . There may be .kt file , please remove it or place it on inside java folder where other .kt files are . In res folder you can place xml or png,jpg files but not Java or kotlin files.

Upvotes: 2

Related Questions