Skip to content

Commit

Permalink
added second blackjack model
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbodnar committed Jun 30, 2019
1 parent a085c9c commit 63c624f
Show file tree
Hide file tree
Showing 7 changed files with 10,755 additions and 54 deletions.
10 changes: 5 additions & 5 deletions Blackjack.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def hand( montecarlo, debug ):
# if hitting
if choice is "h":
# add data
data = data + [ hand_value( players_hand ) ]
data = data + [ [ hand_value( players_hand ), int( dealers_hand[0][:dealers_hand[0].index('-')] ) ] ]
# hit
players_hand = players_hand + [ deck.deal() ]
summ = hand_value( players_hand )
Expand Down Expand Up @@ -118,7 +118,7 @@ def hand( montecarlo, debug ):
# if staying
else:
# add data
data = data + [ hand_value( players_hand ) ]
data = data + [ [ hand_value( players_hand ), int( dealers_hand[0][:dealers_hand[0].index('-')] ) ] ]
if debug:
print( "Staying" )
# print current game
Expand Down Expand Up @@ -185,15 +185,15 @@ def hand( montecarlo, debug ):
# data points to the blackjack data set
def gen_data_set( ):
# loop through a thousand simulations
for i in range( 3000 ):
for i in range( 2000 ):
print( i )
data, tags = hand( True, False )

print( data )
print( tags )

dataf = open( "data_sets/blackjack.data.1", "a" )
tagf = open( "data_sets/blackjack.tags.1", "a" )
dataf = open( "data_sets/blackjack.data.2", "a" )
tagf = open( "data_sets/blackjack.tags.2", "a" )
for datum in data:
dataf.write( str( datum ) + "\n" )
for tag in tags:
Expand Down
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# card-games
A collection of Python classes and scripts for using card games in Monte Carlo simulations and Machine Learning.
A collection of Python classes and scripts for using card games in Monte Carlo simulations and Machine Learning.

# Deck.py
Deck.py is a class for a deck of cards. An instance of this class holds an array of 52 strings representing cards. Card strings consist of the face value and a suit seperated by a hyphen. Face values { A, 2, ... Q, K } are represented as { 1, 2, ... 12, 13 }. Suits are { 's', 'd', 'c', 'h' }. Some examples of valid card strings are '1-s' for the ace of spades, '4-c' for the four of clubs, and '12-h' for the queen of hearts.

Usage:

from Deck import Deck
deck = Deck()

Functions

deck.shuffle()
returns the array to 52 randomly organized card strings.

deck.deal()
removes a card string from the Deck object, and returns it

deck.checkDeck()
prints the current content of the deck


# blackjack.py
Blackjack.py is an implementation of the classic card game using the Deck class, and played from the terminal. This script is used to generate data about hands of blackjack via Monte Carlo simulations. This is done by generating random hands and storing representations of the hands, tagged with the eventual outcome of the decision.
Expand Down Expand Up @@ -109,21 +129,16 @@ The model learned to hit on any hand value below 17. This happens to be the stra

Future data sets and models will focus learning more about the hand, ie. dealers cards, counting how many times a player has hit, etc. The longer term goal is to teach a model to count cards.

# Deck.py
Deck.py is a class for a deck of cards. An instance of this class holds an array of 52 strings representing cards. Card strings consist of the face value and a suit seperated by a hyphen. Face values { A, 2, ... Q, K } are represented as { 1, 2, ... 12, 13 }. Suits are { 's', 'd', 'c', 'h' }. Some examples of valid card strings are '1-s' for the ace of spades, '4-c' for the four of clubs, and '12-h' for the queen of hearts.
# second Blackjack model

Usage:
This model will use all the previous techniques, but the data set will now include the dealer's upward facing card.

from Deck import Deck
deck = Deck()
So where previously we used the single integer for data, we will now use a tuple of players_hand, and dealers_hand respectively.

Functions

deck.shuffle()
returns the array to 52 randomly organized card strings.
Example

deck.deal()
removes a card string from the Deck object, and returns it
[ 18, 13, s ]

deck.checkDeck()
prints the current content of the deck
The data set consists of 5,323 entries, located in data_sets/blackjack.data.2 and data_sets/blackjack.tags.2.. The neural network used a similar layer scheme as the previous, with an 8-neuron second layer. The optimizer was 'nadam,' and there were 100 epochs.

The model has an accuracy of 0.74. A slightly better accuracy than model #1.
Loading

0 comments on commit 63c624f

Please sign in to comment.