Function Challenge

Your challenge is to modify the Nim program shown below so that the GetStartingValue function makes use of the ValidateData (Minimum, Maximum) function. The suggested range of data, that is minimum and maximum, should be 5 and 30 respectively.

Note: If you copy and paste the code from here into a text editor make sure that the quotation marks are correct.

<html>
<head>

<script>

// Data
var Total = 0 ; // The integer starting value entered by the player
var Number = 0 ; // The number subtracted from the total by the player
var PlayerID = 'X' ;

// Initialise and declare GameOver to be false
var GameOver = false ;

// Module Reference 1
function GetStartingValue ()
{ // begin GetStartingValue
var choice = 'N' ;

choice = prompt ('Should the computer choose a random starting value between 1 and 30?')

if (choice.toUpperCase() == 'Y')
{ // begin if

Total = Math.floor((Math.random() * 30) + 1) ;

} // end if
else
{ // begin else

// Get the starting value from the player
Total = prompt ('Enter a starting value: ') ;

} // end else

alert ('The starting value = ' + Total) ;

} // End GetStartingValue

function PlayGame ()
{ // beging PlayGame

// Loop until one of the players wins
while (GameOver == false)
{ // begin while

PlayerID = 'Computer' ;

ComputersTurn () ;

CheckForWin (PlayerID) ;

// Player's turn
if (GameOver == false)
{ // begin if

PlayerID = 'Human' ;

PlayersTurn () ;

CheckForWin (PlayerID) ;

} // end if

} // end while - The while will terminate when GameOver is equal to true

} // end PlayGame

// Module Reference 2.1
function ComputersTurn ()
{ // begin ComputersTurn

// Computers's turn
// Integer division to check for best move
if ((Total % 3) == 2)
{ // begin if
Total = Total - 2 ;
alert ('Computer subtracted 2 from total. Total is now ' + Total) ;
} // end if
else
{  // begin else
Total = Total - 1 ;
alert ('Computer subtracted 1 from total. Total is now ' + Total) ;
} // end else

} // end ComputersTurn

// Module Reference 2.2
function CheckForWin (PlayerID)
{ // begin CheckForWin
// Declare and initialise local variables for use with ValidateData function
var Minimum = 0 ;
var Maximum = 0 ;

// Check for winning result
if (Total == 0)
{ // begin if
alert ('The ' + PlayerID + ' wins!') ;
GameOver = true ;
} // end if

} // end CheckForWin

// Module Reference 2.3
function PlayersTurn ()
{ // begin PlayersTurn

Number = prompt ('The total is : ' + Total + '\n' + 'How many do you want to subtract?') ;

// Set range for validation
Minimum = 1 ;
Maximum = 2 ;
ValidateData (Minimum, Maximum) ;

Total = Total - Number ; // Take players valid data input away from Total

} // end PlayersTurn

// Module Reference 2.3.1
function ValidateData (Minimum, Maximum)
{ // begin ValidateData

// Validate data entry to allow only a 1 or 2 to be entered
while (Number < Minimum || Number > Maximum)
{  // begin while
Number = prompt ('You can only enter ' + Minimum + ' OR ' + Maximum + '. Try again') ;
} // end while

} // end ValidateData

// Main program
GetStartingValue () ;

PlayGame () ;

alert ('Thanks for playing') ;

</script>
</head>
<body>
</body>
</html>

Next: Test the Code