perennialAutodidact / memory-snap

0 stars 0 forks source link

6 tilegrid reset unmatched pairs #60

Closed russfraze closed 1 year ago

russfraze commented 1 year ago

## Description of changes I added a flipped property to the game context which is an empty array. When flip tile is called, the tile is added to the flipped array. Once the length of flipped is two, the reset tiles action is called, checking that the tiles don't match and then reseting their faceUp values to false.

Test steps

Flip two game tiles and check that they flip back automatically if they don't match.

russfraze commented 1 year ago

I added a test to check that the flipped tiles faceUp value is toggled when the RESET_TILES action is called. I think this PR could be ready to go. Please take a look.

I can see that we will have to prevent the user from clicking tiles once they have been flipped, but thats an easy change. Let me know if you want me to make that happen on this PR or another ticket.

perennialAutodidact commented 1 year ago

I also don't see a test for flipping the tile. There should be a tests for:

These tests will probably have to live in the TileGrid component

russfraze commented 1 year ago

@perennialAutodidact

I think something that'd be helpful in this would be reinstating the companion property for each tile object which would have the index in the array of the companion tile. Then the flipped array in context could contain the indices of the tiles, rather than the tiles themselves. This would make it easier to set the isMatched value of each tile because the flipped array could be mapped over and the objects at each index could be manipulated from there.

Screen Shot 2023-10-11 at 12 49 41 PM

I was intending to use a HANDLE_MATCH action to toggle isMatched, like this.

I'm clear on the other changes you requested for this PR but I'm still not seeing the benefit in having a companion property. Is that less complicated than doing it like this?

perennialAutodidact commented 1 year ago

The only issue I would see with that would be that it would require some kind of logic for deciding when to dispatch the HANDLE_MATCH action and it's recommended to put as much state-manipulating logic in the reducer as possible so it's all in one place. I think the matching logic could be done in the FLIP_TILE reducer when a tile is flipped.

perennialAutodidact commented 1 year ago

My thinking with the companion property is that we could use the index of a tile's companion to make it easy to toggle both of them without needing to loop through the array.

I'm still not 100% convinced it's the best way to do it, but it was the first thing that came to mind.

perennialAutodidact commented 1 year ago

I also noticed that matching tiles are still reset after the timeout when they should remain flipped up for now. tile_bug