//0 for stand, 1 for hit public int choosePlayerAction(Hand playerHand, Hand dealerHand, Deck deck) { int action = 0; int playerVal = playerHand.getValue(); int dealerShown = dealerHand.getDealerShowing(); var aceVal = playerHand.getAceValue(); var dubVal = 0.0f; if (playerHand.canDouble()) dubVal = 1.0f; var splVal = 0.0f; if (playerHand.canSplit()) splVal = 1.0f; state = genInputVec(playerVal, dealerShown, deck, aceVal, dubVal, splVal); float[] qScores = new float[numActions]; for(int act = 0; act < numActions; act++) { var input = actionPlusState(act, state); var a = net.forward(input); qScores[act] = a[0]; } //do epsilon greedy action selection if(eps > r.NextDouble()) //choose best action { action = getMaxAct(qScores, playerHand); } else // choose worst { action = getExploreAction(qScores, playerHand); } action = shouldSplit(playerHand, dealerHand, action, playerHand.canSplit()); return action; }
public void PrintCards(Hand hand) { string Cards = ""; foreach (Card c in hand.Data) Cards += c.ToString() + ", "; Console.WriteLine(hand.Name + " cards: " + Cards.Remove(Cards.LastIndexOf(",")) + " ("+hand.TotalPoints+" total)"); }
public int chooseDealerAction(Hand dealerHand) { int action = 0; var dealerVal = dealerHand.getValue(); if (dealerVal <= 16) //hit 16 and less { action = 1; } if (dealerVal >= 17) //stand 17 and more { action = 0; } return action; }
public float[] getQScores(Hand playerHand, Hand dealerHand, Deck deck) { int playerVal = playerHand.getValue(); int dealerShown = dealerHand.getDealerShowing(); var aceVal = playerHand.getAceValue(); var dubVal = 0.0f; if (playerHand.canDouble()) dubVal = 1.0f; var splVal = 0.0f; if (playerHand.canSplit()) splVal = 1.0f; state = genInputVec(playerVal, dealerShown, deck, aceVal, dubVal, splVal); float[] qScores = new float[numActions]; for (int act = 0; act < numActions; act++) { var input = actionPlusState(act, state); var a = net.forward(input); qScores[act] = a[0]; } return qScores; }
private static List<double> simpleRandomStrategy(int numOfHands) { var ret = new List<double>(); int totalHandsPlayed; double winLoss = 0.0; //-1 for loss, +1 for win. +1.5 for blackjack. 0 for draw var policy = new RandomStrategy(); int numBlackJacks = 0; int numWins = 0; int numDraws = 0; int numLosses = 0; //do each hand for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++) { Deck deck = new Deck(6); deck.shuffleCards(); Hand playerHand = new Hand(); Hand dealerHand = new Hand(); //deal initial cards playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); if (playerHand.getValue() == 21 && dealerHand.getValue() != 21) { //BLACK JACK numBlackJacks++; winLoss += 1.5; } else if (dealerHand.getValue() == 21) { //Dealer BJ numLosses++; winLoss -= 1.0; } else { //player decisions var action = policy.choosePlayerAction(playerHand, dealerHand); while (action == 1) { playerHand.addCards(deck.getCard()); action = policy.choosePlayerAction(playerHand, dealerHand); } //see if we busted if (playerHand.getValue() > 21) { numLosses++; winLoss -= 1.0; } else { //play dealer var dealerAction = policy.chooseDealerAction(dealerHand); while (dealerAction == 1) { dealerHand.addCards(deck.getCard()); dealerAction = policy.chooseDealerAction(dealerHand); } if (dealerHand.getValue() > 21) //dealer busts { numWins++; winLoss += 1.0; } else if (dealerHand.getValue() < playerHand.getValue()) { numWins++; winLoss += 1.0f; } else if (dealerHand.getValue() == playerHand.getValue()) { numDraws++; } else { numLosses++; winLoss -= 1.0; } } } if(totalHandsPlayed % 1000 == 0) { var x = winLoss / (1000.0); ret.Add(x); numWins = 0; numLosses = 0; numDraws = 0; numBlackJacks = 0; winLoss = 0.0; } } Console.WriteLine("Wins: " + numWins); Console.WriteLine("Losses: " + numLosses); Console.WriteLine("Draws: " + numDraws); Console.WriteLine("BJ: " + numBlackJacks); Console.WriteLine("WinLoss: " + winLoss); return ret; }
static void Play() { Hand playerHand = new Hand(); Hand dealerHand = new Hand(); Deck dealerDeck = new Deck().ShuffleDeck(); for (int i = 0; i < 2; i++) { playerHand.TakeCard(dealerDeck.DealFromDeck()); dealerHand.TakeCard(dealerDeck.DealFromDeck()); } Console.WriteLine("Players hand:"); for (int i = 0; i <= playerHand.handPosition; i++) { Console.WriteLine(playerHand.hand[i]); } playerHand.CalculateScore(); Console.WriteLine("Player score: {0}", playerHand.score); if (playerHand.score == 21) { Console.WriteLine("BlackJack!! you're the winner"); AskToPlayAgain(); } Console.WriteLine(); Console.WriteLine("Dealer's hand:"); Console.WriteLine(dealerHand.hand[0]); dealerHand.CalculateScore(); Console.WriteLine(); //Console.WriteLine("Dealer score: {0}", dealerHand.score); string playerAnswer; do { Console.WriteLine("Would you like to hit or stay (H/S)?"); playerAnswer = Console.ReadLine().ToUpper(); if (playerAnswer == "H") { Console.WriteLine("Player's hand:"); playerHand.TakeCard(dealerDeck.DealFromDeck()); Console.WriteLine(playerHand.hand[playerHand.handPosition - 1]); playerHand.CalculateScore(); Console.WriteLine("Player score: {0}", playerHand.score); if (playerHand.score > 21) { Console.WriteLine("You're busted!!"); AskToPlayAgain(); } } } while (playerAnswer == "H"); for (int i = 0; i <= playerHand.handPosition; i++) { Console.WriteLine(playerHand.hand[i]); } Console.WriteLine("Player score: {0}", playerHand.score); if (playerHand.score > 21) { Console.WriteLine("You're busted!!"); AskToPlayAgain(); } if (dealerHand.score < 16) { dealerHand.TakeCard(dealerDeck.DealFromDeck()); dealerHand.CalculateScore(); if (dealerHand.score < 16) { dealerHand.TakeCard(dealerDeck.DealFromDeck()); dealerHand.CalculateScore(); } if (dealerHand.score < 16) { dealerHand.TakeCard(dealerDeck.DealFromDeck()); dealerHand.CalculateScore(); } } dealerHand.CalculateScore(); Console.WriteLine("Dealer score {0}", dealerHand.score); if (dealerHand.score > 21 && playerHand.score <= 21) { Console.WriteLine("Dealer busts! Player wins!"); } else if (playerHand.score > dealerHand.score) { Console.WriteLine("Player wins!!"); AskToPlayAgain(); } else if (dealerHand.score > playerHand.score) { Console.WriteLine("Dealer wins!!"); AskToPlayAgain(); } else if (playerHand.score > 21 && dealerHand.score > 21) { Console.WriteLine("It's a draw!!"); AskToPlayAgain(); } else if (dealerHand.score == playerHand.score) { Console.WriteLine("It's a tie..."); AskToPlayAgain(); } Console.WriteLine("Goodbye"); Console.ReadLine(); }
/// <summary> /// Deals a random card to a hand. /// </summary> /// <param name="hand">Hand to deal a card to</param> public void DealCard(Hand hand) { Card card = Data[Shuffler.Next(Data.Count)]; hand.Data.Add(card); Data.Remove(card); }
private int getMaxAct(float[] qScores, Hand playerHand) { float max = -10000000.0f; int maxI = -1; int secondMax = -1; bool canDub = playerHand.canDouble(); bool canSpl = playerHand.canSplit(); for (int i = 0; i < qScores.Length; i++) { if (i == 2 && !canDub) secondMax = maxI; if (qScores[i] > max) { max = qScores[i]; maxI = i; } } //if we picked double and we cant dub if (!canDub && maxI == 2) { //hit with neg reward and pick second best runBackwards(-1.0f, 2); maxI = secondMax; } return maxI; }
/* static private void showPolicy(Net.Net n) { Console.Write(" "); var pol = new NNBasicStrategy(n, 1.0); for (int d = 2; d <= 11; d++) { Console.Write(d + " "); } Console.WriteLine(); //Try each possible input and get the output. for (int p = 21; p >= 4; p--) { if(p >= 10) Console.Write(p + " "); else Console.Write(p + " "); for (int d = 2; d <= 11; d++) { Hand pH = new Hand(); Hand dH = new Hand(); pH.addCards(p); dH.addCards(d); //var a = pol.choosePlayerAction(pH, dH, deck); Console.Write(a + " "); } Console.WriteLine(); } //do for soft totals Console.WriteLine(); Console.WriteLine("------ SOFTS ------"); for (int p = 2; p <= 9; p++) { Console.Write(p + " "); for (int d = 2; d <= 11; d++) { Hand pH = new Hand(); Hand dH = new Hand(); pH.addCards(11); pH.addCards(p); dH.addCards(d); // var a = pol.choosePlayerAction(pH, dH); Console.Write(a + " "); } Console.WriteLine(); } } */ /* private static double simpleBasicStrategy(int numOfHands) { var ret = new List<double>(); int totalHandsPlayed; double winLoss = 0.0; //-1 for loss, +1 for win. +1.5 for blackjack. 0 for draw var policy = new BasicStrategy(); //do each hand Deck deck = new Deck(6); deck.shuffleCards(); for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++) { if (deck.isDeckFinished()) { deck = new Deck(6); deck.shuffleCards(); } //decide bet var trueCount = deck.getTrueCount(); var bet = 1.0; if (trueCount > 2) bet = 10.0; Hand playerHand = new Hand(); Hand dealerHand = new Hand(); //deal initial cards playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); playHandBasic(ref deck, playerHand, ref dealerHand, ref policy, ref winLoss, bet); } var x = winLoss / (numOfHands); return x; } */ private static double ccBasicStrategy(Net.Net bettingNet, int numOfHands, double eps, ref Deck deck, bool isTraining = false) { int totalHandsPlayed; double winLoss = 0.0; //-1 for loss, +1 for win. +1.5 for blackjack. 0 for draw var policy = new BasicStrategy(); var bettingPolicy = new NNBettingStrategy(bettingNet, eps); //do each hand for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++) { if (deck.isDeckFinished()) { deck = new Deck(6); deck.shuffleCards(); } //figure out bet var bet = 1.0f; var actionTaken = bettingPolicy.chooseBet(deck); if (actionTaken == 1) bet = 5.0f; if (actionTaken == 2) bet = 10.0f; Hand playerHand = new Hand(); Hand dealerHand = new Hand(); //deal initial cards playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); var winBefore = winLoss; var winAfter = winLoss; var diff = winAfter - winBefore; var reward = playHandBasic(ref deck, playerHand, ref dealerHand, ref policy, ref winLoss); reward = reward * bet; if(isTraining ) bettingPolicy.runBackwards(reward, actionTaken); diff = (bet * diff) - diff; winLoss += diff; } var x = winLoss / (numOfHands); return x; }
//if start button clicked private void btnStart_Click(object sender, RoutedEventArgs e) { //clears card value text txtBlCardValue.Text = ""; //check if the game has been restarted to 0 if (gameRestarted == true && gameStarted == false) { //check if nothing is in text box if (String.IsNullOrEmpty(txtBxEnterName.Text)) { txtBlNameFound.Text = "Enter Name First"; DialogHost.IsOpen = true; } else { //else start the game thus turing the game started bool too true gameStarted = true; playerFound = false; deck = new Deck(); #region PlayerHand //creates the players hand Hand playerHand = new Hand(deck); //draws 2 cards for player hand Card firstCard = deck.DrawCard(playerHand); Card secondCard = deck.DrawCard(playerHand); //gets the sums of these cards int firstCardNum = playerHand.AddValue(firstCard, playerSum); int secondCardNum = playerHand.AddValue(secondCard, playerSum); //equal them too overall player sum playerSum = firstCardNum + secondCardNum; //display sum playerSumString = playerSum.ToString(); txtBlPlayerTotal.Text = playerSumString; //display the image of the first 2 cards BitmapImage userFirstbitmapImage = Convert(firstCard.ReturnImage()); ImgUserFirstCard.Source = userFirstbitmapImage; BitmapImage userSecondbitmapImage = Convert(secondCard.ReturnImage()); ImgUserSecondCard.Source = userSecondbitmapImage; #endregion PlayerHand #region DealerHand //creates the dealers hand Hand dealerHand = new Hand(deck); //draws dealers first card Card dealerCard = deck.DrawCard(dealerHand); //gets card value and equal it too dealer sum dealerSum = dealerHand.AddValue(dealerCard, dealerSum); //display dealer sum dealerSumString = dealerSum.ToString(); txtBlDealerTotal.Text = dealerSumString; //display dealer card image BitmapImage dealerbitmapImage = Convert(dealerCard.ReturnImage()); ImgDealerFirstCard.Source = dealerbitmapImage; #endregion DealerHand //check if player is a returning one foreach (Player returningPlayer in players) { //check if player name can be found in player list if so turn returning player too true and display message if (returningPlayer.PlayerName == txtBxEnterName.Text) { playerReturned = true; txtBlNameFound.Text = "Returning Player"; DialogHost.IsOpen = true; } } //if player returning is true if (playerReturned == true) { //display there name foreach (Player returningPlayer in players) { if (returningPlayer.PlayerName == txtBxEnterName.Text) { returningPlayer.PlayerName = txtBxEnterName.Text; txtBlCurrentPlayer.Text = returningPlayer.PlayerName; } } //get whats turned into the txt box turn it into a string string x = txtBxEnterName.Text; gameInProgress = false; //compare the string too the list and see which matches foreach (Player currentPlayer in players) { //if the string matches the players name if (x == currentPlayer.PlayerName) { //if the player num is equal too 21 they win if (playerSum == 21) { Win(); return; } } } } else { //clears all wins losses and drwas for new player txtBlWin.Text = "0"; txtBlLosses.Text = "0"; txtBlDraws.Text = "0"; //if player is not a returning player create a new player Player newPlayer = new Player(); players.Add(newPlayer); newPlayer.PlayerName = txtBxEnterName.Text; txtBlCurrentPlayer.Text = newPlayer.PlayerName; string x = txtBxEnterName.Text; gameInProgress = false; foreach (Player currentPlayer in players) { if (x == currentPlayer.PlayerName) { if (playerSum == 21) { Win(); return; } } } } } } //if game has not been restarted display message else { txtBlNameFound.Text = "Restart Game and press start"; DialogHost.IsOpen = true; } }
//Method for when it's the dealers turn public void Dealer() { //gets dealers second card displays total and card image #region DealerCard dealerHand = new Hand(deck); Card firstCard = deck.DrawCard(dealerHand); int firstCardNum = dealerHand.AddValue(firstCard, dealerSum); dealerSum = firstCardNum; dealerSumString = dealerSum.ToString(); txtBlDealerTotal.Text = dealerSumString; BitmapImage userFirstbitmapImage = Convert(firstCard.ReturnImage()); ImgDealerSecondCard.Source = userFirstbitmapImage; #endregion DealerCard //if dealer has exactly 21 you lose if (dealerSum == 21) { Loss(); return; } //if dealer's number is below 21 else if (dealerSum < 21) { //go through loop 10 times for (int i = 0; i < 10; i++) { //if dealer number more then player number and dealer number less then or equal to 21 you lose if (dealerSum > playerSum && dealerSum <= 21) { Loss(); return; } //if dealer number below 21 and also below the player's number else if (dealerSum <= 21 && dealerSum < playerSum) { Card newCard = deck.DrawCard(dealerHand); int newCardNum = dealerHand.AddValue(newCard, dealerSum); dealerSum = newCardNum; dealerSumString = dealerSum.ToString(); txtBlDealerTotal.Text = dealerSumString; BitmapImage dealerNewCardbitmapImage = Convert(newCard.ReturnImage()); //depending on if the place for the new card image is null display it this place if it is not null look at the next spot if (ImgDealerThirdCard.Source == null) { ImgDealerThirdCard.Source = dealerNewCardbitmapImage; } else if (ImgDealerFourthCard.Source == null) { ImgDealerFourthCard.Source = dealerNewCardbitmapImage; } else if (ImgDealerFifthCard.Source == null) { ImgDealerFifthCard.Source = dealerNewCardbitmapImage; } else { ImgDealerFirstCard.Source = dealerNewCardbitmapImage; } //if dealer number = too 21 and equal too player number it's a draw if (dealerSum == 21 && dealerSum == playerSum) { Draw(); return; } //if dealer number = to 21 and more then player number you lose else if (dealerSum == 21 && dealerSum > playerSum) { Loss(); return; } //if dealer number more then 21 you win else if (dealerSum > 21) { Win(); return; } } //if dealer number less than = 21 and less then player num else if (dealerSum <= 21 && dealerSum < playerSum) { Win(); return; } //if dealer number == player number its a draw else if (dealerSum == playerSum) { Draw(); return; } //else if dealer num is more than player num else if (dealerSum > playerSum) { Loss(); return; } //if dealer number is more than 21 else if (dealerSum > 21) { Win(); return; } } } }
//When double down is clicked private void btnDoubleDown_Click(object sender, RoutedEventArgs e) { if (gameRestarted == true && gameStarted == true) { if (String.IsNullOrEmpty(txtBxEnterName.Text)) { txtBlNameFound.Text = string.Format("Enter Name First"); DialogHost.IsOpen = true; } //if hit button has been pressed cant double down else if (ifHit == true) { txtBlNameFound.Text = string.Format("Cannot double down after hit was pressed"); DialogHost.IsOpen = true; } else { playerFound = false; string x = txtBxEnterName.Text; foreach (Player newPlayer in players) { if (newPlayer.PlayerName == x) { playerFound = true; //get 2 new cards add them too the total and display them playerHand = new Hand(deck); Card firstCardDoubleDown = deck.DrawCard(playerHand); Card secondCardDoubleDown = deck.DrawCard(playerHand); int firstCardDoubleDownNum = playerHand.AddValue(firstCardDoubleDown, playerSum); int secondCardDoubleDownNum = playerHand.AddValue(secondCardDoubleDown, playerSum); int total = (firstCardDoubleDownNum + secondCardDoubleDownNum) - playerSum; playerSum = total; playerSumString = playerSum.ToString(); txtBlPlayerTotal.Text = playerSumString; BitmapImage userFirstbitmapImage = Convert(firstCardDoubleDown.ReturnImage()); BitmapImage userSecondbitmapImage = Convert(secondCardDoubleDown.ReturnImage()); ImgUserThirdCard.Source = userFirstbitmapImage; ImgUserFourthCard.Source = userSecondbitmapImage; //if player sum over 21 they lose same as 21 they win otherwise the dealer can now play if (playerSum > 21) { Loss(); return; } else if (playerSum == 21) { Win(); return; } else { Dealer(); } } } if (playerFound == false) { txtBlNameFound.Text = string.Format("Player changed, please press start"); DialogHost.IsOpen = true; } } } else { txtBlNameFound.Text = string.Format("Restart Game and press Start"); DialogHost.IsOpen = true; } }
//if btn has been clicked private void btnHitMe_Click(object sender, RoutedEventArgs e) { //if game has been restarted and the game has been started if (gameRestarted == true && gameInProgress == false && gameStarted == true) { //name has to be entred to use this button if (String.IsNullOrEmpty(txtBxEnterName.Text)) { txtBlNameFound.Text = "Enter Name First"; DialogHost.IsOpen = true; } else { //set player found too false playerFound = false; //get the name in the textbox string x = txtBxEnterName.Text; //check in the list if that name matches with the player that is playing foreach (Player newPlayer in players) { if (newPlayer.PlayerName == x) { //if player found set too true playerFound = true; ifHit = true; //get random card between 1 and 10 and add it too your total playerHand = new Hand(deck); Card HitCard = deck.DrawCard(playerHand); int HitCardNum = playerHand.AddValue(HitCard, playerSum); playerSum = HitCardNum; playerSumString = playerSum.ToString(); txtBlPlayerTotal.Text = playerSumString; BitmapImage userHitbitmapImage = Convert(HitCard.ReturnImage()); if (ImgUserThirdCard.Source == null) { ImgUserThirdCard.Source = userHitbitmapImage; } else if (ImgUserFourthCard.Source == null) { ImgUserFourthCard.Source = userHitbitmapImage; } else if (ImgUserFifthCard.Source == null) { ImgUserFifthCard.Source = userHitbitmapImage; } else { ImgUserFirstCard.Source = userHitbitmapImage; } //if player gets more then 21 they lose or if player gets exactly 21 they win if (playerSum > 21) { Loss(); return; } else if (playerSum == 21) { Win(); return; } } } //if player cant be found as the same player in the txtbx get a warning if (playerFound == false) { txtBlNameFound.Text = "Player changed, please press start"; DialogHost.IsOpen = true; } } } else { //warning too restart the game txtBlNameFound.Text = "Press Restart and then press start game"; DialogHost.IsOpen = true; } }
public void Initialize() { _hand = new Hand(); }
public void runBackwardsHit(Hand newPlayer, Hand newDealer, Deck deck) { //get Q value from hitting var x = actionPlusState(1, state); var q = net.forward(x); //get next Q value int playerVal = newPlayer.getValue(); int dealerShown = newDealer.getDealerShowing(); var aceVal = newPlayer.getAceValue(); var newState = genInputVec(playerVal, dealerShown, deck, aceVal, 0.0f, 0.0f); float[] qScores = new float[numActions]; for (int act = 0; act < 2; act++) { var input = actionPlusState(act, newState); var a = net.forward(input); qScores[act] = a[0]; } //find max Q float max = -1000.0f; for (int i = 0; i < 2; i++) { if (qScores[i] > max) max = qScores[i]; } //discount reward var target = lambda * (max - q[0]); if(target > 0.0f) { var i = 1; } //back propagate float[] goal = new float[1]; goal[0] = (float)target; net.forward(x); net.backward(goal); }
private int getExploreAction(float[] qScores, Hand playerHand) { float max = -10000000.0f; int maxI = -1; bool canDub = playerHand.canDouble(); bool canSpl = playerHand.canSplit(); for (int i = 0; i < qScores.Length; i++) { if(i == 2 && !canDub) continue; if (qScores[i] > max) { max = qScores[i]; maxI = i; } } //now get a random worse action var a = maxI; while(a == maxI || ((!canDub && a == 2) || (!canSpl && a == 3))) { a = r.Next(0, qScores.Length); } return a; }
private static void commandLineBJ() { Stream stream = File.Open("playingNet.xml", FileMode.Open); BinaryFormatter formatter = new BinaryFormatter(); Net.Net playingNet = null; playingNet = (Net.Net)formatter.Deserialize(stream); stream.Close(); /* Stream stream1 = File.Open("bettingNet.xml", FileMode.Open); BinaryFormatter formatter1 = new BinaryFormatter(); Net.Net bettingNet = null; bettingNet = (Net.Net)formatter1.Deserialize(stream1); stream1.Close(); */ Random r = new Random(); var playingPolicy = new NNBasicStrategy(playingNet, 1.0);//fixed policy for playing //var bettingPolicy = new NNBettingStrategy(bettingNet, eps); Deck deck = new Deck(6); deck.shuffleCards(); bool exit = false; while(!exit) { if (deck.isDeckFinished()) { deck = new Deck(6); deck.shuffleCards(); } Hand playerHand = new Hand(); Hand dealerHand = new Hand(); //deal initial cards playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); bool handFinished = false; while (!handFinished) { Console.WriteLine("Enter Q to quit"); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("PLAYER HAND: " + playerHand.handToString()); Console.WriteLine("DEALER SHOWING: " + dealerHand.getDealerShowing()); Console.WriteLine("Q Values: "); var qVals = playingPolicy.getQScores(playerHand, dealerHand, deck); var action = playingPolicy.choosePlayerAction(playerHand, dealerHand, deck); foreach (var q in qVals) { Console.Write(" " + q); } if (action != 3) { var x = (float)(-1 * r.NextDouble()); Console.Write(" " + x); } else { var x = (float)(r.NextDouble()); while (x < .5f) x = (float)(r.NextDouble()); Console.Write(" " + x); } Console.WriteLine(); Console.WriteLine("Choose action (0 = stay, 1 = hit, 2 = double, 3 = split)"); var line = Console.ReadLine(); if (line.Equals("Q") || line.Equals("q")) { exit = true; continue; } var act = Int32.Parse(line); if (act == 0) //stand handFinished = true; else if(act == 1) //hit { playerHand.addCards(deck.getCard()); if (playerHand.getValue() > 21) handFinished = true; } else if(act == 2) //double { playerHand.addCards(deck.getCard()); handFinished = true; } else { Console.WriteLine("Split doesnt work with the command line interface right now"); } } if (playerHand.getValue() > 21) { Console.WriteLine("LOST -- WE BUSTED"); continue; } //play dealer var dealerVal = playDealer(ref deck, ref dealerHand); Console.WriteLine("PLAYER HAND: " + playerHand.handToString()); Console.WriteLine("DEALER HAND: " + dealerHand.handToString()); if (dealerVal > 21) //dealer busts { Console.WriteLine("WINNER -- DEALER BUSTED"); } else if (dealerVal < playerHand.getValue()) //we beat dealer { Console.WriteLine("WINNER"); } else if (dealerVal == playerHand.getValue()) //draw { Console.WriteLine("DRAW"); } else //we lost to dealer { Console.WriteLine("YOU LOST"); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("ENTER TO CONTINUE"); Console.ReadLine(); } }
private int shouldSplit(Hand playerHand, Hand dealerHand, int action, bool canSplit) { if (!canSplit) return action; var ret = action; var playerVal = playerHand.getValue(); var dealerShown = dealerHand.getDealerShowing(); if (playerVal == 12 && playerHand.getAceValue() == 1.0f) //AA ret = 3; else if (playerVal == 18 && (dealerShown <= 6 || dealerShown == 8 || dealerShown == 9)) ret = 3; else if (playerVal == 16) ret = 3; else if (playerVal == 14 && dealerShown <= 7) ret = 3; else if (playerVal == 12 && dealerShown <= 6) ret = 3; else if (playerVal == 8 && (dealerShown == 5 || dealerShown == 6)) ret = 3; else if (playerVal == 6 && (dealerShown <= 7)) ret = 3; else if (playerVal == 4 && (dealerShown <= 7)) ret = 3; return ret; }
private static double NNCountingBasicStrategy(Net.Net playingNet, Net.Net bettingNet, int numOfHands, double eps, ref Deck deck, bool isTraining = false) { int totalHandsPlayed; double winLoss = 0.0; //-1 for loss, +1 for win. +1.5 for blackjack. 0 for draw //input, playersVal (17), dealersVal(10), playerHasAce(1) var playingPolicy = new NNBasicStrategy(playingNet, 1.0);//fixed policy for playing var bettingPolicy = new NNBettingStrategy(bettingNet, eps); //do each hand for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++) { //figure out bet var bet = 1.0f; var actionTaken = bettingPolicy.chooseBet(deck); if (actionTaken == 1) bet = 5.0f; if (actionTaken == 2) bet = 10.0f; if (deck.isDeckFinished()) { deck = new Deck(6); deck.shuffleCards(); } Hand playerHand = new Hand(); Hand dealerHand = new Hand(); //deal initial cards playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); var reward = playHand(ref deck, playerHand, ref dealerHand, ref playingPolicy, ref winLoss, isTraining); reward = reward * bet; bettingPolicy.runBackwards(reward, actionTaken); } var x = winLoss / (1.0 * numOfHands); return x; }
private static double NNsimpleBasicStrategy(Net.Net net, int numOfHands, double eps, ref Deck deck, bool isTraining = false) { int totalHandsPlayed; double winLoss = 0.0; //-1 for loss, +1 for win. +1.5 for blackjack. 0 for draw //input, playersVal (17), dealersVal(10), playerHasAce(1) var policy = new NNBasicStrategy(net, eps); //do each hand for (totalHandsPlayed = 0; totalHandsPlayed < numOfHands; totalHandsPlayed++) { if (deck.isDeckFinished()) { deck = new Deck(6); deck.shuffleCards(); } Hand playerHand = new Hand(); Hand dealerHand = new Hand(); //deal initial cards playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); playerHand.addCards(deck.getCard()); dealerHand.addCards(deck.getCard()); playHand(ref deck, playerHand, ref dealerHand, ref policy, ref winLoss, isTraining); } var x = winLoss / (1.0 * numOfHands); return x; }
private static int playDealer(ref Deck deck, ref Hand dealerHand) { var policy = new DealerPolicy(); //play dealer var dealerAction = policy.chooseDealerAction(dealerHand); while (dealerAction == 1) { dealerHand.addCards(deck.getCard()); dealerAction = policy.chooseDealerAction(dealerHand); } return dealerHand.getValue(); // return value of dealer hand. }
public void Start() { MainDeck = new Deck(); Dealer = new Hand(2, "Dealer"); Player = new Hand(2, "Player"); Console.Clear(); for (int i = 0; i < 2; i++) { MainDeck.DealCard(Dealer); MainDeck.DealCard(Player); } Console.WriteLine("-- Initial hands --"); //1st card is secret Console.WriteLine("Dealer initial card: " + Dealer.Data[0]); Console.WriteLine("Player cards: " + Player.Data[0] + ", " + Player.Data[1]); Console.WriteLine("-- Start player turn --"); while (Player.TotalPoints < 21) { Console.WriteLine("You have " + Player.TotalPoints + " points, hit? (y/n)"); if (Console.ReadKey(true).Key == ConsoleKey.Y) { MainDeck.DealCard(Player); PrintCards(Player); } else break; if (Player.TotalPoints == 21) { Console.WriteLine("You have 21 points, you win!"); break; } if (Player.TotalPoints > 21) { Console.WriteLine("You have " + Player.TotalPoints + " points, you lose!"); break; } } if (Player.TotalPoints < 21) { Console.WriteLine("-- Start dealer turn --"); if (Dealer.TotalPoints > 21) { Console.WriteLine("You win, dealer has more than 21 points."); } PrintCards(Dealer); if (Dealer.TotalPoints > Player.TotalPoints && Dealer.TotalPoints < 21) Console.WriteLine("Dealer wins, more points than you."); if (Dealer.TotalPoints == 21) Console.WriteLine("Dealer wins with 21 points."); while (Dealer.TotalPoints <= Player.TotalPoints) { MainDeck.DealCard(Dealer); PrintCards(Dealer); if (Dealer.TotalPoints == 21) { Console.WriteLine("Dealer wins with 21 points."); break; } if (Dealer.TotalPoints > 21) { Console.WriteLine("You win, dealer has over 21 points."); break; } if (Dealer.TotalPoints > Player.TotalPoints) { Console.WriteLine("Dealer wins, higher points than you."); break; } } } Console.WriteLine("-- Play again? (y/n) --"); if (Console.ReadKey(true).Key == ConsoleKey.Y) Start(); }
private static float playHandBasic(ref Deck deck, Hand playerHand, ref Hand dealerHand, ref BasicStrategy policy, ref double winLoss) { var reward = 0.0f; var mult = 1.0f; //check for blackjack. if (playerHand.getValue() == 21 && dealerHand.getValue() != 21) { winLoss += 1.5; return 1.5f; } else if (dealerHand.getValue() == 21) //dealer got blackjack { winLoss -= 1.0; return -1.0f; } else { //player decisions var actionTaken = policy.choosePlayerAction(playerHand, dealerHand); if (actionTaken == 1)//hit { //player decisions var action = policy.choosePlayerAction(playerHand, dealerHand); while (action == 1) { playerHand.addCards(deck.getCard()); action = policy.choosePlayerAction(playerHand, dealerHand); } //see if we busted if (playerHand.getValue() > 21) { winLoss -= 1.0; } else { actionTaken = 0; } } else if (actionTaken == 2) //double { playerHand.addCards(deck.getCard()); if (playerHand.getValue() > 21) { winLoss -= 2.0; reward = -1.0f; mult = 2.0f; } else { mult = 2.0f; actionTaken = 0; } } else if (actionTaken == 3) //split { Hand pH1 = new Hand(); Hand pH2 = new Hand(); var val = playerHand.getValue() / 2; //split card and get an extra. pH1.addCards(val); pH2.addCards(val); pH1.addCards(deck.getCard()); pH2.addCards(deck.getCard()); //win loss for the hands reward = playHandBasic(ref deck, pH1, ref dealerHand, ref policy, ref winLoss); reward += playHandBasic(ref deck, pH2, ref dealerHand, ref policy, ref winLoss); winLoss += reward; } if (actionTaken == 0) //stand { //play dealer var dealerVal = playDealer(ref deck, ref dealerHand); if (dealerVal > 21) //dealer busts { winLoss += 1.0 * mult; reward = 1.0f * mult; } else if (dealerVal < playerHand.getValue()) //we beat dealer { winLoss += 1.0f * mult; reward = 1.0f * mult; } else if (dealerVal == playerHand.getValue()) //draw { reward = 0.0f; } else //we lost to dealer { winLoss -= 1.0 * mult; reward = -1.0f * mult; } } } return reward; }
/// <summary> /// Deals a card to a hand. /// </summary> /// <param name="hand">Hand to deal a card to</param> public void DealCard(Hand hand, int CardIndex) { Card card = Data[CardIndex]; hand.Data.Add(card); Data.Remove(card); }
private static void playHit(ref Deck deck, ref Hand playerHand, Hand dealerHand, ref NNBasicStrategy policy, bool isTraining) { var actionTaken = 1; while (actionTaken == 1) // hit { playerHand.addCards(deck.getCard()); if (playerHand.getValue() > 21) { break; } else { if (isTraining) { policy.runBackwardsHit(playerHand, dealerHand, deck); } } //need to do delayed reward. actionTaken = policy.choosePlayerAction(playerHand, dealerHand, deck); } }
public Player(Hand hand, Dealer dealer) { this.hand = hand; this.dealer = dealer; }
public void init() { hand = new Hand(new List<Card>(){ deck.popCard(), deck.popCard() }); }