Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package games.game2048
- import board.Cell
- import board.Direction
- import board.Direction.*
- import board.GameBoard
- import board.createGameBoard
- import games.game.Game
- /*
- * Your task is to implement the game 2048 https://en.wikipedia.org/wiki/2048_(video_game).
- * Implement the utility methods below.
- *
- * After implementing it you can try to play the game running 'PlayGame2048'.
- */
- fun newGame2048(initializer: Game2048Initializer<Int> = RandomGame2048Initializer): Game =
- Game2048(initializer)
- class Game2048(private val initializer: Game2048Initializer<Int>) : Game {
- private val board = createGameBoard<Int?>(4)
- override fun initialize() {
- repeat(2) {
- board.addNewValue(initializer)
- }
- }
- override fun canMove() = board.any { it == null }
- override fun hasWon() = board.any { it == 2048 }
- override fun processMove(direction: Direction) {
- if (board.moveValues(direction)) {
- board.addNewValue(initializer)
- }
- }
- override fun get(i: Int, j: Int): Int? = board.run { get(getCell(i, j)) }
- }
- /*
- * Add a new value produced by 'initializer' to a specified cell in a board.
- */
- fun GameBoard<Int?>.addNewValue(initializer: Game2048Initializer<Int>) {
- val (c, i) = initializer.nextValue(this)!!
- this[c] = i
- }
- /*
- * Update the values stored in a board,
- * so that the values were "moved" in a specified rowOrColumn only.
- * Use the helper function 'moveAndMergeEqual' (in Game2048Helper.kt).
- * The values should be moved to the beginning of the row (or column),
- * in the same manner as in the function 'moveAndMergeEqual'.
- * Return 'true' if the values were moved and 'false' otherwise.
- */
- @OptIn(ExperimentalStdlibApi::class)
- fun GameBoard<Int?>.moveValuesInRowOrColumn(rowOrColumn: List<Cell>): Boolean {
- val c1 = rowOrColumn.size
- val values = mutableListOf<Int?>()
- for (c in rowOrColumn) {
- values.add(this[c]) // build the current values list of rowOrColumn cells list
- }
- val res = values.moveAndMergeEqual { it*2 }.toMutableList<Int?>()
- val c2 = res.size
- val i = res.iterator()
- for (c in rowOrColumn){
- if (i.hasNext()){
- this[c]=res.removeFirst() // override the current values with the move and merged values
- } else this[c]=null // add null to fill it up
- }
- return c2!=c1 // have the values been changed
- }
- /*
- * Update the values stored in a board,
- * so that the values were "moved" to the specified direction
- * following the rules of the /////2048 game .
- * Use the 'moveValuesInRowOrColumn' function above.
- * Return 'true' if the values were moved and 'false' otherwise.
- */
- fun GameBoard<Int?>.moveValues(direction: Direction): Boolean { // thanks mook2_y2
- fun getRowOrColumn(direction: Direction, index:Int):List<Cell>{
- return when(direction){
- UP -> this.getColumn(1.. this.width, index)
- DOWN -> this.getColumn(this.width downTo 1, index)
- RIGHT -> this.getRow(index, this.width downTo 1)
- LEFT -> this.getRow(index, 1..this.width)
- }
- }
- return (1 .. this.width).fold(false){ isMoved, idx ->
- this.moveValuesInRowOrColumn(getRowOrColumn(direction, idx))||isMoved
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement