2048: Difference between revisions

Content added Content deleted
No edit summary
Line 1,633: Line 1,633:


fun shiftCellsLeft(grid: Array<Array<Int>>): Array<Array<Int>> =
fun shiftCellsLeft(grid: Array<Array<Int>>): Array<Array<Int>> =
grid.map { row -> mergeAndShiftCells(row) }.toTypedArray()
grid.map { row -> mergeAndOrganizeCells(row) }.toTypedArray()


fun shiftCellsRight(grid: Array<Array<Int>>): Array<Array<Int>> =
fun shiftCellsRight(grid: Array<Array<Int>>): Array<Array<Int>> =
grid.map { row -> mergeAndShiftCells(row.reversed().toTypedArray()).reversed().toTypedArray() }.toTypedArray()
grid.map { row -> mergeAndOrganizeCells(row.reversed().toTypedArray()).reversed().toTypedArray() }.toTypedArray()


fun shiftCellsUp(grid: Array<Array<Int>>): Array<Array<Int>> {
fun shiftCellsUp(grid: Array<Array<Int>>): Array<Array<Int>> {
Line 1,649: Line 1,649:


rows.map { row ->
rows.map { row ->
mergeAndShiftCells(row)
mergeAndOrganizeCells(row)
}.forEachIndexed { rowIdx, row ->
}.forEachIndexed { rowIdx, row ->
updatedGrid[0][rowIdx] = row[0]
updatedGrid[0][rowIdx] = row[0]
Line 1,671: Line 1,671:


rows.map { row ->
rows.map { row ->
mergeAndShiftCells(row)
mergeAndOrganizeCells(row)
}.forEachIndexed { rowIdx, row ->
}.forEachIndexed { rowIdx, row ->
updatedGrid[3][rowIdx] = row[0]
updatedGrid[3][rowIdx] = row[0]
Line 1,682: Line 1,682:
}
}


fun mergeAndShiftCells(row: Array<Int>): Array<Int> = organize(merge(row.copyOf()))
fun mergeAndOrganizeCells(row: Array<Int>): Array<Int> = organize(merge(row.copyOf()))


fun merge(row: Array<Int>, idxToMatch: Int = 0, idxToCompare: Int = 1): Array<Int> {
fun merge(row: Array<Int>, idxToMatch: Int = 0, idxToCompare: Int = 1): Array<Int> {