ผลต่างระหว่างรุ่นของ "01219245/javascript1/tutorial3/2"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 99: แถว 99:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
We shall use jQuery method '''<tt>append</tt>''' to add more items to the list.  A list item should be an LI element, so we have to enclose our message with "<li></li>".
+
We shall use jQuery method '''<tt>append</tt>''' to add more items to the list.  A list item should be an LI element, so we have to enclose our message with <nowiki><li></li></nowiki>.
  
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">

รุ่นแก้ไขเมื่อ 17:34, 27 มกราคม 2557

Your current JavaScript program in the script block probably look like this:

var numSticks = 21;

function pickSticks() {
    var num = parseInt( $( "#pickNum" ).val() );
    numSticks -= num;
    $( "#numSticks" ).text( numSticks );
}

$(function(){
    $("#pickButton").click(pickSticks);
});

Valid number of sticks

Let's add the code that check that the number of sticks the player want to take is valid, i.e., it is between 1 to 3 and is no larger than the current number of sticks. Let's assume that the input is an integer for now (i.e., that the user won't enter 1.5). If the amount is invalid, let's throw an alert for now.

function pickSticks() {
    var num = parseInt( $( "#pickNum" ).val() );
    if ( ( num >= 1 ) && ( num <= 3 ) && ( num <= numSticks ) ) {
        numSticks -= num;
        $( "#numSticks" ).text( numSticks );
    } else {
        alert( "Invalid amount.  Please try again" );
    }
}
Gitmark.png Try to see if the condition works. If it is working, commit your work.

The code is getting harder to read, so let's try to clean it up a bit. To make it clearer, we shall extract the code for validating conditions to a new function. Function pickStick and the new function is shown below:

function isValidPickNum( num, numSticks ) {
    return ( num >= 1 ) && ( num <= 3 ) && ( num <= numSticks );
}

function pickSticks() {
    var num = parseInt( $( "#pickNum" ).val() );
    if ( isValidPickNum( num, numSticks ) ) {
        numSticks -= num;
        $( "#numSticks" ).text( numSticks );
    } else {
        alert( "Invalid amount.  Please try again" );
    }
}
Gitmark.png Let's commit our work.

A randomized computer player

It is time to introduce the computer player. At this point we just want something that plays, so let's write a simple player that only random a number between 1 to 3. We shall write that as a function and call it in pickSticks

EXERCISE Write function computerPickAmount.

function computerPickAmount() {
    // EXERCISE: write this function
}

function pickSticks() {
    var num = parseInt( $( "#pickNum" ).val() );
    if ( isValidPickNum( num, numSticks ) ) {
        numSticks -= num;
        $( "#numSticks" ).text( numSticks );

        numSticks -= computerPickAmount();
        $( "#numSticks" ).text( numSticks );
    } else {
        alert( "Invalid amount.  Please try again" );
    }
}

Click here to see the solution when you are done.

function computerPickAmount() {
    return 1 + Math.floor( Math.random() * 3 );
}

Try it to see if the number of sticks decreases more than the number you enter. While this should indicate that computerPickAmount does something. How can we be sure that function computerPickAmount works fine? So let's add a game transcript so that you can see how the game proceeds and also to help us test the code.

Gitmark.png Before we add the transcript, commit the current work.

A transcript

Let's add another HTML element to keep the transcript. Let's use a UL element, so that we have a list. (Add this before the first script block.)

  <ul id="gameTranscript"></ul>

We shall use jQuery method append to add more items to the list. A list item should be an LI element, so we have to enclose our message with <li></li>.

function pickSticks() {
    var num = parseInt( $( "#pickNum" ).val() );
    if ( isValidPickNum( num, numSticks ) ) {
        numSticks -= num;
        $( "#numSticks" ).text( numSticks );
        $( "#gameTranscript" ).append( "<li>You pick " + num + "</li>" );

        computerNum = computerPickAmount();
        numSticks -= computerNum;
        $( "#gameTranscript" ).append( "<li>Computer picks " + computerNum + "</li>" );
        $( "#numSticks" ).text( numSticks );
    } else {
        alert( "Invalid amount.  Please try again" );
    }
}

Using objects to encapsulate behavior

Computer player