rorywalsh/wizard

Validating moves and rules..

Opened this issue · 1 comments

@Robzon33 I just pushed some changes to my crazy 8s branch. First thing to note is I should change the name of the branch to Switch, as this is the game I'm prototyping!

In my Switch game class I've created a validateMove() method. It takes the cards in the discard pile, i.e. those card that have been shedded by players, and the card the player is attempting to play. It's quite rough, but I just wanted to see if this approach might be valid:

https://github.com/rorywalsh/wizard/blob/crazyEightsTest/public/gameImplementations/switch.js#L36-L68

In the game state class we try to validate the move:

https://github.com/rorywalsh/wizard/blob/crazyEightsTest/public/game.state.js#L65-L77

So the idea here is we use our game class to check if moves are valid. But the validation check is triggered from the game state class. If people create their own extended game classes, they can add implement their own validateMove methods? Anyhow, just a rough brain fart in terms of actual code. But worth discussing all the same.

Hi @Robzon33 I just deleted that branch! I pushed my changes back into develop. Once again the changes here are merely to test some approaches that might work. If you look at this updated method here:

https://github.com/rorywalsh/wizard/blob/develop/public/gameImplementations/switch.js

You'll see that this method now returns a simple object with a message, and in certain cases an instruction. For example, when a user drops a 2, this method will verify that it's a valid move, and if so return the following:

return {
    message: 'Player played a 2 - next player picks 2 cards',
    instruction: "Pick up 2"
};

That instruction is then passed to the next player. So this simple baseCardGame class can verify if a player has played a legal move, and if so, provide instructions to the players on what happens next. Of course we would need to better abstract the instructions, but for now it's just a proof of concept.

To test it, one could write a new game implementation class and see if it still works. Theoretically, the only code one would need to implement is the validateMove() function. 🤔 Note this should be a member method of one of the base classes...