在2048游戏中只向右移动

时间:2019-05-27 14:05:33

标签: javascript

我有4x4矩阵,就像在2048游戏中一样,我需要向右移动一格的结果。例如,我们有矩阵:

0 0 2 2
4 4 8 4
64 64 8 4
16 8 64 8

Result is:

0 0 0 4
0 8 8 4
0 128 8 4
16 8 64 8

我的代码:

function solution(x){
  for (let i = 0; i < x.length; i++){
  	for (let j = 0; j < x.length; j++){
  	  if (x[i][j] == x[i][j+1]){
        x[i][j+1] = x[i][j+1] * 2;
        x[i][j] = 0;
      }
  	}
  }
  return JSON.stringify(x)
}
console.log(solution([[0,0,2,2], [4,4,8,4], [64,64,8,4], [16,8,64,8]]));
console.log(solution([[2,2,4,8],[8,8,64,64],[64,8,16,8],[8,8,8,8]]))
console.log(solution([[64,64,64,64],[8,8,8,8],[4,4,4,4],[2,2,2,2]]))
console.log(solution([[0,0,4,4],[4,8,4,8],[8,8,8,8],[64,8,16,8]]))
console.log(solution([[2,0,4,4],[4,4,4,4],[8,0,0,8],[0,64,64,64]]))

在第二个版本中,我在第一行[0,0,0,16]上得到错误的结果,正确的结果是[0,4,4,8,8]

3 个答案:

答案 0 :(得分:3)

您正在修改前面的数字,然后然后检查下一个数字。因此,您实际上创建了一个临时结果,因此最终结果在某种程度上说 是正确的:

+----+-------+------+------+
| id | risk1 | risk2| risk3|
+----+-------+------+------+
| 0  |   h   |   l  |   h  |
| 1  |   m   |   l  |   l  |
| 2  |   h   |   h  |   h  |
| 3  |   l   |   l  |   h  |
+----+-------+------+------+

如果您想忽略同一运行中所做的更改,则可以在发现更改后将[ 2, 2, 4, 8] -> [ 0, 4, 4, 8 ] -> [ 0, 0, 8, 8 ] -> [ 0, 0, 0, 16 ] ^ ^ ^ ^ ^ ^ ^ 增加j

每行进行所有组合的示例,将当前修改考虑在内

1

如果您只希望每行更改一次,则可以使用function solution(x) { for (let i = 0; i < x.length; i++) { for (let j = 0; j < x.length; j++) { if (x[i][j] == x[i][j + 1] && x[i][j] !== 0) { x[i][j + 1] = x[i][j] * 2; x[i][j] = 0; j += 1; } } } return JSON.stringify(x) } console.log(solution([[0,0,2,2], [4,4,8,4], [64,64,8,4], [16,8,64,8]])); console.log(solution([[2,2,4,8],[8,8,64,64],[64,8,16,8],[8,8,8,8]])) console.log(solution([[64,64,64,64],[8,8,8,8],[4,4,4,4],[2,2,2,2]])) console.log(solution([[0,0,4,4],[4,8,4,8],[8,8,8,8],[64,8,16,8]])) console.log(solution([[2,0,4,4],[4,4,4,4],[8,0,0,8],[0,64,64,64]]))

break的示例,每行仅更新 1

break

答案 1 :(得分:1)

如果要将数字向右移动,则需要从右向左遍历数组。另外,在2048年,您需要在每次合并后将所有数字移到右侧。

这将起作用:

function solution(x) {
  for (let i = 0; i < x.length; i++) {
    for (let j = x.length - 1; j >= 0; j--) {
      if (x[i][j - 1] == x[i][j]) {
        x[i][j] = x[i][j] * 2;

        // move all numbers on the left to the right by one
        if (j > 1) {
          for (let k = j - 1; k > 0; k--) {
            x[i][k] = x[i][k - 1];
          }
        }
        x[i][0] = 0;
      }
    }
  }
  return JSON.stringify(x)
}
console.log(solution([
  [0, 0, 2, 2],
  [4, 4, 8, 4],
  [64, 64, 8, 4],
  [16, 8, 64, 8]
]));
console.log(solution([
  [2, 2, 4, 8],
  [8, 8, 64, 64],
  [64, 8, 16, 8],
  [8, 8, 8, 8]
]))
console.log(solution([
  [64, 64, 64, 64],
  [8, 8, 8, 8],
  [4, 4, 4, 4],
  [2, 2, 2, 2]
]))
console.log(solution([
  [0, 0, 4, 4],
  [4, 8, 4, 8],
  [8, 8, 8, 8],
  [64, 8, 16, 8]
]))
console.log(solution([
  [2, 0, 4, 4],
  [4, 4, 4, 4],
  [8, 0, 0, 8],
  [0, 64, 64, 64]
]))

答案 2 :(得分:0)

这是一个简单的详细解决方案。 (我敢肯定,这是一种更优雅的方法):

function newState(oldState){
  // Defines an array to hold our result
  let result = [];
  // Examines each row of the old state separately
  for (let row of oldState){
    // Names the columns a, b, c, & d
    let a = row[0], b = row[1], c = row[2], d = row[3];
    // Checks for match in the rightmost pair
    if(d == c && d != 0){
      d = d * 2; // Combines matching cells
      // Checks for secondary match
      if(b == a && b != 0){
        // Traditional 2048 logic: combines matching cells *into 'c'* & shifts the rest
        c = b * 2; b = 0; a = 0;
        // For the alternative logic requested in a comment by OP,
        //   replace the above line with `c = 0; b = b * 2; a = 0;`
      }
      else{
        c = b; b = a; a = 0; // Shifts the rest
      }
    }
    else{
      // Checks for match in the middle pair
      if(c == b && c != 0){
        c = c * 2; b = a; a = 0; // Combines matching cells and shifts the rest
      }
      else{
        // Checks for match in the leftmost pair
        if(b == a && b != 0){
          b = b * 2; a = 0; // Combines matching cells and shifts the rest
        }
      }
    }
    // Populates the current row in the result matrix
    result.push([a, b, c, d]);
  }
  // Returns the result matrix
  return JSON.stringify(result);
}
  
console.log(newState([[0,0,2,2], [4,4,8,4], [64,64,8,4], [16,8,64,8]]));
console.log(newState([[2,2,4,8],[8,8,64,64],[64,8,16,8],[8,8,8,8]]));
console.log(newState([[64,64,64,64],[8,8,8,8],[4,4,4,4],[2,2,2,2]]));
console.log(newState([[0,0,4,4],[4,8,4,8],[8,8,8,8],[64,8,16,8]]));
console.log(newState([[2,0,4,4],[4,4,4,4],[8,0,0,8],[0,64,64,64]]));