代码拉取完成,页面将自动刷新
/**
* Created by Administrator on 2014/7/6.
*/
function game2048()
{
var Ex, Ey, Sx, Sy;
var touchkey;
var keychar;
var phone=false;
$(function () {
document.getElementById("div2048").addEventListener('touchstart', touchStart);
document.getElementById("div2048").addEventListener('touchmove', touchMove);
document.getElementById("div2048").addEventListener('touchend', touchEnd);
});
function touchStart(e) {
e.preventDefault();
Sx = e.touches[0].pageX;
Sy = e.touches[0].pageY;
}
function touchMove(e) {
e.preventDefault();
Ex = e.touches[0].pageX;
Ey = e.touches[0].pageY;
}
function touchEnd(e) {
/*alert("touchend");
alert(Sx);
alert(Sy);
alert(Ex);
alert(Ey);*/
var Cx=Ex-
Sx;
var Cy=Ey - Sy;
var ch='Z'
/* alert(Math.abs(Cy));
alert(Cy);
alert(Math.abs(Cx));
alert(Cx);*/
if ((Math.abs(Cy)>Math.abs(
Cx))&&(Cy<0)) ch
='W';
else if(( Math.abs(Cy) >Math.abs(Cx)
)&&(Cy>0)) ch= 'S'; else if((Math.abs(Cx) > Math.abs(Cy))&&(Cx>0)) ch='D';
else if(( Math.abs(Cx) >Math.abs(Cy))&&(Cx<0
)) ch= 'A';
if ((Cx>250)&&(Cy>250)) ch='H';
if ((Cx>250)&&(Cy<-250)) ch='B';
keychar=ch;
phone=true;
merge();
}
var Mymodel=document.getElementById('myModal');
function modelon()
{
Mymodel.className='modal';
Myscore.innerHTML=score;
Historyscore.innerHTML=historyscore;
}
function modeloff()
{
Mymodel.className='modal fade'
}
// $('MymyModal2').modal('hide');
// alert("game2048");
var container;
var startBtn;
var contrlscore;
var changebackground1;
var changebackgroundnum=0;
var changebackgroundarray1=new Array(3);
// changebackgroundarray1[0]="RGB(193,255,249)";
changebackgroundarray1[0]="RGB(149,195,228)";
changebackgroundarray1[1]="RGB(255,247,223)";
changebackgroundarray1[2]="RGB(255,231,200)";
var changebackground2;
var changebackgroundarray2=new Array(3);
changebackgroundarray2[0]="RGB(149,195,228)";
changebackgroundarray2[1]="RGB(187,173,160)";
changebackgroundarray2[2]="RGB(233,184,152)";
var changebackground3;
var changebackgroundarray3=new Array(3);
changebackgroundarray3[0]="RGB(137,187,223)";
changebackgroundarray3[1]="RGB(204,192,178)";
changebackgroundarray3[2]="RGB(232,157,120)";
var createElementlocal;
var tiles=new Array(16);
var blocks=new Array([0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]);
var ie =navigator.appName=="Microsoft Internet Explorer"?true:false;
document.onkeydown = keyDown;
var realkey;
var nkey;
var historyscore=0;
var Myscore=document.getElementById('Myscore');
var Historyscore=document.getElementById('Historyscore');
var score;
init();
restart();
// alert("ss");
function merge()
{
if (phone==false) keychar = String.fromCharCode(nkey);
// alert(keychar);
switch (keychar)
{
case 'A' :left();break;
case 'S' :down();break;
case 'W' :up();break;
case 'D' :right();break;
case 'B' :chb();break;
case 'H' :boundmodel();break;
}
if (fail()!=-1) {
if ((keychar=='A')||(keychar=='S')||(keychar=='W')||(keychar=='D')) { createnewblock();showdisplay();}
}
else {restart();}
}
function chb()
{
// alert("changbk");
// changebackground.style.background="grey";
// changebackground.style.backgroundColor=changebackgroundarray[changebackgroundnum];
changebackgroundnum++;
changebackgroundnum=changebackgroundnum%3;
changebackground1.style.background=changebackgroundarray1[changebackgroundnum];
// alert(changebackgroundarray1[0]);
// alert(changebackgroundnum);
// changebackground2.style.background=changebackgroundarray2[changebackgroundnum];
// changebackground3.style.background="grey";
}
function fail()
{
/* int flag=-1; //die
for (int i=0;i<4;i++)
for(int j=0;j<4;j++)
if (blocks[i][j].value==0) flag=i*4+j; //live
if (flag==-1) {System.out.println("you die");}
return flag;*/
var flag=-1;
var a=findavailableblock();
if (a[16]>=1) {flag=1;}
else {up();down();right();left();
showdisplay();
var b=findavailableblock();
if (b[16]>0)
{flag=1;}else
{
if (score>historyscore ) historyscore=score;
alert('没有可移动的方块了哦O(∩_∩)O~\n您的分数为: '+score+'\n历史最高分为: '+historyscore);
flag=-1;
}
}
return flag;
}
function createnewblock()
{
// Random rand=new Random();
var a=new Array(17);
a=findavailableblock();
// alert(a[16]);
// alert(a[1]);
// alert(a[13]);
if (a[16]>=1) {
var randnum=parseInt(a[16]*Math.random());
// alert(randnum);
// alert(parseInt(a[16]*Math.random()));
var Lvalue=a[randnum];
createElementlocal=Lvalue;
// alert(Lvalue);
// alert(Lvalue/4);
// alert(Lvalue%4);
randnum=parseInt(4*Math.random());
var createscore=((randnum%2)==1)?4:2;
blocks[Math.floor(Lvalue/4)][Lvalue%4]=createscore;
score=score+createscore;
// alert(Value);
// System.out.println(Value/4+" "+Value%4+" "+Value);
}
}
/**
* Minmize the numberrow
* @param size
* @param a
* @return if it is minmize
*/
function Minmize(size,a)
{
var flag=1; //is min
var b=Array([0,0,0,0,0,0]);
var i=0;
var j=0;
var cutsize=0;
while(j<size)
{
if (a[j]!=0) {b[i]=a[j];i++;j++;}
else {j++;flag=-1;cutsize++;}//not min
}
b[4]=flag;
b[5]=size-cutsize;
// for (int k=0;k<6;k++)
// System.out.print(b[k]+" ");
return b;
}
/**
* Merge Minmize numberrow;
* @param size
* @param a
* @return
*/
function merginlingrow(size,a)
{
var i=0;
while (i<size)
{
if(i+1==size) {i++;}
else
if ( a[i]==a[i+1] )
{
a[i]=a[i]+a[i+1];
if (a[i]==2048)
{
a[i]=2048;
alert("从棋盘的左上角向右下角滑动,即可进入奖励模式。O(∩_∩)O~方块变形,分数翻翻哦!");
}
score=score+a[i];
a[i+1]=0;
i=i+2;
a[4]=-1;
}else{
i++;
}
}
// for (int k=0;k<6;k++)
// System.out.print(a[k]+" ");
return a;
}
function right()
{
// alert("right");
var a= new Array(6);
var size=4;
// alert(5);
// for (var k=0;k<2;k++)
// alert(k);
for (var i=0;i<4;i++)
{
// alert(i);
for (var j=0;j<4;j++)
{
a[j]=blocks[i][j];
blocks[i][j]=0;
}
a[4]=-1;a[5]=size;
// alert(i+' '+a[5]);
a=Minmize(size,a);
a=merginlingrow(a[5],a);
while(a[4]==-1)
{
merginlingrow(a[5], a);
a=Minmize(a[5], a);
}
// for (int j=a[5];j<4)
// for (int j=0;j<4;j++)
// System.out.print(a[j]+" ");
for (var j=a[5];j<4;j++)
{
a[j]=0;
}
// alert(a[0]+' '+a[1]+' '+a[2]+' '+a[3]+' ');
for (var j=0;j<a[5];j++)
{
blocks[i][3-j]=a[a[5]-j-1];
}
}
}
function boundmodel()
{
blocks=new Array([0,0,0,0],[0,2,4,8],[128,64,32,16],[256,512,1024,2048]);
score=score*2;
showdisplay();
}
function restart()
{
blocks=new Array([0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]);
createnewblock();
createnewblock();
score=0;
showdisplay();
}
function up()
{
// alert("up");
var a= new Array(6);
var size=4;
// alert(4);
for (var i=0;i<4;i++)
{
for (var j=0;j<4;j++)
{
a[j]=blocks[j][i];
blocks[j][i]=0;
}
a[4]=-1;a[5]=size;
// alert(i+' '+a[5]);
a=Minmize(size,a);
a=merginlingrow(a[5],a);
while(a[4]==-1)
{
merginlingrow(a[5], a);
a=Minmize(a[5], a);
}
// for (int j=a[5];j<4)
// for (int j=0;j<4;j++)
// System.out.print(a[j]+" ");
for (var j=a[5];j<4;j++)
{
a[j]=0;
}
// alert(a[0]+' '+a[1]+' '+a[2]+' '+a[3]+' ');
for (var j=0;j<4;j++)
{
blocks[j][i]=a[j];
}
}
}
function down()
{
var a= new Array(6);
var size=4;
for (var i=0;i<4;i++)
{
for (var j=0;j<4;j++)
{
a[j]=blocks[j][i];
blocks[j][i]=0;
}
a[4]=-1;a[5]=size;
a=Minmize(size,a);
a=merginlingrow(a[5],a);
while(a[4]==-1)
{
merginlingrow(a[5], a);
a=Minmize(a[5], a);
}
// for (int j=a[5];j<4)
// for (int j=0;j<4;j++)
// System.out.print(a[j]+" ");
for (var j=a[5];j<4;j++)
{
a[j]=0;
}
for (var j=0;j<a[5];j++)
{
blocks[3-j][i]=a[a[5]-j-1];
}
}
}
function left()
{
var a= new Array(6);
var size=4;
// alert("left");
for (var i=0;i<4;i++)
{
for (var j=0;j<4;j++)
{
a[j]=blocks[i][j];
blocks[i][j]=0;
}
a[4]=-1;a[5]=size;
a=Minmize(size,a);
a=merginlingrow(a[5],a);
while(a[4]==-1)
{
merginlingrow(a[5], a);
a=Minmize(a[5], a);
}
// for (int j=a[5];j<4)
// for (int j=0;j<4;j++)
// System.out.print(a[j]+" ");
for (var j=a[5];j<4;j++)
{
a[j]=0;
}
// alert(a[0]+' '+a[1]+' '+a[2]+' '+a[3]+' ');
for (var j=0;j<a[5];j++)
{
blocks[i][j]=a[j];
}
}
}
function findavailableblock()
{
var a=new Array(17);
var k=0;
for (var i=0;i<4;i++)
for(var j=0;j<4;j++)
if (blocks[i][j]==0)
{
a[k]=i*4+j;
k++;
}
a[16]=k;
//alert(a[16]);
/* for (int i=0;i<17;i++)
System.out.print(a[i]+" ");
System.out.println();*/
return a;
//live
}
function showdisplay()
{
for (var i=0;i<4;i++)
for (var j=0;j<4;j++) {
changeTile((i * 4 + j), blocks[i][j])
}
contrlscore.innerHTML=score;
if (phone==false){
$(document).ready(function(){
var div=$(tiles[createElementlocal]);
div.animate({fontSize:'10px'});
div.animate({fontSize:'80px'});
div.animate({fontSize:'40px'})
});}else{
$(document).ready(function(){
var div=$(tiles[createElementlocal]);
div.animate({fontSize:'20px'});
div.animate({fontSize:'60px'});
div.animate({fontSize:'30px'})
});
}
}
function changeTile(num,val)
{
tiles[num].className = 'div2048divtile div2048divtile' + val;
tiles[num].setAttribute('val',val);
// tiles[num].setAttribute('index',i);
tiles[num].innerHTML = val> 0 ? val : '';
}
function init()
{
container=document.getElementById('div2048');
startBtn=document.getElementById('start');
contrlscore=document.getElementById('score');
changebackground1 = document.getElementById('chbackground');
// changebackground2 = document.getElementById('div2048');
// document.styleSheets[0].cssText=document.styleSheets[0].cssText.replace(".div2048divtile0{
// background: RGB(137,187,223);
// })
// changebackground3 = document.getElementById('css');
// changebackground3.div2048divtile0=" background: RGB(13,17,23);"
// changebackground3=document.styleSheets[4];
// alert(changebackground3);
// changebackground3.cssText='background: RGB(13,17,23);';
//alert(changebackground3);
// changebackground3.style.background='RGB(50,50,50)';
// changebackground3.style.backgroundColor="grey";
// alert("init");
// startBtn.remove
// container.style.display='none';
startBtn.style.display='none';
//newTile(0);
//newTile(8);
for (var i=0;i<16;i++)
{
tiles[i]=newTile(0);
tiles[i].setAttribute('index',i);
}
}
function newTile(val)
{
var tile = document.createElement('div');
tile.className='div2048divtile div2048divtile'+val;
tile.setAttribute('val',val);
tile.innerHTML = val> 0 ? val : '';
container.appendChild(tile);
return tile;
}
function keyDown(e)
{
if(!ie)
{
nkey=e.which;
var iekey='现在是ns浏览器';
realkey=String.fromCharCode(e.which);
}
if(ie)
{
var iekey=event.keyCode;
nkey='现在是ie浏览器';
realkey=String.fromCharCode(event.keyCode);
if(event.keyCode==32){realkey='\' 空格\''}
if(event.keyCode==13){realkey='\' 回车\''}
if(event.keyCode==27){realkey='\' Esc\''}
if(event.keyCode==16){realkey='\' Shift\''}
if(event.keyCode==17){realkey='\' Ctrl\''}
if(event.keyCode==18){realkey='\' Alt\''}
}
// alert('ns浏览器中键值:'+nkey+'\n'+'ie浏览器中键值:'+iekey+'\n'+'实际键为'+realkey);
merge();
}
}
/*function changebackground()
{
/* var changebackground;
var changebackgroundnum=0;
var changebackgroundarray=new Array(["#b8af9e","#00000","#FFFFF"]);
changebackground=document.getElementById('changebackground');
changebackground.style.backgroundColor=changebackgroundarray[changebackgroundnum];
changebackgroundnum++;
changebackgroundnum=changebackgroundnum%3;
alert("chang");
}*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。