Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need some help with functions (homework)

    Hello all,
    I’ve got a homework to do and I'm stuck, as you can see i have done all the function's (i think they are ok but not shure) with a main program I'm stuck don’t know even where to begin :/ could anyone please please help a bit?

    best regards


    <HTML>
    <HEAD>
    <TITLE>
    The game of 21
    </TITLE>

    <SCRIPT LANGUAGE = "JavaScript">

    //FUNCTIONS
    /*
    *removes a given number of sticks
    *
    *function takes one argument numberToRemove that represents the number of sticks to be removed
    * if numberToRemove is more than numberOfSticks, reduces numberOfSticks to 0
    * otherwise reduces numberOfSticks by numberToRemove
    *function returns no value
    */
    function removeSticks(numberToRemove)
    {
    if (numberOfSticks< numberToRemove)
    {
    numberOfSticks =0
    }
    else
    {
    (numberOfSticks = numberOfSticks - numberToRemove);
    }

    }

    /*
    *switches current player
    *
    *function takes no argument
    * if current player is 1, sets current player to 2
    * otherwise sets current player to 1
    *function returns no value
    */

    function switchCurrentPlayer()
    {
    if (currentPlayer ==1)
    {
    return currentPlayer = 2;
    }
    {
    return currentPlayer = 1;
    }
    }

    /*
    *gets a number from a prompt dialogue
    *
    *function takes no argument
    * prompts the current player for a number of sticks to remove, with a suitable message
    *function returns the number the player enters
    */

    function getChosenNumber()
    {
    ChosenNumber = window.prompt('Player ' + currentPlayer + ': Please enter either 1, 2 OR 3.');
    return ChosenNumber;
    }


    //VARIABLES -- do not change this section
    var numberOfSticks = 21;//number of matchsticks, initially 21
    var currentPlayer = 1;//player whose turn it is, Player 1 goes first
    var numberChosen;//variable used to hold the number the current player chooses

    //MAIN PROGRAM
    The players take turns to choose how many matches to remove, as long as there are still matches left. After each go the program outputs the number of matches that still remain and if this number is zero a message is displayed to say the current player is the winner. The turn then passes to the other player.


    </SCRIPT>

    </HEAD>
    <BODY>
    </BODY>
    </HTML>
    Last edited by sparhavk; 06-29-2010 at 06:05 AM.

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    OK, so we don't like doing homework for others, but let me try to get you started. You need a way for the users to control the flow of the program.

    Start by creating some element that someone wants to click, like <button>Click!</button>.

    Then you need to attach an event handler to the button that fires when the "click" event happens. You can do this in 3 ways. I will give you the most basic way, called DOM Level 0. It is the least desirable way to do it, but I will leave it to you to research DOM Level 1 and DOM Level 2 so you can learn at your own pace.

    <button onclick="getChosenNumber();">Click!</button>

    Hopefully, this breaks your mental block and gets you rolling. Have fun!

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,132
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by Beagle View Post
    I will give you the most basic way, called DOM Level 0. It is the least desirable way to do it, but I will leave it to you to research DOM Level 1 and DOM Level 2 so you can learn at your own pace.
    there is no DOM Level 1 Event Handling. It is only addressed in levels 0, 2 & 3.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanx mate, how to display a prompt window isn't the problem, my problem is ho to put the variable var numberChosen witch we can get from window prompt defined by function getChosenNumber(), but how to get the numberChosen into
    function removeSticks(numberToRemove). But still thanks I will look into DOM

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,132
    Thanks
    12
    Thanked 332 Times in 328 Posts
    my problem with this homework is the setup. personally, I’d code it as an object. that keeps the sticks, the players and the actions neatly together. but the setup requires you to use global variables (something you usually try to avoid as much as possible).


    a possible setup I’d go for:
    Code:
    function Game()
    {
        this.player1 = true;
        var sticks = 21; // private (= inaccessible from outside (no cheating))
        this.remove = function (x) { /* … */ ) }; // mutator for sticks
        this.sticks = function () { /* … */ ) }; // accessor for sticks
    }
    
    Game.prototype.choose = function () { /* … */ ) }; // getChosenNumber()
    
    Game.prototype.toggle = function () { /* … */ ) }; // switchCurrentPlayer()
    
    Game.prototype.win = function () { /* … */ ) };
    Last edited by Dormilich; 06-29-2010 at 07:20 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Don't ask me I'm just trying to get it sorted, but it took almost 3 hours and i have only managed to add some pieces and they even work's

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,132
    Thanks
    12
    Thanked 332 Times in 328 Posts
    I guess you need a main function, that controls the other functions.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    as a main program i have got something like
    getChosenNumber();
    removeSticks(numberChosen);
    if (numberOfSticks = 0)
    {
    document.write ('Player ' + currentPlayer + ' wins')
    }
    else
    {
    document.write ('Number of sticks left ' + numberOfSticks)
    getChosenNumber;
    }

    but unfortunatly I'm ending up with Number of sticks 0

    :/

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,132
    Thanks
    12
    Thanked 332 Times in 328 Posts
    you mustn’t use document.write(). that will wipe your page. rather set the value of a text field.

    some general idea for the main function (this might be implemented recursively or in a loop)
    Code:
    if/while (sticks available) {
        choose sticks
        test for winning
    }
    you lost
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #10
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    the problem is that i have to use it
    "After each go the program outputs the number of matches that still remain and if this number is zero a message is displayed to say the current player is the winner."

    It's almost working but the loop doesn't work any ideas??

    getChosenNumber()
    if (numberOfSticks != 0)
    {
    removeSticks(numberChosen);
    document.write ('Number of sticks left ' + numberOfSticks + '<BR>')
    switchCurrentPlayer()
    }
    else
    {
    document.write ('The winner is Player '+currentPlayer )
    }

  • #11
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Right, my mistake, DOM level 1 doesn't change how event handling works. Thanks for the clarification.

    You're using global variables. We're not going to get into trying to teach OOP right now when you need to get your basic programming completed in a way that makes sense to you, and it seems that procedural + global variables is a way that makes sense to you.

    So, plan your program on paper. How does it work?

    1) What is your initial state?
    2) What is the first input that occurs?
    3) What happens after that first input?

    There are hundreds of ways to do what you want to do. But they all start with understanding what you're actually going to do from a logical standpoint.

    Sounds to me like you've got a classic game loop.

    Code:
    while (winConditionNotMet) {
      display Info
      take input
      process input
      display result
    }
    That's pseudo-code. Display info can be alert() whatever message you want.

    The win condition is numberOfMatches <= 0, so winConditionNotMet should be numberOfMatches > 0

    And then get user input is call that function.

    A process user input is simply a subtraction: numberofSticks = numberOfSticks - chosenNumberOfSticks;

    Take a crack and paste your code and we'll review.

  • #12
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ok thats what ive come to :/ o know it's crap but thats how they teach us to do ... it does most of the things i want but loop doesnt work

    <HTML>
    <HEAD>
    <TITLE>
    The game of 21
    </TITLE>

    <SCRIPT LANGUAGE = "JavaScript">

    //FUNCTIONS
    /*
    *removes a given number of sticks
    *
    *function takes one argument numberToRemove that represents the number of sticks to be removed
    * if numberToRemove is more than numberOfSticks, reduces numberOfSticks to 0
    * otherwise reduces numberOfSticks by numberToRemove
    *function returns no value
    */
    function removeSticks(numberToRemove)
    {
    if (numberOfSticks< numberToRemove)
    {
    numberOfSticks = 0
    }
    else
    {
    (numberOfSticks = numberOfSticks - numberToRemove);
    }

    }

    /*
    *switches current player
    *
    *function takes no argument
    * if current player is 1, sets current player to 2
    * otherwise sets current player to 1
    *function returns no value
    */

    function switchCurrentPlayer()
    {
    if (currentPlayer ==1)
    {
    return currentPlayer = 2;
    }
    {
    return currentPlayer = 1;
    }
    }

    /*
    *gets a number from a prompt dialogue
    *
    *function takes no argument
    * prompts the current player for a number of sticks to remove, with a suitable message
    *function returns the number the player enters
    */

    function getChosenNumber()
    {
    numberChosen = window.prompt('Player ' + currentPlayer + ': Please enter either 1, 2 OR 3.');
    return numberChosen;
    }



    //VARIABLES -- do not change this section
    var numberOfSticks = 21;//number of matchsticks, initially 21
    var currentPlayer = 1;//player whose turn it is, Player 1 goes first
    var numberChosen;//variable used to hold the number the current player chooses

    //MAIN PROGRAM
    //Part (iii)(b)
    //Complete the main program following the description given in the question and the structured
    //English you have written in (iii)(a).
    if (numberOfSticks == 0)
    {
    document.write ('The winner is Player '+currentPlayer )
    }
    else
    {
    getChosenNumber()
    removeSticks(numberChosen)
    document.write ('Number of sticks left ' + numberOfSticks + '<BR>')
    switchCurrentPlayer();
    }


    </SCRIPT>

    </HEAD>
    <BODY>
    </BODY>
    </HTML>

  • #13
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    You don't have a loop. You need a while loop. And don't use document.write. Use alert(). Even if you eventually change it back. Don't use document.write for now, it will trip you up while you're trying to solve this problem.

    while (condition) {
    do stuff
    }

    follow my pseudo-code in my previous post.

  • #14
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ok changed it into

    while ( numberOfSticks== 0)
    {
    getChosenNumber()
    removeSticks(numberChosen)
    alert ('Number of sticks left ' + numberOfSticks + '<BR>')
    switchCurrentPlayer();
    }
    else
    {
    alert ('The winner is Player '+currentPlayer )
    }

    but now program doesn't display prompt window

  • #15
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Right, that's not the correct syntax for a while loop. There's no "else" block in a while loop.

    Also, a while loop only works if the condition is met. Since you start with numbeOfSticks at some number other than 0, the while loop never starts.

    Try numberOfSticks > 0.

    Also, use the return value of getChosenNumberOfSticks. Don't just call it, use the return value in your loop.

    To display success, just put your win code after the loop. The loop ends when the win condition is met.

  • Users who have thanked Beagle for this post:

    sparhavk (06-29-2010)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •