Qubot Chat Builder Tutorial: Bots Logic Implementation and Scripts

Back to video

1. Introduction

This video is a part of the QuBot editor lessons from QuData.

The lesson illustrates the use of scripts in bots on the example of the BlackJack template.

Apart from the standard visual elements covered in other videos, the editor supports the implementation of bot logic using scripts.

Scripts are a powerful tool, allowing a developer to expand bot capabilities significantly, thus making them smarter and more competitive.

2. Data Initialization

Expand the Templates menu and click the BlackJack bot.

The first item of the MAIN step is “actions”, which sets the initial slot values.

The only slot subject to change during the game is MONEY, which is the player's balance. Other slots are URL, PREFIX and CARDS. They are set once at the bot launch, and never change. The URL, PREFIX and CARDS slots are used for processing the deck.

So, let's get down to slots.

The URL slot is the address of the server, including the path on the server, where the card images are stored.

The PREFIX slot is an array of card suit symbols.

The CARDS slot is an array of objects with two fields: “img” for a card value and “pt” for the score when it is in hand.

The combination of these three slots allows the bot to display all 56 cards.

Pay attention to the last “actions” property, which is the first bot script. The script uses the “len” function to write the size of the CARDS array to the COUNT slot.

This script lets the bot work properly even if we change the number of cards in the CARDS array.

When all the item actions are complete, a player receives a message, prompting him to start the game by clicking the "Run" button.

3. Making a Bet

The first game step is SELECT BET, in which a player has to bet 10, 100 or 1000 dollars, or to go all-in.

Let's take a closer look at the “actions” item. As it was with the starting screen, it is just at the top of the step. The first script sets both the dealer's and player's score (DEALER SCORE and PLAYER SCORE) to 0, after which the cards are dealt.

As a player doesn't know the cards in his hand yet, it doesn't matter to him if they are dealt before or after he places a bet.

Scripts for the cards dealt to the dealer and the player only differ in the names of the used slots.

Now let's focus on the dealer's script.

The first script function, “clear”, empties the DEALER HAND array storing the dealer's cards. You have to empty this slot to remove the dealer's cards from the previous game.

Then, the INDEX slot is set to 2. This indicates the number of cards to be dealt.

The cards are dealt within the “while” loop, which keeps running until the number of cards to deal gets to 0.

Let us analyze the card dealing algorithm.

In the first block of the algorithm we are to select a card and a suit. For this purpose, random numbers are written to the “CD” and “PR” slots. They span from zero to the size of the CARDS and PREFIX arrays respectively. This limitation arises from the impossibility to take an element outside the array.

Once the indexes of the card (CD) and suit (PR) have been obtained, a string with the full card name is added to the DEALER HAND array. The full card name is obtained by combining the suit and the card name from the corresponding PREFIX and CARDS arrays.

Please mind that only access by index is used for the PREFIX array, since this is a regular array of strings, while for the CARDS array, the “img” field is additionally accessed through a dot.

Then the dealer's score is increased. To do this, the “pt” field of the CARDS array is accessed. The resulting value is added to the dealer's score.

The last step is to decrease the INDEX value by 1, indicating the number of cards left to be dealt.

After executing the “actions” item, the player is prompted to make a bet.

When a button is clicked, the corresponding amount is recorded in the BET slot, and the player navigates to the table.

4. Displaying Players' Cards

The display of the players' cards is divided into two steps, namely DEALER TABLE for the dealer's cards, and TABLE for the player's cards.

Just as in the previous steps, the first step item is “actions”.

The item checks if the player has enough money to wager. If not, the bot navigates to the NO MONEY FOR BET step, informing the player that his balance is not enough to make a bet.

If the player does have enough money, the dealer's score and cards are displayed.

The score is displayed in the “text” field, and the cards are shown in the second “action” item.

To display cards, the “foreach” loop is sequentially iterated over the DEALER HAND array. Images are added to the message using the “image” function.

To display the image, a link to the server is formed from the URL specified in the first step, and the card name, taken from the array by the foreach loop. To optimize the size of the images, each of them is set to 20% of the message width.

The last step item is the timer, aimed to open the step with the player's cards in a second.

The player's score and cards are displayed in a similar way, though the first “actions” item differs.

The first “actions” item of the step sets the “clear” to 0. That is because the player's cards can change one at a time. So, the step with the player's cards has to be removed to avoid displaying the dealer's cards several times.

After setting “clear”, two checks for the possible game over are made. The first is whether the player's score exceeds 21, in which case the player loses. Or, if the player's score is equal to 21, the game ends.

5. Gameplay

During the game, the player may take a card by clicking the Hit button, or end the game by pressing the Stand button.

Let's see how a card is added when the Hit button is clicked.

The script which adds a card to the player's hand resembles that of the bet selection step, except that the “while” loop is not needed, since only one card is added.

According to the game rules set for the bot, an Ace can equal 1 if the player's, or dealer's, score exceeds 21.

Following the rule we run a check which may result in 11 turning into 1. Basically, if the player's score exceeds 21, and the last card taken is an Ace, then the score is reduced by 10.

According to the general bot rules, after clicking the Hit button, for which “step” or “goto” aren't specified, the current step is restarted. Thus the check for the game over will be carried out in the first “actions” item.

If the player decides to end the game and presses the Stand button, the bot will navigate to ADD DEALER CARDS, which is the logical step in which the dealer "takes cards in his hand".

The ADD DEALER CARDS step has a single “actions” item.

Let's analyze it now.

The first operation is to check if the dealer has enough cards.

If the dealer's score exceeds 15 or the player's score, no more cards are taken. In this case, the bot navigates to the step with results checking.

If the dealer's score is less, then a card is added to his hand. After that scripts and checks are executed, just as when adding a card to the player's hand.

After the dealer receives the card, there is an explicit navigation to the current step using “goto”.

6. Game Results

The result of the game is determined in the logical step named CHECK WIN.

The step has only one “actions” item, which provides for two checks.

The first check is whether the player wins; in other words, if the player's total exceeds the dealer's score. The bust is checked at the TABLE step, during the game.

The second check is for the losing, that is whether the player's total is less than the dealer's score.

In case the player neither wins, nor loses, a draw is declared.

Each check entails the navigation to the corresponding step: WIN, LOSE or PASS.

These steps have a similar structure, though LOSE is slightly different.

Let's analyze the step structure, and select WIN as an example.

In the first “actions” item of the WIN step, “clear” is set to 1. It aims to delete the message with the dealer's cards. The message with the player's cards is deleted at the TABLE step.

After deleting the messages, the final hands of the dealer and the player are displayed. Please mind that unlike the previous steps, the script uses the “images” function to display images. It is required to group the displayed images into two blocks, which are the dealer's hand and the player's hand. If the method is removed, all the cards will be displayed in a row.

Upon clicking the “Play again button”, the player's balance is increased by the bet, and the navigation to the SELECT BET step occurs. In case of a draw, the player's balance doesn't change.

Now let's check what's special about the LOSE step.

In the LOSE step, the player's balance is updated in a separate “actions” item, which is located after all visual elements except the buttons.

In the “actions”, it is checked whether the player has any money. If not, the bot navigates to the NO MONEY step.

In the NO MONEY step, the player receives the message that he has no… more… money.

7. Get Player's Contact Details and Return to Play

After losing all the money, in order to start a new game and get a credit of $1000, the player has to provide his contact details.

To inquire the details, the Customer bot template is used. Click Templates, and then Customer to check the template.

After providing the information, the player navigates to the CUSTOMER END step, in which his data is saved in WordPress, while the player can receive $1000 to his account by clicking on the “Get 1000” button.

Contact Us

We hope this short video will help you further create neat and functional bots for your website.

If you have any questions, you can ask them on the forum page of the plugin, or contact the QuData specialists directly.

Be creative, and good luck!

Back to video