Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Created August 18, 2010 20:30
Show Gist options
  • Save dieseltravis/536081 to your computer and use it in GitHub Desktop.
Save dieseltravis/536081 to your computer and use it in GitHub Desktop.
My entry into the 10k.aneventapart.com
Entry: http://10k.aneventapart.com/Entry/201
Title: Yahtml
Description: a 5-dice game like Yahtzee, but in html+javascript+css
Browsers: IE9p4, Firefox 3.6, Chrome 6b, Safari 5, iPhone 3G, Nokia E71x
Files:
~/yahtml/index.html
10,105 Bytes
~/yahtml/ya.manifest
25 Bytes
total size:
10,130 Bytes
Helper sites to get down to 10KB:
http://closure-compiler.appspot.com/home
http://www.minifycss.com/css-compressor/
<!DOCTYPE html><html lang=en manifest="ya.manifest"><head><meta charset=utf-8><link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHAQMAAAD+nMWQAAAABlBMVEUAAAD///+l2Z/dAAAAHElEQVR4XiXFQQkAAAACsesfyVBGEAUfYwSNfwEshAZbaHnTlAAAAABJRU5ErkJggg==" type="image/png"><title>Yahtml</title><meta name=description content="5-dice game"><meta name=author content="Travis Hardiman"><meta name=viewport content="width=device-width; initial-scale=1.0; maximum-scale=1.0;"><style>html{display:block;text-align:center;background-color:#FFF}body{font-family:Cambria,serif;position:relative;display:inline-block;border:1px solid #BBB;-moz-border-radius:12px;-webkit-border-radius:12px;border-radius:12px;background-color:#F8F8F8;color:#000;-moz-box-shadow:0 3px 10px #DDD;-webkit-box-shadow:0 3px 10px #DDD;box-shadow:0 3px 10px #DDD;text-shadow:1px 1px 3px #AAA;margin:0 auto;padding:3px}h1{font-size:14px;text-shadow:1px 1px 3px #CCC;color:#333}h6{font-size:8px;color:#333}a,a:visited{color:#00C;text-decoration:none}a:hover{color:#00F;text-decoration:underline}tr{font-family:Calibri,sans-serif;text-align:center}th{font-size:10px}td{border:solid 1px #F8F8F8}label,input{font-family:Corbel,sans-serif;cursor:pointer}#sh{border-collapse:separate;table-layout:auto;border-spacing:5px;empty-cells:hide;margin:auto}#D{height:55px}#D td{background-color:#FFF;border:solid 3px #444;border-top:solid 1px #CCC;border-left:solid 1px #CCC;-moz-border-radius:4px;-webkit-border-radius:4px;border-radius:4px;height:50px;width:50px;font-family:Consolas,monospace;padding:0}#D i{display:block;position:relative;font-style:normal;height:50px;width:50px;margin:0}#D b{display:block;position:absolute;line-height:12px;font-size:23px;width:12px;height:12px}.b1,.b4{top:2px}#D b.b0,#D b.b2,#D b.b5{top:50%;margin-top:-6px}.b3,.b6{bottom:3px}#D b.b0{width:50px;display:inline-block;text-align:center}.b1,.b2,.b3{left:3px}.b4,.b5,.b6{right:3px}.rL{-moz-transform:rotate(4deg);-o-transform:rotate(4deg);-webkit-transform:rotate(4deg)}.rR{-moz-transform:rotate(-4deg);-o-transform:rotate(-4deg);-webkit-transform:rotate(-4deg)}#R{position:absolute;left:5px}#nG{position:absolute;right:5px}#sc{border-collapse:separate;border-spacing:1px;border:none;margin:2em auto}#sc th{border:none;border-bottom:dotted 1px #600;border-spacing:0;-moz-border-radius:6px 6px 0 0;-webkit-border-radius:3px;border-radius:6px 6px 0 0;width:14%;background-color:#C00;background-image:-moz-linear-gradient(top,#D00,#900);background-image:-webkit-gradient(linear,0 0,0 bottom,color-stop(0,#D00),color-stop(1,#900));color:#FFF;margin:0;padding:2px 0}#sc td{border:none;border-spacing:0;-moz-border-radius:0 0 4px 4px;-webkit-border-radius:2px;border-radius:0 0 4px 4px;width:14%;background-color:#CCC;color:#999;font-family:Consolas,monospace;margin:0;padding:3px 0}#sc td.hv{outline:dotted 1px #999;background-color:#FFF;color:#333;cursor:pointer}#sc td.sd{outline:none;background-color:#DDD;color:#000;cursor:text}#sc .ts th{border-bottom:dotted 1px #C90;background-color:#FC3;background-image:-moz-linear-gradient(top,#FE5,#EB2);background-image:-webkit-gradient(linear,0 0,0 bottom,color-stop(0,#FE5),color-stop(1,#EB2));color:#333}#sc .ts td{background-color:#FFC;color:#333;-moz-box-shadow:2px 2px 8px #BBB;-webkit-box-shadow:2px 2px 8px #BBB;box-shadow:2px 2px 8px #BBB;text-shadow:1px 1px 3px #BBB}#sc #T{color:#000}#ts td{font-weight:700}.f{color:rgba(99,99,99,.5);text-shadow:#000 0 0 4px}.b{text-decoration:blink}.d{opacity:.5;outline:none;cursor:not-allowed}</style></head><body><h1>Yahtml is <em>like</em> Yahtzee<a href="#r">®</a>, but html</h1><table id=sh><tr id=D><td class=Di>Ya</td><td class=Di>h</td><td class=Di>t</td><td class=Di>m</td><td class=Di>l</td></tr><tr><td><label>hold <input id=h1 type=checkbox class=h disabled></label></td><td><label>hold <input id=h2 type=checkbox class=h disabled></label></td><td><label>hold <input id=h3 type=checkbox class=h disabled></label></td><td><label>hold <input id=h4 type=checkbox class=h disabled></label></td><td><label>hold <input id=h5 type=checkbox class=h disabled></label></td></tr></table><input id=R type=button value=Roll><input id=nG type=button value="New Game"><table id=sc><tr><th>1’s</th><th>2’s</th><th>3’s</th><th>4’s</th><th>5’s</th><th>6’s</th></tr><tr id=s1 class=Ss><td id=S1>0</td><td id=S2>0</td><td id=S3>0</td><td id=S4>0</td><td id=S5>0</td><td id=S6>0</td></tr><tr><th>3/kind</th><th>4/kind</th><th>full house</th><th>sm. str.</th><th>lg. str.</th><th>chance</th><th>yahtml</th></tr><tr id=s2 class=Ss><td id=tk>0</td><td id=fk>0</td><td id=fh>0</td><td id=ss>0</td><td id=ls>0</td><td id=ch>0</td><td id=y>0</td></tr><tr class=ts><th>top sub</th><th>top bonus</th><th>top total</th><th>y. bonus</th><th>total</th><th>best</th></tr><tr id=ts class=ts><td id=sT>0</td><td id=sB>0</td><td id=tT>0</td><td id=yB>0</td><td id=T>0</td><td id=B>0</td></tr></table><h6 id=r><a href="http://wikipedia.org/wiki/Yahtzee">Yahtzee</a> is © Milton Bradley Company, All Rights Reserved</h6><script>(function(j){var e=j.document;if(e&&!e.getElementsByClassName)e.getElementsByClassName=function(a){var b=[];a=RegExp("\\b"+a+"\\b");for(var c=this.getElementsByTagName("*"),d=0,f=c.length;d<f;d++)a.test(c[d].className)&&b.push(c[d]);return b};var s=e.getElementById("B"),G=e.getElementById("s1"),H=e.getElementById("s2"),I=e.getElementsByClassName("Ss"),R=e.getElementById("sT"),S=e.getElementById("sB"),T=e.getElementById("tT"),t=e.getElementById("T"),U=e.getElementById("ts"),o=e.getElementsByClassName("Di"), k=e.getElementById("R"),A=e.getElementById("yB"),V=e.getElementById("y"),W=e.getElementsByClassName("h"),J=0,u=0,p=false,q=0,m=function(a){return parseInt(a.innerHTML,10)},K=function(a){return"<i>"+(a==1?"<b class='b0'>•</b>":a==2?"<b class='b1'>•</b><b class='b6'>•</b>":a==3?"<b class='b1'>•</b><b class='b0'>•</b><b class='b6'>•</b>":a==4?"<b class='b1'>•</b><b class='b3'>•</b><b class='b4'>•</b><b class='b6'>•</b>":a==5?"<b class='b1'>•</b><b class='b3'>•</b><b class='b0'>•</b><b class='b4'>•</b><b class='b6'>•</b>": "<b class='b1'>•</b><b class='b2'>•</b><b class='b3'>•</b><b class='b4'>•</b><b class='b5'>•</b><b class='b6'>•</b>")+"</i>"},g=function(a,b){a.className=a.className.replace(RegExp("\\b"+b+"\\b","gm")," ")},i=function(a,b){for(var c=0,d=a.length;d--;)b(a[c],c++)},n=function(a,b){if(a&&a.hasChildNodes&&a.hasChildNodes()){var c=0;i(a.childNodes,function(d){d.nodeName=="TD"&&b(d,c++)})}},B=function(a,b,c){if(a.addEventListener)a.addEventListener(b,c,false);else a.attachEvent&& a.attachEvent("on"+b,c)},y=function(a,b){B(a,"click",b)},L=function(a,b){var c=new Date;c.setFullYear(c.getFullYear()+3);c=";expires="+c.toGMTString();j.document.cookie=a+"="+b+c+";path=/"},M=function(a){for(var b=a+"=",c=j.document.cookie.split(";"),d=0,f=c.length;d<f;d++){for(var h=c[d];h.charAt(0)===" ";)h=h.substring(1,h.length);if(h.indexOf(b)===0){b=h.substring(b.length,h.length);L(a,b);return b}}return null},N=function(a){for(var b=[],c=a.length;c--;){var d=-1;if(b.indexOf)d=b.indexOf(a[c]); else for(var f=b.length;f--;)if(b[f]==a[c]){d=f;break}d<0&&b.push(a[c])}return b},v=function(a,b){return N(a).length+b<=6},O=function(a,b){a=N(a);a.sort();for(var c=a.length;c--;){for(var d=1,f=1;f<b;f++)if(a[c]-f==a[c-f])d++;else d=1;if(d>=b)return true}return false},P=function(){var a=[];i(o,function(b,c){a[c]=b.innerHTML.match(/<b/gi).length});return a},C=function(){var a=0;i(o,function(b){a+=b.innerHTML.match(/<b/gi).length});return a},r=function(a){var b=0;i(o,function(c){c.innerHTML.match(/<b/gi).length== a&&b++});return b*a},w=function(a){i(o,function(b){b.style.cursor=a?"pointer":"not-allowed"});i(W,function(b){b.disabled=!a;if(!a)b.checked=false})};s.innerHTML=M("y")||"0";y(k,function(){if(u<3){q=0;i(o,function(a,b){if(!e.getElementById("h"+(b+1)).checked){q++;a.className+=" f";for(var c=function(){var f="";if(a.className.indexOf("rL")>-1){g(a,"rL");f="rR"}else{g(a,"rR");f="rL"}a.innerHTML=K(Math.floor(Math.random()*6)+1);a.className+=" "+f},d=1;d<6;d++)j.setTimeout(c,d*100);j.setTimeout(function(){a.innerHTML= K(Math.floor(Math.random()*6)+1);q--;if(p&&q===0&&v(P(),5)){J++;j.alert("☺ YAHTML!")}},700);j.setTimeout(function(){g(a,"f");g(a,"rL");g(a,"rR")},800)}});u++;p=true}if(u>=3){k.disabled=true;k.className+=" d";w(false)}else w(true)});y(e.getElementById("nG"),function(){if(j.confirm("New game?")){var a=function(b){b.innerHTML="0";g(b,"sd");g(b,"hv")};n(G,a);n(H,a);n(U,a);w(false);p=k.disabled=false;u=0;g(k,"d");g(t,"b");s.innerHTML=M("y")||"0"}});i(o,function(a,b){y(a,function(){var c=e.getElementById("h"+ (b+1));if(!c.disabled)c.checked=!c.checked})});i(I,function(a){n(a,function(b){B(b,"mouseover",function(){if(p&&q===0&&(!b.className||b.className&&b.className.indexOf("sd")==-1))b.className+=" hv"});B(b,"mouseout",function(){if(!b.className||b.className&&b.className.indexOf("sd")==-1)g(b,"hv")});y(b,function(){if(p&&q===0){var c=0,d=P();switch(this.id){case "S1":c=r(1);break;case "S2":c=r(2);break;case "S3":c=r(3);break;case "S4":c=r(4);break;case "S5":c=r(5);break;case "S6":c=r(6);break;case "tk":if(v(d, 3))c=C();break;case "fk":if(v(d,4))c=C();break;case "fh":for(var f=0,h=0,D=d.length;D--;){for(var z=0,E=d.length;E--;)E!=D&&d[E]==d[D]&&z++;if(z==4){f++;h++}else if(z>=2)f++;else z>=1&&h++}if(f>=1&&h>=1)c=25;break;case "ss":if(O(d,4))c=30;break;case "ls":if(O(d,5))c=40;break;case "ch":c=C();break;case "y":if(v(d,5))c=50;break}this.innerHTML=c;this.className+=" sd";var l=0;n(G,function(x){l+=m(x)});R.innerHTML=l;if(l>=63){l+=35;S.innerHTML="35"}T.innerHTML=l;n(H,function(x){l+=m(x)});if(v(d,5)&&J> 1&&m(V)>0){c=m(A)+100;A.innerHTML=c}l+=m(A);t.innerHTML=l;if(l>m(s))t.className+=" b";else g(t,"b");var Q=0;i(I,function(x){n(x,function(F){F&&!(F.className&&F.className.indexOf("sd")>-1)&&Q++})});if(Q===0){w(false);k.disabled=true;k.className+=" d";c=m(t);d="";if(c>m(s)){s.innerHTML=c;L("y",c);d="\nNew Best!"}j.alert("Final: "+c+d)}p=false;u=0;k.disabled=false;g(k,"d");w(false)}})})})})(window);</script></body></html>
Higher:
* BUG: only blink total when best is > 0
* BUG: 4/kind accepts full-house.
* BUG: rolling shouldn't be possible after game end, unless reset.
* BUG: implement multi-yahtml wild cards for straight boxes.
Lower:
* add keyboard shortcuts
* sound?
* attempt to compile down to 1K ;-)
* use these characters: ⚀ ⚁ ⚂ ⚃ ⚄ ⚅
<!DOCTYPE html>
<html lang="en" manifest="ya.manifest">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHAQMAAAD+nMWQAAAABlBMVEUAAAD///+l2Z/dAAAAHElEQVR4XiXFQQkAAAACsesfyVBGEAUfYwSNfwEshAZbaHnTlAAAAABJRU5ErkJggg==" type="image/png" />
<title>Yahtml</title>
<meta name="description" content="5-dice game" />
<meta name="author" content="Travis Hardiman" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
<style>
html {
display: block;
text-align: center;
background-color: #FFF;
}
body {
font-family: Cambria, serif;
position: relative;
display: inline-block;
margin: 0 auto;
padding: 3px;
border: 1px solid #BBB;
-moz-border-radius: 12px;
-webkit-border-radius: 12px;
border-radius: 12px;
background-color: #F8F8F8;
color: #000;
-moz-box-shadow: 0 3px 10px #DDD;
-webkit-box-shadow: 0 3px 10px #DDD;
box-shadow: 0 3px 10px #DDD;
text-shadow: 1px 1px 3px #AAA;
}
h1 {
font-size: 14px;
text-shadow: 1px 1px 3px #CCC;
color: #333;
}
/* Disclaimer */
h6 {
font-size: 8px;
color: #333;
}
a,a:visited {
color: #00C;
text-decoration: none;
}
a:hover {
color: #00F;
text-decoration: underline;
}
tr {
font-family: Calibri, sans-serif;
text-align: center;
}
th {
font-size: 10px;
}
td {
border: solid 1px #F8F8F8;
}
label, input {
font-family: Corbel, sans-serif;
cursor: pointer;
}
/* Shaker */
#sh {
border-collapse: separate;
margin: auto;
table-layout: auto;
border-spacing: 5px;
empty-cells: hide;
}
/* dice */
#D {
height: 55px;
}
/* each die */
#D td {
background-color: #FFF;
border: solid 3px #444;
border-top: solid 1px #CCC;
border-left: solid 1px #CCC;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
height: 50px;
width: 50px;
padding: 0;
font-family: Consolas, monospace;
}
/* die digits */
#D i {
display: block;
position: relative;
font-style: normal;
height: 50px;
width: 50px;
margin: 0;
}
#D b {
display: block;
position: absolute;
line-height: 12px;
font-size: 23px;
width: 12px;
height: 12px;
}
/* dot grid */
.b1, .b4 {
top: 3px;
}
#D b.b0, #D b.b2, #D b.b5 {
top: 50%;
margin-top:-6px;
}
.b3, .b6 {
bottom: 3px;
}
#D b.b0 {
width: 50px;
display:inline-block;
text-align: center;
}
.b1, .b2, .b3 {
left: 3px;
}
.b4, .b5, .b6 {
right: 3px;
}
/* roll left */
.rL {
-moz-transform: rotate(4deg);
-o-transform: rotate(4deg);
-webkit-transform: rotate(4deg);
}
/* roll right */
.rR {
-moz-transform: rotate(-4deg);
-o-transform: rotate(-4deg);
-webkit-transform: rotate(-4deg);
}
/* roll dice */
#R {
position:absolute;
left:5px;
}
/* new game */
#nG {
position:absolute;
right:5px;
}
/* score card */
#sc {
border-collapse: separate;
margin: 2em auto;
border-spacing: 1px;
border: none;
}
/* score headers */
#sc th {
border: none;
border-bottom: dotted 1px #600;
border-spacing: 0;
-moz-border-radius: 6px 6px 0 0;
-webkit-border-radius: 3px;
border-radius: 6px 6px 0 0;
width: 14%;
margin: 0;
padding: 2px 0;
background-color: #C00;
/*background-image:0*/
background-image:-moz-linear-gradient(top,#D00,#900);background-image:-webkit-gradient(linear,0 0,0 bottom,color-stop(0,#D00),color-stop(1,#900));
color: #FFF;
}
/* score values */
#sc td {
border: none;
border-spacing: 0;
-moz-border-radius: 0 0 4px 4px;
-webkit-border-radius: 2px;
border-radius: 0 0 4px 4px;
width: 14%;
margin: 0;
padding: 3px 0;
background-color: #CCC;
color: #999;
font-family: Consolas, monospace;
}
/* hover */
#sc td.hv {
outline: dotted 1px #999;
background-color: #FFF;
color: #333;
cursor: pointer;
}
/* scored */
#sc td.sd {
outline: none;
background-color: #DDD;
color: #000;
cursor: text;
}
/* Score total headers */
#sc .ts th {
border-bottom: dotted 1px #C90;
background-color: #FC3;
/*background-image:0*/
background-image:-moz-linear-gradient(top,#FE5,#EB2);background-image:-webkit-gradient(linear,0 0,0 bottom,color-stop(0,#FE5),color-stop(1,#EB2));
color: #333;
}
/* Score total values */
#sc .ts td {
background-color: #FFC;
color: #333;
-moz-box-shadow: 2px 2px 8px #BBB;
-webkit-box-shadow: 2px 2px 8px #BBB;
box-shadow: 2px 2px 8px #BBB;
text-shadow: 1px 1px 3px #BBB;
}
/* Grand Total */
#sc #T {
color: #000;
}
/* read only values */
#ts td {
font-weight: bold;
}
/* Fuzzy */
.f {
color: rgba(99,99,99,.5);
text-shadow: #000 0px 0px 4px;
}
/* Blinky */
.b {
text-decoration: blink;
}
/* Disabled */
.d {
opacity: .5;
outline: none;
cursor: not-allowed;
}
</style>
</head>
<body>
<h1>Yahtml is <em>like</em> Yahtzee<a href="#r">&#174;</a>, but html</h1>
<table id="sh">
<tr id="D">
<td class="Di">Ya</td>
<td class="Di">h</td>
<td class="Di">t</td>
<td class="Di">m</td>
<td class="Di">l</td>
</tr>
<tr>
<td><label>hold <input id="h1" type="checkbox" class="h" disabled="disabled" /></label></td>
<td><label>hold <input id="h2" type="checkbox" class="h" disabled="disabled" /></label></td>
<td><label>hold <input id="h3" type="checkbox" class="h" disabled="disabled" /></label></td>
<td><label>hold <input id="h4" type="checkbox" class="h" disabled="disabled" /></label></td>
<td><label>hold <input id="h5" type="checkbox" class="h" disabled="disabled" /></label></td>
</tr>
</table>
<input id="R" type="button" value="Roll" /><input id="nG" type="button" value="New Game" />
<table id="sc">
<tr>
<th>1&#8217;s</th>
<th>2&#8217;s</th>
<th>3&#8217;s</th>
<th>4&#8217;s</th>
<th>5&#8217;s</th>
<th>6&#8217;s</th>
</tr>
<tr id="s1" class="Ss">
<td id="S1">0</td>
<td id="S2">0</td>
<td id="S3">0</td>
<td id="S4">0</td>
<td id="S5">0</td>
<td id="S6">0</td>
</tr>
<tr>
<th>3/kind</th>
<th>4/kind</th>
<th>full house</th>
<th>sm. str.</th>
<th>lg. str.</th>
<th>chance</th>
<th>yahtml</th>
</tr>
<tr id="s2" class="Ss">
<td id="tk">0</td>
<td id="fk">0</td>
<td id="fh">0</td>
<td id="ss">0</td>
<td id="ls">0</td>
<td id="ch">0</td>
<td id="y">0</td>
</tr>
<tr class="ts">
<th>top sub</th>
<th>top bonus</th>
<th>top total</th>
<th>y. bonus</th>
<th>total</th>
<th>best</th>
</tr>
<tr id="ts" class="ts">
<td id="sT">0</td>
<td id="sB">0</td>
<td id="tT">0</td>
<td id="yB">0</td>
<td id="T">0</td>
<td id="B">0</td>
</tr>
</table>
<h6 id="r"><a href="http://wikipedia.org/wiki/Yahtzee">Yahtzee</a> is &#169; Milton Bradley Company, All Rights Reserved</h6>
<script>
(function (w) {
var d = w.document;
if (d && !d.getElementsByClassName) {
d.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b' + cl + '\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0, l = elem.length; i < l; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) {
retnode.push(elem[i]);
}
}
return retnode;
};
}
var domBest = d.getElementById("B"),
domScores1 = d.getElementById("s1"),
domScores2 = d.getElementById("s2"),
domAllScores = d.getElementsByClassName("Ss"),
domSubTotal = d.getElementById("sT"),
domSubBonus = d.getElementById("sB"),
domTopTotal = d.getElementById("tT"),
domTotal = d.getElementById("T"),
domTotals = d.getElementById("ts"),
domDies = d.getElementsByClassName("Di"),
domRoll = d.getElementById("R"),
domYB = d.getElementById("yB"),
domY = d.getElementById("y"),
domHolds = d.getElementsByClassName("h"),
yahtzeeCount = 0,
rollCount = 0,
rolled = false,
rollingTheDice = 0,
getHtmlInt = function (elem) {
return parseInt(elem.innerHTML, 10);
},
getDie = function (i) {
// dice dots
var h,
t = "<b class='b",
c = "'>\u2022</b>";
if (i == 1) {
h = t + 0 + c;
} else if (i == 2) {
h = t + 1 + c +
t + 6 + c;
} else if (i == 3) {
h = t + 1 + c +
t + 0 + c +
t + 6 + c;
} else if (i == 4) {
h = t + 1 + c +
t + 3 + c +
t + 4 + c +
t + 6 + c;
} else if (i == 5) {
h = t + 1 + c +
t + 3 + c +
t + 0 + c +
t + 4 + c +
t + 6 + c;
} else { // 6
h = t + 1 + c +
t + 2 + c +
t + 3 + c +
t + 4 + c +
t + 5 + c +
t + 6 + c;
}
return "<i>" + h + "</i>";
},
getDieVal = function (e) {
// get dice dot value based on length
return e.innerHTML.match(/<b/gi).length;
},
// de-jQuerified functions:
addClass = function (elem, className) {
elem.className += " " + className;
},
removeClass = function(elem, className) {
elem.className = elem.className.replace(new RegExp("\\b" + className + "\\b", "gm"), " ");
},
eachItem = function(elemArray, anon) {
for (var i = 0, l = elemArray.length; l--;) {
anon(elemArray[i], i++);
}
},
eachChildCell = function(elem, anon) {
if (elem && elem.hasChildNodes && elem.hasChildNodes()) {
var tdIndex = 0;
eachItem(elem.childNodes, function(item, i) {
if (item.nodeName == "TD") {
anon(item, tdIndex++);
}
});
}
},
setEvent = function (elem, eventName, handler) {
if (elem.addEventListener) {
elem.addEventListener(eventName, handler, false);
} else if (elem.attachEvent) {
elem.attachEvent("on" + eventName, handler);
}
},
setClick = function (elem, handler) {
setEvent(elem, "click", handler);
},
// cookies:
createCookie = function (name, value) {
var date = new Date();
date.setFullYear(date.getFullYear() + 3);
var expires = ";expires=" + date.toGMTString();
w.document.cookie = name + "=" + value + expires + ";path=/";
},
readCookie = function (name) {
var nameEQ = name + "=";
var ca = w.document.cookie.split(';');
for (var i = 0, l = ca.length; i < l; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
var value = c.substring(nameEQ.length, c.length);
createCookie(name, value);
return value;
}
}
return null;
},
unique = function(thisArray) {
var a = [];
for (var l = thisArray.length; l--;) {
var indexOf = -1;
if (a.indexOf) {
indexOf = a.indexOf(thisArray[l]);
} else {
for (var i = a.length; i--;) {
if (a[i] == thisArray[l]) {
indexOf = i;
break;
}
}
}
if (indexOf < 0) {
a.push(thisArray[l]);
}
}
return a;
},
getRandom = function (lower, upper) {
return Math.floor(Math.random() * (upper - lower + 1)) + lower;
},
ofAKind = function (items, howMany) {
return ((unique(items).length + howMany) <= 6);
},
isStraight = function (items, howMany) {
items = unique(items);
items.sort();
for (var l = items.length; l--;) {
var hasStraight = 1;
for (var c = 1; c < howMany; c++) {
if ((items[l] - c) == items[l - c]) {
hasStraight++;
} else {
hasStraight = 1;
}
}
if (hasStraight >= howMany) {
return true;
}
}
return false;
},
getDiceArray = function () {
var diceVals = [];
eachItem(domDies, function (item, i) {
diceVals[i] = getDieVal(item);
});
return diceVals;
},
getDiceSum = function () {
var thisSum = 0;
eachItem(domDies, function (item, i) {
thisSum += getDieVal(item);
});
return thisSum;
},
getSpecificSum = function (diceNum) {
var diceCount = 0;
eachItem(domDies, function (item, i) {
if (getDieVal(item) == diceNum) {
diceCount++;
}
});
return diceCount * diceNum;
},
enableHolds = function (isEnabled) {
eachItem(domDies, function (item, i) {
item.style.cursor = isEnabled ? "pointer" : "not-allowed";
});
eachItem(domHolds, function (item, i) {
item.disabled = !isEnabled;
if (!isEnabled) {
item.checked = false;
}
});
};
domBest.innerHTML = (readCookie("y") || "0");
// Roll the dice
setClick(domRoll, function () {
if (rollCount < 3) {
rollingTheDice = 0;
eachItem(domDies, function (item, i) {
if (!d.getElementById("h" + (i + 1)).checked) {
rollingTheDice++;
addClass(item, "f");
var rolling = function () {
var newClass = "";
if (item.className.indexOf("rL") > -1) {
removeClass(item, "rL");
newClass = "rR";
} else {
removeClass(item, "rR");
newClass = "rL";
}
item.innerHTML = getDie(getRandom(1, 6));
addClass(item, newClass);
};
for (var di = 1; di < 6; di++) {
w.setTimeout(rolling, di * 100);
}
w.setTimeout(function () {
item.innerHTML = getDie(getRandom(1, 6));
rollingTheDice--;
if (rolled && (rollingTheDice === 0) && (ofAKind(getDiceArray(), 5))) {
yahtzeeCount++;
w.alert("\u263A YAHTML!");
}
}, 700);
w.setTimeout(function () {
removeClass(item, "f");
removeClass(item, "rL");
removeClass(item, "rR");
}, 800);
}
});
rollCount++;
rolled = true;
}
if (rollCount >= 3) {
domRoll.disabled = true;
addClass(domRoll, "d");
enableHolds(false);
} else {
enableHolds(true);
}
});
// New Game
setClick(d.getElementById("nG"), function () {
if (w.confirm("New game?")) {
var resetScore = function (elem, i) {
elem.innerHTML = "0";
removeClass(elem, "sd");
removeClass(elem, "hv");
};
eachChildCell(domScores1, resetScore);
eachChildCell(domScores2, resetScore);
eachChildCell(domTotals, resetScore);
enableHolds(false);
domRoll.disabled = false;
rolled = false;
rollCount = 0;
removeClass(domRoll, "d");
removeClass(domTotal, "b");
domBest.innerHTML = (readCookie("y") || "0");
}
});
eachItem(domDies, function(item, i) {
setClick(item, function () {
var h = d.getElementById("h" + (i + 1));
if (!h.disabled) {
h.checked = !h.checked;
}
});
});
eachItem(domAllScores, function(elem, c) {
eachChildCell(elem, function (item, i) {
setEvent(item, "mouseover", function () {
if (rolled && (rollingTheDice === 0) && (!item.className || (item.className && item.className.indexOf("sd") == -1))) {
addClass(item, "hv");
}
});
setEvent(item, "mouseout", function () {
if (!item.className || (item.className && item.className.indexOf("sd") == -1)) {
removeClass(item, "hv");
}
});
setClick(item, function () {
if (rolled && (rollingTheDice === 0)) {
var thisSum = 0;
var da = getDiceArray();
switch (this.id) {
case "S1":
thisSum = getSpecificSum(1);
break;
case "S2":
thisSum = getSpecificSum(2);
break;
case "S3":
thisSum = getSpecificSum(3);
break;
case "S4":
thisSum = getSpecificSum(4);
break;
case "S5":
thisSum = getSpecificSum(5);
break;
case "S6":
thisSum = getSpecificSum(6);
break;
case "tk":
if (ofAKind(da, 3)) {
thisSum = getDiceSum();
}
break;
case "fk":
if (ofAKind(da, 4)) {
thisSum = getDiceSum();
}
break;
case "fh":
var has3 = 0;
var has2 = 0;
var items = da;
for (var l = items.length; l--;) {
var matches = 0;
for (var c = items.length; c--;) {
if (c != l && (items[c] == items[l])) {
matches++;
}
}
if (matches == 4) {
has3++;
has2++;
} else if (matches >= 2) {
has3++;
} else if (matches >= 1) {
has2++;
}
}
if ((has3 >= 1 && has2 >= 1)) {
thisSum = 25;
}
break;
case "ss":
if (isStraight(da, 4)) {
thisSum = 30;
}
break;
case "ls":
if (isStraight(da, 5)) {
thisSum = 40;
}
break;
case "ch":
thisSum = getDiceSum();
break;
case "y":
if (ofAKind(da, 5)) {
thisSum = 50;
}
break;
}
this.innerHTML = (thisSum);
addClass(this, "sd");
var subTotal = 0;
eachChildCell(domScores1, function (item, i) {
subTotal += getHtmlInt(item);
});
domSubTotal.innerHTML = (subTotal);
if (subTotal >= 63) {
subTotal += 35;
domSubBonus.innerHTML = "35";
}
domTopTotal.innerHTML = (subTotal);
eachChildCell(domScores2, function (item, i) {
subTotal += getHtmlInt(item);
});
if (ofAKind(da, 5) && yahtzeeCount > 1 && getHtmlInt(domY) > 0) {
var yScore = getHtmlInt(domYB) + 100;
domYB.innerHTML = (yScore);
}
subTotal += getHtmlInt(domYB);
domTotal.innerHTML = (subTotal);
if (subTotal > getHtmlInt(domBest)) {
addClass(domTotal, "b");
} else {
removeClass(domTotal, "b");
}
var unscored = 0;
eachItem(domAllScores, function(elem, c) {
eachChildCell(elem, function (item, i) {
if (item && !(item.className && item.className.indexOf("sd") > -1)) {
unscored++;
}
});
});
if (unscored === 0) {
enableHolds(false);
domRoll.disabled = true;
addClass(domRoll, "d");
var grandTotal = getHtmlInt(domTotal);
var isBest = "";
if (grandTotal > getHtmlInt(domBest)) {
domBest.innerHTML = (grandTotal);
createCookie("y", grandTotal);
isBest = "\nNew Best!";
}
w.alert("Final: " + grandTotal + isBest);
}
rolled = false;
rollCount = 0;
domRoll.disabled = false;
removeClass(domRoll, "d");
enableHolds(false);
}
});
});
});
})(window);
</script>
</body>
</html>
CACHE MANIFEST
index.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment