-
Notifications
You must be signed in to change notification settings - Fork 80
Pine-Marjan, Megan, Roslyn #56
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ import Board from './components/Board'; | |
|
|
||
| const PLAYER_1 = 'X'; | ||
| const PLAYER_2 = 'O'; | ||
| let TURN = true; | ||
|
|
||
|
|
||
| const generateSquares = () => { | ||
| const squares = []; | ||
|
|
@@ -25,15 +27,38 @@ const generateSquares = () => { | |
| return squares; | ||
| }; | ||
|
|
||
|
|
||
| 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(TURN); | ||
| const [winner, setWinner] = useState(''); | ||
| let string = 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 | ||
| const updateSquares = (id) => { | ||
| let newSquares = []; | ||
| let strVal = ''; | ||
| strVal = turn==true? 'x':'o'; | ||
| for (let row of squares){ | ||
| let newRow = row.map((square)=>{ | ||
| if ((square.id===id) && (!square.value)) { | ||
| square.value = strVal; | ||
| return square; | ||
| } | ||
| return square; | ||
| }); | ||
| newSquares.push(newRow); | ||
| } | ||
| let newTurn = !turn; | ||
| setTurn(newTurn); | ||
| setSquares(newSquares); | ||
| checkForWinner(); | ||
| }; | ||
|
|
||
| const checkForWinner = () => { | ||
| // Complete in Wave 3 | ||
|
|
@@ -45,21 +70,85 @@ const App = () => { | |
| // 3 squares in each column match | ||
| // 3. Go across each diagonal to see if | ||
| // all three squares have the same value. | ||
|
|
||
| const winning = [ new Set([0,3,6]), new Set([1,4,7]), new Set([2,5,8]), new Set ([0,1,2]), | ||
| new Set([3,4,5]), new Set([6,7,8]),new Set([0,8,4]),new Set([2,4,6])]; | ||
|
|
||
| let count = 0; | ||
|
|
||
| const xSet=new Set(); | ||
| const oSet= new Set(); | ||
| for(let row of squares){ | ||
| for (let element of row){ | ||
| // console.log('element=', element); | ||
| if (element.value === 'x'){ | ||
| xSet.add(count); | ||
| } else if (element.value === 'o') { | ||
| oSet.add(count); | ||
| } | ||
| count ++; | ||
| } | ||
| } | ||
| const myX = []; | ||
|
|
||
| console.log('xSet', xSet); | ||
| console.log('oSet', oSet); | ||
|
|
||
| // Roslyn is done | ||
|
|
||
| for (let combo of winning){ | ||
| if (isSuperset(xSet,combo)){ | ||
| myX.push(combo); | ||
| } | ||
| } | ||
|
Comment on lines
+99
to
+103
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. Interesting approach! |
||
| // const myO=winning.filter((val)=>{ | ||
| // isSuperset(oSet,val); | ||
| // }); | ||
| const myO = []; | ||
| for (let combo of winning){ | ||
| if (isSuperset(oSet,combo)){ | ||
| myO.push(combo); | ||
| } | ||
| } | ||
|
|
||
| console.log('myX', myX); | ||
| console.log('myO', myO); | ||
|
|
||
| if (myX.length > myO.length) { | ||
| string = 'x'; | ||
| } else if (myO.length > myX.length){ | ||
| string = 'o'; | ||
| } else if ((oSet.size + xSet.size) === 9){ | ||
| string = 'Tie'; | ||
| } else { | ||
| string = ''; | ||
| } | ||
| setWinner(string); | ||
| return string; | ||
| }; | ||
|
|
||
| const resetGame = () => { | ||
| // Complete in Wave 4 | ||
| const isSuperset = (set, subset) => { | ||
| for (let elem of subset) { | ||
| if (!set.has(elem)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| }; | ||
|
Comment on lines
+130
to
+137
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. 👍 |
||
|
|
||
| const resetGame = () => { | ||
| setSquares(generateSquares()); | ||
| }; | ||
|
|
||
| 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> | ||
| <button onClick={()=>{resetGame();}}>Reset Game</button> | ||
| </header> | ||
| <main> | ||
| <Board squares={squares} /> | ||
| <Board squares={squares} onClickCallback={updateSquares}/> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ div.grid { | |
| display: grid; | ||
| grid-template: repeat(3, 1fr) / repeat(3, 1fr); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,16 +9,34 @@ const generateSquareComponents = (squares, onClickCallback) => { | |
| // squares is a 2D Array, but | ||
| // you need to return a 1D array | ||
| // of square components | ||
| console.log('We are about to iterate through squares'); | ||
| let oneD =[]; | ||
| // let classVal=''; | ||
| for (let row of squares){ | ||
| for (let square of row) { | ||
| // if (square.value==='x'){ | ||
| // classVal='jasmine'; | ||
| // }else if (square.value==='o'){ | ||
| // classVal='chris'; | ||
| // } | ||
| oneD.push(<Square key={square.id} id={square.id} value={square.value} onClickCallback={onClickCallback} />); | ||
|
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. Nice! |
||
| } | ||
| } | ||
| return oneD; | ||
|
|
||
| } | ||
| }; | ||
|
|
||
| const Board = ({ squares, onClickCallback }) => { | ||
| const squareList = generateSquareComponents(squares, onClickCallback); | ||
| console.log(squareList); | ||
| return <div className="grid" > | ||
| {squareList} | ||
| </div> | ||
| } | ||
| return( | ||
| <div className="grid" > | ||
| {squareList} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| Board.propTypes = { | ||
| squares: PropTypes.arrayOf( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,4 +9,14 @@ | |
| justify-content: center; | ||
| align-items: center; | ||
| margin: 2px; | ||
| } | ||
| } | ||
|
|
||
| .jasmine { | ||
| background-image: url("https://ca.slack-edge.com/T024N8K3W8N-U029WFRD8HW-807f2572ed86-512"); | ||
| Background-size: 200px 200px; | ||
| } | ||
|
|
||
| .chris { | ||
| background-image: url("https://ca.slack-edge.com/T024N8K3W8N-U0283SDM62H-f8f25fdaa3da-512"); | ||
| Background-size: 200px 200px; | ||
| } | ||
|
Comment on lines
+14
to
+22
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. 😂 |
||
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.
There is a small logical bug here. There is no check here to see if there is a winner, so it is possible to continue to play. This can result in unexpected behavior. For example, if the board looks like this:
the game will display 'Winner is x'. However, 'o' can still move, so if 'o' clicks in the middle left square:
the game will display "Winner is ". This continues until the entire board is filled, at which point the game will display "Winner is Tie".