Last active
July 27, 2022 03:04
-
-
Save mcorrigan/086ff9b5f4de18be1a68420aca876612 to your computer and use it in GitHub Desktop.
A Quick Fully Computerized Game of Chutes and Ladders (Since its totally random)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- this was just a rapid development program for fun --> | |
<html> | |
<body> | |
<h1>Chutes and Ladders!</h1> | |
<button onclick="beginGame()">Play Game</button> | |
<img src="https://thumbs.dreamstime.com/z/s-board-games-chutes-ladders-woodbridge-new-jersey-october-circa-game-shown-128701391.jpg" /> | |
<script> | |
// general configs | |
var spin_min = 1, spin_max = 6; | |
var exact_space_win = true; | |
var winner_space = 100; | |
// game play data | |
var players = []; | |
var player_history = []; // multi dim, last value is current place on board | |
var game_over = false; | |
// fixed data | |
var ladders = []; | |
ladders[4] = 14; | |
ladders[12] = 31; | |
ladders[20] = 38; | |
ladders[28] = 84; | |
ladders[36] = 44; | |
ladders[40] = 42; | |
ladders[51] = 67; | |
ladders[80] = 100; | |
ladders[90] = 91; | |
var chutes = []; | |
chutes[16] = 6; | |
chutes[47] = 26; | |
chutes[49] = 11; | |
chutes[56] = 53; | |
chutes[62] = 19; | |
chutes[64] = 60; | |
chutes[87] = 24; | |
chutes[93] = 73; | |
chutes[95] = 75; | |
chutes[98] = 78; | |
// mimic spinning a very fast spinner | |
function spinWheel() { | |
return Math.floor(Math.random() * spin_max) + spin_min; | |
} | |
// player takes their turn | |
function takeTurn(player_index){ | |
var spinAmount = spinWheel(); | |
var currentPlayerHistory = player_history[player_index]; | |
// if no history, add a state for starting | |
if (!currentPlayerHistory){ | |
currentPlayerHistory = []; | |
currentPlayerHistory.push({ | |
'space': 0, | |
'action': 'start' | |
}); | |
} | |
var currentPos = currentPlayerHistory[currentPlayerHistory.length - 1]; | |
var newSpace = currentPos.space + spinAmount; | |
if (exact_space_win && newSpace > winner_space){ | |
currentPlayerHistory.push({ | |
'space': currentPos.space, | |
'action': 'spin wheel (' + spinAmount + ') -- rolled over last space - (' + newSpace + ')' | |
}); | |
player_history[player_index] = currentPlayerHistory; // put it back in | |
return; | |
} | |
currentPlayerHistory.push({ | |
'space': newSpace, | |
'action': 'spin wheel (' + spinAmount + ')' | |
}); | |
if (ladders[newSpace]){ | |
newSpace = ladders[newSpace]; | |
currentPlayerHistory.push({ | |
'space': newSpace, | |
'action': 'ladder up to (' + newSpace + ')' | |
}); | |
}else if (chutes[newSpace]){ | |
newSpace = chutes[newSpace]; | |
currentPlayerHistory.push({ | |
'space': newSpace, | |
'action': 'chute down to (' + newSpace + ')' | |
}); | |
} | |
player_history[player_index] = currentPlayerHistory; // put it back in history | |
if (newSpace >= winner_space){ | |
game_over = true; | |
} | |
} | |
function beginGame(){ | |
// reset game play data | |
players = []; | |
player_history = []; | |
game_over = false; | |
alert('Welcome to (computerized) Chutes and Ladders!'); | |
alert('I am going to play the game, but you tell me who is playing and pick your favorite character.'); | |
var player_count = 0; | |
while(!(player_count > 1)){ | |
player_count = parseInt(prompt('How many players would you like to play?')); | |
if (!(player_count > 1)) | |
alert('Please choose at least 2 or more players.'); | |
} | |
var player_name = ''; | |
var i = 0; | |
for(; i < player_count; i++){ | |
player_name = prompt('Please provide a name for: Player ' + i); | |
if (player_name == ''){ | |
alert('It looks like you are done providing names, I will just fill in the rest.'); | |
break; | |
}else{ | |
players[i] = player_name; | |
} | |
} | |
for(; i < player_count; i++){ | |
players[i] = 'Bob ' + i; | |
} | |
var msg = 'Players: \n ' + players.slice(0, 6).join('\n '); | |
if (players.length > 6){ | |
msg += '\n ...and ' + ( players.length - 6) + ' others'; | |
} | |
alert(msg); | |
alert('Here we go!'); | |
var winner; | |
while(!game_over){ | |
for(var k = 0; k < players.length; k++){ | |
takeTurn(k); | |
if (game_over){ | |
winner = k; | |
break; | |
} | |
} | |
} | |
alert('Game Over - ' + players[winner] + ' won!'); | |
console.table(player_history[winner], ['space', 'action']); | |
var player_selection = '' | |
while(player_selection != 'y' && player_selection != 'n'){ | |
try { | |
player_selection = prompt('Play again? (y/n)').toLowerCase(); | |
} catch (error) { | |
break; | |
} | |
if (player_selection != 'y' && player_selection != 'n') | |
alert('Oops, try that again.') | |
else | |
break; | |
} | |
if (player_selection.toLowerCase() == 'y'){ | |
beginGame(); | |
}else{ | |
alert('Thanks for playing, goodbye!'); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment