codice:
<html>
<head>
<title>Gioco dei Dadi - Craps</title>
<script>
var cwidth=400;
var cheight=300;
var dicex=50;
var dicey=50;
var dicewidth=100;
var diceheight=100;
var dotrad=6;
var ctx;
var dx;
var dy;
var firstturn = true;
var point;
function throwdice(){
var sum;
var ch = 1+Math.floor(Math.random()*6);
sum = ch;
dx = dicex;
dy = dicey;
drawface(ch);
dx = dicex + 150;
ch=1 + Math.floor(Math.random()*6);
sum += ch;
drawface(ch);
var bank = Number(document.f.bank.value);
if (bank<10){
alert("hai finito i soldi! Aggiungine altri e prova ancora.");
return;
}
bank = bank -10;
document.f.bank.value = String(bank);
if (firstturn){
switch(sum){
case 7:
case 11:
document.f.outcome.value="Hai Vinto!";
bank = Number(document.f.bank.value);
bank +=20;
document.f.bank.value = String (bank);
break;
case 2:
case 3:
case 12:
document.f.outcome.value="Hai Perso!";
break;
default:
point = sum;
document.f.pv.value=point;
firstturn = false;
document.f.stage.value="Ritira il Dado.";
document.f.outcome.value=" ";
}
}
else{
switch(sum){
case point:
document.f.outcome.value="Hai Vinto!";
document.f.stage.value="Ricomincia dal Primo Lancio.";
document.f.pv.value=" ";
firstturn = true;
bank = Number(document.f.bank.value);
bank +=20;
document.f.bank.value = String (bank);
break;
case 7:
document.f.outcome.value="Hai Perso!";
document.f.stage.value="Ricomincia dal Primo Lancio.";
document.f.pv.value=" ";
firstturn = true;
}
}
}
function drawface(n){
ctx=document.getElementById('canvas').getContext('2d');
ctx.lineWidth=5;
ctx.clearRect(dx,dy,dicewidth,diceheight);
ctx.strokeRect(dx,dy,dicewidth,diceheight);
var dotx;
var doty;
ctx.fillStyle="#009966";
switch(n){
case 1:
draw1();
break;
case 2:
draw2();
break;
case 3:
draw2();
draw1();
break;
case 4:
draw4();
break;
case 5:
draw4();
draw1();
break;
case 6:
draw4();
draw2mid();
break;
}
}
function draw1(){
var dotx;
var doty;
ctx.beginPath();
dotx = dx + .5*dicewidth;
doty = dy + .5*diceheight;
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
}
function draw2(){
var dotx;
var doty;
ctx.beginPath();
dotx = dx + 3*dotrad;
doty = dy + 3*dotrad;
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
dotx = dx+dicewidth-3*dotrad;
doty = dx+diceheight-3*dotrad;
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
}
function draw4(){
var dotx;
var doty;
ctx.beginPath();
dotx = dx + 3*dotrad;
doty = dy + 3*dotrad;
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
dotx = dx+dicewidth-3*dotrad;
doty = dy+diceheight-3*dotrad;
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
ctx.beginPath();
dotx = dx + 3*dotrad;
doty = dy + diceheight-3*dotrad;// no change
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
dotx = dx+dicewidth-3*dotrad;
doty = dy+3*dotrad;
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
}
function draw2mid(){
var dotx;
var doty;
ctx.beginPath();
dotx = dx + 3*dotrad;
doty = dy + .5*diceheight;
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
dotx = dx+dicewidth-3*dotrad;
doty = dy + .5*diceheight; //no change
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
}
</script>
</head>
<body>
<canvas id="canvas" width="400" height="300">
Your browser doesn't support the HTML5 element canvas.
</canvas>
<button onClick="throwdice();">Tira i Dadi </button>
<form name="f">
Turno: <input name="stage" value="Primo Lancio"/>
Punteggio: <input name="pv" value=" "/>
Esito: <input name="outcome" value=" "/>
Cassa: <input name="bank" value="100"/>
</form>
</body>
</html>
suggerimenti?