Thousand Card Game: Building a Game Bot

in utopian-io •  last year  (edited)

Image source:

What is this project about?

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



Here is a list of features / improvements applied within the contribution:

1. Bot Feature

1.1 Why?

Just for testing purpose. Thousand is a three-players (might be extended to four!) card game, so testing it is a hell sometimes.

1.2 How is it implemented?

Firstly, we have to spot which game phases requires user action. This includes:

  • throwing cards,
  • bidding,
  • sharing stock cards with opponents.

So we have three actions to cover. The bot is intended to be as simple as possible, so forgive me its low IQ level =)

How does the bot respond to a given actions?

  • throwing cards - it will throw first possible card, no matter it was not worth doing so.
  • bidding - it will not bid at all. Just pass.
  • sharing stock cards with opponents - it will select and share first card, no matter its real value.

Such non-complex approach was embodied with just 48 lines of code (see source code here)

The bot logic is wrapped with getPreferredAction function:

const action = getPreferredAction('adasq', gameState)
// action might be:
// {type: 'throwCard', args: ['9♣', 'adasq']}

The function takes a player name and a game state. On the other hand, it returns action which might be performed by the player adasq according to the given gameState.

This is a pure bot logic. Now we have to link it to the UI. The state of each game is stored in Firebase Realtime Database.

Thanks to the Google's tech stack, we can also take an advantage of Cloud Functions for Firebase capabilities. It allows us to watch for changes in database and then run some javascript code.

This is exactly what we need here. We need to watch for changes in the game state, and then (when possible) perform a bot action.

The code of Firebase Function is short. You can preview it here. What it does is watching for changes on game state. Then we are checking, whether the given game has a bot player. If so, we do check whether the bot can perform any action and eventually perform it.

Next step is to update game client in order to let players invite a bot.

A new button, INVITE BOT was added (code for it here):

Each piece of code (described above) works together to provide fully responsive opponents experience:

2. Multiple action bug fixed

2.1 Why?

Thousand card game logic allows to put our async logic in the middle of state transition. It's fine, becasue sometimes we want to perform animation which takes time.

On the other hand, we have to prevent on watching for new action (and managing it) while animation is being performed.

2.2 How is it implemented?

I resolved this issue by creating boolean flag in global store, which informs us whether the animation is in progress:

//performing animation...

Each animation execution is now wrapped with Promises, so we have flow under control. After promise resolves, we can markAnimationAsFinished.

This changes might be previewed here

3. Public availability

3.1 Description

Frontend build system was linked with Heroku CI, so each PR merge to both projects: 1k-client or 1k-table forces a new features to be available for public under given addresses:

client for phone
client for TV

For those who would like to give it a try:

  1. Create a new game table by navigating You will be assigned with specific room name.
  2. Open, pick a name, then find your room, take a sit to the table and invite bots.
3.2 How is it implemented?

Projects are temporary hosted on Heroku Cloud Platform.

The following steps are applied during application deployment into remote server:

  1. npm install (installing required dependencies).
  2. npm postinstall (over here we forces frontend resources to be build, see here).
  3. npm start (we are starting simple http server, which will provide UI side to the public).

Described above steps are common for both 1k-client and 1k-table.

4. Declaring a Bomb

Bomb declaration button has been added to the client app.
It's a simple action request being sent to the database. More here.

More details? See PRs linked above!

Cheers, players!

Recent articles:

  1. Thousand Card Game: Not enough points to start a battle...
  2. Thousand Card Game: Increasing bid value!
  3. Thousand Card Game: Connecting to the game table

Posted on - 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:  

Hey @adasq! Thank you for the great work you've done!

We're already looking forward to your next contribution!

Fully Decentralized Rewards

We hope you will take the time to share your expertise and knowledge by rating contributions made by others on to help us reward the best contributions together.

Utopian Witness!

Vote for Utopian Witness! We are made of developers, system administrators, entrepreneurs, artists, content creators, thinkers. We embrace every nationality, mindset and belief.

Want to chat? Join us on Discord


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

You got your First payout
Award for the total payout received

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

Upvote this notification to help all Steemit users. Learn why here!