forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Tic Tac Toe... in React Mode #12
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
copelandhouse2
wants to merge
4
commits into
gh-pages
Choose a base branch
from
week7a-reactTTT
base: gh-pages
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
4 commits
Select commit
Hold shift + click to select a range
ef464ae
Initial push. Controlling turns. Added message.
copelandhouse2 e8e6e8f
working on checkForWin. Saving what I have.
copelandhouse2 755bf3f
Fixed checkForWin. Ran Linter. There is only 1 linting error. It d…
copelandhouse2 879d094
added reset button (after nearly 2 hours ugh)
copelandhouse2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,118 @@ | ||
| 'use strict'; | ||
|
|
||
|
|
||
|
|
||
| class TicTacToe extends React.Component { | ||
|
|
||
|
|
||
| constructor(props) { | ||
| super(props); | ||
| } | ||
| this.state = { | ||
| turn: 'X', | ||
| }; | ||
| this.initialState = { | ||
| turn: 'X', | ||
| }; | ||
| } | ||
|
|
||
| checkForWin(myObj) { | ||
| // wins array stores all the win combinations. wincombo array is one of the wins. | ||
| // wins.some ... at least one of these win combos needs to be true. | ||
| // aWin.every ... for a win, all the squares in the particular win must equal the players token (X or O) | ||
| const wins = [ | ||
| [0,1,2], [3,4,5], [6,7,8], // horizontal wins | ||
| [0,3,6], [1,4,7], [2,5,8], // vertical wins | ||
| [0,4,8], [2,4,6] // diagonal wins | ||
| ]; | ||
|
|
||
| return wins.some(aWin => aWin.every(index => myObj[`cell${index}`] === myObj['turn'])); | ||
| } // checkForWin() | ||
|
|
||
| // handleClick. Method is called when a square is clicked. Based on onClick | ||
| // event in each div data-cell below. | ||
| // The TTT logic is in here. a) Making sure it is valid move b) validating win c) messaging. | ||
| handleClick(cell) { | ||
| const state = { ...this.state }; | ||
| if (!state[cell]) { | ||
| state[cell] = this.state.turn; | ||
| // console.log('value of checkForWin', this.checkForWin(state)); | ||
| if (this.checkForWin(state)) { //checkForWin returns TRUE if someone won. It returns FALSE if no win yet. | ||
| this.message = `Congratulations ${this.state.turn}. You won! Start a new game by refreshing`; | ||
|
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. Why are you storing message in a class global instead of the state? |
||
| // If there have been 9 turns but no winner. It is a tie. | ||
| // Splicing to get rid of turn key value pair. That will give me the | ||
| // correct number of cells populated so far. | ||
| } else if (Object.values(state).splice(1).length == 9) { | ||
| this.message = `We have a tie folks. Start a new game by refreshing`; | ||
| } else { | ||
| state['turn'] = this.state.turn === 'X'? 'O' : 'X'; // Switch between X and O | ||
| this.message = ''; | ||
| } | ||
| } else { | ||
| this.message = 'Try again please. That square is taken.'; | ||
| } | ||
| this.setState(state) | ||
| }; | ||
|
|
||
| resetBoard () { | ||
| console.log('Button clicked', this.state, this.initialState); | ||
| // const state = { ...this.state }; | ||
| // this.state = state; | ||
| // const myState = this.initialState.turn; | ||
| const keyArr = Object.keys(this.state); | ||
| console.log(keyArr); | ||
| keyArr.forEach(key => { | ||
| if (key === 'turn') { | ||
| console.log('in the if.true', key); | ||
| this.state[key] = 'X'; | ||
| } else { | ||
| console.log('in the if.false', key); | ||
| delete this.state[key]; | ||
| } | ||
| this.message = ''; | ||
| }); | ||
|
|
||
| // for (let i=0; i < keyArr.length; i++) { | ||
| // console.log('in the for', keyArr[i]) | ||
| // if (keyArr[i] === 'turn') { | ||
| // this.state[keyArr[i]] = 'X'; | ||
| // } else { | ||
| // delete this.state[keyArr[i]]; | ||
| // } | ||
| // } | ||
|
|
||
| // keyArr.map((key) => { | ||
| // | ||
| // }); | ||
| this.setState(this.state) | ||
| console.log(this.state); | ||
| // this.render(); | ||
| } | ||
| // render method displays the TTT board. | ||
| render() { | ||
| return ( | ||
| <div> | ||
| <div className="row"> | ||
| <div data-cell="0"></div> | ||
| <div data-cell="1"></div> | ||
| <div data-cell="2"></div> | ||
| <div data-cell="0" onClick={() => this.handleClick('cell0')}>{this.state.cell0}</div> | ||
| <div data-cell="1" onClick={() => this.handleClick('cell1')}>{this.state.cell1}</div> | ||
| <div data-cell="2" onClick={() => this.handleClick('cell2')}>{this.state.cell2}</div> | ||
| </div> | ||
| <div className="row"> | ||
| <div data-cell="3"></div> | ||
| <div data-cell="4"></div> | ||
| <div data-cell="5"></div> | ||
| <div data-cell="3" onClick={() => this.handleClick('cell3')}>{this.state.cell3}</div> | ||
| <div data-cell="4" onClick={() => this.handleClick('cell4')}>{this.state.cell4}</div> | ||
| <div data-cell="5" onClick={() => this.handleClick('cell5')}>{this.state.cell5}</div> | ||
| </div> | ||
| <div className="row"> | ||
| <div data-cell="6"></div> | ||
| <div data-cell="7"></div> | ||
| <div data-cell="8"></div> | ||
| <div data-cell="6" onClick={() => this.handleClick('cell6')}>{this.state.cell6}</div> | ||
| <div data-cell="7" onClick={() => this.handleClick('cell7')}>{this.state.cell7}</div> | ||
| <div data-cell="8" onClick={() => this.handleClick('cell8')}>{this.state.cell8}</div> | ||
| </div> | ||
| <div id='announce-winner'>{this.message}</div> | ||
| <button type="button" onClick={() => this.resetBoard()}>Reset to Play Again</button> | ||
|
|
||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| ReactDOM.render(<TicTacToe />, document.getElementById('tic-tac-toe')); | ||
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 |
|---|---|---|
|
|
@@ -14,5 +14,7 @@ div[data-cell] { | |
|
|
||
| #announce-winner { | ||
| clear: both; | ||
| font-size: 50px; | ||
| font-size: 24px; | ||
| font-family: Arial; | ||
|
|
||
| } | ||
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.
This doesn't seem to be used anywhere...