Skip to end of metadata
Go to start of metadata

This first project for Data Structures and Algorithms helped me familiarize myself with object oriented Java programming. Simulating a game of Blackjack was a suitable task because the game lends itself well to objects, where a card, hand, deck, and blackjack game all take their own class. Blackjack also has a relatively simply rule set, allowing for good models out of simple code.

The most interesting part of this project for me was implementing the "basic strategy" of Blackjack. Blackjack is a game without any real complexity or decision making; the best player options for any permutation of visible cards can be summarized in a table. Even following this strategy, the player is bound to lose to the house in the long run. This basic strategy of play is summarized on wikipedia here: http://en.wikipedia.org/wiki/Blackjack#Basic_strategy. I decided to simplify the game by eliminating the double down, split, and surrender rules, but I did implement an ace rule so the player can have a hard or "soft" hand.

I turned that wikipedia table into a rather long playerTurn method with if-then statements and while loops to make the player always take the best choices considering his hand and the dealer's one visible card. Critical to my playerTurn method was having a way to test whether his hand was "hard" or "soft." A hard hand is a hand without an ace, or with an ace that has to be considered low for the player not to bust. A "soft" hand is a hand where the ace can be taken high or low, and the player is not busted either way. There are different strategies for hard and soft hands so I had to make a way to test the hand's hardness. A loop through the hand that finds no aces guarantees that the hand is hard, but a hand with an ace that totals 12 or more when the ace is taken as low is also hard. Only a hand with an ace that values less than 12 is a soft hand. I had to code all of this into an algorithm.

I did the 1000 game simulation using both the ace rule and the no ace rule versions of the game. It was interesting to see that the two versions didn't show much variation in win percentages. Both versions showed the player winning about 43% of the time and losing about 48% of the time, with a standard deviation of about 10 times the difference between the win percentage of the two versions. The conclusion I draw from that is that Blackjack is an arbitrarily complex game that gives the player options that appear to add choice and strategy but really don't change the long term outcome, i.e. losing 5% of your money. 

My stats:

 

playerwins

dealerwins

draw

no soft

463

445

92

 

422

491

87

 

427

501

72

 

430

486

84

 

444

482

74

 

425

484

91

 

417

503

80

 

477

450

73

 

414

504

82

 

409

504

87

avg

432

485

 

stdev

22.0

21.5

 

 

 

 

 

soft

444

463

93

 

438

469

93

 

441

484

75

 

465

439

96

 

413

493

94

 

415

495

90

 

442

476

82

 

429

497

74

 

418

479

103

 

425

478

97

avg

433

477

 

stdev

16.1

17.4

 

Labels