I have done some but cannot figure out hot to set the player’s position to the number of the square at the same index in the connected squares array (from
specialSquaresArray to connectedSquaresArray).
I have structured English:
call the function rollDie and store the return value as the player’s score
write out the score
add the score to the player’s position
write out a message saying what square the player is on
call the function findIndexOf with suitable arguments to look for the index
of the player’s position in the special squares array and store the value
if the index is not -1
set the player’s position to the number of the square at the same index
in the connected squares array
write out a message saying what square the ladder has taken the player
write out a line break
But I just cannot figure out hot to access those arrays.
Any help appreciated.
Snakes and Ladders
* M150 TMA03 2010J Q4.
* Program to simulate a one-player version of 'Snakes and Ladders'
//PROVIDED FUNCTIONS -- you do not need to change these
*imitates a normal 6-sided die, by returning a random whole number between 1 and 6 inclusive.
return Math.floor(Math.random() * 6) + 1;
*searches for a number in a number array.
*function takes two arguments
* the number to search for
* the number array to search
*function returns the array index at which the number was found, or -1 if not found.
function findIndexOf(number, numberArray)
var indexWhereFound = -1;
for (var position = 0; position < numberArray.length; position = position + 1)
if (numberArray[position] == number)
indexWhereFound = position;
//ARRAYS that represent the board -- you do not need to change these
//array of special squares
var specialSquaresArray = [1,7,25,32,39,46,65,68,71,77];
//array of corresponding squares the player ascends or descends to
var connectedSquaresArray = [20,9,29,13,51,41,79,73,35,58];
//VARIABLES used -- you do not need to change these
//the square the player is currently on
//play is initially at START
playersPosition = 0;
//what they score when they roll the die
//the index of the player's position in the special squares array or -1
while (playersPosition < planszaArray)
playersScore = rollDie();
document.write('Score : ' + playersScore);
playersPosition = playersPosition + playersScore;
document.write(' Square : ' + playersPosition + '<BR>');
indexOfNumber = findIndexOf(playersPosition,specialSquaresArray);
if (indexOfNumber != -1)
-------------------It must go here-------------------------
//TODO add code here for parts (iii), (iv)(b), (v), and (vi)