Skip to content

Yoman Project 1 PTBC5#10

Open
Flashrob wants to merge 8 commits intorocketacademy:mainfrom
yomansg:main
Open

Yoman Project 1 PTBC5#10
Flashrob wants to merge 8 commits intorocketacademy:mainfrom
yomansg:main

Conversation

@Flashrob
Copy link
Copy Markdown

No description provided.

src/App.js Outdated
const { name, value } = event.target;
event.preventDefault();
this.state.audio.play(); // start playing background music
this.state.audio.loop = true;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

We never want to reassign state like so, but always use the setState function. By doing this here, you will run into trouble with missing rerenders and possibly out-of-sync state.

src/App.js Outdated
this.state.diceOrder === 1
? this.state.playerDiceRolls[0] * 10 + this.state.playerDiceRolls[1]
: this.state.playerDiceRolls[1] * 10 + this.state.playerDiceRolls[0];
const newScores = this.state.userScores.slice(); //copy the array
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

We could just do this

Suggested change
const newScores = this.state.userScores.slice(); //copy the array
const newScores = [...this.state.userScores]

src/App.js Outdated

resetGame = () => {
this.setState({
// Reset the whole game to let user play again
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think ideally we define the defaultValues from 36 - 48 here. We could create a defaultValues object, and use it in line 36, and here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggested change
// Reset the whole game to let user play again
this.setState(defaultState)

src/App.js Outdated

toggleMusic = (event) => {
event.preventDefault();
let isMusicOn = this.state.isMusicOn;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think no need to define variable for state, which is a variable already.

src/App.js Outdated
Comment on lines +130 to +134
const gameStarted = this.state.gameStarted;
const userRollDice = this.state.userRollDice;
const userOrderDice = this.state.userOrderDice;
const playerDiceRolls = this.state.playerDiceRolls;
const userScores = this.state.userScores;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Same here, no need to create new variables. If you absolutely want to, use destructuring for nicer syntax!

Suggested change
const gameStarted = this.state.gameStarted;
const userRollDice = this.state.userRollDice;
const userOrderDice = this.state.userOrderDice;
const playerDiceRolls = this.state.playerDiceRolls;
const userScores = this.state.userScores;
const { gameStatrted, userRollDice, userOrderDice, playerDiceRolls, userScores } = this.state;

src/App.js Outdated
this.state.currentPlayer,
this.state.numberOfRounds,
this.state.numberOfPlayers,
this.state.userOrderDice
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

since you defined userOrderDice right above, let's use that variable at least

import five from "./assets/five.png";
import six from "./assets/six.png";

export const DiceImage = ({ roll }) => {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Very nice component!

src/DiceImage.js Outdated
Comment on lines +9 to +21
switch (roll) {
case 1:
return <img className="dice-image" src={one} alt="1" />;
case 2:
return <img className="dice-image" src={two} alt="2" />;
case 3:
return <img className="dice-image" src={three} alt="3" />;
case 4:
return <img className="dice-image" src={four} alt="4" />;
case 5:
return <img className="dice-image" src={five} alt="5" />;
default:
return <img className="dice-image" src={six} alt="6" />;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

We could improve this like so:

First name your images as numbers, instead of letters.
"./assets/1.png"; etc.

then like so:

Suggested change
switch (roll) {
case 1:
return <img className="dice-image" src={one} alt="1" />;
case 2:
return <img className="dice-image" src={two} alt="2" />;
case 3:
return <img className="dice-image" src={three} alt="3" />;
case 4:
return <img className="dice-image" src={four} alt="4" />;
case 5:
return <img className="dice-image" src={five} alt="5" />;
default:
return <img className="dice-image" src={six} alt="6" />;
return <img className="dice-image" src={`./assets/${roll}.png`} alt={roll} />;

Comment on lines +2 to +13
if (userRollDice) {
return (
<img
src="https://www.animatedimages.org/data/media/710/animated-dice-image-0085.gif"
border="0"
alt="animated-dice-image-0085"
className="animated-gif"
/>
);
} else {
return <p>Click [Continue] button...</p>;
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggested change
if (userRollDice) {
return (
<img
src="https://www.animatedimages.org/data/media/710/animated-dice-image-0085.gif"
border="0"
alt="animated-dice-image-0085"
className="animated-gif"
/>
);
} else {
return <p>Click [Continue] button...</p>;
}
return userRollDice ? <img
src="https://www.animatedimages.org/data/media/710/animated-dice-image-0085.gif"
border="0"
alt="animated-dice-image-0085"
className="animated-gif"
/> : <p>Click [Continue] button...</p>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

or get rid of the else statement for better readability

Suggested change
if (userRollDice) {
return (
<img
src="https://www.animatedimages.org/data/media/710/animated-dice-image-0085.gif"
border="0"
alt="animated-dice-image-0085"
className="animated-gif"
/>
);
} else {
return <p>Click [Continue] button...</p>;
}
if (userRollDice) return (
<img
src="https://www.animatedimages.org/data/media/710/animated-dice-image-0085.gif"
border="0"
alt="animated-dice-image-0085"
className="animated-gif"
/>
);
return <p>Click [Continue] button...</p>;

src/PlayForm.js Outdated
isLastPlayer,
isMusicOn,
}) => {
const captionMusic = isMusicOn ? "Music OFF" : "Music ON";
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

captionMusic sounds like an action, a function. musicCaption might be better

@Flashrob
Copy link
Copy Markdown
Author

@yomansg please see these comments

@Flashrob Flashrob changed the title Code review PR Yoman Project 1 PTBC5 Nov 2, 2022
Comment on lines +193 to +198
<div className="Main-body height-fixed">
{/* Constantly show the players' scores on a scoreboard*/}
{gameStarted && <h4>Leaderboard 🏆</h4>}
{gameStarted && <hr />}
{gameStarted && <LeaderBoard userScores={userScores} />}
</div>
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggested change
<div className="Main-body height-fixed">
{/* Constantly show the players' scores on a scoreboard*/}
{gameStarted && <h4>Leaderboard 🏆</h4>}
{gameStarted && <hr />}
{gameStarted && <LeaderBoard userScores={userScores} />}
</div>
{ gameStarted && (<div className="Main-body height-fixed">
{/* Constantly show the players' scores on a scoreboard*/}
<h4>Leaderboard 🏆</h4>
<hr />
<LeaderBoard userScores={userScores} />
</div>)}

src/App.js Outdated
Comment on lines +66 to +67
this.state.audio.play(); // start playing background music
this.state.audio.loop = true;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggested change
this.state.audio.play(); // start playing background music
this.state.audio.loop = true;
const audio = this.state.audio;
audio.play();
audio.loop = true;

[name]: value,
userScores: Array(this.state.numberOfPlayers).fill(0),
gameStarted: true,
isMusicOn: true,
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggested change
isMusicOn: true,
isMusicOn: true,
audio: {
...this.state.audio,
loop: true
}

label: "Game music",
},
];

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggested change
const defaultState = {
gameStarted: false,
numberOfPlayers: 2, // form input state
numberOfRounds: 3,
userRollDice: false,
userOrderDice: false,
userScores: [],
currentPlayer: 0,
currentRound: 0,
playerDiceRolls: [0, 0],
currentPlayerScore: 0,
diceOrder: 1,
audio: new Audio(audioClips[2].sound), // set background music
isMusicOn: false, // background music is initially OFF
}

src/App.js Outdated
Comment on lines +35 to +49
this.state = {
gameStarted: false,
numberOfPlayers: 2, // form input state
numberOfRounds: 3,
userRollDice: false,
userOrderDice: false,
userScores: [],
currentPlayer: 0,
currentRound: 0,
playerDiceRolls: [0, 0],
currentPlayerScore: 0,
diceOrder: 1,
audio: new Audio(audioClips[2].sound), // set background music
isMusicOn: false, // background music is initially OFF
};
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggested change
this.state = {
gameStarted: false,
numberOfPlayers: 2, // form input state
numberOfRounds: 3,
userRollDice: false,
userOrderDice: false,
userScores: [],
currentPlayer: 0,
currentRound: 0,
playerDiceRolls: [0, 0],
currentPlayerScore: 0,
diceOrder: 1,
audio: new Audio(audioClips[2].sound), // set background music
isMusicOn: false, // background music is initially OFF
};
this.state = defaultState;

src/App.js Outdated
Comment on lines +119 to +123
if (isMusicOn) {
this.state.audio.pause(); // Pause music if it is playing
} else {
this.state.audio.play(); // Play music if it is paused
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Suggested change
if (isMusicOn) {
this.state.audio.pause(); // Pause music if it is playing
} else {
this.state.audio.play(); // Play music if it is paused
}
isMusicOn ? this.state.audio.pause() : this.state.audio.play()

charlesleezhaoyi added a commit to charlesleezhaoyi/project1-bootcamp that referenced this pull request Nov 18, 2023
charlesleezhaoyi added a commit to charlesleezhaoyi/project1-bootcamp that referenced this pull request Nov 18, 2023
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