www.webdeveloper.com
Results 1 to 2 of 2

Thread: Problem with snippet of game code?

  1. #1
    Join Date
    Jun 2014
    Posts
    9

    Problem with snippet of game code?

    Hey all,

    I have a code for a 1-player Tic Tac Toe, and the algorithm that I have for the CPU behavior is a little off. But I can't tell what the problem is. Could someone help me out and tell me what the problem is? I'm new to JS.

    The part commented out is the one causing the problem, I know that much.

    Code:
    		function CPUmove() 
    		{
    			/* 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[content[winCombo[z][0]] == false) 
    				return boxNumber = content[winCombo[z][0]];
    				if (filled[winCombo[z][0]] == true && filled[winCombo[z][2]] == true 
    				&& content[winCombo[z][0]] == content[winCombo[z][2]] && filled[content[winCombo[z][1]] == false) 
    				return boxNumber = content[winCombo[z][1]];
    				if (filled[winCombo[z][0]] == true && filled[winCombo[z][1]] == true 
    				&& content[winCombo[z][0]] == content[winCombo[z][1]] && filled[content[winCombo[z][2]] == false) 
    				return boxNumber = content[winCombo[z][2]];
    			} */
    			
    			if (filled[1] == false)	return boxNumber = 1;
    			else if (filled[3] == false) return boxNumber = 3;
    			else if (filled[7] == false) return boxNumber = 7;
    			else if (filled[9] == false) return boxNumber = 9;
    			else if (filled[5] == false) return boxNumber = 5;
    			else return boxNumber = Math.floor((Math.random() * 9) + 1);			
    		}

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434
    You might get some ideas about how to fix the code here.
    See: http://javascriptkit.com/script/script2/tactoe.shtml
    or: http://ostermiller.org/calc/tictactoe.html
    Last edited by JMRKER; 06-05-2014 at 11:15 PM.

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