Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b306084
ran npm install and start to see local host
marks214 Dec 29, 2020
d2fe50a
Update tests to reflect requirements for waves
roshni-patel Dec 29, 2020
37a19d1
Merge branch 'master' into wave1
marks214 Dec 29, 2020
a8e41ee
added the onButtonClick arrow function to enable the Square component…
marks214 Dec 29, 2020
7a33d12
generateSquareComponents returns a 1D array of square components
marks214 Dec 29, 2020
2384636
psuedo code for wave 2
marks214 Dec 29, 2020
bc9b397
Merge pull request #1 from roshni-patel/wave1
roshni-patel Dec 29, 2020
115be3d
Merge branch 'master' into AAO/wave2
marks214 Dec 29, 2020
a4952ef
more psuedo code, from watching Devin's nested events video
marks214 Dec 29, 2020
be2a0f5
first x appears
marks214 Dec 30, 2020
00e5456
changed conditional logic, switching player turns is now working
marks214 Dec 30, 2020
f7e5064
Fixed a few failing tests
roshni-patel Dec 30, 2020
2d716ec
Merge pull request #2 from roshni-patel/AAO/wave2
roshni-patel Dec 30, 2020
ca017f9
minor changes, removed comments/changed syntax/style
marks214 Dec 30, 2020
331a54e
Merge branch 'master' into AAO/wave2
marks214 Dec 30, 2020
d6321ec
added psuedocode to check for wins
marks214 Dec 30, 2020
2d86b3e
Merge pull request #3 from roshni-patel/AAO/wave2
roshni-patel Dec 31, 2020
216739c
set winner is not working
marks214 Dec 31, 2020
631bd6b
check winner is now working, game stops when a winner is declared
marks214 Dec 31, 2020
efc759e
tests unskipped and passing for wave 3 - checks winner
marks214 Dec 31, 2020
1b3c666
Merge pull request #4 from roshni-patel/AAO/wave3
roshni-patel Dec 31, 2020
02feaea
reset game is working
marks214 Dec 31, 2020
9c8dab4
displays current player and correct winner
marks214 Dec 31, 2020
6eef0a0
added code to handle ties - used conditional rendering
marks214 Dec 31, 2020
a19a5d8
Merge pull request #5 from roshni-patel/wave4
marks214 Dec 31, 2020
bffcf6a
Unskipped a test
roshni-patel Dec 31, 2020
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
36,009 changes: 36,009 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

114 changes: 90 additions & 24 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import './App.css';

import Board from './components/Board';

const PLAYER_1 = 'X';
const PLAYER_2 = 'O';
const PLAYER_1 = 'x';
const PLAYER_2 = 'o';

const generateSquares = () => {
const squares = [];

let currentId = 0;

for (let row = 0; row < 3; row += 1) {
for (let row = 0; row < 3; row ++) {
squares.push([]);
for (let col = 0; col < 3; col += 1) {
for (let col = 0; col < 3; col ++) {
squares[row].push({
id: currentId,
value: '',
Expand All @@ -27,45 +27,111 @@ const generateSquares = () => {

const App = () => {

// This starts state off as a 2D array of JS objects with
// empty value and unique ids.
const [squares, setSquares] = useState(generateSquares());
const [turnNumber, setTurnNumber] = useState(0);
const [winner, setWinner] = useState('');
const [player, setPlayer] = useState(PLAYER_1);

// Wave 2
// You will need to create a method to change the square
// When it is clicked on.
// Then pass it into the squares as a callback
const onClickCallback = (id) => {
if (winner === '') {
for (let row = 0; row < 3; row += 1) {
for (let col = 0; col < 3; col += 1) {
if (squares[row][col].id === id && squares[row][col].value === '') {
squares[row][col].value = player;
player === PLAYER_1? setPlayer(PLAYER_2) : setPlayer(PLAYER_1);
setTurnNumber(turnNumber + 1);
}
}
}
setSquares(squares);

if (turnNumber > 3) {
setWinner(checkForWinner());
}

}
}

const checkForWinner = () => {
// Complete in Wave 3
// You will need to:
// 1. Go accross each row to see if
// 3 squares in the same row match
// i.e. same value
// 2. Go down each column to see if
// 3 squares in each column match
// 3. Go across each diagonal to see if
// all three squares have the same value.
const winCombos = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
Comment on lines +56 to +65
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh, snazzy winCombos array!


const [firstRow, secondRow, thirdRow] = squares;
const board = [ ...firstRow, ...secondRow, ...thirdRow];
const xOPositions = [];

// board = ['','X','','O','','X','X','X' ]
for (let i = 0; i < winCombos.length; i++) {

const position1 = board[winCombos[i][0]];
const position2 = board[winCombos[i][1]];
const position3 = board[winCombos[i][2]]
xOPositions.push([position1, position2, position3]);;
}

for (let i = 0; i < xOPositions.length; i++) {
if (xOPositions[i][0].value === 'x' && xOPositions[i][1].value === 'x' && xOPositions[i][2].value === 'x') {
return PLAYER_1
}
else if (xOPositions[i][0].value === 'o' && xOPositions[i][1].value === 'o' && xOPositions[i][2].value === 'o') {
return PLAYER_2
}
}

return '';

}

const resetGame = () => {
// Complete in Wave 4
setSquares(generateSquares());
setTurnNumber(0);
setWinner('');
setPlayer(PLAYER_1);
}

const onClick = () => {
resetGame()
}

return (
<div className="App">
<header className="App-header">
<h1>React Tic Tac Toe</h1>
<h2>The winner is ... -- Fill in for wave 3 </h2>
<button>Reset Game</button>
<h1>React Tic Tac Toe</h1>
{ winner != '' &&
<h2>
Winner is {winner}
</h2>
}
{winner === '' && turnNumber === 9 &&
<h2>
TIE!
</h2>
}
{ winner === '' && turnNumber < 9 &&
<h2>
Current Player {player}
</h2>
}
<button
onClick={onClick}
>Reset Game</button>
</header>
<main>
<Board squares={squares} />
<Board
squares={squares}
onClickCallback={onClickCallback}/>
</main>
</div>
);
}

export default App;

Loading