Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m Advertisement # Making a Blackjack Game in Corona - Implementing Gameplay Difficulty:IntermediateLength:LongLanguages: In the previous part of this series, we put our interface together for a Blackjack game and created the deck of cards. In this part of the tutorial, we'll add the necessary Blackjack game logic. Let's get started! ## 17. CreateDataFile() We need a way to store the player's money between game sessions, and to do this we'll use a simple text file. Add the following code beneath the createDeck() function. Here we are creating a file named "data.txt" and writing 500 to it. The player will start the game with$500.00. It's important to make sure you always call io.close() when you're finished with your operations.

Now that we have a way to create our data file, we need a method to read its contents. Enter the following beneath the createDataFile() function you entered in the step above.

We open the file using the same method, then we use read("*n") to get the value out of the text file. The "*n" means read as a number.

## 19. SaveMoney()

To complete our file handling operations, we need a way to save. Enter the following beneath the code you entered in the step above.

Here we open the file for writing as denoted by the "w" in the open() method. We then write money to the file that was passed in as a parameter.

## 20. Setting the Initial Balance

We now need to create the initial balance when the game first starts. Add the following to the top of Setup().

If you open the Corona terminal and run the app twice you should see "DataFile Exists Already" printed out to the terminal. I left the print() messages in the file handling code so you could see the steps and any errors. If all is working well, feel free to remove them.

## 21. Showing Initial Balance

So now that we have the balance set, let's show it in our app. Change the following code within the setupTextFields() function.

## 36. Finishing GameOver()

After each round we move the coins to the winner and then start a new game. Enter the following beneath the code you entered in the step above.

Here we see who won the round, and then we animate the coins to them. When the animation completes we remove all the coins from the coinContainer, and set them to nil since we are done with them. Lastly, we call newGame() after two seconds, we also reset our coinContainer position.

## 37. NewGame()

Enter the following beneath the doGameOver() function.

Here we set the money to visible, remove the cards from both the player and the dealer groups, and reset all of our variables.

## Conclusion

We've coded a fun and interesting blackjack game using the Corona SDK. Thanks for reading, I hope you found this tutorial helpful!