Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Slot Machine 2

Status
Not open for further replies.

Tarwn

Programmer
Joined
Mar 20, 2001
Messages
5,787
Location
US
For those of us that don't have hours trying to solve the large puzzle of mwolfs ;)
Code:
<HTML>
<head>
<style>
select{
	width:65px;
}
option{
	text-align: center;
}
.slots{
	padding: 20px;
	background-color: #AAAACC;
	border: 2px solid #8888AA;
	width:240px;
}
.message{
	padding:20px;
	width:240px;
	background-color: #AAAACC;
	border: 2px solid #8888AA;
	text-align: center;
	color: purple;
	font-size: 20px;
}
.arm{
	width:35px;
	height: 100px;
	color: #003399;
	background-color: #9999BB;
	border: 2px solid #777799;
}
</style>
<script>
var tokens = 25;
var wheels, wheels_cur, wheels_tar;
var choices = [&quot;cherry&quot;,&quot;banana&quot;,&quot;apple&quot;,&quot;cherry&quot;,&quot;melon&quot;,&quot;cherry&quot;,&quot;banana&quot;,&quot;7&quot;,&quot;banana&quot;,&quot;melon&quot;,&quot;apple&quot;,&quot;777&quot;,&quot;[BAR]&quot;,&quot;apple&quot;,&quot;melon&quot;];

function MakeEm(){
	var i, slots, a_wheel;
	slots = document.getElementById(&quot;slot_machine&quot;);
	slots.innerHTML = &quot;&quot;;
	a_wheel = &quot;<select name='wheel' size='5'></select>&quot;;
	for(i = 0;i<3;i++){
		slots.innerHTML += a_wheel;
	}
	wheels = document.getElementsByName(&quot;wheel&quot;);
	wheels_cur = new Array(wheels.length);
	wheels_tar = new Array(wheels.length);
	var x;
	for(x=0;x<wheels.length;x++){
		wheels_cur[x] = 0;
	}
	GraphEm();
	frmChoice.txtTokens.value=&quot;25&quot;;
}

function SpinEm(){
	var x, y;
	y = 0;
	for(x=0;x<wheels.length;x++){
		y = Math.floor((Math.random() * choices.length) + y + 2 * choices.length);
		wheels_tar[x] = (wheels_cur[x] + y);
	}
	setTimeout(&quot;NudgeEm();&quot;,50);
	document.getElementById(&quot;slot_arm&quot;).disabled = true;
}

function NudgeEm(){
	var x;
	var done = true;
	for(x=0;x<wheels.length;x++){
		if(wheels_tar[x] != wheels_cur[x]){
			wheels_cur[x] = (wheels_cur[x] + 1);
			done = false;
		}
	}
	GraphEm();
	for(x=0;x<wheels.length;x++){
		if(wheels_tar[x] == wheels_cur[x]){
			wheels[x].selectedIndex=2;
		}
	}
	if(done){
		PayEm();
	}
	else{
		setTimeout(&quot;NudgeEm();&quot;,75);
	}
}

function GraphEm(){
	var x;
	var y;
	var opt;
	for(x = 0;x < wheels.length;x++){
		CleanEm(wheels[x]);
		for(y = 0;y < 5;y++){
			opt = ((choices.length - 1) + wheels_cur[x] - 1 + y) % choices.length;
			wheels[x].options[y] = new Option(choices[opt],choices[opt]);
		}
	}
}

function CleanEm(elem){
	var i;
	for(i=elem.options.length-1;i>=0;i--){
		elem.options[i] = null;
	}
}

function PayEm(){
	//center row
	// 1 cherry - 1 tokens
	// 2 cherry - 5 tokens
	// 2 fruit + 1 cherry  - 25 tokens
	// 3 fruit  - 25 tokens
	// 3 cherry - 25 tokens
	// 2 7's    - 35 tokens
	// 2 777's  - 35 tokens
	// 2 7's, 1 777 - 40 tokens
	// 1 7, 2 777's - 50 tokens
	// 3 7's   - 125 tokens
	// 3 777's - 250 tokens
	// 3 bars - 500 tokens
	var x;
	var cherries, apples, melons, bananas, sevens, triples, bars;
	cherries = 0;
	apples = 0;
	melons = 0;
	bananas = 0;
	sevens = 0;
	triples = 0;
	bars = 0;
	for(x=0;x<wheels.length;x++){
		switch(wheels[x].options[2].value){
			case &quot;cherry&quot;:
				cherries++;
				break;
			case &quot;apple&quot;:
				apples++;
				break;
			case &quot;melon&quot;:
				melons++;
				break;
			case &quot;banana&quot;:
				bananas++;
				break;
			case &quot;7&quot;:
				sevens++;
				break;
			case &quot;777&quot;:
				triples++;
				break;
			case &quot;[bar]&quot;:
				bars++;
				break;
		}	
	}
	if(bars == 3){
		FlashEm(500);
	}
	else if(triples == 3){
		FlashEm(250);
	}
	else if(sevens == 3){
		FlashEm(125);
	}
	else if((triples == 2)&&(sevens==1)){
		FlashEm(50);
	}
	else if((triples == 1)&&(sevens==2)){
		FlashEm(40);
	}
	else if(cherries==3){
		FlashEm(25);
	}
	else if((apples == 3)||(melons==3)||(bananas==3)){
		FlashEm(25);
	}
	else if(cherries==2){
		FlashEm(15);
	}
	else if(((apples == 2)||(melons==2)||(bananas==2))&&(cherries==1)){
			FlashEm(25);
	}
	else if((triples == 2)||(sevens==2)){
		FlashEm(35);
	}
	else if(cherries==1){
		FlashEm(1);
	}
		document.getElementById(&quot;slot_arm&quot;).disabled = false;
}

function FlashEm(num){
	var msg = document.getElementById(&quot;message&quot;);
	if(msg.style.borderColor != &quot;red&quot;){
		msg.innerText = &quot;Winner!&quot;;
		msg.style.borderStyle = &quot;dotted&quot;;
		msg.style.borderColor = &quot;red&quot;;
	}
	else{
		msg.innerText = &quot;Winner!&quot;;
		msg.style.borderStyle = &quot;dashed&quot;;
		msg.style.borderColor = &quot;yellow&quot;;
	}
	if(num == 0){
		msg.innerText = &quot;Tarwn's Slot Machine!&quot;;
		msg.style.borderStyle = &quot;solid&quot;;
		msg.style.borderColor = &quot;#AAAACC&quot;;
	}
	else{
		frmChoice.txtTokens.value = parseInt(frmChoice.txtTokens.value) + 1;
		setTimeout(&quot;FlashEm(&quot; + (num - 1) + &quot;);&quot;,100);
	}
}

function ChargeEm(){
	var amt = parseInt(frmChoice.txtTokens.value);
	if(amt == 0){
		alert(&quot;You seem to be out of cash, better luck next time.&quot;)
	}
	else{
		amt = amt - 1;
		frmChoice.txtTokens.value = amt;
	}

	SpinEm();
}
</script>
</head>
<body onLoad=&quot;MakeEm();&quot;>
<span class=&quot;message&quot; id=&quot;message&quot;>Tarwn's Slot Machine</span><br>
<span id=&quot;slot_machine&quot; class=&quot;slots&quot;>
</span>
<input type=&quot;button&quot; onClick=&quot;ChargeEm();&quot; value=&quot;Spin&quot; class=&quot;arm&quot; id=&quot;slot_arm&quot;>
<form name=&quot;frmChoice&quot;>
Tokens: <input type=&quot;text&quot; name=&quot;txtTokens&quot; value=&quot;0&quot; readonly>
</form>
<table style=&quot;font:verdana;border: 1px solid #aaaacc;&quot;><tr><th>Payoffs</th></tr>
<tr><td>1 cherry</td><td>1 tokens</td></tr>
<tr><td>2 cherry</td><td>5 tokens</td></tr>
<tr><td>2 fruit + 1 cherry </td><td>25 tokens</td></tr>
<tr><td>3 fruit </td><td>25 tokens</td></tr>
<tr><td>3 cherry</td><td>25 tokens</td></tr>
<tr><td>2 7's   </td><td>35 tokens</td></tr>
<tr><td>2 777's </td><td>35 tokens</td></tr>
<tr><td>2 7's, 1 777</td><td>40 tokens</td></tr>
<tr><td>1 7, 2 777's</td><td>50 tokens</td></tr>
<tr><td>3 7's  </td><td>125 tokens</td></tr>
<tr><td>3 777's</td><td>250 tokens</td></tr>
<tr><td>3 bars</td><td>500 tokens</td></tr></table>
</body>
</html>

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
very cool...seems to pay out a lot in the 25 - 35 range

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Yeah, I didn't really spend a long time balancing the payouts, just kind of winged those :P

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top