The Essential Guide to HTML5

(Greg DeLong) #1

CHAPTER 2


var ctx;


var dx;


var dy;


var firstturn = true; Global variable, initialized to the
value true


var point; Global variable, does not need to
be initialized because it will be set
before use


function throwdice() { Start of throwdice function


var sum; Variable to hold the sum of the
values for the 2 dice


var ch = 1+Math.floor(Math.random()*6); Set ch with the first random value


sum = ch; Assign this to sum


dx = dicex; Set dx


dy = dicey; set dy


drawface(ch); Draw the first die face


dx = dicex + 150; Adjust the horizontal position


ch=1 + Math.floor(Math.random()*6); Set ch with a random value. This
is the one for the second die.


sum += ch; Add ch to what is al ready in sum


drawface(ch); Draw the second die


if (firstturn) { Now start the implementation of
the rules. Is it a first turn?


switch(sum) { If it is, start a switch with sum as
the condition


case 7: For 7

Free download pdf