www.webdeveloper.com
Results 1 to 11 of 11

Thread: Wait function not working

  1. #1
    Join Date
    Jun 2014
    Posts
    9

    Wait function not working

    Hey everyone,

    I've got a 1-player Tic Tac Toe game that is fully functional, but I have to find a way to insert a wait function so that instead of how it is right now where the CPU makes its move instantly after the player makes his/her own, I want it to be the CPU waiting a second and then making its move. I tried inserting a wait function throughout different parts of my code, but it hasn't been working (the program compiles and works as if the function isn't even there). This is my code; and I took out the wait function from it for now (ignore the HTML, I guess?):

    Code:
    <!DOCTYPE HTML>
    <script>
    
    		var a, box, c, context, content, filled, winCombo;
    		var turn = 0;
    		var playerTurn, CPUturn, sym, variable, fullName, boxNumber, CPUbox, CPUnumber, CPUc, CPUcontext;
    		var BoxesFilled = 0;
    		window.onload=function()
    		{			
    			filled = new Array();
    			content = new Array();
    			winCombo = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]];
    
    			for(var i = 1; i < 10; i++)
    			{
    			filled[i] = false;
    			content[i] = '';
    			}
    		}
    		
    		function Symbol() {
    		
    			document.getElementById(fullName).src = '/home/fizan/Documents/AAT Tic Tac Toe/Ambassador Pics/O.gif';			
    			if (document.getElementById('1').hidden != true) document.getElementById('1').style.visibility = 'hidden';
    			if (document.getElementById('2').hidden != true) document.getElementById('2').style.visibility = 'hidden';
    			if (document.getElementById('3').hidden != true) document.getElementById('3').style.visibility = 'hidden';
    			if (document.getElementById('4').hidden != true) document.getElementById('4').style.visibility = 'hidden';
    			if (document.getElementById('5').hidden != true) document.getElementById('5').style.visibility = 'hidden';
    			if (document.getElementById('6').hidden != true) document.getElementById('6').style.visibility = 'hidden';
    			if (document.getElementById('7').hidden != true) document.getElementById('7').style.visibility = 'hidden';
    			if (document.getElementById('8').hidden != true) document.getElementById('8').style.visibility = 'hidden';
    			if (document.getElementById('9').hidden != true) document.getElementById('9').style.visibility = 'hidden';
    			if (document.getElementById('10').hidden != true) document.getElementById('10').style.visibility = 'hidden';
    			}
    
    		function chooseBox()
    		{
    			
    			box = "Box" + boxNumber;
    			c = document.getElementById(box);
    			context = c.getContext("2d");
    		
    			if(filled[boxNumber] == false)
    			{
    				if(turn % 2 == 0)
    				{
    				content[boxNumber] = 'X';
    				var Ximage = new Image();
    				Ximage.src = '/home/fizan/Documents/AAT.jpg';
    				Ximage.onload = function() {
    				context.drawImage(Ximage, -5, 20);
    				checkWin(content[boxNumber]);	
    				};
    				}
    				
     				turn++; 
     				playerTurn = !playerTurn; 
     				CPUturn = !CPUturn; 
     				filled[boxNumber] = true; 
     				BoxesFilled++;
    				
    				if(BoxesFilled >= 9)
    				{
    					alert("Tie game. You both stink!");
    					location.reload(true);
    				}
    			}
    			else if(filled[boxNumber] == true) alert("What are you trying? That space is taken, dummy!");
    			else alert("ERROR");
    		}
    		
    		function CPUboxNumber() {
    			if (filled[5] == false) return CPUnumber = 5;
    
    			if ((filled[1] == true && (filled[7] == true || filled[9] == true) && filled [4] == true && 
    			filled[5] == true && filled[6] == false && filled[8] == false) && (content[1] == content[9] || content[1] == content[7]) && (content[4] == content[5]) && (filled[6] == false)) return CPUnumber = 6;
    
    			if ((filled[1] == true && filled[3] == true && filled[8] == true && filled[2] == true && 
    			filled[5] == true) && (content[1] == content[3] && content[3] == content[8] && content[2] == content[5]) && filled[6] == false)
    			return CPUnumber == 6;
    		
    			for (var z = 0; z < 8; z++) {  
    				if ((filled[winCombo[z][1]] == true) && (filled[winCombo[z][2]] == true) 
    				&& (content[winCombo[z][1]] == content[winCombo[z][2]]) && (filled[winCombo[z][0]] == false)) 
    				return CPUnumber = winCombo[z][0];
    				else if ((filled[winCombo[z][0]] == true) && (filled[winCombo[z][2]] == true) 
    				&& (content[winCombo[z][0]] == content[winCombo[z][2]]) && (filled[winCombo[z][1]] == false)) 
    				return CPUnumber = winCombo[z][1];
    				else if ((filled[winCombo[z][0]] == true) && (filled[winCombo[z][1]] == true) 
    				&& (content[winCombo[z][0]] == content[winCombo[z][1]]) && (filled[winCombo[z][2]] == false)) 
    				return CPUnumber = winCombo[z][2];
    			} 
    			
    			if ((filled[1] == true && filled[2] == true) && (content[1] != content[2])) return CPUnumber = 5;
    			else if (((filled[1] == true && filled[5] == true && filled[9] == true) && 
    			(content[1] == content[9]) && (content[5] != content[9]))
    			|| ((filled[3] == true && filled[5] == true && filled[7] == true) &&
    			(content[3] == content[7]) && (content[5] != content[7]))) {
    			if (filled[4] == false) return CPUnumber = 4;
    			else if (filled[6] == false) return CPUnumber = 6;
    			} 		
    			
    			else if (filled[1] == false) return CPUnumber = 1;
    			else if (filled[3] == false) return CPUnumber = 3;
    			else if (filled[7] == false) return CPUnumber = 7;
    			else if (filled[9] == false) return CPUnumber = 9;
    			else if (filled[5] == false) return CPUnumber = 5;
    		}
    		
    		function CPUmove() 
    		{
    			CPUboxNumber();		
    			box = "Box" + CPUnumber;
    			CPUc = document.getElementById(box);
    			CPUcontext = CPUc.getContext("2d");
    		
    			if(filled[CPUnumber] == false)
    			{
    				if(turn % 2 != 0)
    				{
    				content[CPUnumber] = 'O';				
    				var Oimage = new Image();
    				Oimage.src = '/home/fizan/Documents/AAT Tic Tac Toe/Ambassador Pics/' + fullName + '.png';
    				Oimage.onload = function() {
    				CPUcontext.drawImage(Oimage, 2.5, 2.5);
    				checkWin(content[CPUnumber]);	
    				};
    				}
    
     				turn++; 
     				playerTurn = !playerTurn; 
     				CPUturn = !CPUturn; 
     				filled[CPUnumber] = true; 
     				BoxesFilled++;
    				
    				if(BoxesFilled >= 9)
    				{
    					alert("Tie game. You both stink!");
    					location.reload(true);
    				}
    			}
    		}
    			
    		function checkWin(symbol)
    		{		
    			for(var j = 0; j < winCombo.length; j++)
    			{
    			if(content[winCombo[j][0]] == symbol && content[winCombo[j][1]] == symbol &&
    			content[winCombo[j][2]] == symbol)
    			{
    				alert(symbol + " Wins!");
    				location.reload(true);
    			}
    		}
    		}
    		
    		</script></head>
    		<body>
     <!-- ....HTML code...>
    	</body>
    	</html>

  2. #2
    Join Date
    Nov 2010
    Posts
    1,102
    have you looked at setTimeout?

  3. #3
    Join Date
    Jun 2014
    Posts
    9
    Yes, I have looked at setTimeout(), but I didn't include it in the code because I tried it in a bunch of different places but it always produced the same result: the program would compile as if the function wasn't even there. Here is an example of how I used setTimeout():
    Code:
    function CPUmove() 
    {
           setTimeout(1000, CPUboxNumber());
           ...........
           ...........
    }

  4. #4
    Join Date
    Mar 2009
    Posts
    523
    function CPUmove()
    {
    setTimeout(1000, CPUboxNumber());
    ...........
    ...........
    }
    That's not how you use setTimeout. You have the arguments reversed.

    Try
    Code:
    setTimeout(CPUboxNumber, 1000);
    instead.

  5. #5
    Join Date
    Jun 2014
    Posts
    9
    I tried that, the program just behaves really strangely with that. The CPU move doesn't even appear for the first 4 or 5 moves, and when it finally does it still doesn't wait for the 1 second. What could be done about this?

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,511
    You need to use setTimeout("CPUboxNumber()", 1000); for a delay.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  7. #7
    Join Date
    Jun 2014
    Posts
    9
    Quote Originally Posted by \\.\ View Post
    You need to use setTimeout("CPUboxNumber()", 1000); for a delay.
    I tried that, too, and it still produces the same result as my initial function; it compiles like the setTimeout() isn't even there.

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,511
    Then the issue will be where you are using the call.

    What would be easier is if you show where you put this pause into play.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  9. #9
    Join Date
    Jun 2014
    Posts
    9
    Quote Originally Posted by \\.\ View Post
    Then the issue will be where you are using the call.

    What would be easier is if you show where you put this pause into play.
    That's exactly what the problem turned out to be. I already have it fixed, though. Thank you very much for your help!

  10. #10
    Join Date
    Nov 2010
    Posts
    1,102
    anyway, all that CPUboxNumber does is return a number. Wouldn't you be better calling:
    Code:
    setTimeout(CPUmove, 1000);
    at the end of the player move

    but I don't see how this code works - when is CPUmove ever called? or chooseBox?

  11. #11
    Join Date
    Jun 2014
    Posts
    9
    Quote Originally Posted by xelawho View Post
    anyway, all that CPUboxNumber does is return a number. Wouldn't you be better calling:
    Code:
    setTimeout(CPUmove, 1000);
    at the end of the player move

    but I don't see how this code works - when is CPUmove ever called? or chooseBox?
    This is only the JS part of the code. There's an HTML portion of code to it as well, and that's where the functions are called. I already have the problem fixed, though. Thank you anyways.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles