Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
1. After you have a general understanding find the `// @TODO` comments and fix the problems described.
1. Go to the HTML file and create all the rows you need for a complete Tic Tac Toe board.
1. When you've finished create a Pull Request (*PR*) on the original repo and turn in the URL of that PR.

1. could not find error.
*******

## Follow-Up Video
Expand Down
24 changes: 18 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@
<!-- link to CSS code -->
<link rel="stylesheet" href="tictactoe.css">
<!-- link to JavaScript code -->
<script src="scripts.js"></script>
<script src="scripts.js" defer></script>
</head>

<body>
<div class="container">
<div class="jumbotron">
<h1>Welcome to Tic Tac Toe</h1>
<p>Get ready to play!</p>
<!-- uses the built-in onclick attribute to call the resetBoard function -->
<button onclick="resetBoard()" type="button" class="btn btn-primary btn-lg" name="button">Restart</button>
<button onclick="resetBoard()" type="button" class="btn btn-primary btn-lg" name="button">Restart the Game</button>
</div>
<table>
<tr>
<!-- the onclick calls a function called "handleClick" and passes itself to it -->
<td id='top-left' onclick="handleClick(this)" ></td>
<td id='top-middle'></td>
<td></td>
</tr>
<td id='0-0' onclick="handleClick(this)"></td>
<td id='0-1' onclick="handleClick(this)"></td>
<td id='0-2' onclick="handleClick(this)"></td>

<tr>
<td id='1-0' onclick="handleClick(this)"></td>
<td id='1-1' onclick="handleClick(this)"></td>
<td id='1-2' onclick="handleClick(this)"></td>

<tr>
<td id='2-0' onclick="handleClick(this)"></td>
<td id='2-1' onclick="handleClick(this)"></td>
<td id='2-2' onclick="handleClick(this)"></td>
</tr>

<!-- @TODO create new rows and cells for the rest of your TTT board. -->
<!-- @TODO give each new cell an id & an event listener to call the "handleClick" function -->
</table>
Expand Down
78 changes: 60 additions & 18 deletions scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
// next to each @TODO you will find tasks that need to be finished

// The variable will change from X to O based on what player turn it is. We need to hold this so we can place an X or O on the board when they're clicked.
let board = [
["", "", ""],
["", "", ""],
["", "", ""]
]

let currentMarker = 'X'


Expand All @@ -16,6 +22,8 @@ let currentMarker = 'X'
// "this" is a special word in JS but "element" could have been "thing" or "el" or whatever we wanted it to be as long as we use it again in the "console.log" statement
const handleClick = (element) => {



// this uses the "log" method on the "console" to log out the element's id so we can see it with our human eyes
console.log(`The element you clicked on has an id: ${element.id}`)

Expand All @@ -29,35 +37,28 @@ const handleClick = (element) => {










// this function places the "currentMarker" inside the HTML element that was clicked and calls the "changeMarker" function.
const addMarker = (id) => {
const row = parseInt(id.charAt(0))
const column = parseInt(id.charAt(2))


// @TODO-1: Open the console tab in your Chrome Inspector Tool and click on the top-left square to see what's logged to the console.
console.log(`*** The current marker is: ${currentMarker}. ***`)
console.log(`Therefore, a "${currentMarker}" should be placed in the square with the id: ${id}`)

// @TODO-2: Build a line of code that will set the innerHTML property of the element that was clicked to the "currentMarker"

document.getElementById(id).innerHTML= currentMarker
board[row][column]= currentMarker
console.log(board)
checkForWin()
// @TODO-2.5: MIX & MATCH, You will need the following pieces of code to build that line:
// = currentMarker
// .getElementById(id)
// document
// .innerHTML

changeMarker()
}









Expand All @@ -78,9 +79,6 @@ const changeMarker = () => {






// This "resetBoard" function is called when the user clicks on the "Restart" button.
const resetBoard = () => {

Expand All @@ -94,6 +92,7 @@ const resetBoard = () => {
// document
// const

const squares= document.getElementsByTagName("TD")
// loops over the HTML Collection of TDs and clears out the Xs and Os
for (i=0; i < squares.length; i++) {

Expand All @@ -103,4 +102,47 @@ const resetBoard = () => {
// sets the innerHTML to null to replace the "X" or "O"
squares[i].innerHTML = null
}

}

const checkForWin= ()=>{
if (horizontalWin()|| verticleWin()|| diagonalWin()){
window.alert(`Player ${currentMarker} Won`)
}
else {
changeMarker()
}

}
const horizontalWin= ()=> {
if((board[0][0] == "X" && board[0][1] == "X" && board[0][2] == "X")
|| (board[0][0] == "O" && board[0][1] == "O" && board[0][2] == "O")
|| (board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X")
|| (board[1][0] == "O" && board[1][1] == "O" && board[1][2] == "O")
|| (board[2][0] == "X" && board[2][1] == "X" && board[2][2] == "X")
|| (board[2][0] == "O" && board[2][1] == "O" && board[2][2] == "O")
){
return true
}
}
const verticleWin= ()=> {
if((board[0][0] == "X" && board[1][0] == "X" && board[2][0] == "X")
|| (board[0][0] == "O" && board[1][0] == "O" && board[2][0] == "O")
|| (board[0][1] == "X" && board[1][1] == "X" && board[2][1] == "X")
|| (board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O")
|| (board[0][2] == "X" && board[1][2] == "X" && board[2][2] == "X")
|| (board[0][2] == "O" && board[1][2] == "O" && board[2][2] == "O")
){
return true
}
}

const diagonalWin= ()=> {
if((board[0][0] == "X" && board[1][1] == "X" && board[2][2] == "X")
|| (board[0][0] == "O" && board[1][1] == "O" && board[2][2] == "O")
|| (board[0][2] == "X" && board[1][1] == "X" && board[2][0] == "X")
|| (board[0][2] == "O" && board[1][1] == "O" && board[2][0] == "O")
){
return true
}
}
7 changes: 4 additions & 3 deletions tictactoe.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.jumbotron {
margin: 5% auto;
margin-left: 43%;
margin: 2% auto;
margin-left: 35%;
}

td {
Expand All @@ -13,4 +13,5 @@ td {

table {
margin: auto auto;
}
}