Game-3in-a-row/PROG2700-Assignment4B-3inarow_StarterFiles/game.js
// FUNCTION: The following function goes through the row objects and checks if the current state matches the correct state. If all are a match, the game is won and a success message is inserted into the DOM, else a continue message is inserted. 
The following function goes through the row objects and checks if the current state matches the correct state. If all are a match, the game is won and a success message is inserted into the DOM, else a continue message is inserted.
The following function goes through the row objects and checks if the current state matches the correct state . If all are a match the game is won and a success message is inserted into the DOM else a continue message is inserted .
function checkProgress() {
      let totalTiles = document.querySelectorAll('td');
      let currentCorrectTiles = []; 
      let currentIncorrectTiles = []; 
      gameJsonData.rows.forEach((row, indexA) => {
        row.forEach((object, indexB) => {
          if (object.currentState === object.correctState) {
            currentCorrectTiles.push(document.querySelector(`[data-row="${indexA}"], [data-Square="${indexB}"]`));
          } else if (object.currentState !== 0) {
            currentIncorrectTiles.push(document.querySelector(`[data-row="${indexA}"], [data-Square="${indexB}"]`))
          }
        })
      })
      let phraseNode = document.createElement('h2');
      let phraseText;
      if (currentCorrectTiles.length !== totalTiles.length && currentIncorrectTiles.length === 0) {
        phraseText = document.createTextNode('So far so good');
      } else if (currentCorrectTiles.length !== totalTiles.length) {
        phraseText = document.createTextNode('Something is wrong');
      } else {
        phraseText = document.createTextNode("Yes!!! You did it!");
      }
      phraseNode.appendChild(phraseText);
      if (document.querySelector('#theGame').lastElementChild.tagName === 'H2') {
        document.querySelector('#theGame').replaceChild(phraseNode);
      } else {
        document.querySelector('#theGame').appendChild(phraseNode);
      }
      if (document.querySelector('#theGame').children = 'h2') {
        setTimeout(() => {
          document.querySelector('#theGame').lastElementChild.remove();
        }, 7000);
      }
    }
function checkProgress ( ) { let totalTiles = document . querySelectorAll ( 'td' ) ; let currentCorrectTiles = [ ] ; let currentIncorrectTiles = [ ] ; gameJsonData . rows . forEach ( ( row , indexA ) => { row . forEach ( ( object , indexB ) => { if ( object . currentState === object . correctState ) { currentCorrectTiles . push ( document . querySelector ( ` ${ indexA } ${ indexB } ` ) ) ; } else if ( object . currentState !== 0 ) { currentIncorrectTiles . push ( document . querySelector ( ` ${ indexA } ${ indexB } ` ) ) } } ) } ) let phraseNode = document . createElement ( 'h2' ) ; let phraseText ; if ( currentCorrectTiles . length !== totalTiles . length && currentIncorrectTiles . length === 0 ) { phraseText = document . createTextNode ( 'So far so good' ) ; } else if ( currentCorrectTiles . length !== totalTiles . length ) { phraseText = document . createTextNode ( 'Something is wrong' ) ; } else { phraseText = document . createTextNode ( "Yes!!! You did it!" ) ; } phraseNode . appendChild ( phraseText ) ; if ( document . querySelector ( '#theGame' ) . lastElementChild . tagName === 'H2' ) { document . querySelector ( '#theGame' ) . replaceChild ( phraseNode ) ; } else { document . querySelector ( '#theGame' ) . appendChild ( phraseNode ) ; } if ( document . querySelector ( '#theGame' ) . children = 'h2' ) { setTimeout ( ( ) => { document . querySelector ( '#theGame' ) . lastElementChild . remove ( ) ; } , 7000 ) ; } }
FUNCTION: The following function goes through the row objects and checks if the current state matches the correct state.
FUNCTION : The following function goes through the row objects and checks if the current state matches the correct state .
// array to hold correct answer tiles // array to hold incorrect answer tiles // correct answer tile // writing success, continue or failure message to DOM // remove message from DOM after amount of time. 