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
44 changes: 42 additions & 2 deletions dom-tictactoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const handleClick = (element) => {

const addMarker = (id) => {
console.log(`We'll place a mark on square: ${id}`)
document.getElementById(id).innerHTML = currentMarker
// @TODO, Mix & Match.
// You will need the following pieces:

Expand All @@ -45,12 +46,12 @@ const updateBoard = (id) => {
// parses the id string into a number then captures the first and last part the newly create number as row & column
const row = parseInt(id.charAt(0))
const column = parseInt(id.charAt(2))

console.log(`you clicked the sq at ${row} and ${column}`)
console.log(board)

// @TODO, Your code here: use the above information to change the board variable(array of arrays)
// HINT: in your browser open up the dev tools -> console
board[row][column] = currentMarker
}

const checkForWin = () => {
Expand All @@ -63,17 +64,50 @@ const checkForWin = () => {
changeMarker()
}
}
// if(horizontalWin()|| verticalWin() || diagonalWin()) {
// console.log(`Player ${playerTurn} won!`)
// return true
// } 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")) {
return true
} else if ((board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X") || (board[1][0] == "O" && board[1][1] == "O" && board[1][2] == "O")) {
return true
} else if ((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
} else {
return false
}
// @TODO, Your code here: to check for horizontal wins
}

const verticalWin = () => {
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")) {
return true
} else if ((board[0][1] == "X" && board[1][1] == "X" && board[2][1] == "X") || (board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O")) {
return true
} else if((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
} else {
return false
}
// @TODO, Your code here: to check for vertical wins
}

const diagonalWin = () => {
// @TODO, Your code here: to check for diagonal wins
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")) {
return true
} else if((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
} else {
return false
}
// @TODO, Your code here: to check for diagonal win

}

const changeMarker = () => {
Expand All @@ -92,6 +126,12 @@ const resetBoard = () => {
for (i=0; i<squares.length; i++) {
console.log(squares[i])
squares[i].innerHTML = null
board = [
['','',''],
['','',''],
['','','']
];

}

// @TODO, Your code here: make sure to reset the array of arrays to empty for a new game
Expand Down
43 changes: 43 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const rl = readline.createInterface({
output: process.stdout
});

//start here//

// creates and empty "board" for the user to see where marks can be placed.
// using let because the variable is expected to change with more 'X's and 'O's to add
let board = [
Expand All @@ -34,25 +36,66 @@ const printBoard = () => {

const horizontalWin = () => {
// Your code here to check for horizontal wins
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")) {
return true
} else if ((board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X") || (board[1][0] == "O" && board[1][1] == "O" && board[1][2] == "O")) {
return true
} else if ((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
} else {
return false
}
}

const verticalWin = () => {
// Your code here to check for vertical wins
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")) {
return true
} else if ((board[0][1] == "X" && board[1][1] == "X" && board[2][1] == "X") || (board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O")) {
return true
} else if((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
} else {
return false
}
}

const diagonalWin = () => {
// Your code here to check for diagonal wins
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")) {
return true
} else if((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
} else {
return false
}
}

const checkForWin = () => {
// Your code here call each of the check for types of wins
if(horizontalWin()|| verticalWin() || diagonalWin()) {
console.log(`Player ${playerTurn} won!`)
return true
} else {
changeMarker()
}
}

const ticTacToe = (row, column) => {
board[row][column] = playerTurn
changeMarker()
// Your code here to place a marker on the board
// then check for a win
}

const changeMarker = () => {
if(playerTurn === "X"){
playerTurn = "O"
} else {
playerTurn = "X"
}
}

const getPrompt = () => {
printBoard();
console.log("It's Player " + playerTurn + "'s turn.");
Expand Down