1 Star 0 Fork 0

xiaogeqq1034/My2048

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
My2048.js 15.11 KB
一键复制 编辑 原始数据 按行查看 历史
xiaogeqq1034 提交于 2014-07-09 13:07 . My2048
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
/**
* 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");
}*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/xiaogeqq1034/My2048.git
git@gitee.com:xiaogeqq1034/My2048.git
xiaogeqq1034
My2048
My2048
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385