Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="topSection">
  <h1 class="title">Lazy Robot and Humans</h1>
  <h5 class="subtitle">play noughts and crosses</h5>
</div>
<div class="messageDisplayBox">
  <h2 class="messageDisplay">Let's play!</h2>
</div>
<div class="mainSection">
  <h1 class="choosePlayers">One Robot
    <a class="oneOrTwo">
                <i class="fas fa-toggle-on"></i>
            </a> Two Humans</h1>
  <h1>
    U Human is: X
    <a class="switchPlayerMarks">
                <i class="fas fa-toggle-off"></i>
            </a> O</h1>
  <div class="controlPlayers">
    <h1 class="playerOneMessage">Human 1</h1>
    <h1 class="playerTwoMessage">Human 2</h1>
  </div>
  <div class="board">
    <div class="row rowTop">
      <h2 class="box " id="box0"></h2>
      <h2 class="box columnMiddle" id="box1"></h2>
      <h2 class="box" id="box2"></h2>
    </div>
    <div class="row rowMiddle">
      <h2 class="box" id="box3"></h2>
      <h2 class="box columnMiddle" id="box4"></h2>
      <h2 class="box" id="box5"></h2>
    </div>
    <div class="row rowBottom">
      <h2 class="box" id="box6"></h2>
      <h2 class="box columnMiddle" id="box7"></h2>
      <h2 class="box" id="box8"></h2>
    </div>
  </div>
  <div class="controlSettings">
    <h1>
      Reset Game
      <a class="resetGame">
                    <i class="fas fa-undo"></i>
                </a>
    </h1>
  </div>
</div>
<h1 class="myMark">An app by Tom Corbin</h1>
              
            
!

CSS

              
                /* font families */
/* font-family: 'Arimo', sans-serif; */
@import url("https://fonts.googleapis.com/css?family=Arimo");

/* CSS Variables */
:root {
  --yellow: #f7b733;
  --orange: #ff3b3d;
  --apricot: #f7882f;
  --blue: #4abdac;
  --blueGrey: #6e7a94;
  --silver: #f3f1f5;
  --sky: #cceaf4;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
  font-family: "Arimo", sans-serif;
  font-size: 10px;
  background: var(--silver);
  margin: 20px 0;
}

/* Top section */
.topSection {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 280px;
  background: var(--sky);
  box-shadow: 2px 2px 4px var(--blueGrey);
  border: 1px solid var(--blueGrey);
  border-radius: 40px 20px;
  margin: 2px;
}

.title {
  color: var(--apricot);
  text-shadow: 1px 0px var(--orange);
  font-size: 2em;
  padding: 1px;
  margin: 1px;
}

.subtitle {
  color: var(--blue);
  text-shadow: 1px 1px var(--blueGrey);
  font-size: 1.5em;
  padding: 2px;
  margin: 0 0 2px 0;
}

.messageDisplayBox {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 280px;
  height: 50px;
  background: var(--yellow);
  box-shadow: 2px 2px 4px var(--blueGrey);
  text-align: center;
  padding: 1px;
  border: 1px solid var(--blueGrey);
  border-radius: 20px 40px;
  margin: 1px;
}

.messageDisplay {
  width: 250px;
  color: var(--blueGrey);
  text-shadow: 1px 0px var(--sky);
  font-size: 1.75em;
  padding: 2px;
  margin: 1px;
}

/* Main section */
.mainSection {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: var(--blueGrey);
  text-shadow: 2px 0px var(--blue);
}

svg {
  color: var(--blue);
}

svg:hover {
  color: var(--orange);
}

svg:active {
  color: var(--yellow);
}

.choosePlayers {
  display: flex;
  justify-content: space-around;
  width: 260px;
  font-size: 2em;
  padding: 1px;
  margin: 3px;
}

.controlPlayers {
  display: flex;
  justify-content: space-around;
  text-align: center;
  width: 280px;
  padding: 1px;
  margin: 1px;
}

.playerOneMessage,
.playerTwoMessage {
  width: 120px;
  color: var(--apricot);
  text-shadow: 1px 1px var(--orange);
  background: var(--sky);
  padding: 5px;
  border: 1px solid var(--blueGrey);
  box-shadow: 2px 2px 4px var(--blueGrey);
  border-radius: 30px 15px;
  margin: 1px;
}

#playerActive {
  color: var(--blueGrey);
  text-shadow: 1px 1px var(--sky);
  background: var(--yellow);
  box-shadow: 2px 2px 4px var(--blue);
  text-transform: uppercase;
}

.board {
  display: flex;
  flex-direction: column;
  color: var(--orange);
  font-size: 3em;
  text-align: center;
  margin: 5px 0;
}

.row {
  display: flex;
}

.rowMiddle {
  border-top: 2px solid var(--yellow);
  border-bottom: 2px solid var(--yellow);
}

.columnMiddle {
  border-left: 2px solid var(--yellow);
  border-right: 2px solid var(--yellow);
}

.box {
  min-width: 75px;
  min-height: 75px;
  font-size: 2em;
  padding: 10px;
  margin: 0;
}

.controlSettings {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  width: 200px;
  font-size: 1em;
  padding: 1px;
  margin: 1px;
}

h1 {
  margin: 0;
}

.myMark {
  color: var(--apricot);
  margin: 5px;
}
              
            
!

JS

              
                "use strict";

// Initial settings
let gameIsActive = false;
let playerOneIsX = true;
let playerOneIsActive = true;
let robot = false;
let selector;
let winner = false;
let countBoxesMarked = 0;
let p1 = "X";
let p2 = "O";
const originalPositions = "012345678";
let position = Array.from(originalPositions);
let numberFromIdOfThis;
const toggleOn = ` <i class="fas fa-toggle-on"></i>`;
const toggleOff = ` <i class="fas fa-toggle-off"></i>`;
const messageDisplay = document.querySelector(".messageDisplay");
const originalMessage = messageDisplay.textContent;
const oneOrTwo = document.querySelector(".oneOrTwo");
let randomNumber;
const playerOneMessage = document.querySelector(".playerOneMessage");
const playerTwoMessage = document.querySelector(".playerTwoMessage");
const switchPlayerMarks = document.querySelector(".switchPlayerMarks");
const resetGame = document.querySelector(".resetGame");
const boxes = document.querySelectorAll(".box");
oneOrTwo.addEventListener("click", selectPlayers);
switchPlayerMarks.addEventListener("click", playersSwopXO);
resetGame.addEventListener("click", reset);
boxes.forEach(val => val.addEventListener("click", markBox));

// Control functions
function lazyRobotNumber() {
  randomNumber = Math.floor(Math.random() * 9); // random between 0 and 8
}

function goLazyRobot() {
  lazyRobotNumber();
  if (position[randomNumber] === "X" || position[randomNumber] === "O") {
    goLazyRobot();
  } else {
    markBox();
  }
}

function selectPlayers() {
  if (
    gameIsActive === false &&
    oneOrTwo.innerHTML.includes("fas fa-toggle-on")
  ) {
    robot = true;
    oneOrTwo.innerHTML = toggleOff;
    playerOneMessage.textContent = "U Human";
    playerTwoMessage.textContent = "I Robot";
  } else if (gameIsActive === false) {
    robot = false;
    oneOrTwo.innerHTML = toggleOn;
    playerOneMessage.textContent = "Human 1";
    playerTwoMessage.textContent = "Human 2";
  } else {
    console.log(
      "Human error: Robot unable to change players when game is active."
    );
  }
}

function playersSwopXO() {
  if (robot === true && gameIsActive === false && playerOneIsX === true) {
    p1 = "O";
    p2 = "X";
    playerOneIsX = false;
    playerOneIsActive = false;
    switchPlayerMarks.innerHTML = toggleOn;
    goLazyRobot();
  } else if (
    robot === true &&
    gameIsActive === false &&
    playerOneIsX === false
  ) {
    p1 = "X";
    p2 = "O";
    playerOneIsX = true;
    playerOneIsActive = true;
    switchPlayerMarks.innerHTML = toggleOff;
  } else {
    console.log(
      "Human error: Robot unable to switch marks when robot is not active nor when game is active."
    );
  }
}

function reset() {
  // do not reset robot
  // no need to reset selector nor randomNumber
  gameIsActive = false;
  playerOneIsX = true;
  playerOneIsActive = true;
  winner = false;
  countBoxesMarked = 0;
  p1 = "X";
  p2 = "O";
  position = Array.from(originalPositions);
  playerOneMessage.removeAttribute("id");
  playerTwoMessage.removeAttribute("id");
  messageDisplay.textContent = originalMessage;
  switchPlayerMarks.innerHTML = toggleOff;
  boxes.forEach(val => (val.textContent = ""));
}

// Board functions

function checkWinner() {
  if (
    (position[0] === position[1] && position[0] === position[2]) ||
    (position[3] === position[4] && position[3] === position[5]) ||
    (position[6] === position[7] && position[6] === position[8]) ||
    (position[0] === position[3] && position[0] === position[6]) ||
    (position[1] === position[4] && position[1] === position[7]) ||
    (position[2] === position[5] && position[2] === position[8]) ||
    (position[0] === position[4] && position[0] === position[8]) ||
    (position[6] === position[4] && position[6] === position[2])
  ) {
    winner = true;
    if (robot === false) {
      playerOneIsActive === true
        ? (messageDisplay.textContent = "Human 1 wins!")
        : (messageDisplay.textContent = "Human 2 wins!");
    } else {
      playerOneIsActive === true
        ? (messageDisplay.textContent =
            "U Human wins, and I Robot kens random sadness.")
        : (messageDisplay.textContent = "Lazy Robot wins random game, human!");
    }
    playerOneMessage.removeAttribute("id");
    playerTwoMessage.removeAttribute("id");
  }
}

function changeActivePlayer() {
  if (playerOneIsActive === false) {
    playerOneMessage.setAttribute("id", "playerActive");
    playerTwoMessage.removeAttribute("id");
    playerOneIsActive = true;
  } else {
    playerOneMessage.removeAttribute("id");
    playerTwoMessage.setAttribute("id", "playerActive");
    playerOneIsActive = false;
    if (robot === true) {
      goLazyRobot();
    }
  }
}

function markBox() {
  gameIsActive = true;

  robot === true
    ? (messageDisplay.textContent =
        "Danger! Random play is in progress, human.")
    : (messageDisplay.textContent = "Game is active, humans.");

  if (robot === true && playerOneIsActive === false) {
    position[randomNumber] = p2;
    selector = `#box${randomNumber}`;
    document.querySelector(selector).textContent = p2;
    updateGameStatus();
  } else if (this.textContent === "") {
    // remove "box" from id name "#box1" etc. helps enable robot one person game
    numberFromIdOfThis = this.getAttribute("id").slice(-1);
    if (playerOneIsActive === true) {
      this.textContent = p1;
      position[numberFromIdOfThis] = p1;
    } else if (playerOneIsActive === false) {
      this.textContent = p2;
      position[numberFromIdOfThis] = p2;
    }
    updateGameStatus();
  } else {
    console.log("Human error: This box is already ticked.");
  }
}

function updateGameStatus() {
  countBoxesMarked += 1;
  checkWinner();
  if (winner === true) {
    window.setTimeout(reset, 3000);
  } else if (countBoxesMarked === 9) {
    messageDisplay.textContent = "Robot's Cat's Game!";
    playerOneMessage.removeAttribute("id");
    playerTwoMessage.removeAttribute("id");
    window.setTimeout(reset, 3000);
  } else {
    changeActivePlayer();
  }
}

              
            
!
999px

Console