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.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Very Simple Word Game Code Executes As Text :(

    I am so sorry for yet another nooby thread, but I am just starting out and having a few teething problems.

    I have tried a few things with the following, but nothing seems to work. I have tried <script> tags, and different browsers. A friend told me it executes fine.

    Here is the code. Please don't judge. This is my first ever self-wrote code. I know it sounds crap (especially towards the end when I just wanted to put any text in to check the multiple if statement). Anyhow, here is my code:

    Code:
    <script>
    	var age = prompt("How old are you?");
    
    if( age < 18)
    
    	{
    	alert("You are to young to play this game!");
    	}
    
    		else
    
    	{
    	alert("Enjoy the game");
    
    .
    	}
    
    	alert("You're walking down the street and come to a fork. To the right you see beautiful streams,\
    	\
    	and rolling countryside. To the left, dead trees, rain, sleet, and misery. Which way would you like\
    	\
    	to go?");
    
    var choiceOne = prompt("left (press 1) or right (press 2)?");
    
    	if(choiceOne == "1")
       
    {
    
    		alert("As you begin your path the rain pelts hard against your face. You are beginning to regret your decision, but\
    	
    		still feel you made the right choice. You hear a cackling laugh coming from behind a nearby tree.");
    
    var choiceTwo = promt("Investigate the laugh (press 1) or ignore (press 2)?")
    
    		if(choiceTwo = "1")
    
    	{
    	alert("out jumps a monster and eats you"); //fix this Mikey FFS!
    	}
    
    		else
    
    		{
    		alert("you win");
    		}
     	
    
    	else
    
    	{
    
    	alert("Feeling the warm sun on your face you allow yourself a wry smile. The bird song in your ears and the scent of flowers in your nose.");
    	}
    
    
    </script>

  • #2
    Regular Coder
    Join Date
    Jun 2010
    Posts
    121
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Do you mean it actually displays all of the statements as html in the page?

    Maybe you have javascript turned off? where are you putting this script?

    Let us see your whole HTML code

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Firstly, using alerts for this is ridiculous, even I have to admit. Better off using form elements and DOM for altering text, etc.

    Secondly, you can't spread an alert across more than one line, AFAIK. If you need, declare a variable, and do:
    thisVar = "blah blah blah";
    thisVar += "more blah more blah";
    thisVar += "EVEN MORE";
    alert(thisVar);

    I rarely use prompt, and I don't think keyboard 1 or 2 will affect anything.. just clicks.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #4
    Regular Coder
    Join Date
    Jun 2010
    Posts
    121
    Thanks
    0
    Thanked 3 Times in 3 Posts

    how to get an array position inside another array

    sorry
    Last edited by pdiddles03; 05-06-2013 at 09:53 PM. Reason: meant to start thread

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,348
    Thanks
    23
    Thanked 618 Times in 617 Posts
    @Effexion, you don't need to aplogize for starting to learn code. We've all been there and stop saying
    Please don't judge.
    We all do, but don't always say anything. LOL

    WolfShade is correct coding with alerts is a bad habit. We do use them in trouble shooting errors though.

    You have left something out like brackets and semi-colons. Here's a working code:
    Code:
    <script>
    var age = prompt("How old are you?");
    if( age < 18){
    	alert("You are to young to play this game!");
    }else{
    	alert("Enjoy the game");
    }
    
    alert("You're walking down the street and come to a fork. To the right you see beautiful streams,\
    and rolling countryside. To the left, dead trees, rain, sleet, and misery. Which way would you like\
    to go?");
    
    var choiceOne = prompt("left (press 1) or right (press 2)?");
    if(choiceOne == "1"){
    	alert("As you begin your path the rain pelts hard against your face. You are beginning to regret your decision,\
    	but still feel you made the right choice. You hear a cackling laugh coming from behind a nearby tree.");
    }else{
    	alert("Feeling the warm sun on your face you allow yourself a wry smile. The bird song in your ears and the scent of flowers in your nose.");
    }
    
    var choiceTwo = prompt("Investigate the laugh (press 1) or ignore (press 2)?");
    if(choiceTwo == "1"){
    	alert("out jumps a monster and eats you"); //fix this Mikey FFS!
    }else{
    	alert("you win");
    }
    </script>
    @pdiddles03, Don't highjack another's thread. Start your own.

  • #6
    Regular Coder
    Join Date
    Jun 2010
    Posts
    121
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Mistake buddy,

    also, read what the guy said, he said he's new to the stuff, so don't expect him to know everything. WE don't all need to jump on him about the alert thing, he is just trying to create a conditional.

    Quote Originally Posted by sunfighter View Post
    @Effexion, you don't need to aplogize for starting to learn code. We've all been there and stop saying We all do, but don't always say anything. LOL

    WolfShade is correct coding with alerts is a bad habit. We do use them in trouble shooting errors though.

    You have left something out like brackets and semi-colons. Here's a working code:
    Code:
    <script>
    var age = prompt("How old are you?");
    if( age < 18){
    	alert("You are to young to play this game!");
    }else{
    	alert("Enjoy the game");
    }
    
    alert("You're walking down the street and come to a fork. To the right you see beautiful streams,\
    and rolling countryside. To the left, dead trees, rain, sleet, and misery. Which way would you like\
    to go?");
    
    var choiceOne = prompt("left (press 1) or right (press 2)?");
    if(choiceOne == "1"){
    	alert("As you begin your path the rain pelts hard against your face. You are beginning to regret your decision,\
    	but still feel you made the right choice. You hear a cackling laugh coming from behind a nearby tree.");
    }else{
    	alert("Feeling the warm sun on your face you allow yourself a wry smile. The bird song in your ears and the scent of flowers in your nose.");
    }
    
    var choiceTwo = prompt("Investigate the laugh (press 1) or ignore (press 2)?");
    if(choiceTwo == "1"){
    	alert("out jumps a monster and eats you"); //fix this Mikey FFS!
    }else{
    	alert("you win");
    }
    </script>
    @pdiddles03, Don't highjack another's thread. Start your own.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Minor comment:

    If you want your code to be readable, and you want to use long strings, just break them up and then concatenate them:
    Code:
    alert(   "You're walking down the street and come to a fork.\n"
           + "To the right you see beautiful streams and rolling countryside.\n"
           + "To the left, dead trees, rain, sleet, and misery.\n"
           + "Which way would you like to go?" );
    And, yes, alert() should be avoided. But the above would still be one way to create long strings. Perhaps:
    Code:
    var current = document.getElementById("currentLocation");
    ...
    current.innerHTML = 
             "You're walking down the street and come to a fork.<br/>"
           + "To the right you see beautiful streams and rolling countryside.<br/>"
           + "To the left, dead trees, rain, sleet, and misery.<br/>"
           + "Which way would you like to go?";
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Also, using prompts and alerts and tons of "if" statements as you are, the game coding is going to get really ugly, really fast. Even if prompts and alerts looked good, which they don't, you need to find another way to represent the game to make the coding easier and make it easier to read and repair it when something goes wrong.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New to the CF scene
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Many, many thanks Old Pedant. I have been learning using Codecademy. This is the practice they sort of teach so I was just adapting what I had learned to blindly make that game. It isn't anything serious, just a little practice run for me until I learn further. I really appreciate your help there man, I will be sure to take it in to account and learn from it.

    Thanks once again.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    I wrote a tiny text adventure game (even called it "PicoAdventure") many many moons ago to demonstrate some features of a new version of BASIC that we had created.

    And even then I did it by creating a set of data the describe the rooms and the choices.

    In JavaScript, it might look something like this:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Sample adventure code</title>
    <style type="text/css">
    #theroom {
       font-size: x-large;
       border: solid blue 2px;
       background-color: lightblue;
       padding: 8px;
    }
    #gowhere li {
       font-size: large;
       color: green;
       cursor: pointer;
    }
    </style>
    </head>
    <body>
    Your current location:<br/><br/>
    <span id="theroom"></span>
    <br/><br/>
    Where will you go next?
    <ul id="gowhere"></ul>
    
    <script type="text/javascript">
    // this is the constructor to create one Choice object:
    function Choice( text, room )
    {
        this.choiceText = text;
        this.roomName = room;
    }
    
    // this is the contructor to create one Room object:
    function Room( desc, options )
    {
        this.description = desc;
        this.choices = options;
    }
    
    var rooms = {
        "Entrance" : new Room( "You are at the entrance",
                               [ new Choice("To the left you see a blue glow","Grotto"),
                                 new Choice("To the right you see only darkness", "Attic" ) 
                               ] ),
        "Grotto" :   new Room("You are in a beautiful blue grotto",
                              [ new Choice("In front of you is a treasure chest", "Chest" ),
                                new Choice("Behind you is the entrance", "Entrance" ),
                                new Choice("In front of you is what looks like a mine", "Mine" )
                              ] ),
        "Chest" :    new Room("You opened the treasure chest.  It exploded.  You died.",
                              [ new Choice("Quit game", "Exit" ),
                                new Choice("Try again", "Entrance" )
                              ] ),
        "Mine" :     new Room("You entered the mine.  You fall down a deep shaft.  You died.",
                              [ new Choice("Quit game", "Exit" ),
                                new Choice("Try again", "Entrance" )
                              ] ),
        "Attic":     new Room("You find yourself if the attic of a haunted house.",
                              [ new Choice("Go downstairs", "Entrance" ) 
                              ] ),
        "Exit":      new Room("Thank you for playing", 
                             [ ] /* blank array: no choices of places to go */ 
                             )
    };
    
    function moveto(where)
    {
        var room = rooms[where]; // get the info about appropriate room
        document.getElementById("theroom").innerHTML = room.description; // put description in place
        // the choices will go into the UL:
        var ul = document.getElementById("gowhere")
        // first, remove all LIs from the UL, if any:
        while ( ul.hasChildNodes() ) { ul.removeChild(ul.lastChild); }
        // then add all the choices:
        for ( var c = 0; c < room.choices.length; ++c )
        {
            var choice = room.choices[c];
            var li = document.createElement("li");
            li.innerHTML = choice.choiceText;
            li.onclick = goroom(choice.roomName);
            ul.appendChild(li);
        }
        function goroom(rm)
        {
            return function() { moveto(rm); }
        }
    }
    // start game:
    moveto("Entrance");
    </script>
    </body>
    </html>
    (The real pico adventure wasn't quite *that* simple. <grin/>)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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