# Update amountleft on Dice Game roll

Page 1 of 1

## 6 Replies - 1149 Views - Last Post: 11 June 2009 - 08:40 PM

### #1 chez007

Reputation: 0
• Posts: 13
• Joined: 28-April 09

# Update amountleft on Dice Game roll

Posted 10 June 2009 - 09:27 PM

I am just about finished the dice game but am having trouble doing the calculation where if a player wins then they double their venture.

I am not too sure how the game stores what the user picks in their venture 100 or 200.

I am already subtracting their venture from the total when they initially make their venture. But if they win then I need to add back the venture to the total by 2.

Not too sure how to do this. Do I use a new function. How can I tell which button the user chooses 100 or 200 and then add on that amount.

Here is my code
```
function decvars()
{
amtleft=0;
venture1=100;
venture2=200;
ventureflag = 0;
roller = 0;
selvalue = 0;
gameover=0;
}

// This is the routine to display the amountleft
var amtleft;
function bal()
{
document.balance.aleft.value = amtleft;
timer=setTimeout("bal()", 1000)
}

// This is the function for the user to click an image and deposit money into their account - Cherylene Anderson 2009
function make_deposit(imgclicked)
{
amtleft = amtleft + imgclicked;
alert("You have deposited \$" + imgclicked);
}

// This is the function for the user to click button and make venture 100 or 200 - Cherylene Anderson 2009

var takeapick=0
function myventure()
{

if	(document.balance.aleft.value >0)
{
if (takeapick == 1)
{
amtleft = amtleft - venture1;
ventureflag = 1;
ventvalue = 1;
}
else if (takeapick == 2)
{
amtleft = amtleft - venture2;
ventureflag = 1;
ventvalue = 2;
}
}
else
{
alert("You must deposit money before making a venture");
}
}

// This is the function for user to choose what they think the dice will roll 1 to 4, 5 to 8 or 9 to 12 - Cherylene Anderson 2009

var userchoice=0
function selection()
{

if (ventureflag >0)
{
if (userchoice == 1)
{
alert ("You have chosen 1 to 4");
roller = 1;
selvalue = 1;
}
else if (userchoice == 2)
{
alert ("You have chosen 5 to 8");
roller = 1;
selvalue =2;
}
else if (userchoice == 3)
{
alert ("You have chosen 9 to 12");
roller = 1;
selvalue =3;
}
}
else
{
alert("You must make a venture before making a selection");
}
}

// This is the function for rolling two dice - Cherylene Anderson 2009

var diceroll;
function dice_roll()
{
if (roller >0)
{

var mydice = new Array
mydice[1] = "images/dice1.gif"
mydice[2] = "images/dice2.gif"
mydice[3] = "images/dice3.gif"
mydice[4] = "images/dice4.gif"
mydice[5] = "images/dice5.gif"
mydice[6] = "images/dice6.gif"
var diceimage=Math.round(Math.random()*5)
var diceimage2=Math.round(Math.random()*5)
diceimage = diceimage +1;
diceimage2 = diceimage2 +1;
document.diceimage2.src=mydice[diceimage2];
document.diceimage1.src=mydice[diceimage];
var dicetotal = diceimage + diceimage2;
alert("Dice total is " + dicetotal);

if (dicetotal <=4)
{
diceroll = 1;
}
else if	(dicetotal <=8)
{
diceroll = 2;
}
else if (dicetotal <=12)
{
diceroll = 3;
}
}
else
{
alert("You must make a selection before rolling the dice");
}

}

// This is the function for checking to see if the users pick matches that of the dice roll - Cherylene Anderson 2009

function CheckRoll()
{
if (selvalue == 1 && diceroll == 1)
{
}
else if (selvalue == 2 && diceroll == 2)
{
}
else if (selvalue == 3 && diceroll == 3)
{
}
else
{
}
}

//if (document.balance.aleft.value =0)
//{
//alert ("Sorry game over, try again");
//}
</script>

```

I have set variables ventvalue =1 for 100 and ventvalue = 2 for the 200 venture. When I am doing the CheckRoll function can I add the statements in there without having to do another function for calculating the win.

If they lose the diceroll the amount stays the same. I just need to double their venture if they select either 100 or 200.

Can you help. This is the last thing I need to do on this assignment.

Is This A Good Question/Topic? 0

## Replies To: Update amountleft on Dice Game roll

• MSX

Reputation: 131
• Posts: 722
• Joined: 28-April 07

## Re: Update amountleft on Dice Game roll

Posted 11 June 2009 - 11:28 AM

Hello there,

if I understand you correctly, you want to keep whatever the (venture value) user choose in memory to be able to double it on CheckRoll function.
if this is the point, here is the a possible solution:

decvars() function:
replace:
```venture1=100;
venture2=200;

```

with:
```ventures=new Array(100,200);
venturevalue=0;

```

and
replace myventure() function with this one:
```function myventure()// Updated
{

if	(document.balance.aleft.value >0)
{
venturevalue = ventures[takeapick-1]
amtleft = amtleft - venturevalue;
ventureflag = 1;
ventvalue = takeapick;

}
else
{
alert("You must deposit money before making a venture");
}
}

```

now you can use the ventvalue var inside the CheckRoll function to do whatever,
well, I didn't find where you want /do use the doubled venture value, so I may be missed something here

anyways, I did a few modifications to the code you sent just to make the code size smaller

here is the code:
```function decvars()
{
amtleft=0;
ventures=new Array(100,200);
venturevalue=0;
ventureflag = 0;
roller = 0;
selvalue = 0;
gameover=0;
}

// This is the routine to display the amountleft

var amtleft;
function bal()
{

document.balance.aleft.value = amtleft;
timer=setTimeout("bal()", 1000)
}

// This is the function for the user to click an image and deposit money into their account - Cherylene Anderson 2009

function make_deposit(imgclicked) // Updated
{
amtleft = amtleft + imgclicked;
alert("You have deposited \$" + imgclicked);
}

// This is the function for the user to click button and make venture 100 or 200 - Cherylene Anderson 2009

var takeapick=0
function myventure()// Updated
{

if	(document.balance.aleft.value >0)
{
venturevalue = ventures[takeapick-1]
amtleft = amtleft - venturevalue;
ventureflag = 1;
ventvalue = takeapick;

}
else
{
alert("You must deposit money before making a venture");
}
}

// This is the function for user to choose what they think the dice will roll 1 to 4, 5 to 8 or 9 to 12 - Cherylene Anderson 2009

var userchoice=0
function selection()// Updated
{

if (ventureflag >0)
{

var from=(userchoice-1)*4+1
var to=from+3

alert ("You have chosen "+from+" to "+to);
roller = 1;
selvalue = userchoice;

}
else
{
alert("You must make a venture before making a selection");
}
}

// This is the function for rolling two dice - Cherylene Anderson 2009

var diceroll;
function dice_roll() // Updated
{
if (roller >0)
{

var mydice = new Array();
for (i=1;i<=6;i++){
mydice[i] = "images/dice"+i+".gif";
}
var diceimage=Math.round(Math.random()*5)
var diceimage2=Math.round(Math.random()*5)
diceimage = diceimage +1;
diceimage2 = diceimage2 +1;
document.diceimage2.src=mydice[diceimage2];
document.diceimage1.src=mydice[diceimage];
var dicetotal = diceimage + diceimage2;
alert("Dice total is " + dicetotal);

diceroll=Math.floor((dicetotal-1)/4)+1;
}
else
{
alert("You must make a selection before rolling the dice");
}

}

// This is the function for checking to see if the users pick matches that of the dice roll - Cherylene Anderson 2009

function CheckRoll()
{
if(setvalue==diceroll){
}
else
{
}
}

```

Note: I didn't test this because we don't know the cycle of your game, and how it affects on Vars and Functions,

Regards

This post has been edited by ahmad_511: 11 June 2009 - 05:39 PM

### #3 chez007

Reputation: 0
• Posts: 13
• Joined: 28-April 09

## Re: Update amountleft on Dice Game roll

Posted 11 June 2009 - 05:18 PM

ahmad_511, on 11 Jun, 2009 - 10:28 AM, said:

Hello there,

if I understand you correctly, you want to keep whatever the (venture value) user choose in memory to be able to double it on CheckRoll function.
if this is the point, here is the a possible solution:

decvars() function:
replace:
```venture1=100;
venture2=200;

```

with:
```ventures=new Array(100,200);
ventvalue=0;

```

and
replace myventure() function with this one:
```function myventure()// Updated
{

if	(document.balance.aleft.value >0)
{
ventvalue = ventures[takeapick-1]
amtleft = amtleft - ventvalue;
ventureflag = 1;
ventvalue = takeapick;

}
else
{
alert("You must deposit money before making a venture");
}
}

```

now you can use the ventvalue var inside the CheckRoll function to do whatever,
well, I didn't find where you want /do use the doubled venture value, so I may be missed something here

anyways, I did a few modifications to the code you sent just to make the code size smaller

here is the code:
```function decvars()
{
amtleft=0;
ventures=new Array(100,200);
ventvalue=0;
ventureflag = 0;
roller = 0;
selvalue = 0;
gameover=0;
}

// This is the routine to display the amountleft

var amtleft;
function bal()
{

document.balance.aleft.value = amtleft;
timer=setTimeout("bal()", 1000)
}

// This is the function for the user to click an image and deposit money into their account - Cherylene Anderson 2009

function make_deposit(imgclicked) // Updated
{
amtleft = amtleft + imgclicked;
alert("You have deposited \$" + imgclicked);
}

// This is the function for the user to click button and make venture 100 or 200 - Cherylene Anderson 2009

var takeapick=0
function myventure()// Updated
{

if	(document.balance.aleft.value >0)
{
ventvalue = ventures[takeapick-1]
amtleft = amtleft - ventvalue;
ventureflag = 1;
ventvalue = takeapick;

}
else
{
alert("You must deposit money before making a venture");
}
}

// This is the function for user to choose what they think the dice will roll 1 to 4, 5 to 8 or 9 to 12 - Cherylene Anderson 2009

var userchoice=0
function selection()// Updated
{

if (ventureflag >0)
{

var from=(userchoice-1)*4+1
var to=from+3

alert ("You have chosen "+from+" to "+to);
roller = 1;
selvalue = userchoice;

}
else
{
alert("You must make a venture before making a selection");
}
}

// This is the function for rolling two dice - Cherylene Anderson 2009

var diceroll;
function dice_roll() // Updated
{
if (roller >0)
{

var mydice = new Array();
for (i=1;i<=6;i++){
mydice[i] = "images/dice"+i+".gif";
}
var diceimage=Math.round(Math.random()*5)
var diceimage2=Math.round(Math.random()*5)
diceimage = diceimage +1;
diceimage2 = diceimage2 +1;
document.diceimage2.src=mydice[diceimage2];
document.diceimage1.src=mydice[diceimage];
var dicetotal = diceimage + diceimage2;
alert("Dice total is " + dicetotal);

diceroll=Math.floor((dicetotal-1)/4)+1;
}
else
{
alert("You must make a selection before rolling the dice");
}

}

// This is the function for checking to see if the users pick matches that of the dice roll - Cherylene Anderson 2009

function CheckRoll()
{
if(setvalue==diceroll){
}
else
{
}
}

```

Note: I didn't test this because we don't know the cycle of your game, and how it affects on Vars and Functions,

Regards

Thanks so much for your help. I'm pretty new to all of this so it is all a learning experience. By doing this game from scratch I certainly have learnt a bit.

Basically in the dice game if the user wins then they get their venture back and double it. So if they deposited 500 and ventured 200 and they win they should get 700.

Should I put this after the alert ("You Win"). Something like

```amtleft = amtleft + venturevalue * 2 etc.
```

One more thing how do I end the game. If the amountleft reaches 0 the game should end and say sorry game over.

Should it be something like:

```If (document.balance.aleft.value =0)
```

Should I put this at the end of my scripting.

• MSX

Reputation: 131
• Posts: 722
• Joined: 28-April 07

## Re: Update amountleft on Dice Game roll

Posted 11 June 2009 - 05:54 PM

about game over: you can add the code inside but at the end of CheckRoll()
also you can use the amtleft instead of using getElementById() since you already have it.

Note: I found a var conflict in my previous code ( i used an already exist var name (ventvalue which it used to store different kind of values)

so please, check the code again
and here is the CheckRoll() function after updating it:
```function CheckRoll()
{
if(setvalue==diceroll){
amtleft = amtleft + venturevalue * 2

}
else
{
}
if(amtleft <=0){
}
}

```

### #5 chez007

Reputation: 0
• Posts: 13
• Joined: 28-April 09

## Re: Update amountleft on Dice Game roll

Posted 11 June 2009 - 06:55 PM

ahmad_511, on 11 Jun, 2009 - 04:54 PM, said:

about game over: you can add the code inside but at the end of CheckRoll()
also you can use the amtleft instead of using getElementById() since you already have it.

Note: I found a var conflict in my previous code ( i used an already exist var name (ventvalue which it used to store different kind of values)

so please, check the code again
and here is the CheckRoll() function after updating it:
```function CheckRoll()
{
if(setvalue==diceroll){
amtleft = amtleft + venturevalue * 2

}
else
{
}
if(amtleft <=0){
}
}

```

Thanks. I have changed the variable name and it is working. I have been working on this assignment for so many hours, thank you for all your help.

Is there any way you could explain to me what the code does, just to see how you did it differently to mine. (userchoice-1)*4 +1. I know you said it was more compact then mine but it seems more complex too.

```var from = (userchoice-1)*4 + 1
var to = from + 3

```

Why also do you use this in the myventure function [takeapick-1]. Just need to know so I can explain it properly in my report.

Thank you

• MSX

Reputation: 131
• Posts: 722
• Joined: 28-April 07

## Re: Update amountleft on Dice Game roll

Posted 11 June 2009 - 07:38 PM

maybe it's more complex than yours but it's more public
think about how many userchoice(s) you have.
you need two alert message and two assignment for each

so, what to do if you have for example 10 userchoice(s), it will be hard to code that and there are more abilities to make some mistakes

1- the choices you have are (1,2,3) which are identical with selvalue , so we can shorten this by assigning the selvalue to userchoice and no need for hard coding it
2- if you look at the first alert message you'll see that the only different is the (chosen * to *) and they're increased according to userchoice
so, try to calculate the from/to vars when userchoice is equals to (1,2,3,...)
you'll find that with one line of code we can generate all of the needed messages

that's it, yours will work perfectly, but it needs a manual update if you decided to change choice's count.

3- i saw there is a relation between takeapick and the venture1,venture2
depending on the we replaced the venture1, venture2 with one Array to hold all possible venture values
and since array's indexes are zero based and takeapick starts from 1
if we need then to refer to the first venture in the array we have to less 1 from the takeapick to match the sequence in the array.
for example when takeapick =1 we will get the array's element (1-1=0)
in another words ventures[0] which is equals to 100

so, it's just a matter of linking things together, observing the relations and playing with it to meet our needs

I hope I didn't make it more complex instead of clarifying it

Regards

### #7 chez007

Reputation: 0
• Posts: 13
• Joined: 28-April 09

## Re: Update amountleft on Dice Game roll

Posted 11 June 2009 - 08:40 PM

ahmad_511, on 11 Jun, 2009 - 06:38 PM, said:

maybe it's more complex than yours but it's more public
think about how many userchoice(s) you have.
you need two alert message and two assignment for each

so, what to do if you have for example 10 userchoice(s), it will be hard to code that and there are more abilities to make some mistakes

1- the choices you have are (1,2,3) which are identical with selvalue , so we can shorten this by assigning the selvalue to userchoice and no need for hard coding it
2- if you look at the first alert message you'll see that the only different is the (chosen * to *) and they're increased according to userchoice
so, try to calculate the from/to vars when userchoice is equals to (1,2,3,...)
you'll find that with one line of code we can generate all of the needed messages

that's it, yours will work perfectly, but it needs a manual update if you decided to change choice's count.

3- i saw there is a relation between takeapick and the venture1,venture2
depending on the we replaced the venture1, venture2 with one Array to hold all possible venture values
and since array's indexes are zero based and takeapick starts from 1
if we need then to refer to the first venture in the array we have to less 1 from the takeapick to match the sequence in the array.
for example when takeapick =1 we will get the array's element (1-1=0)
in another words ventures[0] which is equals to 100

so, it's just a matter of linking things together, observing the relations and playing with it to meet our needs

I hope I didn't make it more complex instead of clarifying it

Regards

Thanks for explaining it to me your help has been much appreciated.