The project was to create a blackjack simulator that could run games for one player and a dealer and record the results. The purpose of this project was to learn about Java and its basic data structures like ArrayLists. I used ArrayLists as well as a custom Stack class to store my data. The Stack class was similar to Java's built in stack, although it had an extra swap method which helped me with shuffling the deck. Using the Simulation method, the code runs 1,000 games of Blackjack and returns the results. The dealer usually wins about 49% of games, while the player wins about 42%.
The main data structure used in this project was a Stack. It stores ordered data with a top and a bottom. When you "push" to the stack, you add a new value, and it becomes the new top with the old top directly below it. When you "pop" from the stack, you take the top value, and make the value below it the new top. The built-in Stack in java also has a peek() method for looking at the top card without removing it, but that is slightly redundant at you can just use get(0). The Stack structure is perfect for storing a deck of cards, as popping a method off the top models dealing a card very well. The Stack store stores data using "Nodes." Each node stores both a value, and a "next," which is the Node below it in the stack (the bottom node will have next = null). The Stack class only stores the top Node and accesses the rest by running through each Node's next.
In my Stack, I added a swap() function which allows me to shuffle it more easily. I also leave out several functions like peek() because I didn't need them for the code. My Stack only stores the deck, and not either hand because unlike the Deck, the hands aren't ordered. The size of my Stack is kept track of using an int, when I push to the Stack, I increment it by one, and when I pull from the Stack I decrease it by one. This way, when I call the size() function I don't have to count each Node, just reference the size variable. The clear() method is very easy with a stack, because all it needs to do is set the root to null and the size to 0. Using Stack made the deck very easy to store.
The dealer always wins more games than the player. Here are the outputs from running the games 3 times:
Player has hand (2 5 ) with value 7. Dealer has hand (J A ) with value 21.
Player has hand (2 5 10 ) with value 17. Dealer has hand (J A ) with value 21.
Player has hand (2 A ) with value 13. Dealer has hand (2 6 ) with value 8.
Player has hand (2 A 4 ) with value 17. Dealer has hand (2 6 Q ) with value 18.
Player has hand (8 4 ) with value 12. Dealer has hand (Q 8 ) with value 18.
Player has hand (8 4 9 ) with value 21. Dealer has hand (Q 8 ) with value 18.
Additionally, here are the results of running 100,000,000 games (the number of games run in the Simulation class can be adjusted using the numGames variable):
Dealer won 49176406 games. 49.176406% of total games.
Player won 42020429 games. 42.020429% of total games.
Tied 8803165 games. 8.803165% of total games.
These percentages are very representative of the results when running multiple games.
I made my own Stack class to hold the deck of cards. It has all the functions needed to function properly, using linked nodes to hold a generic data type.
I made face cards that print their letter instead of value when the Card's toString() method is called.
I made aces count as 1 or 11 depending on what is more advantageous.
I made a way to execute more than 1,000 games in the simulation class, which prints percentage completion and keeps track of the time it takes to execute all the games.
I learned how to use generics and use them to implement a data structure. I also learned how to shuffle data more effectively. Additionally, I learned how to use time and random numbers in java.