...

View Full Version : Simple JavaScript game gone awry due to possible math problem



Cubefreak101
10-11-2008, 04:59 AM
The program I'm having trouble with is a Slot Machine game I made in JavaScript. Everything works aesthetically fine, but the actual math part (calculating the money gained/lost from wins and losses) is doing something pretty strange....

For instance, I bet $0.25 in one instance so the game subtracted $0.25 from my original total of $22.75 It then added $1.25 (the winning bonus for sevens is x5) to my dollar amount. Despite this, my value is showing up as 22.750.50.751.25 I have no idea what I might be doing wrong. I'm sure you will find many more instances of these strange glitches of it adding multiple decimals or thousands of dollars to the total whenever you win.

The program is hosted here (http://www.geocities.com/cubefreak1001/programs/slotmachine.html). I would post the bit of code I think may be causing the problem, but I really have no idea what could cause this. The best I can do is post the function that does the calculations for the slot machine change and the money values.


function slotMachine(betAmnt, betMoney) {
// alert("Still working out the bugs!")

var imgChange = Math.floor(Math.random()*3+1)

var slot1 = document.getElementById('slot_1')

var slot2 = document.getElementById('slot_2')

var slot3 = document.getElementById('slot_3')

if (betMoney.value <= 0) {
betMoney.value = 0
alert("You lost all your money!")
document.getElementById('restart').style.display='inline'
}

else {

switch(imgChange) {
case 1: document.getElementById('slot_1').src="seven.png";
break;

case 2: document.getElementById('slot_1').src="gold.jpg";
break;

case 3: document.getElementById('slot_1').src="lemon.jpg";
break;
}

imgChange = Math.floor(Math.random()*3+1)

switch(imgChange) {
case 1: document.getElementById('slot_2').src="seven.png";
break;

case 2: document.getElementById('slot_2').src="gold.jpg";
break;

case 3: document.getElementById('slot_2').src="lemon.jpg";
break;
}

imgChange = Math.floor(Math.random()*3+1)

switch(imgChange) {
case 1: document.getElementById('slot_3').src="seven.png";
break;

case 2: document.getElementById('slot_3').src="gold.jpg";
break;

case 3: document.getElementById('slot_3').src="lemon.jpg";
break;
}

if (slot1.src == slot2.src && slot1.src == slot3.src && slot2.src == slot3.src) {
alert("YOU'RE A WINNER!")

if (slot1.src == "lemon.jpg")
alert("Lemon wins!")
betMoney.value = betMoney.value + (betAmnt.value * 2)

if (slot1.src == "gold.jpg")
alert("Gold wins!")
betMoney.value = betMoney.value + (betAmnt.value * 3)

if (slot1.src == "seven.png")
alert("Seven wins!")
betMoney.value = betMoney.value + (betAmnt.value * 5)
}

else {
betMoney.value = betMoney.value - betAmnt.value
}

if (betMoney.value < 0)
betMoney.value = 0
}
}

I've run through my code meticulously several times, finding no errors that I could detect, meaning it's a weakness of JavaScript or something I just don't know of... please help!

vwphillips
10-11-2008, 12:46 PM
easier if you post the relavent HTML

but you could try to make sure your adding numbers not strings


function slotMachine(betAmnt, betMoney) {
// alert("Still working out the bugs!")

var imgChange = Math.floor(Math.random()*3+1)

var slot1 = document.getElementById('slot_1')

var slot2 = document.getElementById('slot_2')

var slot3 = document.getElementById('slot_3')

if (betMoney.value <= 0) {
betMoney.value = 0
alert("You lost all your money!")
document.getElementById('restart').style.display='inline'
}

else {

switch(imgChange) {
case 1: document.getElementById('slot_1').src="seven.png";
break;

case 2: document.getElementById('slot_1').src="gold.jpg";
break;

case 3: document.getElementById('slot_1').src="lemon.jpg";
break;
}

imgChange = Math.floor(Math.random()*3+1)

switch(imgChange) {
case 1: document.getElementById('slot_2').src="seven.png";
break;

case 2: document.getElementById('slot_2').src="gold.jpg";
break;

case 3: document.getElementById('slot_2').src="lemon.jpg";
break;
}

imgChange = Math.floor(Math.random()*3+1)

switch(imgChange) {
case 1: document.getElementById('slot_3').src="seven.png";
break;

case 2: document.getElementById('slot_3').src="gold.jpg";
break;

case 3: document.getElementById('slot_3').src="lemon.jpg";
break;
}

if (slot1.src == slot2.src && slot1.src == slot3.src && slot2.src == slot3.src) {
alert("YOU'RE A WINNER!")

if (slot1.src == "lemon.jpg")
alert("Lemon wins!")
betMoney.value = betMoney.value*1 + (betAmnt.value * 2)

if (slot1.src == "gold.jpg")
alert("Gold wins!")
betMoney.value = betMoney.value*1 + (betAmnt.value * 3)

if (slot1.src == "seven.png")
alert("Seven wins!")
betMoney.value = betMoney.value*1 + (betAmnt.value * 5)
}

else {
betMoney.value = betMoney.value*1 - betAmnt.value*1
}

if (betMoney.value < 0)
betMoney.value = 0
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum