NullLogic

blackjack game for web servers using AJAX

#!/usr/bin/nsp # blackjack sample prog by Dan Cahill if (!include("_cards.ns")) print("failed to include _cards.ns\r\n"); if (!include("_ajax.ns")) print("failed to include _ajax.ns\r\n"); function cardval(p) { if (typeof(p)!='number') return 0; x=tonumber(p)+1; if (x<1) return 0; x=math.ceil(x/4); if (x>10) x=10; if (x==1) x=11; return x; } 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 prepcards() { if (_SERVER['REQUEST_METHOD']=='POST') { global d = { regcard(_POST['D0']), regcard(_POST['D1']) }; global c = { regcard(_POST['C0']), regcard(_POST['C1']) }; global dv = { cardval(d[0]), cardval(d[1]) }; global cv = { cardval(c[0]), cardval(c[1]) }; if (_POST['D2']) { d[2]=regcard(_POST['D2']); dv[2]=cardval(d[2]); } if (_POST['D3']) { d[3]=regcard(_POST['D3']); dv[3]=cardval(d[3]); } if (_POST['D4']) { d[4]=regcard(_POST['D4']); dv[4]=cardval(d[4]); } if (_POST['C2']) { c[2]=regcard(_POST['C2']); cv[2]=cardval(c[2]); } if (_POST['C3']) { c[3]=regcard(_POST['C3']); cv[3]=cardval(c[3]); } if (_POST['C4']) { c[4]=regcard(_POST['C4']); cv[4]=cardval(c[4]); } if (_POST['OPT']!=null) { global opt = _POST['OPT']; } else { global opt = ""; } if (opt=="Hit") { if (c[0]==null) { print("weird error 1"); } else if (c[1]==null) { print("weird error 2"); } else if (c[2]==null) { c[2]=getcard(); cv[2]=cardval(c[2]); } else if (c[3]==null) { c[3]=getcard(); cv[3]=cardval(c[3]); } else if (c[4]==null) { c[4]=getcard(); cv[4]=cardval(c[4]); } } else if (opt=="Stand") { for (n=2;n<5;n++) { global dt=tonumber(dv[0])+tonumber(dv[1])+tonumber(dv[2])+tonumber(dv[3])+tonumber(dv[4]); if ((dt>21)&&(dv[4]==11)) dv[4]=1; if ((dt>21)&&(dv[3]==11)) dv[3]=1; if ((dt>21)&&(dv[2]==11)) dv[2]=1; if ((dt>21)&&(dv[1]==11)) dv[1]=1; if ((dt>21)&&(dv[0]==11)) dv[0]=1; global dt=tonumber(dv[0])+tonumber(dv[1])+tonumber(dv[2])+tonumber(dv[3])+tonumber(dv[4]); if (dt>15) break; d[n]=getcard(); dv[n]=cardval(d[n]); } } } else { global d = { getcard(), getcard(), null, null, null }; global c = { getcard(), getcard(), null, null, null }; global dv = { cardval(d[0]), cardval(d[1]), null, null, null }; global cv = { cardval(c[0]), cardval(c[1]), null, null, null }; global opt = ""; } global dt=tonumber(dv[0])+tonumber(dv[1])+tonumber(dv[2])+tonumber(dv[3])+tonumber(dv[4]); global ct=tonumber(cv[0])+tonumber(cv[1])+tonumber(cv[2])+tonumber(cv[3])+tonumber(cv[4]); if ((ct>21)&&(cv[4]==11)) cv[4]=1; if ((ct>21)&&(cv[3]==11)) cv[3]=1; if ((ct>21)&&(cv[2]==11)) cv[2]=1; if ((ct>21)&&(cv[1]==11)) cv[1]=1; if ((ct>21)&&(cv[0]==11)) cv[0]=1; global ct=tonumber(cv[0])+tonumber(cv[1])+tonumber(cv[2])+tonumber(cv[3])+tonumber(cv[4]); return; } function printredraw() { print("<SCRIPT LANGUAGE=\"JavaScript\" TYPE=\"text/javascript\">\n<!--\n"); print("function redraw(c) {\n"); print(" r=c.responseText;\n"); print(" document.getElementById('remstatus').innerHTML=r;\n"); print("}\n"); print("// -->\n</SCRIPT>\n"); return; } function main() { if (_SERVER['REQUEST_METHOD']==null) { global _SERVER = { REQUEST_METHOD="GET", PATH_INFO="/nesla/blah" } } // this is a debug block if (typeof(_SESSION)!='table') global _SESSION = { }; if (typeof(_SESSION['BJ'])!='table') _SESSION['BJ'] = { }; _SESSION['BJ'].won = tonumber(_SESSION['BJ'].won); _SESSION['BJ'].lost = tonumber(_SESSION['BJ'].lost); prepcards(); if (_SERVER['REQUEST_METHOD']!="POST") { print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); print("<HTML>\n<HEAD>\n<TITLE>Nesla-AJAX Blackjack</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"); for (n=0;n<5;n++) if (d[n]!=null) print("<INPUT TYPE=hidden NAME=d", n, " VALUE='", d[n], "'>\n"); for (n=0;n<5;n++) if (c[n]!=null) print("<INPUT TYPE=hidden NAME=c", n, " VALUE='", c[n], "'>\n"); print("<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0>\n"); print("<TR><TD ALIGN=LEFT COLSPAN=2 WIDTH=355>"); if (opt=="Stand") { for (n=0;n<5;n++) if (d[n]!=null) print("<IMG WIDTH=71 HEIGHT=96 SRC='/images/cards/", CARDS[d[n]]['file'] ,"' ALT='", CARDS[d[n]]['name'], "' TITLE='", CARDS[d[n]]['name'], "'>"); } else if (ct>20) { for (n=0;n<5;n++) if (d[n]!=null) print("<IMG WIDTH=71 HEIGHT=96 SRC='/images/cards/", CARDS[d[n]]['file'] ,"' ALT='", CARDS[d[n]]['name'], "' TITLE='", CARDS[d[n]]['name'], "'>"); } else { print("<IMG WIDTH=71 HEIGHT=96 SRC='/images/cards/face.png' ALT='unknown' TITLE='unknown'>"); if (d[1]!=null) print("<IMG WIDTH=71 HEIGHT=96 SRC='/images/cards/", CARDS[d[1]]['file'], "' ALT='", CARDS[d[1]]['name'], "' TITLE='", CARDS[d[1]]['name'], "'>"); } print("</TD></TR>\n"); print("<TR><TD ALIGN=LEFT COLSPAN=2 WIDTH=355>"); for (n=0;n<5;n++) if (c[n]!=null) print("<IMG WIDTH=71 HEIGHT=96 SRC='/images/cards/", CARDS[c[n]]['file'], "' ALT='", CARDS[c[n]]['name'], "' TITLE='", CARDS[c[n]]['name'], "'>"); print("</TD></TR>\n"); print("</TABLE>\n"); if (ct>21) { _SESSION['BJ'].lost++; print("<B><FONT COLOR=RED>bust - DEALER WINS</FONT></B><BR>\n"); print("<A HREF='", _SERVER['PATH_INFO'], "'>Play again</A>\n"); } else if (ct==21) { _SESSION['BJ'].won++; print("<B><FONT COLOR=GREEN>21! - YOU WIN</FONT></B><BR>\n"); print("<A HREF='", _SERVER['PATH_INFO'], "'>Play again</A>\n"); } else if (opt=="Stand") { if (dt>21) { _SESSION['BJ'].won++; print("<B><FONT COLOR=GREEN>dealer busts - YOU WIN</FONT></B><BR>\n"); } else if (dt>ct) { _SESSION['BJ'].lost++; print("dealer has ", dt, " - you have ", ct, "<BR>\n"); print("<B><FONT COLOR=RED>DEALER WINS</FONT></B><BR>\n"); } else { _SESSION['BJ'].won++; print("dealer has ", dt, " - you have ", ct, "<BR>\n"); print("<B><FONT COLOR=GREEN>YOU WIN</FONT></B><BR>\n"); } print("<A HREF='", _SERVER['PATH_INFO'], "' 'onClick=location.replace('", _SERVER['PATH_INFO'], "');return false;'>Play again</A>\n"); } else if (ct<21) { print("[<A HREF=\"javascript:nexthand('Hit');\"> HIT </A>]\n"); print("[<A HREF=\"javascript:nexthand('Stand');\"> STAND </A>]\n"); } print("<BR>wins:", _SESSION['BJ'].won, " - losses:", _SESSION['BJ'].lost, "<BR>\n"); print("runtime = ", runtime(), " seconds\n"); print("</SPAN>\n"); print("</FORM>\n"); if (_SERVER['REQUEST_METHOD']!="POST") { print("</CENTER>\n</BODY>\n</HTML>\n"); } return; } if (typeof(_SERVER)=='table') main();