forked from AdaGold/react-tic-tac-toe
-
Notifications
You must be signed in to change notification settings - Fork 71
Rock-Saharai Cante🐨 #44
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
scantea
wants to merge
5
commits into
Ada-C15:master
Choose a base branch
from
scantea:master
base: master
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 all commits
Commits
Show all changes
5 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 |
|---|---|---|
|
|
@@ -3,66 +3,128 @@ import './App.css'; | |
|
|
||
| import Board from './components/Board'; | ||
|
|
||
| const PLAYER_1 = 'X'; | ||
| const PLAYER_2 = 'O'; | ||
| const PLAYER_1 = 'x';//change to lowercase or tests wont pass | ||
| const PLAYER_2 = 'o'; | ||
|
|
||
| const generateSquares = () => { | ||
| const squares = []; | ||
|
|
||
| let currentId = 0; | ||
|
|
||
| for (let row = 0; row < 3; row += 1) { | ||
| squares.push([]); | ||
| squares.push([]); //adding square to squares | ||
| for (let col = 0; col < 3; col += 1) { | ||
| squares[row].push({ | ||
| id: currentId, | ||
| value: '', | ||
| }); | ||
| }); //creating a 3x3 array; each square has an id and value | ||
| currentId += 1; | ||
| } | ||
| } | ||
|
|
||
| return squares; | ||
| return squares; //board is constructed | ||
| } | ||
|
|
||
| const App = () => { | ||
|
|
||
| // This starts state off as a 2D array of JS objects with | ||
| // empty value and unique ids. | ||
| const [squares, setSquares] = useState(generateSquares()); | ||
| //Set initial state, to player 1's turn | ||
| const[currentPlayer,setCurrentPlayer]=useState(PLAYER_1); | ||
| //initial number of squares filled equals 0 | ||
| const[numSquaresFilled,setNumSquaresFilled]=useState(0); | ||
| //intial winner is Null | ||
| const[winner,setWinner] = useState(null); | ||
|
|
||
|
|
||
| // 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 | ||
| //PSE code, check for winner | ||
| //accessing value of squares on board through .value notation | ||
| const checkForWinner = () => { | ||
|
|
||
| let i=0; | ||
| while(i<3){ | ||
| // rows match? | ||
| 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; | ||
| //columns match? | ||
| } 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; | ||
| } | ||
| //Diagnol left to right | ||
| 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; | ||
| } | ||
| //diagnol right to left | ||
| 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; | ||
| } | ||
|
|
||
| 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. | ||
| return null; | ||
| } | ||
|
|
||
| const updateSquares = (id) => { | ||
| if (winner !== null) return; | ||
|
|
||
| const newSquares = [...squares]; | ||
| let row = 0; | ||
| let col = 0; | ||
| let found = false; | ||
|
|
||
| while (row < 3 && !found) { | ||
| while (col < 3 && !found) { | ||
| let currentSquare = newSquares[row][col]; | ||
| //id of square matches the current square | ||
| if (currentSquare.id === id) { | ||
| console.log(currentSquare); | ||
| if (currentSquare.value !== '') return; | ||
|
|
||
| found = true; | ||
| currentSquare.value = currentPlayer; | ||
| setNumSquaresFilled(numSquaresFilled + 1);//one square behind | ||
| if (currentPlayer === PLAYER_1) { | ||
| setCurrentPlayer(PLAYER_2) | ||
| } else { | ||
| setCurrentPlayer(PLAYER_1); | ||
| } | ||
| } | ||
| col += 1; | ||
| } | ||
| row += 1; | ||
| col = 0; | ||
| } | ||
| setWinner(checkForWinner()); | ||
| setSquares(newSquares); //updating square content | ||
| } | ||
|
|
||
| const resetGame = () => { | ||
| // Complete in Wave 4 | ||
| setSquares(generateSquares());//setting squares to intial empty board | ||
| setCurrentPlayer('x'); | ||
| setNumSquaresFilled(0); | ||
| setWinner(null); | ||
| } | ||
|
Comment on lines
110
to
116
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. Great job creating the resetGame function! |
||
|
|
||
| //if winner == null, then game continue | ||
| //else winner is winner | ||
| 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>🐨 Saharai's Tic Tac Toe 🐨</h1> | ||
| <h2>{winner === null ? `Current Player ${ currentPlayer }` : `Winner is ${ winner }`}</h2> | ||
| <button onClick={resetGame}>Reset Game</button> | ||
| </header> | ||
| <main> | ||
| <Board squares={squares} /> | ||
| <Board squares={squares} onClickCallback={updateSquares}/> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -2,9 +2,9 @@ div.grid { | |
| width: 600px; | ||
| height: 600px; | ||
| margin: 0 auto; | ||
| background-color: #34495e; | ||
| color: #fff; | ||
| border: 6px solid #2c3e50; | ||
| background-color: #0f7deb; | ||
| color: rgb(193, 131, 232); | ||
| border: 6px solid #a1ccf7; | ||
|
Comment on lines
+5
to
+7
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. Cute!!! Even the kawaii-version branch had an adorable color scheme! |
||
| border-radius: 10px; | ||
| display: grid; | ||
| grid-template: repeat(3, 1fr) / repeat(3, 1fr); | ||
|
|
||
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
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
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.
Great use of state to keep track of current player, winner, and filled squares