Thousand Card Game: Let's test 'battle' API

in utopian-io •  7 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/30

Details:

Lately we have covered phase transition from the very beginning of the game (registering users, more here) until the stock sharing. Now, the best part steps in. Let's cover a battle!

image.png

Battle starts after SHARE_STOCK phases (when the bidding winner shares two cards with opponents). It finishes, after last player will throw last card. Battle includes 8 sub-battles also known as a trick. During the trick, each player throws one card. We have three players so in one trick 3 cards are thrown. Quick calculation - within the whole battle we have 8 * 3 = 24 cards to be thrown and it's basically a entire deck. After that, we can shuffle the deck again, share cards, start bidding and so on...

Within the contribution I prepared unit test for the first trick in the battle. I prepared consistent state for it. Please look at this:

image.png

What you can see above is a state in the middle of a trick.

We have three players. cards is a set of cards they are holding right now in their hand. We have also battle, which show us a state of current battle. Inside battle we have leadPlayer which is basically the player, who is allowed to start a trick. trickCards is set of cards on a the table. We have one card over there so it means we are in the middle of the trick (That's why phase points to TRICK_IN_PROGRESS).

And now the question is, whose turn is it now? Who can throw a card and is this state valid? I mean, is it possible to reach exact state by starting from very beginning of the game (player registration and so on)? You have to believe me, it is valid. And I will explain you why.

First of all, we can say whose of the players was registered first. It's adam, because he is the first place of players array. Next registered player is alan and pic. So the general order is:

Firsty, adam then alan and then pic...

Please, keep that in mind, as it will be really helpful later.

Now, please focus on battlePoints in those player objects. It is a set of numbers representing total points which was assigned to the player after the end of the battle. We have two entries in this field for each player so this is why we have two battles done. We know we are in third battle already.

Ok, now look at bid array. Who is the bid winner? It's pic as his bid value is 100 and the opponents have passed. The order of this array reversed, so the most recent bid activity is on the beginning of the array. Having such information we can say that the default person for this bid was pic, as his bid value is 100. In each round, default player changes, so the another question comes in.

Why pic was selected as the default bidder for current (third) battle?

It's simple, as adam is in the first player in our order, he was the default bidder in first battle. Second battle was initialized by next player in order, alan so in third one we have pic.

Now focus on battle. Over there we have wonCards. You know, that in each trick we have three cards thrown from each of three players. alan have three cards in wonCards so it means, that he won the first trick. As he won, he is a new leadPlayer. As he is the lead player he can initialise next trick. And he have already done this, by throwing 9♦ (see trickCards).

So, whose turn to throw a card is it now? We can retrive this information by looking inside trickCards. The first card is thrown by leadPlayer always, so the next player will be next player in turn after alan. And it's pic.

pic is a person who can throw a card now. So in our test we are checking whether action thousand.throwCard(createCard('A♠'), 'pic') is allowed to be performed. And it's also a part of the current contribution.

As you can see, we can design really specific game cases and test it from every angle. It's massive! That's it for today!

See you soon, thousand'ers!

PS. Coverage increased (+1.7%) to 95.536%



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

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