Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

javascript back to basics

Options
  • 13-01-2008 6:12pm
    #1
    Registered Users Posts: 8,070 ✭✭✭


    Making tic tac toe game for class, this is actually embarassing. Cant get the alrgorithm working in my head.

    Basically, cant get the table to update without printing it again, since html is freekin static, now im not sure if i can use innerHtml etc cause this class is fairly basic.

    //2D Array
    var myarray=new Array(3);
    
    for (k=0; k<3; k++)
    {
    myarray[k]=new Array(3);
    
    }
    
    //to print an empty table
    myarray[0][0]="&nbsp;"
    myarray[0][1]="&nbsp;"
    myarray[0][2]="&nbsp"
    myarray[1][0]="&nbsp"
    myarray[1][1]="&nbsp"
    myarray[1][2]="&nbsp"
    myarray[2][0]="&nbsp"
    myarray[2][1]="&nbsp"
    myarray[2][2]="&nbsp"
    
    
    
    //PRINTS
    document.write('<table cellspacing="1" cellpadding="3" "border="2" width=100 height=60>');
    
    for(i=0; i<myarray.length; i++)
    {
      document.write ('<tr>');
        for(j=0; j<myarray.length; j++)
        {
          document.write('<td>');
          document.write(myarray[i][j]);
          document.write('</td>');
        }
      document.write('</tr>');
    }
    document.write('</table>');
    
    
    //this is where the code will come in......
    var choice;
    switch (choice = prompt('Enter a move'))
    {
    case "1":
    myarray[0][0]="X"
    
    break;
    case "2":
    myarray[0][1]="X"
    
    break;
    }
    
    

    putting print content in a function and calling that makes the table print out again in html.


Comments

  • Registered Users Posts: 8,070 ✭✭✭Placebo


    ***k it gonna use innerhtml.

    thanks


  • Registered Users Posts: 8,584 ✭✭✭TouchingVirus


    Placebo wrote: »
    ***k it gonna use innerhtml.

    thanks

    Four years of CSSE...I'd be embarrassed :p


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    did you read my post? i was trying to get at it without innerHtml, as far as i know its immpossible without some sort of dynamic work around, idiot.


  • Registered Users Posts: 8,584 ✭✭✭TouchingVirus


    Placebo wrote: »
    did you read my post? i was trying to get at it without innerHtml, as far as i know its immpossible without some sort of dynamic work around, idiot.

    No you didn't, you said you weren't sure if you could use innerHtml not that you were trying to deliberately avoid using it.

    I'd appreciate if you refrained from insults aswell


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    There is a way to do it without innerHTML. Have 3 divs stacked on over the other on each square (ids of maybe p1r1c1 (shows player ones image), p2r1c1 (shows player twos image), p0r1c1 (shows an empty square) for row1,col1). Obviously have the blank one display style set to block by default, as the player clicks on a square set document.getElementById('p0r1c1').style.display = "none" and the depending on the player either set document.getElementById('p1r1c1').style.display = "block" or document.getElementById('p2r1c1').style.display = "block";

    And don't use naughty name calling as a way of dealing with frustration :) BTW this method is likely to be more complicated than .innerHTML. For a basic course class I'd suggest going with the .innerHTML method.

    -RD


  • Advertisement
  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I'd appreciate if you refrained from insults aswell

    As would I.


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    thans ron, i went with basic innerHtml, getElement by calling its id did the trick, after that it was easy.


Advertisement