Skip to content

pradeexsu/2048

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

image

transform board with roatation.

Test case

[2,0,2,0] -> [0,0,0,4]
[2,2,2,0] -> [0,0,2,4]
[2,2,2,4] -> [0,2,4,4]
[4,4,4,4] -> [0,0,8,8]
[8,4,2,0] -> [0,8,4,2]

Transform Logic

function swipe(row) {
    let n = row.length
    let last_merge = -1
    let score = 0
    for (let left = 1; left < n; left++) {

        if (row[left] === 0)
            continue
        else {
            // non zero value in iteration ...
            for (let i = left - 1; i > last_merge; i--) {
                if (row[i] === row[left]) {
                    row[i] *= 2;
                    row[left] = 0
                    score += row[i]
                    last_merge = i
                    break
                } else if (i === 0 && row[i] === 0) {
                    row[i] = row[left]
                    row[left] = 0;
                } else if (row[i] !== 0) {
                    row[i + 1] = row[left];
                    if (i + 1 != left) {
                        row[left] = 0
                    }
                    break
                } else if (last_merge + 1 == i) {
                    row[i] = row[left];
                    row[left] = 0
                    break
                }
            }
        }
    }
    return score
}

About

2048 is a single-player sliding tile puzzle video game.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published