Closed russfraze closed 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.
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
@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 theflipped
array in context could contain the indices of the tiles, rather than the tiles themselves. This would make it easier to set theisMatched
value of each tile because theflipped
array could be mapped over and the objects at each index could be manipulated from there.
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?
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.
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.
I also noticed that matching tiles are still reset after the timeout when they should remain flipped up for now.
## 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.