-
Notifications
You must be signed in to change notification settings - Fork 80
PTBC 1 - Quah Zheng Jie #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
quahzhengjie
wants to merge
3
commits into
rocketacademy:main
Choose a base branch
from
quahzhengjie:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,132 @@ | ||
// Please implement exercise logic here | ||
// keep data about the game in a 2-D array | ||
const board = [ | ||
['', '', ''], | ||
['', '', ''], | ||
['', '', ''], | ||
]; | ||
|
||
// the element that contains the rows and squares | ||
let boardElement; | ||
|
||
// the element that contains the entire board | ||
// we can empty it out for convenience | ||
let boardContainer; | ||
|
||
// current player global starts at X | ||
let currentPlayer = 'X'; | ||
|
||
// completely rebuilds the entire board every time there's a click | ||
const buildBoard = (board) => { | ||
// start with an empty container | ||
boardContainer.innerHTML = ''; | ||
boardElement = document.createElement('div'); | ||
boardElement.classList.add('board'); | ||
|
||
// move through the board data array and create the | ||
// current state of the board | ||
for (let i = 0; i < board.length; i += 1) { | ||
// separate var for one row / row element | ||
const row = board[i]; | ||
const rowElement = document.createElement('div'); | ||
rowElement.classList.add('row'); | ||
|
||
// set each square | ||
// j is the column number | ||
for (let j = 0; j < row.length; j += 1) { | ||
// one square element | ||
const square = document.createElement('div'); | ||
square.classList.add('square'); | ||
|
||
// set the text of the square according to the array | ||
square.innerText = board[i][j]; | ||
|
||
rowElement.appendChild(square); | ||
|
||
// set the click all over again | ||
// eslint-disable-next-line | ||
square.addEventListener('click', () => { | ||
squareClick(i, j); | ||
}); | ||
} | ||
|
||
// add a single row to the board | ||
boardContainer.appendChild(rowElement); | ||
} | ||
}; | ||
// switch the global values from one player to the next | ||
const togglePlayer = () => { | ||
if (currentPlayer === 'X') { | ||
currentPlayer = 'O'; | ||
} else { | ||
currentPlayer = 'X'; | ||
} | ||
}; | ||
const checkWin = (board) => { | ||
// check every position | ||
// we need to check that board[0][0] is not '' because if all 3 positions are empty, checkWin | ||
// will return true | ||
if ((board[0][0] !== '') && (board[0][0] === board[0][1] && board[0][1] === board[0][2])) { | ||
return true; | ||
} | ||
if ((board[1][0] !== '') && (board[1][0] === board[1][1] && board[1][1] === board[1][2])) { | ||
return true; | ||
} | ||
if ((board[2][0] !== '') && (board[2][0] === board[2][1] && board[2][1] === board[2][2])) { | ||
return true; | ||
} | ||
if ((board[0][0] !== '') && (board[0][0] === board[1][0] && board[1][0] === board[2][0])) { | ||
return true; | ||
} | ||
if ((board[0][1] !== '') && (board[0][1] === board[1][1] && board[1][1] === board[2][1])) { | ||
return true; | ||
} | ||
if ((board[0][2] !== '') && (board[0][2] === board[1][2] && board[1][2] === board[2][2])) { | ||
return true; | ||
} | ||
if ((board[0][0] !== '') && (board[0][0] === board[1][1] && board[1][1] === board[2][2])) { | ||
return true; | ||
} | ||
if ((board[2][0] !== '') && (board[2][0] === board[1][1] && board[1][1] === board[0][2])) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
//Player Actions | ||
const squareClick = (column, row) => { | ||
if (board[column][row] === '') { | ||
board[column][row] = currentPlayer; | ||
buildBoard(board); | ||
if (checkWin(board) === true) { | ||
// game over | ||
// message to tell user who has won | ||
messageContainer.innerText = `${currentPlayer} wins! Refresh page to reset game`; | ||
// set message to disappear and reset the game after 3 seconds | ||
setTimeout(() => { | ||
board = [ | ||
['', '', ''], | ||
['', '', ''], | ||
['', '', ''], | ||
]; | ||
buildBoard(board); | ||
messageContainer.innerText = 'click on a square to start'; | ||
}, 3000); | ||
} else { | ||
togglePlayer(); | ||
} | ||
} | ||
}; | ||
// create the board container element and put it on the screen | ||
const initGame = () => { | ||
messageContainer = document.createElement('div'); | ||
messageContainer.classList.add('message'); | ||
document.body.appendChild(messageContainer); | ||
|
||
boardContainer = document.createElement('div'); | ||
document.body.appendChild(boardContainer); | ||
|
||
// build the board - right now it's empty | ||
buildBoard(board); | ||
}; | ||
initGame() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
body { | ||
background-color: pink; | ||
} | ||
.square { | ||
padding: 10px; | ||
margin: 10px; | ||
background-color: white; | ||
display: inline-block; | ||
height: 10px; | ||
width: 10px; | ||
vertical-align: top; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let should be used here instead of const because board is reassigned in line 107