Skip to content
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

Completed crude tic-tac-toe game #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Devonte202
Copy link

No description provided.

@ROgbonna1 ROgbonna1 requested review from ipc103 and Keyairra-S-Wright and removed request for ipc103 April 29, 2020 18:54

}

class Player{
Copy link

@Keyairra-S-Wright Keyairra-S-Wright May 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend keeping the Player class separate from the Game class, similar to how the <Board/> component is separate from <PlayerOne/> and <PlayerTwo/> components. This will help keep the code base organized and maintainable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes perfect sense actually, I'll make that change

const game = new Game()

const Board = () => {
const [box1, setBox1] = React.useState("")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Within board.jsx, this code base uses hooks and within the game.js module, the code base uses class components. There may be times when developers are updating apps from classes or hooks. Or there may be times when there is a specific tradeoff/benefit for switching between using hooks and class components in the same app.

Given that this code base was newly created by one developer, I'd recommend choosing one paradigm (hooks or classes) and sticking with it. Alternatively, in the Pull Request Summary, a developer could briefly highlight reasoning for switching between React "formats" so that other developers can better understand the decision.

const [box6, setBox6] = React.useState("")
const [box7, setBox7] = React.useState("")
const [box8, setBox8] = React.useState("")
const [box9, setBox9] = React.useState("")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting state to an empty string nine times for each box is a valid approach. To make your code more even easier to reason about, consider refactoring so that the state of all boxes is set to an array.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oddly enough...I had no idea I could do that. Thank you!

Comment on lines +54 to +66
<div onClick={(e) => move(e)} className="box" id="box1">{box1}</div>
<div onClick={(e) => move(e)} className="box" id="box2">{box2}</div>
<div onClick={(e) => move(e)} className="box" id="box3">{box3}</div>
</section>
<section className="row">
<div onClick={(e) => move(e)} className="box" id="box4">{box4}</div>
<div onClick={(e) => move(e)} className="box" id="box5">{box5}</div>
<div onClick={(e) => move(e)} className="box" id="box6">{box6}</div>
</section>
<section className="row">
<div onClick={(e) => move(e)} className="box" id="box7">{box7}</div>
<div onClick={(e) => move(e)} className="box" id="box8">{box8}</div>
<div onClick={(e) => move(e)} className="box" id="box9">{box9}</div>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there were a refactor to use an array as a default state, then there would be an added benefit of being able to map through the boxes in order to render the board.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely a change I'll be making

Comment on lines +19 to +22
if(this.posTracker['0'] === this.posTracker['1'] && this.posTracker['0'] === this.posTracker['2'] && this.posTracker['0'] !== ''){
candidate % 2 === 0 ? game.playerOne.score += 1 : game.playerTwo.score += 1
this.gameOver()
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each of the if statements are essentially the same except for the posTracker position. Consider refactoring in order to reduce the amount of repetitive code here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll work on this!

game.checkBoard(game.turn)

game.turn += 1
return game.turn
Copy link

@Keyairra-S-Wright Keyairra-S-Wright May 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if statements are nearly identical. It appears that the difference is only which box state is set. In that case, this can be refactored into something like:

Suggested change
return game.turn
if(game.turn %2 !==0}{
boxStates[box](<PlayerOne/>)
} else {
boxStates[box](<PlayerTwo/>)
}
document.getElementById(box).removeAttribute("onClick");
//etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants