Results 1 to 4 of 4

Thread: onclick turns for tic tac toe

  1. #1
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default onclick turns for tic tac toe

    Hello all and thank you for reading this. I am trying to start tic tac toe but I dont know how to turn the onclick events displaying X and O. Please I would like some advice or help thank you very much all. this is my code.in the code below X is only displayed on my table and I dont know how to give turns plz help me.

    Code:
    function tableclick()
    {
    this.innerHTML = "X";
    
    }
    
    for (var i=1; i<10; i++)
    {
      document.getElementById("cell"+i).onclick = tableclick;
    }
    Last edited by jscheuer1; 04-27-2012 at 09:40 PM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default Three in a Row!

    Code:
    function tableclick(){
    	this.innerHTML = tableclick.token;
    	tableclick.token = tableclick.token === 'X'? 'O' : 'X';
    }
    tableclick.token = 'X';
    
    for(var i = 1; i < 10; ++i){
    	document.getElementById('cell' + i).onclick = tableclick;
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dear John

    I would like to thank you for your code it worked perfectly but what I did I added a global variable to my code giving the value of X and then I added an IF statement to give the turns ! I am a bit concerned of how to stop overwriting the boxes when they are marked so to only allow the move only if the box("-") has neither of X or O could you help me on that ?

    Many thanks!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In general, due to possible conflicts with other scripts that might be added later, it's a good idea to limit the global exposure of variable and function names. With this code, no global exposure is required, but that would make it a little more complicated. To add the feature you requested, add the highlighted as shown:

    Code:
    function tableclick(){
    	this.innerHTML = tableclick.token;
    	tableclick.token = tableclick.token === 'X'? 'O' : 'X';
    	this.onclick = function(){return;};
    }
    tableclick.token = 'X';
    
    for(var i = 1; i < 10; ++i){
    	document.getElementById('cell' + i).onclick = tableclick;
    }
    To remove all global exposure:

    Code:
    ;(function(){
    	function tableclick(){
    		this.innerHTML = tableclick.token;
    		tableclick.token = tableclick.token === 'X'? 'O' : 'X';
    		this.onclick = function(){return;};
    	}
    	tableclick.token = 'X';
    	
    	for(var i = 1; i < 10; ++i){
    		document.getElementById('cell' + i).onclick = tableclick;
    	}
    })();
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    jangkoo (04-28-2012)

Bookmarks

Posting Permissions

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