前段時間發現網上有不少收費或公開課都有教用 js 作 2048 小遊戲的,而後本身就也想動手作一個,作這個小遊戲主要是爲了鍛鍊本身的邏輯能力,也算是對以前一些學習的總結吧 html
注:vue
這個版本是用原生 ES6 寫的,只實現了遊戲邏輯中的 1,2,3,4;
初版地址:https://github.com/yhtx1997/S...git
以後是打算用 vue 從新寫一遍,而且完善下,由於長時間用原生,致使 vue 有些生疏,藉此機會從新溫習下github
let arr = [ [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ];
let s = 0; let create = () => { let x = Math.floor(Math.random() * 4); let y = Math.floor(Math.random() * 4); // console.log(s) if (s > 100) { s = 0; return; } if (arr[x][y] == 0) { if (Math.floor(Math.random() * 10) % 2 == 0) { arr[x][y] = 2; } else { arr[x][y] = 4; } s = 0; return; } else { s++; return create(); } }
let updateHtml = () => { //獲取元素 let warp = document.getElementById('warp'); let html = ''; //將數據轉換爲 HTML for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { html += `<div class='c c-${arr[i][j]}'>${arr[i][j]==0?'':arr[i][j]}</div>`; } } //將 數據轉換的 HTML 渲染到頁面 warp.innerHTML = html; }
window.onkeydown = (e) => { switch (e.keyCode) { case 37: // ← console.log('←'); arr = new move(arr).moveLeft(); create(); //隨機位置新建 updateHtml(); //更新數據到頁面 break; case 38: // ↑ console.log('↑'); arr = new move(arr).moveUp(); create(); //隨機位置新建 updateHtml(); //更新數據到頁面 break; case 39: // → console.log('→'); arr = new move(arr).moveRight(); create(); //隨機位置新建 updateHtml(); //更新數據到頁面 break; case 40: // ↓ console.log('↓'); arr = new move(arr).moveDown(); create(); //隨機位置新建 updateHtml(); //更新數據到頁面 break; } }
export default function ClearZero (arr){//去零 let clearZero = [[],[],[],[]]; for (let i = 0; i < 4; i++) { for (let j = 0; j < 4; j++) { if (arr[i][j] != 0) { clearZero[i].push(arr[i][j]) } } } return clearZero; }
分組數組
import Deduplication from './deduplication';//將相鄰且相同的數字相加 export default class Grouping{//將相鄰的相同數字分組 constructor(clearZero){ this.clearZero = clearZero; } left(){ let newarr = [[],[],[],[]]; for (let j = 0; j < this.clearZero.length; j++) { let grouping = []; let i = 0; //將重複的 分到一組 while (i < this.clearZero[j].length) { if (this.clearZero[j][i] == this.clearZero[j][i + 1]) { grouping.push([this.clearZero[j][i], this.clearZero[j][i + 1]]); i += 2; } else { grouping.push(this.clearZero[j][i]); i++; } } //去重複 newarr[j] = Deduplication(grouping); } return newarr; } right(){ let newarr = [[],[],[],[]]; for (let i = 0; i < this.clearZero.length; i++) { let grouping = []; let j = this.clearZero[i].length - 1; //將重複的 分到一組 while (j >= 0) { if (this.clearZero[i][j] == this.clearZero[i][j - 1]) { grouping.unshift([this.clearZero[i][j], this.clearZero[i][j - 1]]); j -= 2; } else { grouping.unshift(this.clearZero[i][j]); j--; } } //將重複的進行計算 newarr[i] = Deduplication(grouping); } return newarr; } }
相加dom
export default function Deduplication (grouping){//將相鄰且相同的數字相加 for (let i = 0; i < grouping.length; i++) { if (typeof grouping[i] == 'object') { grouping[i] = grouping[i][0] + grouping[i][1]; } } return grouping; }
export default function AddZero (newarr,w){//加零 for (let i = 0; i < newarr.length; i++) { while (newarr[i].length != 4) { if(w == 'l'){ newarr[i].push(0); }else if(w == 'r'){ newarr[i].unshift(0); } } } return newarr; }
將 Y 軸的處理轉換成 X 軸的處理函數
export default function turn(arr) {//將數組轉一下 let clearZero = [[],[],[],[]]; for (let i = 0; i < 4; i++) { for (let j = 0; j < 4; j++) { clearZero[i][j] = arr[j][i] } } return clearZero; }
等處理完成後再調用上邊的函數,將 X 軸的處理結果轉換回 Y 軸的表現方式學習