forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Class14 tictactoe #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
jmwhitman7
wants to merge
3
commits into
gh-pages
Choose a base branch
from
class14-tictactoe
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
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,55 @@ | ||
| 'use strict'; | ||
|
|
||
| document.addEventListener('DOMContentLoaded', () => { | ||
| // Your code here | ||
| }); | ||
| class TicTacToe extends React.Component { | ||
|
|
||
|
|
||
| constructor(props) { | ||
| super(props); | ||
| this.state = { | ||
| turn: 'X', | ||
| }; | ||
| this.message = ''; | ||
| } | ||
| handleClick=(cell)=>{ | ||
| const state = this.state; | ||
| if (!state[cell]) { | ||
|
|
||
| state[cell] = this.state.turn; | ||
|
|
||
| state['turn'] = this.state.turn === 'X'? 'O' : 'X'; | ||
| this.message = ''; | ||
|
|
||
| } else { | ||
| this.message = 'HEY! That space is taken. Play on a different square!'; | ||
| } | ||
|
|
||
| this.setState(state) | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| render() { | ||
| return ( | ||
| <div> | ||
| <div className="row"> | ||
| <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" 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" 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> | ||
| </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 |
|---|---|---|
| @@ -1,31 +1,123 @@ | ||
| 'use strict'; | ||
|
|
||
| // Create TTT Component | ||
| class TicTacToe extends React.Component { | ||
| // Because we start with a this.state, we have a constructor | ||
| constructor(props) { | ||
| super(props); | ||
| // Create properties to hold 'X' and 'O' | ||
| this.state = { | ||
| cell0: '', | ||
| cell1: '', | ||
| cell2: '', | ||
| cell3: '', | ||
| cell4: '', | ||
| cell5: '', | ||
| cell6: '', | ||
| cell7: '', | ||
| cell8: '', | ||
| // Starting turn of 'X' | ||
| turn: 'X', | ||
| // Declare local var to TTT class, to be filled in later | ||
| message: '' | ||
| }; | ||
| } | ||
| // Click on cell and alternate 'X' and 'O' | ||
| handleClick = (cell) => { | ||
| // Create a local variable to lock in current state 0-8, turn, message | ||
| const state = this.state; | ||
| // Create a var to hold the turn | ||
| const playerTurn = this.state.turn; | ||
| // Label each cell with a state, here being the turn | ||
| state[cell] = playerTurn; | ||
| // Alternates player turn | ||
| state.turn = playerTurn === 'X' ? 'O' : 'X'; | ||
| // set the new, altered properties to a more current version of this.state | ||
| this.setState(state); | ||
| // Call the checkForWin function, pass in the current turn | ||
| this.checkForWin(playerTurn); | ||
| }; | ||
|
|
||
| checkForWin = (playerTurn) => { | ||
| // Create a local variable to lock in current state 0-8, turn, message | ||
| const state = this.state; | ||
| // Create arrays of winning positions, where each position is the same as the others | ||
| const winningPositions = [ | ||
| // Horizontal wins | ||
| ['cell0','cell1','cell2'], | ||
| ['cell3','cell4','cell5'], | ||
| ['cell6','cell7','cell8'], | ||
| // Vertical wins | ||
| ['cell0','cell3','cell6'], | ||
| ['cell1','cell4','cell7'], | ||
| ['cell2','cell5','cell8'], | ||
| // Diagonal wins | ||
| ['cell0','cell4','cell8'], | ||
| ['cell2','cell4','cell6'] | ||
| ]; | ||
| // Set iterator to loop over all winning scenario arrays | ||
| for (var i in winningPositions) { | ||
|
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. don't use for loop, use map or forEach |
||
| // Starting at the first winning array and ending at the last arrays | ||
| // Check the first through thrid positions in the current array against the playerTurn | ||
| // If all 3 elements in the current array are equal to playerTurn 'X' or 'O' | ||
| if (state[winningPositions[i][0]] === playerTurn | ||
| && state[winningPositions[i][1]] === playerTurn | ||
| && state[winningPositions[i][2]] === playerTurn) { | ||
| // Use playerTurn variable to create a variable to hold the winningMessage | ||
| let winningMessage = `${playerTurn} is the winner!` | ||
|
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. this should be const |
||
| // Set the current state's message to the winningMessage var | ||
| this.setState({message: winningMessage}); | ||
| } | ||
| } | ||
| console.log('message', this.state.message); | ||
| }; | ||
|
|
||
| resetBoard = (state) => { | ||
| this.state = { | ||
| cell0: '', | ||
| cell1: '', | ||
| cell2: '', | ||
| cell3: '', | ||
| cell4: '', | ||
| cell5: '', | ||
| cell6: '', | ||
| cell7: '', | ||
| cell8: '', | ||
| // Starting turn of 'X' | ||
| turn: 'X', | ||
| // Declare local var to TTT class, to be filled in later | ||
| message: '' | ||
| }; | ||
| // Reset the state with empty cells, turn = 'X', no message | ||
| this.setState(state); | ||
| } | ||
|
|
||
| render() { | ||
| return ( | ||
| <div> | ||
| <div className="row"> | ||
| <div data-cell="0"></div> | ||
| <div data-cell="1"></div> | ||
| <div data-cell="2"></div> | ||
| </div> | ||
| <div className="row"> | ||
| <div data-cell="3"></div> | ||
| <div data-cell="4"></div> | ||
| <div data-cell="5"></div> | ||
| </div> | ||
| <div className="row"> | ||
| <div data-cell="6"></div> | ||
| <div data-cell="7"></div> | ||
| <div data-cell="8"></div> | ||
| </div> | ||
| <h1> {this.state.message} </h1> | ||
| <section> | ||
| <button type="button" onClick={this.resetBoard}> Reset Board | Play Again </button> | ||
| </section> | ||
| <section> | ||
| <div className="row"> | ||
| <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" 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" 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> | ||
| </section> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| ReactDOM.render(<TicTacToe />, document.getElementById('tic-tac-toe')); | ||
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.
why is message outside of the state?