Sunday, October 20, 2013

My First Javascript Game - Constructing the Turn

In order to have any game run smoothly a you have to build in a flow, an ordered structure to the turn. Building this up for Chinchirorin was certainly not as straight forward as I thought it would be. The only thing more complex than getting the game to proceed in a logical manner for the player (also meaning in a way that they could track) was getting the scoring built in. The two tasks are closely related as it is possible for both the house and player to score automatic wins and losses on their respective turns. I chose to construct both the player and house turns seperately. Though given their similarity this was mostly a choice made to allow my own tracking of the programming to be simplified.

 

function houseturn ()

{stage=2;

score=0;

housescore=0;

winlose="undecided";

triples="undecided";

doubles="undecided";

yourscore=0;

rolldice ();

dieroll1.innerHTML="<p>"+die1+"</p>";

dieroll2.innerHTML="<p>"+die2+"</p>";

dieroll3.innerHTML="<p>"+die3+"</p>";

scoring ();

housescore=score;

if ((die1==4 || die2==4 || die3==4) && (die1==5 || die2==5 || die3==5) && (die1==6 || die2==6 || die3==6))

       {zelda.innerHTML="<p>4-5-6 Straight Kill Win! House Wins!</p>";}

if (triples=="yes")

       {zelda.innerHTML="<p>Leopards Win! House Wins!</p>";}

if (score==6)

       {zelda.innerHTML="<p>Big Six Win! House Wins!</p>";}

if ((die1==1 || die2==1 || die3===1) && (die1==2 || die2==2 || die3==2) && (die1==3 || die2==3 || die3==3))

       {zelda.innerHTML="<p>1-2-3 Straight Lose! You Lose!</p>";}

if (score==1)

       {zelda.innerHTML="<p>Asshole Ones! House Loses!</p>";}

if ((winlose=="undecided") && (score>0))

       {zelda.innerHTML="<p>House Scores "+housescore+", Your Turn!</p>"}

if ((winlose=="undecided") && (score==0))

       {zelda.innerHTML="<p>No Score - House Rolls Again!</p>";}

timer=setInterval(timefunction, 3000);}

 

The houseturn () function above, and playerturn () function below are pretty much identical. The only differences between them is what they set the stage to at the beginning of the turn, and in how the rolls produced during the turn affect the scoring and betting for the game. After designating the stage of the game the function sets all of the temporary score variables to zero. This is done so that previous rolls done in either past turns, or previous iterations of the current turn, can not confuse or make the scoring on the following roll incorrect. Next the dice are rolled, then scored, by calling other functions. Then there are a series of if statements measure whether roll was an automatic win are loss. If none of them apply, after that the last two if statements see whether any score was generated at all. If there was, the game precedes on to either the next turn, or comparing the two scores. If no score was generated, then the turn is repeated.

 

function playerturn ()

{stage=3;

winlose="undecided";

score=0;

triples="undecided";

doubles="undecided";

yourscore=0;

rolldice ();

dieroll1.innerHTML="<p>"+die1+"</p>";

dieroll2.innerHTML="<p>"+die2+"</p>";

dieroll3.innerHTML="<p>"+die3+"</p>";

scoring ();

yourscore=score;

if ((die1==4 || die2==4 || die3==4) && (die1==5 || die2==5 || die3==5) && (die1==6 || die2==6 || die3==6))

       {zelda.innerHTML="<p>4-5-6 Straight Kill Win! You Win!</p>";}

if (triples=="yes")

       {zelda.innerHTML="<p>Leopards Win! You Win!</p>";}

if (score==6)

       {zelda.innerHTML="<p>Big Six Win! You Win!</p>";}

if ((die1==1 || die2==1 || die3===1) && (die1==2 || die2==2 || die3==2) && (die1==3 || die2==3 || die3==3))

       {zelda.innerHTML="<p>1-2-3 Straight Lose! You Lose!</p>";}

if (score==1)

       {zelda.innerHTML="<p>Asshole Ones! House Loses!</p>";}

if ((winlose=="undecided") && (score>0))

       {zelda.innerHTML="<p>You Score: "+yourscore+"</p>"}

if ((winlose=="undecided") && (score==0))

       {zelda.innerHTML="<p>No Score - You Roll Again!</p>";}

timer=setInterval(timefunction, 3000);}

 

It became apparent to me after rhe initial programming was done, that it was quite possible for the game to generate, process, and report results, and then move on, before I could even read that it had done the first step, much less what the results had been. This was the reason that I added the timer line at the end of the player and house turns. I needed to create a function that would simply tell the game to puase, allowing the player to see what was rolled or what was scored - so that they understood what the game wanted them to do next.

 

I mentioned stages briefly earlier when describing how I set up the turns. The stage variable was meant to be a variable that the game could refer to, to let it know what part of the game it currently is running, and therefore what it needed to run next. If the timefunction is called it checks what stage the game is in, as well as ant other important parameters such as the score, and tells the game which function to run next. I set the game up to follow these stages: 1=beginning/place initial bet, 2=houseturn, and 3=playerturn.

 

function timefunction ()

{clearInterval (timer);

if (stage==1)

       {houseturn ()} else

if (stage==2 && winlose=="undecided" && score==0)

       {houseturn ()} else

if (stage==2 && winlose=="undecided" && score>0)

       {playerturn ()} else

if ((stage==2 || stage==3) && (winlose=="win" || winlose=="lose"))

       {autoresolve ()} else

if (stage==3 && winlose=="undecided" && score>0)

       {playerturn ()} else

if (stage==3 && winlose=="undecided" && score>0)

       {comparescore ()}}

 

The last function that was put in to have directdirect bearing on the processing of the turn was the autoresolve function below. This function was designed to be called win an auotmatic win or loss condition was met in one of the turns. It would then call the appropriate end of the round win or loss resolution based on the stage it was called and the score.

 

function autoresolve ()

{if (stage==2 && winlose=="win")

       {stage=2.5;

       youlose ();}

if (stage==2 && winlose=="lose")

       {stage=2.5;

       youwin ();}

if (stage==3 && winlose=="win")

       {stage=3.5;

       youwin ();}

if (stage==3 && winlose=="lose")

       {stage=3.5;

       youlose ();}}