Sum To 21

This is a simple game where we (player) will start the game and play against a bot. The game starts at sum 0. We can add either 1,2 or 3 to the sum and then end our turn, then the bot has to do the same. This will happen until one of the player reaches 21. The code is pretty simple for the game, but how will the bot play?

For any game when the bot has to play, I used the 'rand' function which allows the computer to randomly generate a number. Then we manipulate this number in such a way, that it can have the ability to understand what to do when. In simple terms, understand the game. Similarly,I gave a function with trash talking statements mapped to a number and then on random it makes it appear as though the bot is trash talking. Also, I inserted the 'usleep' function just to add some unwanted yet a feel of realism to the game. Like make the CPU stop doing anything while thinking or while trash talking.

Here is the source code in notepad (just to avoid copy). The algorithm behind this is that 'First To 21' is very much a solved game. If you work the game backwards, then you will know that the first person to reach 1 will win the game. That's why we start the game first. Those who want to play for fun can play against medium and easy level where the algorithm does not try to win from the very beginning, but hard level wins if you don't reach 1. If we try to outsmart by never reaching 1. Then again by working backwards, the first person to 5 will win. So this simple algorithm allowed me to make a bot to play against.