Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. $(document).ready(function()
  2. {
  3. const PLAYER = "X";
  4. const COMPUTER = "O";
  5. let game_status = true;
  6. var emptyCells = [];
  7. var playerPos = [];
  8. var computerPos = [];
  9.  
  10. $('.col').click(function() {
  11. isGameOver(PLAYER);
  12.  
  13. //Do Not Click Filled Div
  14. if ($(this).text().length !== 0) {
  15. return;
  16. }
  17.  
  18. //Save Player Moves
  19. playerPos.push([Number($(this).attr("data-x")), Number($(this).attr("data-y"))]);
  20.  
  21. //Make Click
  22. $(this).html(PLAYER);
  23.  
  24. //Clear Empty Cells To Fill Later!
  25. emptyCells = [];
  26.  
  27. //Let Computer Move
  28. computerMove();
  29. });
  30.  
  31. function computerMove(){
  32. isGameOver(COMPUTER);
  33.  
  34. //Save Computer Moves
  35. computerPos.push([Number($(this).attr("data-x")),Number($(this).attr("data-y"))]);
  36.  
  37. //Add All Empty Cells To An Array
  38. for(let x=1; x<=4; x++){
  39. for (let y=1; y<=4; y++) {
  40. if ($('.col[data-x='+x+'][data-y='+y+']').text().length == 0) {
  41. emptyCells.push($('.col[data-x='+x+'][data-y='+y+']'));
  42. }
  43. }
  44. }
  45.  
  46. //Click Random Empty Cell
  47. if (emptyCells.length > 0) {
  48. let computerChoice = emptyCells[Math.floor(Math.random() * emptyCells.length)];
  49. computerChoice.text(COMPUTER);
  50. }
  51. }
  52.  
  53. function isGameOver($req) {
  54.  
  55. //THIS IS WHERE I STUCK!
  56.  
  57. //Vertical Check
  58.  
  59. //Horizontal Check
  60.  
  61. //Diagonal Check
  62.  
  63. }
  64. });
  65.  
  66. <div class="row">
  67. <div class="col" data-x="1" data-y="1"></div>
  68. <div class="col" data-x="1" data-y="2"></div>
  69. <div class="col" data-x="1" data-y="3"></div>
  70. <div class="col" data-x="1" data-y="4"></div>
  71. </div>
  72.  
  73. <div class="row">
  74. <div class="col" data-x="2" data-y="1"></div>
  75. <div class="col" data-x="2" data-y="2"></div>
  76. <div class="col" data-x="2" data-y="3"></div>
  77. <div class="col" data-x="2" data-y="4"></div>
  78. </div>
  79.  
  80. <div class="row">
  81. <div class="col" data-x="3" data-y="1"></div>
  82. <div class="col" data-x="3" data-y="2"></div>
  83. <div class="col" data-x="3" data-y="3"></div>
  84. <div class="col" data-x="3" data-y="4"></div>
  85. </div>
  86.  
  87. <div class="row">
  88. <div class="col" data-x="4" data-y="1"></div>
  89. <div class="col" data-x="4" data-y="2"></div>
  90. <div class="col" data-x="4" data-y="3"></div>
  91. <div class="col" data-x="4" data-y="4"></div>
  92. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement