/* Monty Hall Paradox
 * ------------------
 * Copyright (C) Juhana Leinonen
 * Creative Commons Attribution 3.0 (http://creativecommons.org/licenses/by/3.0/)
 * 
 * An Undum example. 
 */

undum.game.id = "ff1d80ca-da49-4b5c-a9ac-7c859a661d1c";
undum.game.version = "1.0";

// Because we're using variables we can't use SimpleSituation.
// The variables would be shown in their initial state (undefined), not the
// concurrent state.
undum.game.situations.start = new undum.Situation( {
    enter: function( character, system, from ) {
        system.write( "<p>\"Welcome to <em>Let's Make a Deal</em>! I'm your host "
            + "Monty Hall and our contestant " + character.qualities.name + " is ready "
            + "to pick a prize from behind one of these three doors.\"</p>"
            + "<p>\"Remember that behind two of these doors is a "
            + "<strong>goat</strong> and behind one there is a "
            + "<strong>car</strong>. Only I know which door has the grand prize!\"</p>"
            + "<p>\"So, " + character.qualities.name + ", which door will you choose?\"</p>"
            + "<ul class='options'>"
            + "<li>I've got a good feeling about <a href='./door1'>door number one</a>!</li>"
            + "<li>The car must be behind <a href='./door2'>door number two</a>!</li>"
            + "<li><a href='./door3'>Door number three</a> is most likely the right one!</li>"
            + "</ul>"
        );
    },
    act: function( character, system, action ) {
        // The action variable contains the name of the action (door1, door2 or door3).
        // We'll store that information to a variable and move on to the next situation.
        // The .charAt() method picks one character out of a string. Here we 
        // pick the number from string "doorX" which is character number 4
        // (counting starts from zero).
        undum.game.vars.chosenDoor = action.charAt( 4 );
        system.doLink( 'switch-choice' );
    }
} );

// The situation's name contains a dash so we have to use a different syntax
// to refer to it.
undum.game.situations[ "switch-choice" ] = new undum.Situation( {
    enter: function( character, system, from ) {
        // Have the game host choose a door to open. The game host should
        // never pick the door with the car.
        // We'll do so by choosing a door at random, then check if it's the
        // door chosen by the contestant or the door that has the car.
        // If so, we pick another door and try again.
        // The while() loop continues as long as the condition given to it
        // is true.
        // Note that this technique should only be used if we are certain that
        // there is a free door that can be selected, otherwise we'll end up
        // with an infinite loop.
        while( undum.game.vars.openDoor == 0 ) {
            var randomDoor = system.rnd.randomInt( 1, 3 );
            
            if( randomDoor != undum.game.vars.chosenDoor && randomDoor != undum.game.vars.carDoor ) {
                undum.game.vars.openDoor = randomDoor;
            }
        }
        
        // Now for some maths: We can find out which door is left 
        // (not chosen by the contestant or the host) by deducting
        // the contestant's and the host's choices from 6 (1+2+3):
        var doorChoice = 6 - undum.game.vars.chosenDoor - undum.game.vars.openDoor;
            
        system.write( "<p>\"Excellent choice! Our contestant has selected "
            + "door number " + undum.game.vars.chosenDoor + "\".</p>"
            + "<p>\"Now, let's open door number " + undum.game.vars.openDoor
            + "... You can see that there is a <strong>goat</strong> behind "
            + "door number " + undum.game.vars.openDoor + ".\"</p>" 
            + "<p>\"" + character.qualities.name + ", would you like to change "
            + "your choice to door number " + doorChoice + ", or are you happy " 
            + "with your choice of door number " + undum.game.vars.chosenDoor
            + "?\"</p>"
            + "<ul class='options'>"
            + "<li>I'll stick with <a href='./door" + undum.game.vars.chosenDoor 
            + "'>door number " + undum.game.vars.chosenDoor + "</a>.</li>"
            + "<li>Now that you mention it, <a href='./door" + doorChoice
            + "'>door number " + doorChoice + "</a> looks much better!</li>"
            + "</ul>" );
    },
    act: function( character, system, action ) {
        // Again, determine which number we chose
        undum.game.vars.finalDoor = action.charAt( 4 );
        
        // Print different text depending on whether the contestant
        // changed their mind. chosenDoor still contains the
        // original choice.
        if( undum.game.vars.finalDoor == undum.game.vars.chosenDoor ) {
            system.write( "<p>\"Don't change horses in midstream, right? "
                + "Let's see what Lady Luck has in store for you.\"</p>" );
        }
        else {
            system.write( "<p>\"They say switching <em>is</em> " 
        		+ "statistically the wiser choice... Let's see how it works " 
        		+ "for you.\"</p>" );
        }
        
        system.write( "<p>Door number " + undum.game.vars.finalDoor
            + " reveals...</p>" );
        
        // Depending on what we won, go to the correct ending.
        // Since there's only one short message to print we could do it here,
        // but this way we get rid of the option block as well.
        if( undum.game.vars.finalDoor == undum.game.vars.carDoor ) {
            system.setCharacterText( "<p>A motorist</p>" );
            system.doLink( 'car' );
        }
        else {
            system.setCharacterText( "<p>A goat owner</p>" );
            system.doLink( 'goat' );
        }
    }
} );


undum.game.situations.car = new undum.SimpleSituation( "<h1>A brand new car!</h1>" );
undum.game.situations.goat = new undum.SimpleSituation( "<h1>A beautiful goat!</h1>" );


undum.game.init = function( character, system ) {
    // ask the contestant's name and store it in a variable
    var name = prompt( "What's your name?" );
    
    // if no name was given change the variable to a default name
    if( !name ) {
        character.qualities.name = 'Anonymous';
    }
    else {
        character.qualities.name = name;
    }
    
    // initialize variables
    undum.game.vars = {
        // which door the contestant has selected
        chosenDoor: 0,
        
        // the door that the game host has opened
        openDoor: 0,
        
        // the contestant's final choice
        finalDoor: 0,
        
        // The door that has the car behind it.
        // We know that every other door has a goat behind it
        // so we don't have to store that information separately.
        carDoor: system.rnd.randomInt( 1, 3 )
    };
    
    // set the character description
    system.setCharacterText( "<p>A game show participant</p>" );
    
    // To show off a bit we'll change the word "Character" in the
    // layout to the contestant's name using jQuery.
    $( '#character_panel > h1' ).html( character.qualities.name );
};
