Skip to content

Commit

Permalink
按键控制数字位移
Browse files Browse the repository at this point in the history
  • Loading branch information
DacianSky committed Oct 6, 2016
1 parent 791fd4b commit b972f8f
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"window":{
"backgroundTextStyle":"light",
"navigationBarBackgroundColor": "#faf8ef",
"navigationBarTitleText": "2048 <Author: SDQ>",
"navigationBarTitleText": "2048 <Author: Thou>",
"navigationBarTextStyle":"black"
}
}
190 changes: 169 additions & 21 deletions pages/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,190 @@ var util = require('../../utils/util.js')
Page({
data: {
chessboardDatas: [
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
[2,0,2,0],
[0,2,0,0],
[2,4,2,0],
[0,0,2,4]
]
},
getChessboardCellNum: function(index){
reset: function(){
var chessDefaultDatas = [
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
] ;

var maxInitNum = util.getRandomNum(1,8);
while(maxInitNum>0){
var num = util.getRandomNum(0,15);
this.setChessboardCellNum(chessDefaultDatas,num,2);
maxInitNum--;
}

this.setData({
chessboardDatas: chessDefaultDatas
});
},
getChessboardCellNum: function(array,index){
var loopCount = 0;
for (var i = 0; i < this.data.chessboardDatas.length; i++) {
for (var j = 0; j < this.data.chessboardDatas[i].length; j++) {
var cell;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
if (index == loopCount++) {
cell = this.data.chessboardDatas[i][j];
cell = array[i][j];
}
}
}
return cell;
},
setChessboardCellNum: function(index,num){
setChessboardCellNum: function(array,index,num){
var loopCount = 0;
for (var i = 0; i < this.data.chessboardDatas.length; i++) {
for (var j = 0; j < this.data.chessboardDatas[i].length; j++) {
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
if (index == loopCount++) {
this.data.chessboardDatas[i][j] = num;
array[i][j] = num;
}
}
}
},
generateNewCellNum: function(chessboardDatas){
// 1.求出所有剩余空元素
var remainNullCellNum = 0;
for (var i = 0; i < chessboardDatas.length; i++) {
for (var j = 0; j < chessboardDatas[i].length; j++) {
if (chessboardDatas[i][j]==0) {
remainNullCellNum++;
}
}
}
// 2.随机位置产生一个数
var newCellNumIndex = util.getRandomNum(0,remainNullCellNum);
var count = 0;
for (var i = chessboardDatas.length-1; i >= 0; i--) {
for (var j = chessboardDatas[i].length-1 ; j >= 0; j--) {
if (chessboardDatas[i][j]!=0) {
continue;
}
count++;
if (newCellNumIndex == count) {
chessboardDatas[i][j] = 2;
}
}
}
},
onLoad: function(options) {
var num = util.getRandomNum(0,15);
console.log(num);
this.setChessboardCellNum(num,2);

num = util.getRandomNum(0,15);
console.log(num);
this.setChessboardCellNum(num,4);

console.log(this.data.chessboardDatas);
// wx.onAccelerometerChange(function (res) {
// console.log(res.x)
// console.log(res.y)
// console.log(res.z)
// })
},
turnEnd: function(chessboardDatas){
this.generateNewCellNum(chessboardDatas);
this.setData({
chessboardDatas:chessboardDatas
})
util.scan_array(this.data.chessboardDatas);
},
// FIXME: 不在一起的时候不会合并
turnUp: function(){
var chessboardDatas = this.data.chessboardDatas;
for (var i = 0; i < chessboardDatas.length-1; i++) {
for (var j = 0; j < chessboardDatas[i].length; j++) {
if(chessboardDatas[i][j] == chessboardDatas[i+1][j]){
chessboardDatas[i][j]=chessboardDatas[i][j]+chessboardDatas[i+1][j];
chessboardDatas[i+1][j]=0;
this.reorder_up(chessboardDatas);
}
}
}
this.turnEnd(chessboardDatas);
},
turnDown: function(){
var chessboardDatas = this.data.chessboardDatas;
for (var i = chessboardDatas.length-1; i >= 1; i--) {
for (var j = chessboardDatas[i].length-1 ; j >= 0; j--) {
if(chessboardDatas[i][j] == chessboardDatas[i-1][j]){
chessboardDatas[i][j]=chessboardDatas[i][j]+chessboardDatas[i-1][j];
chessboardDatas[i-1][j]=0;
this.reorder_down(chessboardDatas);
}
}
}
this.turnEnd(chessboardDatas);
},
turnLeft: function(){var chessboardDatas = this.data.chessboardDatas;
for (var j = 0; j < chessboardDatas.length - 1; j++) {
for (var i = 0; i < chessboardDatas.length ; i++) {
if(chessboardDatas[i][j] == chessboardDatas[i][j+1]){
chessboardDatas[i][j]=chessboardDatas[i][j]+chessboardDatas[i][j+1];
chessboardDatas[i][j+1]=0;
this.reorder_left(chessboardDatas);
}
}
}
this.turnEnd(chessboardDatas);
},
turnRight: function(){
var chessboardDatas = this.data.chessboardDatas;
for (var j = chessboardDatas.length-1 ; j >= 0; j--) {
for (var i = chessboardDatas.length-1; i >= 1; i--) {
if(chessboardDatas[i][j] == chessboardDatas[i][j-1]){
chessboardDatas[i][j]=chessboardDatas[i][j]+chessboardDatas[i][j-1];
chessboardDatas[i][j-1]=0;
this.reorder_right(chessboardDatas);
}
}
}
this.turnEnd(chessboardDatas);
},
reorder_up: function(chessboardDatas){
for (var i = 0; i < chessboardDatas.length; i++) {
for (var j = 0; j < chessboardDatas[i].length; j++) {
var rowIndex = i;
while(rowIndex - 1 >=0 && chessboardDatas[rowIndex-1][j]==0){
chessboardDatas[rowIndex-1][j] = chessboardDatas[rowIndex][j];
chessboardDatas[rowIndex][j] = 0;
rowIndex--;
}
}
}
},
reorder_down: function(chessboardDatas){
for (var i = chessboardDatas.length - 2; i >= 0; i-- ) {
for (var j = chessboardDatas[i].length - 1; j >= 0; j--) {
var rowIndex = i;
while(rowIndex + 1 <= chessboardDatas.length - 1 && chessboardDatas[rowIndex+1][j]==0){
chessboardDatas[rowIndex+1][j] = chessboardDatas[rowIndex][j];
chessboardDatas[rowIndex][j] = 0;
rowIndex++;
}
}
}
},
reorder_left: function(chessboardDatas){
for (var j = 0; j < chessboardDatas.length ; j++) {
for (var i = 0; i <chessboardDatas[j].length; i++) {
var rowIndex = j;
while(rowIndex - 1 >=0 && chessboardDatas[i][rowIndex-1]==0){
chessboardDatas[i][rowIndex-1] = chessboardDatas[i][rowIndex];
chessboardDatas[i][rowIndex] = 0;
rowIndex--;
}
}
}
},
reorder_right: function(chessboardDatas){
for (var j = chessboardDatas.length - 2; j >= 0; j--) {
for (var i = chessboardDatas.length - 1; i >= 0; i-- ) {
var rowIndex = j;
while(rowIndex + 1 <= chessboardDatas.length - 1 && chessboardDatas[i][rowIndex+1]==0){
chessboardDatas[i][rowIndex+1] = chessboardDatas[i][rowIndex];
chessboardDatas[i][rowIndex] = 0;
rowIndex++;
}
}
}
}
})
25 changes: 20 additions & 5 deletions pages/index/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
</View>
</View>

<View class="restart center">
<Image src="../../images/restart.png"></Image>
</View>
<Button class="restart center" bindtap="reset" plain="YES">
<Image src="../../images/restart.png"></Image>
</Button>

<View class="best">
<View class="bestContent">
Expand All @@ -20,10 +20,25 @@

<!-- View.chessboard>View.row*4>View.column*4 -->

<View class="chessboard">
<View class="chessboard" catchtouchstart="touchstart" catchtouchend="touchend">
<View class="row" wx:for="{{chessboardDatas}}" wx:for-index="idx" wx:for-item="chessboardColumn">
<View class="column" wx:for="{{chessboardColumn}}" wx:for-item="chessboardRow">{{chessboardRow>0?chessboardRow:''}}</View>
</View>
</View>

<View class="primary-text center">ps: 合并数字得到一个2048!</View>
<View class="direction">
<Button class="directionBtn" bindtap="turnUp" hover-class="directionBtn-hover">
</Button>
<Button class="directionBtn" bindtap="turnDown" hover-class="directionBtn-hover">
</Button>
<Button class="directionBtn" bindtap="turnLeft" hover-class="directionBtn-hover">
</Button>
<Button class="directionBtn" bindtap="turnRight" hover-class="directionBtn-hover">
</Button>
</View>

<View class="tips center">ps: 合并数字得到一个2048!</View>
38 changes: 37 additions & 1 deletion pages/index/index.wxss
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ page{
}
.info .restart
{
flex: 1;
/*flex: 1;*/
/*background-color: transparent;
border-color: transparent;*/
border-width: 0;
}

.info .restart Image
Expand Down Expand Up @@ -117,4 +120,37 @@ page{
flex-direction: column;
text-align: center;
justify-content: center;
}

/* 控制键 */
.direction
{
display: flex;
flex-direction: row;
text-align: center;
justify-content: center;
}

.direction .directionBtn
{
color: #fff;
display: inline-block;
border-width: 1px;
border-color: #bbada0;
background-color: #cdc1b4;
}

.direction .directionBtn-hover
{
color: #000;
border-color: #cdc1b4;
background-color: #bbada0;
}

/* 提示 */
.tips
{
font-size: 14px;
color: #cdc1b3;
margin-top: 20px;
}
21 changes: 20 additions & 1 deletion utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,26 @@ function getRandomNum(Min,Max){
return(Min + Math.round(Rand * Range));
}

function swapItems(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
};

function scan_array(arr) {
var str = '';
for(var key in arr) {
if(typeof(arr[key]) == 'array' || typeof(arr[key]) == 'object') {// 递归调用
scan_array(arr[key]);
} else {
str = str + ' ' + arr[key];
}
}
console.log(str);
}

module.exports = {
formatTime: formatTime,
getRandomNum: getRandomNum
getRandomNum: getRandomNum,
swapItems: swapItems,
scan_array: scan_array
}

0 comments on commit b972f8f

Please sign in to comment.