Thousand Card Game: Sharing stock phase coverage

in #utopian-io7 years ago

image.png

What is this project about?

This contribution is made to gornanization/1k. If you would like to find more info - I strongly recommend reading this post first.

PR:

https://github.com/gornanization/1k/pull/27

Details:

I bet you guys who follows my article series know, that we can continue playing the game from any state we want. This is really valuable. Game state is the one and the only source of truth for the whole game logic in the opposite of object oriented approach, which by design allows you to create huge number of objects, each of them containing some piece of state. By design it’s really hard to maintain. Not speaking about preparing unit tests for specific state of the game. Some time ago (actually, it was three years ago...) I have started writing thousand card game logic in such object oriented way. I have quickly found out that there is no future with this approach, because of unnecessary complexity, decreased readability, huge amount of repeated code, and really hard unit testing.

But, as we are now in totally different world, world of the functional programming, even writing unit tests is real fun (A couple of moth ago I would never imagine myself saying this...). As we want even more fun, let’s test sharing stock case.

Look again at our overall phases flow:

image.png

SHARE_STOCK phase steps in after we have set of players registered, cards assigned to players and a bidding process already finished. Having such set of information (as well as thousand card game rules in mind) we can prepare this case state object:

image.png

Let's review together the state of the game. We have set of players registered (see players property) and the bidding finished (bid). Latest bid offer is on the very beggining of the bid array. In last article we were testing bidding process. You are familiar with this, so the bid array is just a copy of that information.

But are we sure the bidding phase is finished?

Yep - two players have passed, so the bid winner is alan with the 120 value. As he is a winner of bidding, he received extra three cards from stock, which is empty now. Alan have three cards more (7 + 3 = 10) comparing to a opponents.

State is ready, what we want to test? Firstly focus on phases flow. from SHARE_STOCK we can navigate into two states. BATTLE_START and BOMB_DECLARED

Consider case of SHARE_STOCK -> BATTLE_START. When will it happen? When bidding winner shares cards to opponents, so then all players will end up with 8 card. We can achieve it by calling:

thousand.shareStock('alan', 'J♦', 'pic');
thousand.shareStock('pic', '9♦', 'adam'); // this is invalid and should be skiped
thousand.shareStock('alan', '9♦', 'adam');

By doing such set of actions, we successfully transit to BATTLE_START. But while actions are performed, additional events are propagated. We do check them. Also, result of above's actions return boolean value representing whether operation succeed or not. Deliberately, we put some invalid action between those allowed to check, whether validation phase was even called. Second action (thousand.shareStock('pic', '9♦', 'adam');) is invalid, as pic is not the bidding winner, so it will be omitted. It returns false and we also check it. And that's it.

How about SHARE_STOCK -> BOMB_DECLAREDcase? We do follow the same set of rules as described above. Feel free to review this case as well by looking into linked PR.

Within the change, coverage increased (+3.8%) to 93.994%. Mission passed for today.

See you soon!



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Truth never damages a cause that is just.

- Mahatma Gandhi

Congratulations! This post was randomly selected and upvoted by @resteemr!


@resteemr is curating posts tagged #technology this week.
Read More about it here.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @adasq I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Congratulations @adasq! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of comments

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.027
BTC 60256.67
ETH 2327.64
USDT 1.00
SBD 2.46