<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: davidmthompson</title>
    <description>The latest articles on DEV Community by davidmthompson (@davidmthompson).</description>
    <link>https://dev.to/davidmthompson</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1064774%2Fc965bac4-113c-4c9b-be68-fc45b06be1cb.png</url>
      <title>DEV Community: davidmthompson</title>
      <link>https://dev.to/davidmthompson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/davidmthompson"/>
    <language>en</language>
    <item>
      <title>Tic-Tac-Toe</title>
      <dc:creator>davidmthompson</dc:creator>
      <pubDate>Fri, 14 Apr 2023 16:24:51 +0000</pubDate>
      <link>https://dev.to/davidmthompson/tic-tac-toe-2ga</link>
      <guid>https://dev.to/davidmthompson/tic-tac-toe-2ga</guid>
      <description>&lt;p&gt;make a html document name index.html here is the code for it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Tic-Tac-Toe&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="Style.css"&amp;gt;
    &amp;lt;script src="Script.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt; Tic-Tak-Toe&amp;lt;/h1&amp;gt;
    &amp;lt;hr&amp;gt;
    &amp;lt;br&amp;gt;
    &amp;lt;!-- 3X3--&amp;gt;
    &amp;lt;div id="board"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next you will mack a style sheet with css This is the code for this page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body {
    font-family: Arial, Helvetica, sans-serif;
    text-align: center;
}

hr {
    width: 500px;
    height: 2px;
    background-color: black;
}


#board {
    width: 450px;
    height: 450px;
    /* background-color: gray; */
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
}

.tile {
    width: 147px;
    height: 147px;

    /* Text */
    font-size: 150px;
    display: flex;
    justify-content: center; 
    align-items: center;    
}

.winner {
    background-color: lightgray;
    color: red;
}


.horizontal-line {
    border-bottom: 3px solid black;
}

.vertical-line {
    border-right: 3px solid black;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now we will finish it with Javascript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var board;
var playerO = "O";
var playerX = "X";
var currPlayer = playerO;
var gameOver = false;

window.onload = function() {
    setGame();
}

function setGame() {
    board = [
                [' ', ' ', ' '],
                [' ', ' ', ' '],
                [' ', ' ', ' ']
            ]

    for (let r = 0; r &amp;lt; 3; r++) {
        for (let c = 0; c &amp;lt; 3; c++) {
            let tile = document.createElement("div");
            tile.id = r.toString() + "-" + c.toString();
            tile.classList.add("tile");
            if (r == 0 || r == 1) {
                tile.classList.add("horizontal-line");
            }
            if (c == 0 || c == 1) {
                tile.classList.add("vertical-line");
            }
            tile.innerText = "";
            tile.addEventListener("click", setTile);
            document.getElementById("board").appendChild(tile);
        }
    }
}

function setTile() {
    if (gameOver) {
        return;
    }

    let coords = this.id.split("-");    //ex) "1-2" -&amp;gt; ["1", "2'"]
    let r = parseInt(coords[0]);
    let c = parseInt(coords[1]);

    if (board[r][c] != ' ') { 
        //already taken spot
        return;
    }

    board[r][c] = currPlayer; //mark the board
    this.innerText = currPlayer; //mark the board on html

    //change players
    if (currPlayer == playerO) {
        currPlayer = playerX;
    }
    else {
        currPlayer = playerO;
    }

    //check winner
    checkWinner();
}


function checkWinner() {
    //horizontally, check 3 rows
    for (let r = 0; r &amp;lt; 3; r++) {
        if (board[r][0] == board[r][1] &amp;amp;&amp;amp; board[r][1] == board[r][2] &amp;amp;&amp;amp; board[r][0] != ' ') {
            //if we found the winning row
            //apply the winner style to that row
            for (let i = 0; i &amp;lt; 3; i++) {
                let tile = document.getElementById(r.toString() + "-" + i.toString());
                tile.classList.add("winner");
            }
            gameOver = true;
            return;
        }
    }

    //vertically, check 3 columns
    for (let c = 0; c &amp;lt; 3; c++) {
        if (board[0][c] == board[1][c] &amp;amp;&amp;amp; board[1][c] ==  board[2][c] &amp;amp;&amp;amp; board[0][c] != ' ') {
            //if we found the winning col
            //apply the winner style to that col
            for (let i = 0; i &amp;lt; 3; i++) {
                let tile = document.getElementById(i.toString() + "-" + c.toString());                
                tile.classList.add("winner");
            }
            gameOver = true;
            return;
        }
    }

    //diagonally
    if (board[0][0] == board[1][1] &amp;amp;&amp;amp; board[1][1] == board[2][2] &amp;amp;&amp;amp; board[0][0] != ' ') {
        for (let i = 0; i &amp;lt; 3; i++) {
            let tile = document.getElementById(i.toString() + "-" + i.toString());                
            tile.classList.add("winner");
        }
        gameOver = true;
        return;
    }

    //anti-diagonally
    if (board[0][2] == board[1][1] &amp;amp;&amp;amp; board[1][1] == board[2][0] &amp;amp;&amp;amp; board[0][2] != ' ') {
        //0-2
        let tile = document.getElementById("0-2");                
        tile.classList.add("winner");

        //1-1
        tile = document.getElementById("1-1");                
        tile.classList.add("winner");

        //2-0
        tile = document.getElementById("2-0");                
        tile.classList.add("winner");
        gameOver = true;
        return;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
