Skip to content

Steph's Submission#3

Open
Seal125 wants to merge 3 commits intoThe-Marcy-Lab-School:masterfrom
Seal125:master
Open

Steph's Submission#3
Seal125 wants to merge 3 commits intoThe-Marcy-Lab-School:masterfrom
Seal125:master

Conversation

@Seal125
Copy link

@Seal125 Seal125 commented May 4, 2020

No description provided.

Copy link

@ipc103 ipc103 left a comment

Choose a reason for hiding this comment

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

Hi @Seal125 - really nice work on this one! I left a few comments inline - the biggest things to look at would be to double check when the history should be reset, and also potentially think about letting the context do more of the heavy lifting for you to take some of the logic out of the component. Let me know if you have any questions!

import BoardContext from './contexts/BoardContext';

const Game = () => {
const { turn } = useContext(BoardContext);
Copy link

Choose a reason for hiding this comment

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

This is cool - using the context means each component can pull exactly the data it needs. Nice work!

@@ -0,0 +1,7 @@
import React from 'react';

const Square = (props) => {
Copy link

Choose a reason for hiding this comment

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

One small thing - you can use object destructuring if you like to avoid having to repeat props. on the next line. {clickEvent, value}

import React from 'react';

const Square = (props) => {
return <button key={props.index} onClick={props.clickEvent} className='square'>{props.value}</button>;
Copy link

Choose a reason for hiding this comment

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

One tiny thing - the key prop should not be needed here. This is only important for React when you're rendering an array of items so it can keep track of which is which i.e. {board.map(value => >Square key={value} />} Basically, it's important to have it on the array side but not as needed here.

<ol>{playerMoves}</ol>
</div>
<div className='row'>
<Square clickEvent={() => handleClick(0)} value={boardState[0]}/>
Copy link

Choose a reason for hiding this comment

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

I'd probably use board.slice to take the first three items, then map over them for this row. You could then do the same thing for the row below and the second three items. Another option would be to use something like the chunk method from lodash that you could iterate over, render a row, and then render a square for each sub element.

function stepTo(step) {
const boardInHistory = [...history];
setBoardState(boardInHistory[step - 1] || []);
setHistory(boardInHistory[step - 1] || []);
Copy link

Choose a reason for hiding this comment

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

It looks like we are re-setting the history each time we step backwards, so there is no way to step back forward again in the history. This means that this functions more like an undo function instead of a time-travel. For time-travel, you'd just want to not reset the history when moving backwards.

}

const playerMoves = history.map((step, move) => {
const desc = move ?
Copy link

Choose a reason for hiding this comment

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

👍 on configuring this copy - stuff like this can be a bit of a pain. Also, great work on usingmap here to easily render the elements!

status = `The winner is ${winner}!`;
}

function handleClick(num) {
Copy link

Choose a reason for hiding this comment

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

I think some of this logic might make more sense to live on the context instead of on the component. That way, the context would define not just the basic state but also an interface for updating the state. Totally up to you though - this is also a valid implementation, just a little bit different!

const updatedHistory = [...history];
const updatedBoard = [...boardState];
updatedBoard[num] = turn;
updatedHistory.push(updatedBoard);
Copy link

Choose a reason for hiding this comment

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

Another option for storing the history is to store a collection of the turns taken as opposed to the entire board. Something like [{turn: 1, move: 'X', square: 4}, {turn: 2, move: 'O', square: 0}] or something like that. That way, you don't have to track the entire board - sometimes that can be a little bit easier to reason about.

@ipc103 ipc103 removed their assignment Apr 19, 2021
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.

3 participants