forked from AdaGold/react-tic-tac-toe
-
Notifications
You must be signed in to change notification settings - Fork 80
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
Maple - Detroit #75
Open
Detroit-the-Dev
wants to merge
8
commits into
Ada-C16:main
Choose a base branch
from
Detroit-the-Dev:digital-starter
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
Maple - Detroit #75
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5cb7505
generates the board and solution for checkForWinners
spitsfire c487dca
pulls updated eslintrc file
spitsfire 5c65394
fixes semicolon warnings
spitsfire 7e58837
adds list of features that differ from original project
spitsfire 2cb6cfd
changes const variable casing to match test
spitsfire 90b2bda
removes jest from eslinter
spitsfire 87c759e
Updated changes to complete project.
Detroit-the-Dev c7dbb99
Merge branch 'digital-starter' of https://github.com/Detroit-the-Dev/…
Detroit-the-Dev 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 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 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 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 |
---|---|---|
|
@@ -21,8 +21,7 @@ | |
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"react-app", | ||
"react-app/jest" | ||
"react-app" | ||
] | ||
}, | ||
"browserslist": { | ||
|
This file contains 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 |
---|---|---|
|
@@ -3,8 +3,8 @@ 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 = []; | ||
|
@@ -29,37 +29,101 @@ 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 [turn, setTurn] = useState(1); | ||
const [winner, setWinner] = useState(null); | ||
const [gameEnd, setGameEnd] = useState(false); | ||
|
||
// 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 currentPlayer = (turn) => { | ||
if (turn <= 9) { | ||
if (turn % 2 !== 0) { | ||
return PLAYER_1; | ||
} else { | ||
return PLAYER_2; | ||
} | ||
} | ||
}; | ||
|
||
const onClickCallback = (id) => { | ||
const updateSquare = squares.map((square) => { | ||
while (gameEnd === false) { | ||
for (let subSquare of square) { | ||
if (subSquare.id === id && subSquare.value === '') { | ||
subSquare.value = currentPlayer(turn); | ||
if (turn < 9) { | ||
setTurn(turn + 1); | ||
} | ||
} | ||
let checkedWinner = checkForWinner(); | ||
console.log(checkedWinner); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove console log after debugging |
||
if (checkedWinner !== null) { | ||
setWinner(checkedWinner); | ||
setGameEnd(true); | ||
break; | ||
} | ||
} | ||
return square; | ||
} | ||
setSquares(updateSquare); | ||
}); | ||
}; | ||
|
||
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. | ||
let i = 0; | ||
|
||
// Check all the rows and columns for a winner | ||
while (i < 3) { | ||
if ( | ||
squares[i][0].value === squares[i][1].value && | ||
squares[i][2].value === squares[i][1].value && | ||
squares[i][0].value !== '' | ||
) { | ||
return squares[i][0].value; | ||
} else if ( | ||
squares[0][i].value === squares[1][i].value && | ||
squares[2][i].value === squares[1][i].value && | ||
squares[0][i].value !== '' | ||
) { | ||
return squares[0][i].value; | ||
} | ||
i += 1; | ||
} | ||
// Check Top-Left to bottom-right diagonal | ||
if ( | ||
squares[0][0].value === squares[1][1].value && | ||
squares[2][2].value === squares[1][1].value && | ||
squares[1][1].value !== '' | ||
) { | ||
return squares[0][0].value; | ||
} | ||
|
||
// Check Top-right to bottom-left diagonal | ||
if ( | ||
squares[0][2].value === squares[1][1].value && | ||
squares[2][0].value === squares[1][1].value && | ||
squares[1][1].value !== '' | ||
) { | ||
return squares[0][2].value; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const resetGame = () => { | ||
// Complete in Wave 4 | ||
setSquares(generateSquares()); | ||
setTurn(1); | ||
setWinner(null); | ||
setGameEnd(false); | ||
}; | ||
|
||
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> | ||
<h2>Winner is {winner}</h2> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When there isn't a winner you could display who the current player is. Like "Its Player 1's turn" or "It's X's turn" |
||
<button onClick={resetGame}>Reset Game</button> | ||
</header> | ||
<main> | ||
<Board squares={squares} /> | ||
<Board squares={squares} onClickCallback={onClickCallback} /> | ||
</main> | ||
</div> | ||
); | ||
|
This file contains 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 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
Oops, something went wrong.
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.
you could refactor here and use a ternary operator