NullLogic

...

#!/usr/bin/nsp # poker sample prog by Dan Cahill function getcard() { do { x=math.rand(52); } while (CARDS[x].dealt!=null); CARDS[x].dealt=true; return x; } function regcard(p) { if (p==null) return null; x=tonumber(p); CARDS[x].dealt=true; return x; } function showscore(r) { hand_vals = { { name="ROYAL FLUSH", val=500 }; { name="STRAIGH FLUSH", val=100 }; { name="FOUR OF A KIND", val=40 }; { name="FULL HOUSE", val=10 }; { name="FLUSH", val=7 }; { name="STRAIGHT", val=5 }; { name="THREE OF A KIND", val=3 }; { name="TWO PAIRS", val=2 }; { name="JACKS OR HIGHER", val=1 }; { name="NONE", val=0 }; }; printf("<table border=1 cellpadding=2 cellspacing=0 width=400>\n"); row=9; foreach (val in hand_vals) { if (val.name=="NONE") break; printf("<tr id=row%d bgcolor='#%s'><td>%s</td><td align=right>%s</td></tr>\n", row, r==row?"00FF7F":"FFFFFF", val.name, val.val); row--; } print("</table><br>\n"); return; } class hand { function hand() { // this first loop shouldn't be necessary... for (i=0;i<52;i++) CARDS[i].dealt=false; for (i=0;i<5;i++) CARDS[c[i]].dealt=true; }; //CHECK FOR ROYAL FLUSH function hasroyalflush() { for (n=0;n<4;n++) { if ((CARDS[n+36].dealt==true)&&(CARDS[n+40].dealt==true)&&(CARDS[n+44].dealt==true)&&(CARDS[n+48].dealt==true)&&(CARDS[n+0].dealt==true)) return true; } return false; }; // CHECK FOR STRAIGHT FLUSH function hasstraightflush() { for (n=0;n<36;n++) { if ((CARDS[n+0].dealt==true)&&(CARDS[n+4].dealt==true)&&(CARDS[n+8].dealt==true)&&(CARDS[n+12].dealt==true)&&(CARDS[n+16].dealt==true)) return true; } return false; }; // CHECK FOR 4 OF A KIND function hasfour() { kind = { }; for (n=0;n<5;n++) kind[math.floor(tonumber(c[n])/4)]++; for (n=0;n<13;n++) if (kind[n]==4) return true; return false; }; // CHECK FOR A FULL HOUSE function hasfullhouse() { kind = { }; pair=0; for (n=0;n<5;n++) kind[math.floor(tonumber(c[n])/4)]++; for (n=0;n<13;n++) { if (kind[n]==3) pair=pair+1.5; if (kind[n]==2) pair=pair+1; } if (pair==2.5) return true; }; // CHECK FOR A FLUSH function hasflush() { flush = {}; for (n=0;n<13;n++) { if (CARDS[n*4+0].dealt==true) flush[0]++; if (CARDS[n*4+1].dealt==true) flush[1]++; if (CARDS[n*4+2].dealt==true) flush[2]++; if (CARDS[n*4+3].dealt==true) flush[3]++; } for (n=0;n<4;n++) if (flush[n]==5) return true; return false; }; // CHECK FOR A STRAIGHT function hasstraight() { straight = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; for (n=0;n<5;n++) straight[math.floor(tonumber(c[n])/4)]=1; straight[13]=straight[0]; for (n=0;n<10;n++) if (straight[n]+straight[n+1]+straight[n+2]+straight[n+3]+straight[n+4]==5) return true; return false; }; // CHECK FOR 3 OF A KIND function hasthree() { kind = { }; for (n=0;n<5;n++) kind[math.floor(tonumber(c[n])/4)]++; for (n=0;n<13;n++) if (kind[n]==3) return true; return false; }; // CHECK FOR 2 PAIRS function hastwopairs() { pair=0; kind = { }; for (n=0;n<5;n++) kind[math.floor(tonumber(c[n])/4)]++; for (n=0;n<13;n++) if (kind[n]==2) pair=pair+1; if (pair==2) return true; return false; }; // CHECK FOR HIGH PAIR function hashighpair() { kind = { }; for (n=0;n<5;n++) kind[math.floor(tonumber(c[n])/4)]++; if ((kind[10]==2)||(kind[11]==2)||(kind[12]==2)||(kind[0]==2)) return true; return false; }; } function ratehand() { h = new hand(); if (h.hasroyalflush()) return 9; if (h.hasstraightflush()) return 8; if (h.hasfour()) return 7; if (h.hasfullhouse()) return 6; if (h.hasflush()) return 5; if (h.hasstraight()) return 4; if (h.hasthree()) return 3; if (h.hastwopairs()) return 2; if (h.hashighpair()) return 1; return 0; } function printredraw() { print("<SCRIPT LANGUAGE=\"JavaScript\" TYPE=\"text/javascript\">\n<!--\n"); print("function redraw(c) {\n"); print( " r=c.responseXML.documentElement;\n" // " r=c.responseText;\n", // " document.getElementById('remstatus').innerHTML=r;\n", ); for (n=0;n<5;n++) { print(" document.game.C",n,".value=r.getElementsByTagName('C",n,"')[0].firstChild.data;\n"); print(" document.card",n,".src=r.getElementsByTagName('C",n,"F')[0].firstChild.data;\n"); print(" document.card",n,".alt=r.getElementsByTagName('C",n,"N')[0].firstChild.data;\n"); print(" document.card",n,".title=r.getElementsByTagName('C",n,"N')[0].firstChild.data;\n"); } print( " rating=r.getElementsByTagName('rating')[0].firstChild.data;\n", " for (var i=1;i<10;i++) {\n", " if (i==rating) {\n", " document.getElementById('row'+i).bgColor='#00FF7F';\n", " } else {\n", " document.getElementById('row'+i).bgColor='#FFFFFF';\n", " }\n", " }\n", "}\n" ); print( "function toggle(hc) {\n", " var a=null;\n", " if (document.all) {\n", " var coll=document.all.tags('TD');\n", " } else {\n", " var coll=document.getElementsByTagName('TD');\n", " }\n", " if (coll==null) return;\n", " for (i=0;i<coll.length;i++) {\n", " if (coll[i].id=='hbut'+hc) {\n", " a=coll[i];\n", " } else {\n", " }\n", " }\n", " var b=null;\n", " if (document.all) {\n", " var coll=document.all.tags('INPUT');\n", " } else {\n", " var coll=document.getElementsByTagName('INPUT');\n", " }\n", " if (coll==null) return;\n", " for (i=0;i<coll.length;i++) {\n", " if (coll[i].name=='H'+hc) {\n", " b=coll[i];\n", " } else {\n", " }\n", " }\n", " if (a==null || b==null) {\n", " alert('...');\n", " return;\n", " }\n", " if (b.value==0) {\n", " a.bgColor='#00FF7F';\n", " b.value=1;\n", " } else {\n", " a.bgColor='#FFFFFF';\n", " b.value=0;\n", " }\n", "}\n" ); print("// -->\n</SCRIPT>\n"); return; } function xmlmain() { global c = { regcard(_POST['C0']), regcard(_POST['C1']), regcard(_POST['C2']), regcard(_POST['C3']), regcard(_POST['C4']) }; global held = { 0, 0, 0, 0, 0 }; if (tonumber(_POST['H0'])) held[0]=1; else c[0]=getcard(); if (tonumber(_POST['H1'])) held[1]=1; else c[1]=getcard(); if (tonumber(_POST['H2'])) held[2]=1; else c[2]=getcard(); if (tonumber(_POST['H3'])) held[3]=1; else c[3]=getcard(); if (tonumber(_POST['H4'])) held[4]=1; else c[4]=getcard(); // print("<PRE>",exportvar(_GLOBALS),"</PRE>"); global opt = _POST['OPT']; global r=ratehand(); // if response header isn't text/xml, IE will choke on this _HEADER['CONTENT_TYPE']="text/xml"; // print("<?xml version='1.0' encoding='UTF-8' standalone='yes'? >\n"); printf("<?xml version='1.0' encoding='UTF-8' standalone='yes'%s>\n", '?'); print("<response>\n"); for (n=0;n<5;n++) print("<C",n,">",c[n],"</C",n,">\n<C",n,"F>/images/cards/",CARDS[c[n]].file,"</C",n,"F>\n<C",n,"N>",CARDS[c[n]].name,"</C",n,"N>\n"); print(" <rating>",r,"</rating>\n"); print("</response>\n"); if (trailer) print(trailer); return; } function main() { global c = { getcard(), getcard(), getcard(), getcard(), getcard() }; global held = { 0,0,0,0,0 }; global opt = ""; // if (_SERVER==null) { // this is a debug block // global _SERVER = { REQUEST_METHOD="POST", PATH_INFO="/nesla/blah" } // global _POST = { c0=12, c1=16, c2=20, c3=24, c4=28, h0=1, h1=1, h2=1, h3=1, h4=1 } // } print("<!DOCTYPE html>\n"); print("<html>\n<head>\n<title>Nesla-AJAX Poker</title>\n"); print("<style type=text/css>\n"); print("a { color: #0000ff; text-decoration: none; }\n"); print("a:hover { background-color: #e0e0ff; }\n"); print("</style>\n"); print("</head>\n<body>\n<center>\n"); printxml(); printredraw(); print("<FORM ACTION='", _SERVER['PATH_INFO'], "' METHOD='POST' NAME='game'>\n"); print("<SPAN ID='remstatus'>\n"); global r=ratehand(); showscore(r); for (n=0;n<5;n++) { print("<INPUT TYPE=hidden NAME=C", n, " VALUE='", c[n], "'>"); print("<INPUT TYPE=hidden NAME=H", n, " VALUE='", held[n], "'>\n"); } print("<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0><TR>\n"); for (n=0;n<5;n++) { local i=tonumber(c[n]); print("<TD ALIGN=LEFT WIDTH=71><IMG NAME=card", n, " WIDTH=71 HEIGHT=96 SRC='/images/cards/", CARDS[i].file ,"' ALT='", CARDS[i].name, "' TITLE='", CARDS[i].name, "'></TD>\n"); } print("</TR>\n"); // if (string.cmp(opt, "Deal")!=0) { print("<TR>\n"); for (n=0;n<5;n++) { print("<TD ID=hbut",n," ALIGN=CENTER WIDTH=71"); if (held[n]==1) print(" FONT COLOR=green"); print(" onClick=\"toggle(",n,")\">HOLD</TD>\n"); } print("</TR>\n"); print("<TR><TD ALIGN=CENTER COLSPAN=5>[<A HREF=\"javascript:nexthand('Deal');\"> DEAL </A>]</TD></TR>\n"); // } print("</TABLE>\n"); // if (string.cmp(opt, "Deal")==0) { // print("<A HREF='", _SERVER['PATH_INFO'], "' 'onClick=location.replace('", _SERVER['PATH_INFO'], "');return false;'>Play again</A>\n"); // } print("</SPAN>\n"); print("</FORM>\n"); // CARDS=null;print("<PRE>",exportvar(_GLOBALS),"</PRE>"); if (_SERVER['REQUEST_METHOD']=='GET') { print("</center>\n</body>\n</html>\n"); } return; } try { if (!include("_cards.ns")) print("failed to include _cards.ns\r\n"); if (!include("_ajax.ns")) print("failed to include _ajax.ns\r\n"); if (typeof(_SERVER)=='table') { if (_SERVER['REQUEST_METHOD']=='POST') { xmlmain(); } else { main(); } } } catch (ex) { print(serialize(ex)); }