Browse Source

Initial commit

Kristian Vos 2 years ago
commit
aeb5e927c4
10 changed files with 338 additions and 0 deletions
  1. BIN
      Lato-Regular.ttf
  2. 3 0
      README.md
  3. 3 0
      font-awesome.min.css
  4. BIN
      fontawesome-webfont.ttf
  5. BIN
      fontawesome-webfont.woff
  6. BIN
      fontawesome-webfont.woff2
  7. 40 0
      index.html
  8. 1 0
      jquery.min.js
  9. 153 0
      script.js
  10. 138 0
      style.css

BIN
Lato-Regular.ttf


+ 3 - 0
README.md

@@ -0,0 +1,3 @@
+# Tic Tac Toe
+A small project created for a FreeCodeCamp zipline challenge.  
+Created July 8th, 2015, with a few tweaks applied later.  

File diff suppressed because it is too large
+ 3 - 0
font-awesome.min.css


BIN
fontawesome-webfont.ttf


BIN
fontawesome-webfont.woff


BIN
fontawesome-webfont.woff2


+ 40 - 0
index.html

@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="UTF-8" />
+        <title>Tic Tac Toe</title>
+        <link rel="stylesheet" href="./font-awesome.min.css" />
+        <link rel="stylesheet" href="./style.css" />
+    </head>
+    <body>
+        <!-- partial:index.partial.html -->
+        <div id="board">
+            <ul id="spot-ul">
+                <li id="s1" class="spot" data-x="0" data-y="0"></li>
+                <li id="s2" class="spot" data-x="0" data-y="1"></li>
+                <li id="s3" class="spot" data-x="0" data-y="2"></li>
+                <li id="s4" class="spot" data-x="1" data-y="0"></li>
+                <li id="s5" class="spot" data-x="1" data-y="1"></li>
+                <li id="s6" class="spot" data-x="1" data-y="2"></li>
+                <li id="s7" class="spot" data-x="2" data-y="0"></li>
+                <li id="s8" class="spot" data-x="2" data-y="1"></li>
+                <li id="s9" class="spot" data-x="2" data-y="2"></li>
+            </ul>
+        </div>
+
+        <div id="display">
+            <p id="state">State: Running</p>
+            Do you want to be X or O?
+            <small>You can only select this if the game isn't running</small>
+            <ul id="selection">
+                <li id="selection-x" class="selection selected">X</li>
+                <li id="selection-o" class="selection">O</li>
+            </ul>
+            Restart game
+            <div id="restart">Restart</div>
+        </div>
+        <!-- partial -->
+        <script src="./jquery.min.js"></script>
+        <script src="./script.js"></script>
+    </body>
+</html>

File diff suppressed because it is too large
+ 1 - 0
jquery.min.js


+ 153 - 0
script.js

@@ -0,0 +1,153 @@
+var board = [
+    [null, null, null],
+    [null, null, null],
+    [null, null, null],
+];
+var player = "X";
+var cpu = "O";
+var running = true;
+var ended = false;
+
+$(document).ready(function () {
+    $("#selection-x").on("click", function () {
+        if (!running) {
+            player = "X";
+            cpu = "O";
+            $(this).removeClass("selected");
+            $(this).addClass("selected");
+            $("#selection-o").removeClass("selected");
+        }
+    });
+
+    $("#selection-o").on("click", function () {
+        console.log(running);
+        if (!running) {
+            player = "O";
+            cpu = "X";
+            $(this).removeClass("selected");
+            $(this).addClass("selected");
+            $("#selection-x").removeClass("selected");
+        }
+    });
+
+    $("#restart").on("click", function () {
+        if (!ended) {
+            running = false;
+            $("#state").text("State: Stopped");
+            board = board.map(function (current, x) {
+                return current.map(function (current2, y) {
+                    var $li = $("ul").find(
+                        "[data-x='" + x + "'][data-y='" + y + "']"
+                    );
+                    $li.text("");
+                    return (current2 = null);
+                });
+            });
+            running = true;
+            $("#state").text("State: Running");
+        }
+    });
+
+    $(".spot").on("click", function () {
+        if (running) {
+            var $li = $(this);
+            var x = parseInt($li.data("x"));
+            var y = parseInt($li.data("y"));
+            var $X = $('<i class="fa fa-times fa-5x"></i>');
+            var $O = $('<i class="fa fa-circle-o fa-5x"></i>');
+            if (board[x][y] === null) {
+                board[x][y] = player;
+                var $symbol = player === "X" ? $X : $O;
+                $symbol.appendTo($li);
+
+                if (!check()) {
+                    bot();
+                } else {
+                    endGame();
+                }
+            }
+        }
+    });
+});
+
+function endGame() {
+    running = false;
+    $("#state").text("State: Stopped");
+    ended = true;
+    $(".spot").css({
+        color: "purple",
+    });
+    setTimeout(function () {
+        board = board.map(function (current, x) {
+            return current.map(function (current2, y) {
+                var $li = $("ul").find(
+                    "[data-x='" + x + "'][data-y='" + y + "']"
+                );
+                $li.text("");
+                return (current2 = null);
+            });
+        });
+        running = true;
+        $("#state").text("State: Running");
+        $(".spot").removeAttr("style");
+        ended = false;
+    }, 5000);
+}
+
+function bot() {
+    var X = Math.floor(Math.random() * 3);
+    var Y = Math.floor(Math.random() * 3);
+    if (board[X][Y] !== null) {
+        bot();
+    } else {
+        board[X][Y] = cpu;
+        var $li = $("ul").find("[data-x='" + X + "'][data-y='" + Y + "']");
+        var $X = $('<i class="fa fa-times fa-5x"></i>');
+        var $O = $('<i class="fa fa-circle-o fa-5x"></i>');
+        var $symbol = cpu === "X" ? $X : $O;
+        $symbol.appendTo($li);
+
+        if (check()) {
+            endGame();
+        }
+    }
+}
+
+function check() {
+    var boardFull = true;
+    for (var i = 0; i < board.length; i++) {
+        if (board[i].indexOf(null) > -1) {
+            boardFull = false;
+        }
+        if (
+            board[i][0] !== null &&
+            board[i][0] === board[i][1] &&
+            board[i][1] === board[i][2]
+        ) {
+            return true;
+        }
+        if (
+            board[0][i] !== null &&
+            board[0][i] === board[1][i] &&
+            board[1][i] === board[2][i]
+        ) {
+            return true;
+        }
+    }
+    if (
+        board[0][0] !== null &&
+        board[0][0] === board[1][1] &&
+        board[1][1] === board[2][2]
+    ) {
+        return true;
+    }
+    if (
+        board[0][2] !== null &&
+        board[0][2] === board[1][1] &&
+        board[1][1] === board[2][0]
+    ) {
+        return true;
+    }
+
+    return boardFull;
+}

+ 138 - 0
style.css

@@ -0,0 +1,138 @@
+@font-face {
+    font-family: "Lato";
+    src: URL("Lato-Regular.ttf") format("truetype");
+}
+
+body {
+    background-color: lightgray;
+    font-family: "Lato", sans-serif;
+}
+
+#board {
+    width: 500px;
+    height: 500px;
+    background-color: #1a44c4;
+    margin-right: auto;
+    margin-left: auto;
+    margin-top: 50px;
+    padding: 50px;
+    border-radius: 10px;
+}
+
+#spot-ul {
+    list-style: none;
+    padding: 0px;
+    height: 500px;
+    width: 500px;
+    line-height: 166.6666666px;
+    text-align: center;
+    margin: 0;
+}
+
+.spot {
+    height: 163.33333px;
+    width: 163.33333px;
+    float: left;
+    color: white;
+    font-size: 32px;
+}
+
+#s1 {
+    border-right: solid 5px #ff8800;
+    border-bottom: solid 5px #ff8800;
+}
+
+#s2 {
+    border-right: solid 5px #ff8800;
+    border-bottom: solid 5px #ff8800;
+}
+
+#s3 {
+    border-bottom: solid 5px #ff8800;
+}
+
+#s4 {
+    border-right: solid 5px #ff8800;
+    border-bottom: solid 5px #ff8800;
+}
+
+#s5 {
+    border-right: solid 5px #ff8800;
+    border-bottom: solid 5px #ff8800;
+}
+
+#s6 {
+    border-bottom: solid 5px #ff8800;
+}
+
+#s7 {
+    border-right: solid 5px #ff8800;
+}
+
+#s8 {
+    border-right: solid 5px #ff8800;
+}
+
+.fa {
+    cursor: default;
+}
+
+#display {
+    margin-top: 50px;
+    margin-bottom: 50px;
+    height: 350px;
+    width: 200px;
+    background-color: lightblue;
+    margin-right: auto;
+    margin-left: auto;
+    padding: 20px;
+    border: 5px solid turquoise;
+    text-align: center;
+    border-radius: 10px;
+}
+
+#selection {
+    list-style: none;
+    padding: 0;
+    width: 200px;
+    height: 60px;
+}
+
+.selection {
+    display: inline-block;
+    width: 88px;
+    float: left;
+    margin-right: 5px;
+    margin-left: 5px;
+    background-color: #55fa6e;
+    padding-bottom: 20px;
+    padding-top: 20px;
+    border-radius: 10px;
+    cursor: pointer;
+}
+
+.selected {
+    border: 2px solid red;
+}
+
+#restart {
+    width: 70%;
+    margin-right: 15%;
+    margin-left: 15%;
+    padding-top: 20px;
+    padding-bottom: 20px;
+    background-color: #ff7a7a;
+    border-radius: 10px;
+    margin-top: 10px;
+    cursor: pointer;
+}
+
+#state {
+    width: 90%;
+    padding-top: 20px;
+    padding-bottom: 20px;
+    background-color: orange;
+    color: white;
+    margin-right: 5%;
+    margin-left: 5%;
+}

Some files were not shown because too many files changed in this diff