Building Re-Ordering Random Numbers Game

In this article, I will explain clearly and briefly how to use HTML, JavaScript, jQuery, and CSS in building a reordering random numbers game (1,2,3,4,5,6,7,8).
 
Building such games depends on your logic.
 
You can find all the related files (codes) on the attachment.
 
The game will look like the following,
 
Building Re-Ordering Random Numbers Game
 
Building Re-Ordering Random Numbers Game
Building Re-Ordering Random Numbers Game 
 
First, we should build the structure of the UI, so before doing anything create a table and for every td specify its siblings by using a custom attribute data-sibling,
  1. <table>    
  2.           <tr>    
  3.                        <td data-sibling="2and4" Id="td1"></td>    
  4.                        <td data-sibling="1and3and5" Id="td2"></td>    
  5.                        <td data-sibling="2and6" Id="td3"></td>    
  6.                    </tr>    
  7.                    <tr>    
  8.                        <td data-sibling="1and5and7" Id="td4"></td>    
  9.                        <td data-sibling="2and4and6and8" Id="td5"></td>    
  10.                        <td data-sibling="3and5and9" Id="td6"></td>    
  11.                    </tr>    
  12.                    <tr>    
  13.                        <td data-sibling="4and8" Id="td7"></td>    
  14.                        <td data-sibling="5and7and9" Id="td8"></td>    
  15.                        <td data-sibling="6and8" Id="td9"></td>    
  16.          </tr>    
  17.  </table>    
 Then move to apply CSS to get the target UI, you can find the full CSS code with attached files.
  1.  table {    
  2.      background-color: #403737;    
  3.      color: #fffffe;    
  4.      cursor: pointer;    
  5.      border-radius: 5px;    
  6.      width: 100%;    
  7.      margin: 0px auto;    
  8.      float: none;    
  9.      border:10px solid #5a545a;    
  10.      margin :20px;    
  11. }    
  12.  table tr td {    
  13.      border: 5px solid #5a545a;    
  14.      width: 100px;    
  15.      height: 100px;    
  16.      text-align: center;    
  17.      font-size: 20px;    
  18.      font-weight: bold;    
  19. }   

The most important part of the game is handling JavaScript. We need to create a function which is resposible for generating 8 random numbers from 1 - 8 and putting them in an array. Thereafter spread these numbers over tds using $.each jQuery and textContent td property,

  1. function shuffleNumbers() {    
  2.     // Random integer between 1 and 8 (both included)     
  3.     var allshuffledNumbers = [];    
  4.     for (var i = 0; i < 8; i++) {    
  5.         var randomNumber = Math.floor(Math.random() * 8) + 1;    
  6.         while (allshuffledNumbers.indexOf(randomNumber) != -1) {    
  7.             randomNumber = Math.floor(Math.random() * 8) + 1;    
  8.         }    
  9.         allshuffledNumbers.push(randomNumber);    
  10.     }    
  11.     $(allshuffledNumbers).each(function(index, value) {    
  12.         $("#td" + (index + 1))[0].textContent = value    
  13.     });    
  14.       stepCounter=0;    
  15.      $("#td9")[0].textContent="";    
  16.      $("#stepcounter").text(stepCounter);    
  17. }   
After handling each event on td, you need to know the siblings of the clicked td and detect if one of them has empty textContent  to swap it with the textContent of the clicked td.
  1. $("table tr td").click(function(event) {    
  2.      var siblings = $(this).attr('data-sibling');    
  3.      siblingssiblings = siblings.split("and");    
  4.      var clickedText = event.currentTarget.textContent;    
  5.      var emptySibligText = "";    
  6.      $(siblings).each(function(index, value) {    
  7.          if ($("#td" + value)[0].textContent == "") {    
  8.              stepCounter++;    
  9.              $("#stepcounter").text(stepCounter);    
  10.              $("#td" + value)[0].textContent = clickedText;    
  11.              event.currentTarget.textContent = "";    
  12.              var isOrdered = checkOrdering();    
  13.              if (isOrdered == true) {    
  14.                  $("#WinnerModal").modal("show");    
  15.              }    
  16.          }    
  17.      });    
  18.  })    

And on every click event on any td, use checkOrdering function to detect if we got the correct ordering.

Then compare the current ordering (tds textContent ) with the target one.
  1. function checkOrdering() {    
  2.     var targetOrdering = ['1''2''3''4''5''6''7''8'];    
  3.     var isOrdered = true;    
  4.     $(targetOrdering).each(function(index, value) {    
  5.         if ($("#td" + value)[0].textContent != value) {    
  6.             isOrdered = false;    
  7.             return;    
  8.         }    
  9.     });    
  10.     return isOrdered;    
  11. }    

That was everything you need to build the game, hope you enjoyed it.