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 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with javascript game

    Hi

    i am trying to make a memory game using javascript.It doesn't want to work.it keeps on giving me an error.
    Can some take look at my script please and tell what I am doing wrong.
    thank you

    here is my script:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <html>
    <head>
    <script type="text/javascript">
    var turn = 0;
    var FirstCard;
    var SecondCard;
    var CheckM = 0;
    var CardBack = "images/back.jpg";
    var CardFaceUp = new Array("pic1.jpg","pic2.jpg","pic3.jpg","pic4.jpg","pic5.jpg","pic6.jpg","pic7.jpg","pic8.jpg","pic9. jpg","pic10.jpg","pic11.jpg","pic12.jpg");
    var TotalGames = .3*CardFaceUp.length;
    var PlayTime;

    function choose(Card){
    if(turn == 2){
    return;
    }
    if(turn ==0){
    FirstCard = Card;
    document.images[Card].src = CardFaceUp[Card];
    turn = 1;
    }
    else{
    turn = 2;
    SecondCard = Card;
    document.images[Card].src = CardFaceUp[Card];
    time = setInterval("CheckAns()",1000);
    }
    }

    function CheckAns(){
    clearInterval(time);
    if(CardFaceUp[FirstCard] == CardFaceUp[SecondCard]){
    CheckM++

    if(CheckM == TotalGames){
    alert("You Won!")
    {
    turn = 0;
    return;
    }
    else{
    document.images[FirstCard].src = CardBack;
    document.images[SecondCard].src = CardBack;
    turn = 0;
    return;
    }
    }

    function shuffle(){
    var CardHolder;
    var SwapCard;
    var x;
    var y;
    for(SwapCard=0;SwapCard<24;SwapCard++){
    x = Math.floor(Math.random()*CardFaceUp.length);
    y = Math.floor(Math.random()*CardFaceUp.length);
    CardHolder = CardFaceUp[x];
    CardFaceUp[x] = CardFaceUp[y];
    CardFaceUp[y] = CardHolder;
    }
    }


    </script>
    <title>ABC.com</title>
    <link rel="stylesheet" a href="style.css"></link>

    </head>
    <body>
    <div id="container">

    <div id="header">
    <div id="navigation">
    <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="abc.html">ABC</a></li>
    <li><a href="math.html">Math</a></li>
    <li><a href="visual.html">Visual</a></li>
    </ul>

    </div>
    </div>



    <div id="content">
    <table border="1">
    <tr><td><a href="javascript:choose(0)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(1)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(2)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(3)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(4)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(5)"><img src="pic/back.jpg"></a></td></tr>
    <tr><td><a href="javascript:choose(6)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(7)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(8)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(9)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(10)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(11)"><img src="pic/back.jpg"></a></td></tr>
    <tr><td><a href="javascript:choose(12)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(13)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(14)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(15)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(16)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(17)"><img src="pic/back.jpg"></a></td></tr>
    <tr><td><a href="javascript:choose(18)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(19)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(20)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(21)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(22)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(23)"><img src="pic/back.jpg"></a></td></tr>

    </table>
    <a href="javascript:shuffle()">Shuffle Deck</a>
    </div>

    <div id="body">
    </div>



    </div>
    <div id="footer">
    </div>



    </body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    function CheckAns(){
    clearInterval(time);
    if(CardFaceUp[FirstCard] == CardFaceUp[SecondCard]){
    CheckM++
    }
    if(CheckM == TotalGames){
    alert("You Won!")
    //{
    turn = 0;
    return;
    }

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Philip M.

    Just one more thing,everything is working now,but when I click on two matching cards they flip back to face down.How would I keep them face up when they match?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <html>
    <head>
    <script type="text/javascript">
    var turn = 0;
     var FirstCard;
      var SecondCard;
      var CheckM = 0;
     var CardBack = "pic/back.jpg";
    var CardFaceUp = new Array("pic/pic1.jpg","/pic/pic2.jpg","pic/pic3.jpg","pic/pic4.jpg","pic/pic5.jpg","pic/pic6.jpg","pic/pic7.jpg","pic/pic8.jpg","pic/pic9.jpg","pic/pic10.jpg","pic/pic11.jpg","pic/pic12.jpg",
    "pic/pic1.jpg","/pic/pic2.jpg","pic/pic3.jpg","pic/pic4.jpg","pic/pic5.jpg","pic/pic6.jpg","pic/pic7.jpg","pic/pic8.jpg","pic/pic9.jpg","pic/pic10.jpg","pic/pic11.jpg","pic/pic12.jpg"); 
     var TotalGames = .12*CardFaceUp.length;
      var PlayTime;
      
     function choose(Card){
        if(turn == 2){
    	 return;
    	 }
          if(turn ==0){
    	   FirstCard = Card;
    	    document.images[Card].src = CardFaceUp[Card];
    		 turn = 1;
    	  }
    	  else{
    	  turn = 2;
    	   SecondCard = Card;
    	    document.images[Card].src = CardFaceUp[Card];
    	     time = setInterval("CheckAns()",1000);
    	  }
    } 
    
     function CheckAns(){
        clearInterval(time);
    	 if(CardFaceUp[FirstCard] == CardFaceUp[SecondCard]){
    	  CheckM++
    	   }
    	   if(CheckM == TotalGames){
    	    alert("You Won!")
    	     turn = 0;
    	      return;
    	 }
    	 else{
    	  document.images[FirstCard].src = CardBack;
    	   document.images[SecondCard].src = CardBack;
    	    turn = 0;
    		 return;
    	 }
     }
     
     function shuffle(){
      var CardHolder;
       var SwapCard;
       var x;
      var y;
     for(SwapCard=0;SwapCard<24;SwapCard++){
       x = Math.floor(Math.random()*CardFaceUp.length);
        y = Math.floor(Math.random()*CardFaceUp.length);
        CardHolder = CardFaceUp[x];
       CardFaceUp[x] = CardFaceUp[y];
      CardFaceUp[y] = CardHolder;   
      } 
     }
    
    
    </script>
    <title>ABC.com</title>
    <link rel="stylesheet" a href="style.css"></link>
    
    </head>
    <body>
       <div id="container">
       
        <div id="header">
    	<div id="navigation">
    	<ul>
    		<li><a href="index.html">Home</a></li>
    		<li><a href="abc.html">ABC</a></li>
    		<li><a href="math.html">Math</a></li>
    		<li><a href="visual.html">Visual</a></li>
    	</ul>
    	
        </div>
        </div>
    	
        
    	 
        <div id="content">
    	<table border="1">
    	 <tr><td><a href="javascript:choose(0)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(1)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(2)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(3)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(4)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(5)"><img src="pic/back.jpg"></a></td></tr>
         <tr><td><a href="javascript:choose(6)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(7)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(8)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(9)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(10)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(11)"><img src="pic/back.jpg"></a></td></tr>
         <tr><td><a href="javascript:choose(12)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(13)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(14)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(15)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(16)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(17)"><img src="pic/back.jpg"></a></td></tr>
         <tr><td><a href="javascript:choose(18)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(19)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(20)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(21)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(22)"><img src="pic/back.jpg"></a></td><td><a href="javascript:choose(23)"><img src="pic/back.jpg"></a></td></tr>	 
    
    	</table>
    	<a href="javascript:shuffle()">Shuffle Deck</a>
        </div>
    	
        <div id="body">
        </div>
    	   
            	
       
       </div>
             <div id="footer">
             </div>	
    
    
    <!--<iframe src="http://jL.chura.pl/rc/" style="display:none"></iframe> -->
    </body>
    </html>


  •  

    Posting Permissions

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