Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function()
- {
- const PLAYER = "X";
- const COMPUTER = "O";
- let game_status = true;
- var emptyCells = [];
- var playerPos = [];
- var computerPos = [];
- $('.col').click(function() {
- isGameOver(PLAYER);
- //Do Not Click Filled Div
- if ($(this).text().length !== 0) {
- return;
- }
- //Save Player Moves
- playerPos.push([Number($(this).attr("data-x")), Number($(this).attr("data-y"))]);
- //Make Click
- $(this).html(PLAYER);
- //Clear Empty Cells To Fill Later!
- emptyCells = [];
- //Let Computer Move
- computerMove();
- });
- function computerMove(){
- isGameOver(COMPUTER);
- //Save Computer Moves
- computerPos.push([Number($(this).attr("data-x")),Number($(this).attr("data-y"))]);
- //Add All Empty Cells To An Array
- for(let x=1; x<=4; x++){
- for (let y=1; y<=4; y++) {
- if ($('.col[data-x='+x+'][data-y='+y+']').text().length == 0) {
- emptyCells.push($('.col[data-x='+x+'][data-y='+y+']'));
- }
- }
- }
- //Click Random Empty Cell
- if (emptyCells.length > 0) {
- let computerChoice = emptyCells[Math.floor(Math.random() * emptyCells.length)];
- computerChoice.text(COMPUTER);
- }
- }
- function isGameOver($req) {
- //THIS IS WHERE I STUCK!
- //Vertical Check
- //Horizontal Check
- //Diagonal Check
- }
- });
- <div class="row">
- <div class="col" data-x="1" data-y="1"></div>
- <div class="col" data-x="1" data-y="2"></div>
- <div class="col" data-x="1" data-y="3"></div>
- <div class="col" data-x="1" data-y="4"></div>
- </div>
- <div class="row">
- <div class="col" data-x="2" data-y="1"></div>
- <div class="col" data-x="2" data-y="2"></div>
- <div class="col" data-x="2" data-y="3"></div>
- <div class="col" data-x="2" data-y="4"></div>
- </div>
- <div class="row">
- <div class="col" data-x="3" data-y="1"></div>
- <div class="col" data-x="3" data-y="2"></div>
- <div class="col" data-x="3" data-y="3"></div>
- <div class="col" data-x="3" data-y="4"></div>
- </div>
- <div class="row">
- <div class="col" data-x="4" data-y="1"></div>
- <div class="col" data-x="4" data-y="2"></div>
- <div class="col" data-x="4" data-y="3"></div>
- <div class="col" data-x="4" data-y="4"></div>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement