repl.it
@ActionJack/

Chutes and Ladders

JavaScript

Automatically plays a game of Chutes and Ladders with up to 6 players.

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//First we initialize our game board as an array of integers.  Positive numbers are ladders that advance the player that many spaces, and negative numbers are chutes with similar logic.

var board = [
  37, 0, 0, 10, 0, 0, 0, 0, 22, 0, 
  0, 0, 0, 0, 0, 0, -10, 0, 0, 0,
  21, 0, 0, 0, 0, 0, 0, 56, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  16, 0, 0, -20, 0, 0, 0, 0, 0, 0,
  0, -43, 0, -4, 0, 0, 0, 0, 0, 0,
  20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20,
  0, 0, 0, 0, 0, 0, -63, 0, 0, 0,
  0, 0, -20, 0, -20, 0, 0, -19, 0, 0
  ]

//The below number will keep track of whose turn is next.

var nextPlayer= 0;

//We then create an empty array that will later be populated with our players...

var players=[];

//...and another array that will keep track of each player's position on the board.  The game supports six players, but if we have less the end of the array will be ignored.

var positions = [-1,-1,-1,-1,-1,-1];

//Now we have all the variables we need to handle the logic of each turn.

function move(){

//First we grab the name of the active player out of the array so we can effectively report the result of the turn in the console.
  var playerName= players[nextPlayer];
//Then we generate a random number between 1 and 6 to figure out how far the player will move.  
  var move = Math.floor(Math.random()*6) + 1;
//Chutes & Ladders requires the player roll exactly enough to move to the final space, so before moving we check to make sure they will not move past the finish.  If they will, nothing happens and the rest of the function is skipped.  
  if(positions[nextPlayer]+move > 100){
    console.log(playerName + " rolls a " + move  + " but cannot advance past square 100.");
  }
  else{
//If the move is indeed legal, we increment the player's position by the decided amount and report all of this in the console.    
  positions[nextPlayer] += move;
  console.log(playerName + " rolls a " + move  + " and advances to square " + positions[nextPlayer]+ ".")
//Then we check whether they're on a ladder or chute by capturing the number of the space they're standing on.  
  var adjustment = board[positions[nextPlayer]];
//We move the player forward, backward, or nowhere based on that number...
  positions[nextPlayer] += adjustment;
//...then report the news if that number was not zero.  The message is different depending on whether it was a ladder or chute.  
  if(adjustment > 0 ){
    console.log("Hooray! " + playerName + " climbs a ladder, advancing " + adjustment + " squares and finally settling at square " + positions[nextPlayer] +".");
  }
  if(adjustment < 0){
    console.log("Oh no! " + playerName + " trips and falls down a chute, tumbling down " + (-1 * adjustment) + " squares and finally landing in a crumpled heap at square " + positions[nextPlayer] +".")
  }
//If the player is now on square 100, the game is over and we report the winner.  
  if(positions[nextPlayer]===100){
    console.log(playerName + " wins!!!")
  }
}
//If the game is still on, we rotate the active player.  If we are at the end of the list, we make it the first player...
  if(nextPlayer=== players.length-1){
    nextPlayer=0;
  }
//...otherwise we just make it the player to the right.  
  else{
    nextPlayer++;
  }
}

//The below function starts our game by accepting a list of player names.
function chutesAndLadders(playerList){
//First we store that list in the empty array we created before.
  players = playerList;
//Then we pick a random player to move first and announce the result to the console.
  nextPlayer= Math.floor(Math.random()*players.length);
  console.log(players[nextPlayer] + " moves first.")
//Playing a game is just a matter of continuously executing our move function until someone's position reaches square 100.  
  while(positions.indexOf(100)===-1){
    move();
  }
}

chutesAndLadders([ "Adam", "The Challenger"])
Native Browser JavaScript
?