Skip to content

Commit

Permalink
dig race added, a broken replay added. ready for adding settings into…
Browse files Browse the repository at this point in the history
… replay.
  • Loading branch information
farteryhr committed Jul 17, 2015
1 parent b56ac6a commit 20e9f67
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 40 deletions.
183 changes: 183 additions & 0 deletions compress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
function Compress(strNormalString)
{
var strCompressedString = "";
var strBase52 = "";

var ht = new HashTable;
for(i = 0; i < 128; i++) {
var e = new HashTableElement;
e.key = i;
e.code = i;
ht.Insert(e);
}

var used = 128;
var intLeftOver = 0;
var intOutputCode = 0;
var pcode = 0;
var ccode = 0;
var k = 0;

for(var i=0; i<strNormalString.length; i++) {
ccode = strNormalString.charCodeAt(i);
k = (pcode << 8) | ccode;
if((intSearch = ht.Search(k)) != null) {
pcode = intSearch;
} else {
intLeftOver += 12;
intOutputCode <<= 12;
intOutputCode |= pcode;
pcode = ccode;
if(intLeftOver >= 16) {
strCompressedString += String.fromCharCode( intOutputCode >> ( intLeftOver - 16 ) );
intOutputCode &= (Math.pow(2,(intLeftOver - 16)) - 1);
intLeftOver -= 16;
}
if(used < 4096) {
used ++;
var e = new HashTableElement;
e.key = k;
e.code = used - 1;
ht.Insert(e);
}
}
}

if(pcode != 0) {
intLeftOver += 12;
intOutputCode <<= 12;
intOutputCode |= pcode;
}

if(intLeftOver >= 16) {
strCompressedString += String.fromCharCode( intOutputCode >> ( intLeftOver - 16 ) );
intOutputCode &= (Math.pow(2,(intLeftOver - 16)) - 1);
intLeftOver -= 16;
}

if( intLeftOver > 0) {
intOutputCode <<= (16 - intLeftOver);
strCompressedString += String.fromCharCode( intOutputCode );
}

//return strCompressedString;

for(var i=0;i<strCompressedString.length;i++) {
var ch=0,ch0=0,ch1=0,ch2=0;
ch = strCompressedString.charCodeAt(i);
ch0 = ch%52;
ch /= 52;
ch1 = ch%52;
ch /= 52;
ch2 = ch%52;
ch0 += (ch0<26?65:(97-26));
ch1 += (ch1<26?65:(97-26));
ch2 += (ch2<26?65:(97-26));
strBase52 += String.fromCharCode(ch0,ch1,ch2);
}

return strBase52;
}

function Decompress(/*strCompressedString*/ strBase52)
{
var strCompressedString = "";

var Base52Unit = function(num) {
if(num>=65&&num<65+26) num-=65;
else if(num>=97&&ch0<97+26) num-=(97-26);
else return null;
return num;
}

if(strBase52.length % 3 != 0)
return null;

for(var i=0;i<strBase52.length;i+=3) {
var ch=0,ch0=0,ch1=0,ch2=0;
ch0 = strBase52.charCodeAt(i);
ch1 = strBase52.charCodeAt(i+1);
ch2 = strBase52.charCodeAt(i+2);
ch0 = Base52Unit(ch0);
ch1 = Base52Unit(ch1);
ch2 = Base52Unit(ch2);
if(ch0==null||ch1==null||ch2==null)
return null;
ch = ch0 + 52 * (ch1 + 52 * ch2);
strCompressedString += String.fromCharCode(ch);
}

var strNormalString = "";
var ht = new Array;

for(i = 0; i < 128; i++)
{
ht[i] = String.fromCharCode(i);
}

var used = 128;
var intLeftOver = 0;
var intOutputCode = 0;
var ccode = 0;
var pcode = 0;
var key = 0;

for(var i=0; i<strCompressedString.length; i++) {
intLeftOver += 16;
intOutputCode <<= 16;
intOutputCode |= strCompressedString.charCodeAt(i);
while(1) {
if(intLeftOver >= 12) {
ccode = intOutputCode >> (intLeftOver - 12);
if( typeof( key = ht[ccode] ) != "undefined" ) {
strNormalString += key;
if(used > 128) {
ht[ht.length] = ht[pcode] + key.substr(0, 1);
}
pcode = ccode;
} else {
key = ht[pcode] + ht[pcode].substr(0, 1);
strNormalString += key;
ht[ht.length] = ht[pcode] + key.substr(0, 1);
pcode = ht.length - 1;
}used ++;
intLeftOver -= 12;
intOutputCode &= (Math.pow(2,intLeftOver) - 1);
} else {
break;
}
}
}
return strNormalString;
}

function HashTableElement()
{
this.key = null;
this.code = null;
}

function HashTable()
{
this.ht = new Array(4099);

this.Search = function(keyword) {
var arr = this.ht[keyword];
if(typeof(arr) != "undefined") {
for(i = 0; i < arr.length; i ++) {
if(arr[i].key == keyword) return arr[i].code;
}
}
return null;
}
this.Insert = function(e) {
var arr = this.ht[e.key];
if(typeof(arr) == "undefined") {
arr = new Array();
arr[0] = e;
this.ht[e.key] = arr;
} else {
arr[arr.length] = e;
}
}
}
9 changes: 7 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ <h1>Tetr.js - Dig v0.31</h1>
<li><a onclick=init(1)>Marathon 马拉松</a></li>
<!-- <li><a onclick=init(2)>Play Ultra</a></li> -->
<li><a onclick="init(3,{digOffset:0})">Dig! 挖掘机</a></li>
<li><a onclick="init(4)">Dig Race 挖掘练习</a></li>
<li><a onclick=menu(2)><i class=></i>Controls 调整按键</a></li>
<li><a onclick=menu(1)><i class=icon-cog></i>Settings 调整设定</a></li>
<li><a onclick="init(3,{digOffset:500})">Dig - 500+</a></li>
<li><a onclick="init(3,{digOffset:1000})">Dig - 1000+</a></li>
<li><a onclick="init(3,{digOffset:1500})">Dig - 1500+</a></li>
<li><a onclick="init(3,{digOffset:2000})">*MONSTER*</a></li>
<li><a onclick="init(3,{digOffset:2000})">*DANGER*</a></li>
<li><a onclick="tryreplaydata()">Replay(α) 回放(测试)</a></li>

</ul>
</nav>

Expand Down Expand Up @@ -100,7 +103,7 @@ <h2>Controls 调整按键</h2>
<nav id=go class=menu>
<ul>
<li><a onclick=init(gametype,gameparams)>Retry 再来</a></li>
<li><a onclick=init('replay')>Replay 回放</a></li>
<li><a onclick=init('replay')>Replay 回放 <span onclick="showreplaydata()"><u>&gt;&gt; ★</u></span></a></li>
<li><a onclick=menu(5)>Rankings 排行</a></li>
<li><a onclick=menu(0)>Back 回菜单</a></li>
</ul>
Expand Down Expand Up @@ -138,6 +141,8 @@ <h3>Next</h3>
<script src=menu.js></script>
<script src=bg.js></script>
<script src=ranking.js></script>
<script src=compress.js></script>

<!--
<script>_gaq=[['_setAccount','UA-30472693-1'],['_trackPageview']];(function(d){var g=d.createElement('script'),s=d.scripts[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)})(document)</script>
-->
Expand Down
3 changes: 2 additions & 1 deletion preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Preview.prototype.gen = function() {
/* farter */ // proven random shuffle algorithm
for (var i=0;i<7-1;i++)
{
var temp=pieceList[i], rand=~~((7-i)*rng.next())+i;
var temp=pieceList[i];
var rand=~~((7-i)*rng.next())+i;
pieceList[i]=pieceList[rand];
pieceList[rand]=temp;
}
Expand Down
6 changes: 5 additions & 1 deletion ranking.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ function XMLHTTP(url,obj)
}
else
{
leaderboard.innerText = "Problem retrieving leaderboard data" + xmlhttp.status;
leaderboard.innerText = "Problem retrieving leaderboard data\n" +
xmlhttp.status + "\n" + xmlhttp.readyState + "\n" + xmlhttp.statusText + "\n" +
xmlhttp.responseText;
}
}
}
Expand Down Expand Up @@ -108,6 +110,8 @@ function submitscore(obj)
playername=prompt("Enter your nick: 请输入大名:");
if(playername===null)
return;
if(playername==="")
playername="unnamed";
obj.name=playername;
console.log(obj);
XMLHTTP("http://farter.tk:8888", obj);
Expand Down
8 changes: 5 additions & 3 deletions stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Stack.prototype.addPiece = function(tetro) {
// TODO Ponder during the day and see if there is a more elegant solution.
if (count === 10) {
lines++; // NOTE stats
if (gametype === 3) {
if (gametype === 4) { // dig race
if (digLines.indexOf(row) !== -1) {
digLines.splice(digLines.indexOf(row), 1);
}
Expand Down Expand Up @@ -91,8 +91,10 @@ Stack.prototype.rowRise = function(arrRow, objPiece) {
}
this.grid[x][this.grid[x].length-1]=arrRow[x];
}
for(var y = 0; y < digLines.length; y++) {
digLines[y]--;
if(digLines) {
for(var y = 0; y < digLines.length; y++) {
digLines[y]--;
}
}
digLines.push(21);
if (!piece.moveValid(0, 0, piece.tetro)) {
Expand Down
Loading

0 comments on commit 20e9f67

Please sign in to comment.