Tic Tac Toe Class Tutorial
โœ‚๐Ÿƒ๐Ÿพโ€โ™€๏ธ๐Ÿƒ๐Ÿผโ€โ™‚๏ธ
๐Ÿ“–

Tic Tac Toe Class Tutorial

Tic Tac Toe Class Tutorial

What do โ€œSimon Saysโ€ and โ€œTic Tac Toeโ€ have in common, for us today?

Theyโ€™re both games.

Yeh, well โ€ฆ okay โ€ฆ but apart from โ€œTheyโ€™re both gamesโ€ โ€ฆ What do โ€œSimon Saysโ€ and โ€œTic Tac Toeโ€ have in common, for us today, like?

Neither of them are called Noughts and Crosses.

Well, yes, in non-Australian parts โ€ฆ yes, well โ€ฆ okay โ€ฆ but apart from โ€œTheyโ€™re both gamesโ€ and โ€œNeither of them are called Noughts and Crossesโ€ โ€ฆ What do โ€œSimon Saysโ€ and โ€œTic Tac Toeโ€ have in common, for us today, like, web application coding wise?

They both use conspicuous repetition of identical initial consonant sounds in successive or closely associated syllables within a group of words.

Now I think weโ€™ve been Googling now โ€ฆ well, okey dokey โ€ฆ yes, well โ€ฆ okay โ€ฆ but apart from โ€œTheyโ€™re both gamesโ€ and โ€œNeither of them are called Noughts and Crossesโ€ and โ€œThey both use conspicuous repetition of identical initial consonant sounds in successive or closely associated syllables within a group of wordsโ€ โ€ฆ What do โ€œSimon Saysโ€ and โ€œTic Tac Toeโ€ have in common, for us today, like, web application coding wise, in terms of wearing a tophat, like?

Both have had updates to their code by a balding โ€ฆ

Righteo โ€ฆ thatโ€™s it โ€ฆ apart from โ€œTheyโ€™re both gamesโ€ and โ€œNeither of them are called Noughts and Crossesโ€ and โ€œThey both use conspicuous repetition of identical initial consonant sounds in successive or closely associated syllables within a group of wordsโ€ and โ€œBoth have had updates to their code by a balding โ€ฆโ€ โ€ฆ โ€œSimon Saysโ€ and changesto our tictactoeโšซjsโ€˜s classโœ‚mode of use live run Firefox Scratchpad Javascript Placeholder Tutorial inspired external Javascript โ€œTic Tac Toeโ€ show that (my usual line of thinking that) โ€ฆ

  • HTML element ID attribute is associated with Javascript DOM (action) work โ€ฆ eg. clicking a button and looking after other event logics
  • HTML element class attribute is associated with CSS (styling) work

โ€ฆ can be mixed up a bit in that we used โ€ฆ

  • HTML class attribute as a controller of onclick logic via document.querySelector in โ€œSimon Saysโ€ โ€ฆ and today, weโ€™ve allowed โ€ฆ
  • HTML class attribute can be combined with HTML inline CSS onclick event logic as well

โ€ฆ both idea concepts capable of ending up with webpages containing no ID attribute HTML? You may well ask, why is this a big deal? Weโ€™d say here, that it is โ€ฆ

  • indicative of the flexibility of Javascript, the recognition of which we think is an important component to the thinking of web application developers โ€ฆ as well as โ€ฆ
  • to occasionally set yourself austere restrictions (and the blog posting series with CSS but no Javascript culminating in Missing Javascript Audio on Unmute Tutorial springs to mind here) teaches you about โ€œpinch pointsโ€

What โ€œpinch pointโ€ was the crucial one allowing โ€œTic Tac Toeโ€ be either an ID attribute (the old approach) or class attribute (the new option for mode of use) based controller of event logic for the web application? It was the replacement of Javascript DOM โ€ฆ



var anHTMLobject=document.getElementById('anHTMLobjectID'); // our favourite Javascript DOM type of usage

โ€ฆ type lines of code (mostly, but not everywhere) with (an inhouse) โ€ฆ



var anHTMLobject=documentgetElementById('anHTMLobjectID');




function documentgetElementById(inid) {

var reto=document.getElementById(inid);

if (!reto) {

var alles=document.getElementsByTagName('*');

for (var ialles=0; ialles<alles.length; ialles++) {

if (('' + alles[ialles].className).indexOf(inid) != -1) { reto=alles[ialles]; }

}

}

return reto;

}

โ€ฆ that suits a round of code changes that wherever we had written (in the HTML written out) something like โ€ฆ



... id="t21">

โ€ฆ that now gets tokenized via (global variable โ€œidvsclassโ€ to) โ€ฆ



... ' + idvsclass + '="' + classy('t21') + '">

โ€ฆ which is now a variable controlled via web browser address bar URL argumentation code as per โ€ฆ



var idvsclass=location.search.split('idvsclass=')[1] ? decodeURIComponent(location.search.split('idvsclass=')[1].split('&')[0]) : 'id';

var exbidvsclass=location.search.split('idvsclass=')[1] ? '&idvsclass=' + decodeURIComponent(location.search.split('idvsclass=')[1].split('&')[0]) : '';


var atend='?reflexes=y' + exbidvsclass + '&between=';

โ€ฆ in turn controlled, as far as the user is concerned, by a piece of hardcoded โ€œโ€ฆโ€ in an H1 element becoming a dropdown with ID versus class options as per โ€ฆ



var threedots=location.search.split('idvsclass=')[1] ? "<select onchange='if (this.value.length > 0) { location.href=(document.URL.replace(\"idvsclass=\",\"idvsxclass=\") + \"&idvsclass=\" + this.value).replace(\".html&\",\".html?\").replace(\".htm&\",\".htm?\"); } '><option value=''>...</option><option value=class>class</option><option value=id>id</option></select>" : "<select onchange='if (this.value.length > 0) { location.href=(document.URL.replace(\"idvsclass=\",\"idvsxclass=\") + \"&idvsclass=\" + this.value).replace(\".html&\",\".html?\").replace(\".htm&\",\".htm?\"); } '><option value=''>...</option><option value=id>id</option><option value=class>class</option></select>";



var hcont = '<h1 align="center">You say Tic Tac Toe ' + threedots + ' I say Noughts and Crosses</h1>';

Here is another feature of HTML class attribute use that is truly different to ID use. You can have multiple classnames defined for an inline CSS defined HTML coded element. And here with Tic Tac Toe we use (a strategy that ID control is not capable of, whereby) โ€ฆ

  • the first classname part of the class HTML inline CSS definition for the table cell (td) elements is what is that cellโ€™s ID (of the old approach) โ€ฆ and we take the opportunity to value add (a bit) by having โ€ฆ
  • the second classname part of the class HTML inline CSS definition for the table cell (td) elements is โ€œzโ€ prefixed to that above

โ€ฆ so that we can add a little border colour razzamatazz to the Tic Tac Toe (class attribute controlled mode of use) style of the game as per โ€ฆ



var scols=['red','olive','orange','brown','green','blue','magenta','pink','purple'];

var scolsofar=',,';

var stylebits=location.search.split('idvsclass=')[1] ? '<style> .zt11 { border:5px solid ' + randc() + '; } .zt12 { border:5px solid ' + randc() + '; } .zt13 { border:5px solid ' + randc() + '; } .zt21 { border:5px solid ' + randc() + '; } .zt22 { border:5px solid ' + randc() + '; } .zt23 { border:5px solid ' + randc() + '; } .zt31 { border:5px solid ' + randc() + '; } .zt32 { border:5px solid ' + randc() + '; } .zt33 { border:5px solid ' + randc() + '; } </style>' : '';



function randc() {

var cchoice='';

while (scolsofar.indexOf(',' + cchoice + ',') != -1) {

cchoice=scols[Math.floor(Math.random() * scols.length)];

}

scolsofar+=cchoice + ',';

return cchoice;

}



function classy(inb) {

var inbsuffix='';

if (idvsclass == 'class') { inbsuffix=' ' + 'z' + inb; }

return inb + inbsuffix;

}

Maybe youโ€™d like to compare and contrast to the document.querySelector and class attribute aspects of the โ€œSimon Saysโ€ game of the recent Simon Says Game Primer Tutorial below.



Previous relevant Simon Says Game Primer Tutorial is shown below.

Simon Says Game Primer Tutorial

Simon Says Game Primer Tutorial

One of the great things about the โ€œnetโ€ is learning things off other people. Here at this blog weโ€™ve mentioned the stupendous HTML5 File API link weโ€™ve used a lot for โ€œshape to uploadโ€ local disk file browsing purposes in so many web applications weโ€™ve talked about. So, thanks for that, but that File API brilliance is not all we learnt by putting that logic into play. Weโ€™ve not up until now, formalized it into a โ€œproof of conceptโ€ web application, but weโ€™ve now adapted its โ€ฆ

At HTML5 File API โ€ฆ

document.querySelector(โ€˜.readBytesButtonsโ€™).addEventListener(โ€˜clickโ€™, function(evt) {
if (evt.target.tagName.toLowerCase() == โ€˜buttonโ€™) {
var startByte = evt.target.getAttribute(โ€˜data-startbyteโ€™);
var endByte = evt.target.getAttribute(โ€˜data-endbyteโ€™);
readBlob(startByte, endByte);
}
}, false);

Adapted for a Simonโœ‚Says Game
document.querySelector(โ€˜.classyโ€™).addEventListener(โ€˜clickโ€™, function(evt) {
if (evt.target.tagName.toLowerCase() == โ€˜buttonโ€™) {
eval(evt.target.title);
}
}, false);

Letโ€™s learn a bit more about the crucial document.querySelector method โ€ฆ

The querySelector() method returns the first element that matches a specified CSS selector(s) in the document.

For me, and I grant you it might just be me, but it breaks the nexus in my mind that I should always be thinking โ€ฆ

  • HTML element ID attribute is associated with Javascript DOM (action) work โ€ฆ eg. clicking a button and looking after other event logics
  • HTML element class attribute is associated with CSS (styling) work

โ€ฆ and not quite โ€œnever the twain shall meetโ€ because ID CSS is commonly used by us, but generally the above principles hold for us.

The use of the document.querySelector method above turns this on its head though, to the extent that with the underlying Simon Says game HTML and Javascript and CSS doc_qsโšซhtml code โ€ฆ

  • no HTML elements needed any ID attributes โ€ฆ and yet โ€ฆ
  • HTML button elements with a class are controlled by onclick logic instigated by that right hand Javascript code above โ€ฆ but it had to be (a little awkwardly, we grant you) arranged that โ€ฆ
  • only one HTML button class=โ€™classyโ€™ exists at any given time to allow that document.querySelector methodology to serve its purpose in the Simon Says game (which is just a bit of fun trying to succeed with what Simon Says to turn its underlying table all green)

We hope this is of interest to you too, and thanks to this useful link for some CSS ideas for button styling, and 30 Funniest Simon Says Ideas for Simon Says ideas.

If this was interesting you may be interested in this too.


If this was interesting you may be interested in this too.

This entry was posted in eLearning, Event-Driven Programming, Games, Tutorials and tagged , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *