Search code examples
kotlintornadofx

How to add a pieces to a checkers board using Kotlin and TornadoFX


I am currently trying to develop a little board game in Kotlin using TornadoFX, mainly for practice purposes. But I'm currently struggling with adding the pieces to the board. I have tried many different ways that seemed logical to me, but every time its ether giving me an error message or not working at all. So as I ran out of ideas or possible solutions I'm now asking for help here on stackoverflow (for the first time ever :-)).

What's the best way or the most idiomatic way of adding pieces to a checkers board from the Controller? I also should mention that I haven't really worked with JavaFX before, so I'm learning TornadoFX from the ground up.

Here is the View of my board:

class BoardView : View("BoardView")
{
    private val numberOfCells = 8
    private val cellSize = 100.0
    private val controller: BoardController by inject()

    override val root = pane {
        prefHeight = cellSize * numberOfCells
        prefWidth = cellSize * numberOfCells

        gridpane {
            for (y in 0 until numberOfCells)
            {
                row {
                    for (x in 0 until numberOfCells)
                    {
                        if ((x + y) % 2 == 0)
                        {
                            add(ChessCell(cellSize, true, x, y))
                        }
                        else
                        {
                            add(ChessCell(cellSize, false, x, y))
                        }
                    }
                }
            }
        }
    }
}

My pieces Fragment:

class Stone(light: Boolean, x: Double, y: Double): Fragment()
{
    private val colorLight = c("E4E4E4")
    private val colorDark = c("1B1B1B")

    override val root = pane {
        circle {
            fill = if (light)
            {
                colorLight
            }
            else
            {
                colorDark
            }
            radius = 30.0
            centerY = y
            centerX = x
        }
    }
}

And finally my Controller class:

class BoardController(): Controller()
{
    private val board: BoardView by inject()

    fun startGame()
    {
        // How to place a Stone (aka piece) from here?
    }
}

Would really appreciate any help <3


Solution

  • After a week of work I finally got a solution. I will post it here, in case someone needs it in the future.

    I have added the following function inside the BoardView:

    fun placeStone(light: Boolean, xPos: Double, yPos: Double)
    {
        with(root)
        {
            this += Stone(light, xPos, yPos)
        }
    } 
    

    And I changed up my function startGame() inside my BoardController:

    fun startGame()
    {
        for (x in 0 until 8)
        {
            for (y in 0 until 8)
            {
                if ((x + y) % 2 != 0)
                {
                    if (y < 3)
                    {
                        board.placeStone(false, x, y)
                    }
                    else if (y > 4)
                    {
                        board.placeStone(true, x, y)
                    }
                 }
             }
         }
    }