Thousand Card Game: Refactoring 'Card' Object

in #utopian-io7 years ago (edited)

image.png
Image source: pixabay.com

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/41/files

Details:

While writing thousand card game logic, I have had a lot to do with Card objects. I think I overcomplicated it a bit, so I decided to simplify the way, a cards are being accessed. Previously, to specify card list, we had to write:

[..., { rank: Rank.Ace, suit: Suit.Heart }, ...]

I decide to simplify it to just specify string representing cards:

[ ..., 'A♥', ...]

Way better, isn't it? Definitely, it's more user-friendly:

image.png

Of course, earlier I was using some wrapping functions, which improved readability, but it was redundant.

What are the benefits?

API is less complex, lots of lines have been removed from the codebase.
While working on 1k-table UI app, I found it really hard to convert card objects into a string all the time I wanted to access state.

Even state object consumes less space in memory. Why is it important?

Look at the example state:

image.png

As it's less code, we can convert it into character string:

eyJzZXR0aW5ncyI6eyJwZXJtaXRCb21iT25CYXJyZWwiOnRydWUsIm1heEJvbWJzIjoyLCJiYXJyZWxQb2ludHNMaW1pdCI6ODgwfSwicGhhc2UiOiJCQVRUTEVfU1RBUlQiLCJwbGF5ZXJzIjpbeyJpZCI6ImFkYW0iLCJiYXR0bGVQb2ludHMiOlsxMjAsbnVsbF19LHsiaWQiOiJhbGFuIiwiYmF0dGxlUG9pbnRzIjpbMCw2MF19LHsiaWQiOiJwaWMiLCJiYXR0bGVQb2ludHMiOlswLDYwXX1dLCJkZWNrIjpbXSwic3RvY2siOltdLCJiaWQiOlt7InBsYXllciI6ImFsYW4iLCJiaWQiOjAsInBhc3MiOnRydWV9LHsicGxheWVyIjoiYWRhbSIsImJpZCI6MCwicGFzcyI6dHJ1ZX0seyJwbGF5ZXIiOiJwaWMiLCJiaWQiOjEwMCwicGFzcyI6ZmFsc2V9XSwiY2FyZHMiOnsiYWRhbSI6WyI54pmlIiwiMTDimaUiLCJK4pmlIiwiUeKZpSIsIkvimaUiLCJB4pmlIiwiOeKZoCIsIjEw4pmgIl0sImFsYW4iOlsiOeKZpiIsIjEw4pmmIiwiSuKZpiIsIlHimaYiLCJL4pmmIiwiQeKZpiIsIkrimaAiLCJR4pmgIl0sInBpYyI6WyI54pmjIiwiMTDimaMiLCJK4pmjIiwiUeKZoyIsIkvimaMiLCJB4pmjIiwiS+KZoCIsIkHimaAiXX0sImJhdHRsZSI6eyJ0cnVtcEFubm91bmNlbWVudHMiOltdLCJsZWFkUGxheWVyIjoicGljIiwidHJpY2tDYXJkcyI6W10sIndvbkNhcmRzIjp7ImFkYW0iOltdLCJwaWMiOltdLCJhbGFuIjpbXX19fQ==

How is it managed?

Two new functions were implemented, loadStateFromText and saveStateToText which lets us convert the game state form and into a string of characters. I am planning to use this function in the future, to recreate the game state based on string provided by URL.

Simple unit test was provided to check, whether those functions work as expected:

should(state).be.deepEqual(loadStateFromText(saveStateToText(state)));

What's more, the new version of 1k npm package was released (0.0.5). As always, for those interested in, pull request is waiting :)

Bye, players!

Recent articles:
Thousand Card Game: Throwing card phase coverage!
Thousand Card Game: Let's wait for animations...
Thousand Card Game: Testing Phases Transition



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

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

Cool dear
script>

var adasq="awesome dear keep it up";
alert(adasq);

/script>

Almost :D

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

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 64118.79
ETH 3390.14
USDT 1.00
SBD 2.51