css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">
  <div class="board">
    <div class="cell" data-x="0" data-y="0"></div>
    <div class="cell" data-x="0" data-y="1"></div>
    <div class="cell" data-x="0" data-y="2"></div>
    <div class="cell" data-x="1" data-y="0"></div>
    <div class="cell" data-x="1" data-y="1"></div>
    <div class="cell" data-x="1" data-y="2"></div>
    <div class="cell" data-x="2" data-y="0"></div>
    <div class="cell" data-x="2" data-y="1"></div>
    <div class="cell" data-x="2" data-y="2"></div>
  </div>
  <footer>
    <div class="Reset">
  <button type="button" class="btn btn-outline-info reset">Reset</button>
  </div>
  </footer>
 </div> 
  
  
    <!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-body">
        <button type="button" class="btn btn-outline-primary modal-btn btn-lg">X</button>
        <span id="or-span">or</span>
        <button type="button" class="btn btn-outline-primary modal-btn btn-lg">O</button>
      </div>
    </div>
  </div>
</div>










            
          
!
            
              body {
  background: black;
}

.board {
  margin: 0 auto;
  width: 300px;
  display: flex;
  flex-wrap: wrap;
  position: relative;
  top: 100px;
  display: none;
}

.cell {
  background-color: black;
  border: 1px solid blue;
  height: 98px;
  width: 98px;
  text-align: center;
  vertical-align: middle;
  line-height: 98px;
  font-size: 40px;
  color: orange;
}


#or-span {
  margin-right: 50px;
  margin-left: 50px;
}

.modal-content {
  border-radius: 10px;
  background-color: black;
  text-align: center;
  color: white;
  font-size: 20px;
}

.modal-btn {
  font-size: 24px;
}

.modal-btn:hover {
  border-color: orange;
  color: orange;
  background-color: black;
}

.alert {
  float: center;
  margin: 0 auto;
  position: relative;
  text-align: center;
  display: none;
  width: 50%;
}



footer {
  margin: 0 auto;
  width: 300px;
  position: relative;
  top: 100px;
  text-align: center;
}

.reset {
  margin: 0 auto;
  margin-top: 10px;
  display: none;
}


            
          
!
            
              "use strict";
/**
class Cell
Constructor:
@this.status: Represents a 'X' or 'O' item, or '' for empty
Methods:
-play(move): Updates the status for the cell to the 'X' or 'O' move
@move: 'X' or 'O' move to update 
-isEmpty(): Checks if a particular cell is empty ('')
*/
class Cell {
  constructor() {
    this.status = "";
  }
  play(move) {
    this.status = move;
  }
  isEmpty() {
    return this.status === "";
  }
}
/**
class Board
Constructor:
@this.cells: 2-Dimensional Array representing all the class Cells
Methods:
-play(x,y): Takes in an X or Y position and runs the 'play' function from 'class Cells' to update the status at that cells position
@x,y: x/y coordinate for the 2D array of cells
-getEmptyCells(): Returns an array of all empty [X,Y] coordinates IE: [[0,2][1,1]] etc....
*/
class Board {
  constructor() {
    this.cells = [];
    for (let i = 0; i < 3; i++) {
      this.cells[i] = [];
      for (let j = 0; j < 3; j++) {
        this.cells[i][j] = new Cell();
      }
    }
    this.turn = "X";
  }
  play(x, y) {
    this.cells[x][y].play(this.turn);
  }
  getEmptyCells() {
    let empties = [];
    this.cells.forEach(function(line, X, array) {
      line.forEach(function(item, Y, arrayItem) {
        if (item.status === "") {
          empties.push([X, Y]);
        }
      });
    });
    return empties;
  }
}
/**
class Board
Methods:
-render(board): Updates the DOM object that represents the board
@board: board class initialization that represents the Board class
*/
class BoardView {
  render(board) {
    board.cells.forEach(function(line, X, array) {
      line.forEach(function(item, Y, arrayItem) {
        if (item.status === "X") {
          $("[data-x=" + X + "][data-y=" + Y + "]").text("X");
        } else if (item.status === "O") {
          $("[data-x=" + X + "][data-y=" + Y + "]").text("O");
        }
      });
    });
  }
}
/**
class BoardController
Constructor:
@this.player: player item object ('X' or 'O')
@this.comp: comp item object ('X' or 'O')
@this.round: round that has occured
Event:
$("div.cell").click: click handler for each cell item (for player), on click this should 'attempt' to run the players turn and move on that object
Methods:
-checkWinPlayer(entry)
@entry: which player to check on, ie BoardController.player or BoardController.comp object ('X' or 'O'). Returns True or False if game is won for that player
*/
class BoardController {
  constructor(player, comp) {
    this.player = null;
    this.comp = null;
    this.round = 0;

    $("div.cell").click(ev => {
      if (
        board.cells[$(ev.target).data("x")][$(ev.target).data("y")].isEmpty()
      ) {
        if (board.turn === this.player) {
          board.play($(ev.target).data("x"), $(ev.target).data("y"));
          view.render(board);
          controller.round++;
          if(this.checkWinPlayer(this.player))
            {
              alert("Player Wins!, hit reset to start again!");
            }
          else
            {
              board.turn = this.comp;
              Game();
            }
          
        }
      } else {
        alert("That space is taken!!!");
      }
    });
  }

  checkWinPlayer(entry) {
    if (
      board.cells[0][0].status == entry &&
      board.cells[0][1].status == entry &&
      board.cells[0][2].status == entry
    ) {
      return true;
    } else if (
      board.cells[1][0].status == entry &&
      board.cells[1][1].status == entry &&
      board.cells[1][2].status == entry
    ) {
      return true;
    } else if (
      board.cells[2][0].status == entry &&
      board.cells[2][1].status == entry &&
      board.cells[2][2].status == entry
    ) {
      return true;
    } else if (
      board.cells[0][0].status == entry &&
      board.cells[1][0].status == entry &&
      board.cells[2][0].status == entry
    ) {
      return true;
    } else if (
      board.cells[0][1].status == entry &&
      board.cells[1][1].status == entry &&
      board.cells[2][1].status == entry
    ) {
      return true;
    } else if (
      board.cells[0][2].status == entry &&
      board.cells[1][2].status == entry &&
      board.cells[2][2].status == entry
    ) {
      return true;
    } else if (
      board.cells[0][0].status == entry &&
      board.cells[1][1].status == entry &&
      board.cells[2][2].status == entry
    ) {
      return true;
    } else if (
      board.cells[2][0].status == entry &&
      board.cells[1][1].status == entry &&
      board.cells[0][2].status == entry
    ) {
      return true;
    } else return false;
  }
}
  let board = new Board();
  let controller = new BoardController("X", "O");
  let view = new BoardView("board");
$(document).ready(function() {
  Init();
});

/**
function Init()
- Initialize class instances
*/
function Init() {
  //Initalize Classes
  //Initialize Reset Button Handler
  $(".reset").click(ev => {
    Reset();
  });
  //Initialize 'X' or 'O' button handlers
  $(".modal-btn").click(ev => {
    $("#myModal").modal("hide");
    $(".reset").fadeIn(7000).css("display", "block");
    $(".board").fadeIn(5000).css("display", "flex");
    if ($(ev.target).text() == "X") {
      controller.player = "X";
      controller.comp = "O";
      board.turn = "O";
      Game();
    } else {
      controller.player = "O";
      controller.comp = "X";
      board.turn = "X";
      Game();
    }
  });
  //Show 'X' or 'O' modal picker
  $("#myModal").modal("show");
}

function compTurn() {
  //Attempt to Move to center
  if (board.cells[1][1].isEmpty()) {
    board.play(1, 1);
    //Attempt Corners
  } else if (board.cells[0][0].isEmpty()) {
    board.play(0, 0);
  } else if (board.cells[0][2].isEmpty()) {
    board.play(0, 2);
  } else if (board.cells[2][0].isEmpty()) {
    board.play(2, 0);
  } else if (board.cells[2][2].isEmpty()) {
    board.play(2, 2);
  } else if (board.cells[0][1].isEmpty()) {
    //Attempt Sides
    board.play(0, 1);
  } else if (board.cells[1][0].isEmpty()) {
    board.play(1, 0);
  } else if (board.cells[1][2].isEmpty()) {
    board.play(1, 2);
  } else if (board.cells[2][1].isEmpty()) {
    board.play(2, 1);
  }
  
}

function Game() {
  //If a tie hasn't occured and it's the computers turn
      compTurn();
      view.render(board);
      controller.round++;
      if(controller.round === 9)
        {
          alert("Tie Game, reset to go again!")
        }
      if (controller.checkWinPlayer(controller.comp)) {
        alert("Computer Wins!, hit reset to start again!");
      }
      else
        {
          board.turn = controller.player;
        }
}

function Reset() {
  controller.round = 0;
  for (let i = 0; i < 3; i++) {
    for (let j = 0; j < 3; j++) {
      board.cells[i][j].status = "";
    }
  }
  $(".cell").text("");
  board.turn = controller.comp
  Game();
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console