// version 230217 // uses background-color to test cells for X and O this is then covered by a background image. // 070117 - randomise location of questions // 700911 - corrected xbrowser bug re background-color values with spaces // real noughts and crosses // initialise variables superttt.js used by superttt.html var player=0; // declare player variable with player = 2 for first turn var emptycolor=''; var play1color='rgb(255,173,0)'; // set color for player 1 -orange var play2color='rgb(0,140,255)'; // set color for player 2 -blue var randomnumber=0; // declare this globally var winnerstatus=0; // set winner status to 0 var turns=0; var response='0'; // set response to wrong function resetboard(){ //use form reset event to clear board and reset variables uniques(); loadquestions(); soundplayer('correct1'); // Add this quiz title (if any) document.getElementById('title').innerHTML=title; randomnumber=0; // declare this globally winnerstatus=0; // set winner status to 0 turns=0; response='0'; // set response to wrong for (var i=1;i<5;i++) { document.getElementById('a'+i).style.backgroundColor=emptycolor; document.getElementById('a'+i).style.backgroundImage='none'; document.getElementById('a'+i).innerHTML=''; document.getElementById('b'+i).style.backgroundColor=emptycolor; document.getElementById('b'+i).style.backgroundImage='none'; document.getElementById('b'+i).innerHTML=''; document.getElementById('c'+i).style.backgroundColor=emptycolor; document.getElementById('c'+i).style.backgroundImage='none'; document.getElementById('c'+i).innerHTML=''; document.getElementById('d'+i).style.backgroundColor=emptycolor; document.getElementById('d'+i).style.backgroundImage='none'; document.getElementById('d'+i).innerHTML=''; } displayinstructions(); } function unselected(x){ if(response=='1') //ensure player takes their move {//make drop box lose focus document.getElementById('resetbutton').focus(); alertboxer('
Click on a Square to take the move you Won
')} else { if (x.selectedIndex !=0) {// make drop box lose focus document.getElementById('resetbutton').focus(); alertboxer('You cannot change an answer
');} } } function assess(answer){ // assess question // First we blur the current object to lock in the answer // we force loss of focus to prevent changes document.getElementById('resetbutton').focus(); // now check that the game is not over already if (winnerstatus==1){alertboxer('
Pick a Square
"); player=1;} // players turn else {soundplayer('wrong1'); answer.style.color='red'; alertboxer("
Lose a Turn
"); //computers turn player=2; ai();} } } // cell are named as per spreadsheet layout column letter, row number function allowplay(x){ // is player allowed a turn ? if (response=='1'){emptycell(x)} else {alertboxer("You must answer a question correctly to win a move
")} } function emptycell(x){ // is the players selected cell, id=x, empty ? if (document.getElementById(x).style.backgroundColor == emptycolor) {player=1; // player one needed for colour colourcell(x);} //okay cell empty - run whose turn is it else {alertboxer("Press the New Game button to play again.
'; if (row1score==4||row2score==4||row3score==4||row4score==4) {alertboxer('
You have a Winning Row
'+message); winnerstatus=1;} if (row1score==32||row2score==32||row3score==32||row4score==32) {alertboxer('Computer has a Winning Row
'+message); winnerstatus=1;} if (col1score==4||col2score==4||col3score==4||col4score==4) {alertboxer('You have a Winning Column
'+message); winnerstatus=1;} if (col1score==32||col2score==32||col3score==32||col4score==32) {alertboxer('Computer has a Winning Column
'+message); winnerstatus=1;} if (diag1score==4||diag2score==4) {alertboxer('You have a Winning Diagonal
'+message); winnerstatus=1;} if (diag1score==32||diag2score==32) {alertboxer('Computer has a Winning Diagonal
'+message); winnerstatus=1;} turns=turns+1; // add a turn to number of turns // if turns ==16 its a draw, game over reset game if (turns==16&&winnerstatus!=1) {winnerstatus=1; //set winnerstatus to game over alertboxer('Its a Draw, Press New game to play again.
');} else {// no win or draw for human player, let computer have a go // initialise cell variable to empty strings cell1=' ';cell2=' ';cell3=' ';cell4=' '; // First lets stop possible two row column or diagonal wins // Rule 0 Lets stop three in a row 1 but only if game not over if (row1score==3&&winnerstatus!=1&&player==2) {cell1='a1'; cell2='b1'; cell3='c1'; cell4='d1';} // stop three in a row 2 if (row2score==3&&winnerstatus!=1&&player==2) {cell1='a2'; cell2='b2'; cell3='c2'; cell4='d2';} // stop three in a row 3 if (row3score==3&&winnerstatus!=1&&player==2) {cell1='a3'; cell2='b3'; cell3='c3'; cell4='d3';} if (row4score==3&&winnerstatus!=1&&player==2) {cell1='a4'; cell2='b4'; cell3='c4'; cell4='d4';} // stop three in a column 1 if (col1score==3&&winnerstatus!=1&&player==2) {cell1='a1'; cell2='a2'; cell3='a3'; cell4='a4';} // stop three in a column 2 if (col2score==3&&winnerstatus!=1&&player==2) {cell1='b1'; cell2='b2'; cell3='b3'; cell4='b4';} // stop three in a column 3 if (col3score==3&&winnerstatus!=1&&player==2) {cell1='c1'; cell2='c2'; cell3='c3'; cell4='c4';} if (col4score==3&&winnerstatus!=1&&player==2) {cell1='d1'; cell2='d2'; cell3='d3'; cell4='d4';} // stop three in a diagonal 1 if (diag1score==3&&winnerstatus!=1&&player==2) {cell1='a1'; cell2='b2'; cell3='c3'; cell4='d4';} // stop three in a diagonal 2 if (diag2score==3&&winnerstatus!=1&&player==2) {cell1='d1'; cell2='c2'; cell3='b3'; cell4='a4';} // run stopwin if computer is threatened by a 3 else use ai if(cell1!=' '){stopwin(cell1,cell2,cell3,cell4);} if (player==2&&winnerstatus!=1) {ai(); }// get a random computer move } } function stopwin(a,b,c,d){ // a,b,c are the cells totalling 2 // find white cell and colour it to stop player win if (document.getElementById(a).style.backgroundColor==emptycolor){colourcomputercell(a);} if (document.getElementById(b).style.backgroundColor==emptycolor){colourcomputercell(b);} if (document.getElementById(c).style.backgroundColor==emptycolor){colourcomputercell(c);} if (document.getElementById(d).style.backgroundColor==emptycolor){colourcomputercell(d);} } function ai(){ // this is where the computer smarts go //okay can computer take the center or top left // Rule 1 is center taken ? if (document.getElementById('b2').style.backgroundColor == emptycolor) {colourcomputercell('b2');} // take center else{// Rule 2 if center taken is a1 available if (document.getElementById('a1').style.backgroundColor == emptycolor) {colourcomputercell('a1');} // take a1 else{randnum2();} // don't know what else to do - pick a square at random! } } function randnum2(){ // a random number generator function var randomnumber=Math.round(Math.random()*15+1); randomnumber=String(randomnumber); // convert to string -switch needs strings randomcell(randomnumber); } function randomcell(y){ // create cellref as a new string for each case cellref=' '; switch (y) { case '1' : cellref = 'a1'; break; case '2' : cellref = 'b1'; break; case '3' : cellref = 'c1'; break; case '4' : cellref = 'd1'; break; case '5' : cellref = 'a2'; break; case '6' : cellref = 'b2'; break; case '7' : cellref = 'c2'; break; case '8' : cellref = 'd2'; break; case '9' : cellref = 'a3'; break; case '10' : cellref = 'b3'; break; case '11' : cellref = 'c3'; break; case '12' : cellref = 'd3'; break; case '13' : cellref = 'a4'; break; case '14' : cellref = 'b4'; break; case '15' : cellref = 'c4'; break; case '16' : cellref = 'd4'; break; } emptycomputercell(cellref); //call function that checks if computer's choice of cell is empty } function emptycomputercell(x){// is the computers selected cell empty ? if (document.getElementById(x).style.backgroundColor == emptycolor) {colourcomputercell(x);} //okay cell empty colour it else {randnum2();} // cell must have been used, get another random number }