//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 DrawingOneCardResultsInOneCardBeingRemovedFromDeck() { Deck theDeck = new Deck(); int number = 1; theDeck.Draw(number); theDeck.CardsRemaining.Count.Should().Be(51); }
public void DrawingCardFromDeckRemovesItFromDeck() { var theDeck = new Deck(); var dealt = theDeck.Draw(); theDeck.CardsRemaining.Should().NotContain(dealt); }
public void DrawingTopCardFromShuffledDeckDoesNotReturnAceOfSpades() { var theDeck = new Deck(); theDeck.Shuffle(); var dealt = theDeck.Draw(); dealt.Should().NotBe(new Card("Spade",1)); }
static void Main(string[] args) { Deck myDeck = new Deck(); foreach (Card card in myDeck.Cards) { Console.WriteLine(card.Suit); Console.WriteLine(card.Value); } Console.ReadKey(); }
public void DrawingThreeCardsShouldThrowException() { Deck theDeck = new Deck(); int number = 3; bool isValid = true; try { theDeck.Draw(number); } catch (Exception e) { isValid = false; } isValid.Should().BeFalse(); }
public void DrawingNegativeNumberOfCardsShouldThrowException() { Deck theDeck = new Deck(); const int number = -1; var isValid = true; try { theDeck.Draw(number); } catch (Exception e) { isValid = false; } isValid.Should().BeFalse(); }
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; }
public void verify_initial_hands_dealt_alternately() { Dealer dealer = new Dealer(); IPlayer player = new HumanPlayer(); Deck deck = new BlackJack.Deck(); ICard card0 = deck.PeekCard(0); ICard card1 = deck.PeekCard(1); ICard card2 = deck.PeekCard(2); ICard card3 = deck.PeekCard(3); ops.DealInitialHands(deck, dealer, player); ICard playerCard0 = player.GetHand().GetCards()[0]; ICard playerCard1 = player.GetHand().GetCards()[1]; ICard dealerCard0 = dealer.GetHand().GetCards()[0]; ICard dealerCard1 = dealer.GetHand().GetCards()[1]; Assert.IsTrue(card0.Equals(playerCard0)); Assert.IsTrue(card1.Equals(dealerCard0)); Assert.IsTrue(card2.Equals(playerCard1)); Assert.IsTrue(card3.Equals(dealerCard1)); }
public void verify_initial_hands_first_card_dealt_facedown() { Dealer dealer = new Dealer(); IPlayer player = new HumanPlayer(); Deck deck = new BlackJack.Deck(); ICard card0 = deck.PeekCard(0); ICard card1 = deck.PeekCard(1); ICard card2 = deck.PeekCard(2); ICard card3 = deck.PeekCard(3); ops.DealInitialHands(deck, dealer, player); ICard playerCard0 = player.GetHand().GetCards()[0]; ICard playerCard1 = player.GetHand().GetCards()[1]; ICard dealerCard0 = dealer.GetHand().GetCards()[0]; ICard dealerCard1 = dealer.GetHand().GetCards()[1]; Assert.IsFalse(playerCard0.Visible); Assert.IsFalse(dealerCard0.Visible); Assert.IsTrue(playerCard1.Visible); Assert.IsTrue(dealerCard1.Visible); }
public int chooseBet(Deck deck) { int action = 0; state = deck.getBettingState(); 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); } else // choose worst { action = getExploreAction(qScores); } return action; }
/// <summary> /// Puts a card from this hand in a specified deck. /// </summary> public void GiveCard(Deck deck, int CardIndex) { Card card = Data[CardIndex]; deck.Data.Add(card); Data.Remove(card); }
/// <summary> /// Creates a new deck and calls InitializeNewGame with already defined players /// </summary> /// <param name="players">A predefined list of players</param> public Game(Players players) { GameDeck = new Deck(); InitializeNewGame(players); }
/* 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; }
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 static float[] genInputVec(int playerVal, int dealerShowing, Deck deck, float aceVal, float dubVal, float splVal) { float[] ret = new float[38]; var playerVec = playerValVec(playerVal); var dealerVec = dealerValVec(dealerShowing); var cardCountVec = deck.getDeckPercentages(); for (int i = 0; i < ret.Length - 2; i++) { if (i < playerVec.Length) { ret[i] = playerVec[i]; } else if(i < playerVec.Length + dealerVec.Length) { ret[i] = dealerVec[i - playerVec.Length]; } else { ret[i] = cardCountVec[i - (playerVec.Length + dealerVec.Length)]; } } ret[36] = aceVal; ret[37] = dubVal; //ret[29] = splVal; return ret; }
/* static private float[] testFunc(double a, double b, double c, double d) { float[] ret = new float[3]; ret[0] = (float)(Math.Sin(a) * Math.Cos(c + d) + b); ret[1] = (float)(Math.Cos(b) *c); ret[2] = (float)(Math.Sin(b + d)); return ret; } */ // return 1 for player won and -1 for dealer one static Deck dealInitCards(ref int playersVal, ref int dealerShown, ref bool playerHasAce) { var deck = new Deck(1); playerHasAce = false; deck.shuffleCards(); playersVal = deck.getCard(); //get initial players and dealers card var card = deck.getCard(); //player first card if (card == 1) { playersVal = 11; playerHasAce = true; } else { playersVal = card; } //get second card card = deck.getCard(); if (card == 1) { playerHasAce = true; if (playersVal >= 11) playersVal += 1; else playersVal += 11; } else { playersVal += card; } //get dealer showing card dealerShown = deck.getCard(); if (dealerShown == 1) dealerShown = 11; return deck; }
private static void trainCCWithBasic(int numberOfHands, int testInterval) { List<double> percs = new List<double>(); List<double> bas = new List<double>(); List<int> runNum = new List<int>(); int[] y = { 50 }; var bettingNet = new Net.Net(13, y, 1); var eps = .9; //train the counting. var countingTraining = 1000000; testInterval = 10000; Deck deck = new Deck(6); deck.shuffleCards(); for (int i = 0; i < numberOfHands; i++) { if (i % testInterval == 0) { var perc = ccBasicStrategy(bettingNet, 100000, 1.0, ref deck); percs.Add(perc); runNum.Add(i); Console.WriteLine(i + ": " + perc); //set learning rate to perc NetCore.learningRate = -1.0f * (float)(perc / 2.0); if (NetCore.learningRate < 0.0f) NetCore.learningRate = .001f; if (i < 200000) NetCore.learningRate = .2f; Console.WriteLine(NetCore.learningRate); //showPolicy(net); } else { ccBasicStrategy(bettingNet, 1, eps, ref deck, true); } } }
/// <summary> /// Creates a new deck and calls InitializeNewGame /// </summary> public Game() { GameDeck = new Deck(); InitializeNewGame(); }
private void DealNewHand() { cardDeck = new Deck(); string backImageFile = "back.png"; Image backImage = GetImageResource(backImageFile); userBox1.Image = backImage; userBox2.Image = backImage; userBox3.Image = null; userBox4.Image = null; dealerBox1.Image = backImage; dealerBox2.Image = backImage; dealerBox3.Image = null; dealerBox4.Image = null; splitBox1.Image = null; splitBox2.Image = null; splitBox3.Image = null; splitBox4.Image = null; dealerScore = 0; hiddenDealerScore = 0; playerScore = 0; splitScore = 0; currentBet = 0; splitBet = 0; dealerHandIdx = 0; playerHandIdx = 0; splitIndex = 0; txtDealerScore.Text = "0"; txtPlayerScore.Text = "0"; txtPush.Text = "0"; txtCurrentBet.Text = "$0"; Array.Clear(playersHand, 0, playersHand.Length); Array.Clear(dealersHand, 0, dealersHand.Length); Array.Clear(splitHand, 0, splitHand.Length); lblSplitScore.Visible = false; txtSplitScore.Visible = false; btnSplitHit.Visible = false; splitBox1.Visible = false; btnSplitBet.Visible = false; btnSplitStand.Visible = false; lblSplitBet.Visible = false; txtSplitBet.Visible = false; btnSplitStandWasClicked = false; btnStandWasClicked = false; waitForSplit = false; btnSplitWasClicked = false; txtGetSplitBet.Visible = false; lblBetTheSplit.Visible = false; txtBet.Clear(); txtBet.Focus(); }
public BlackJack() { deck = new Deck(); Play(); }
/// <summary> /// Creates a new deck and calls InitializeNewGame with multiplayer /// </summary> /// <param name="numberOfPlayers">How many players will play</param> public Game(int numberOfPlayers) { GameDeck = new Deck(); InitializeNewGame(numberOfPlayers); }
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. }
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 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 void InitializeGame() { currentMoney = 1000; dealerScore = 0; hiddenDealerScore = 0; playerScore = 0; splitScore = 0; playerWins = 0; dealerWins = 0; pushGames = 0; currentBet = 0; splitBet = 0; dealerHandIdx = 0; playerHandIdx = 0; splitIndex = 0; cardDeck = new Deck(); playersHand = new Card[5]; dealersHand = new Card[5]; splitHand = new Card[5]; playedHand = true; btnSplitStandWasClicked = false; btnStandWasClicked = false; waitForSplit = false; btnSplitWasClicked = false; txtMoney.Text = currentMoney.ToString("c"); DealNewHand(); }
private static void trainCC(int numberOfHands, int testInterval) { Stream stream = File.Open("playingNet.xml", FileMode.Open); BinaryFormatter formatter = new BinaryFormatter(); Net.Net playingNet = null; playingNet = (Net.Net)formatter.Deserialize(stream); stream.Close(); List<double> percs = new List<double>(); List<double> bas = new List<double>(); List<int> runNum = new List<int>(); int[] y = { 50 }; var bettingNet = new Net.Net(13, y, 1); var eps = .9; //train the counting. var countingTraining = 1000000; testInterval = 10000; Deck deck = new Deck(6); deck.shuffleCards(); for (int i = 0; i < numberOfHands; i++) { if (i % testInterval == 0) { var perc = NNCountingBasicStrategy(playingNet, bettingNet, 100000, 1.0, ref deck); percs.Add(perc); runNum.Add(i); Console.WriteLine(i + ": " + perc); //set learning rate to perc NetCore.learningRate = -1.0f * (float)(perc / 2.0); if (NetCore.learningRate < 0.0f) NetCore.learningRate = .001f; if (i < 200000) NetCore.learningRate = .2f; Console.WriteLine(NetCore.learningRate); //showPolicy(net); } else { NNCountingBasicStrategy(playingNet, bettingNet, 1, eps, ref deck, true); } } Stream stream1 = File.Open("bettingNet.xml", FileMode.Create); BinaryFormatter formatter1 = new BinaryFormatter(); formatter1.Serialize(stream1, bettingNet); stream.Close(); writeSingleToFile(percs); }
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 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(); } }
public void clearUI() { // add all the cards back to the deck dealDeck = null; dealDeck = new Deck (); // make all the cards null. why aren't there 1's and 2's here? mycard3 = null; mycard4 = null; housecard3 = null; housecard4 = null; // hide the labels myCard3Label.Hidden = true; myCard4Label.Hidden = true; houseCard3Label.Hidden = true; houseCard4Label.Hidden = true; hitButton.Hidden = true; doneButton.Hidden = true; // hide the images myImage1.Image = UIImage.FromBundle (" "); myImage2.Image = UIImage.FromBundle (" "); myImage3.Image = UIImage.FromBundle (" "); myImage4.Image = UIImage.FromBundle (" "); houseImage1.Image = UIImage.FromBundle (" "); houseImage2.Image = UIImage.FromBundle (" "); houseImage3.Image = UIImage.FromBundle (" "); houseImage4.Image = UIImage.FromBundle (" "); myCard1Label.Text = "Card One"; myCard2Label.Text = "Card Two"; houseCard1Label.Text = "Card One"; houseCard2Label.Text = "Card Two"; myCardsLabel.Text = "Your Cards"; houseCardsLabel.Text = "Dealer's Cards"; playButton.Hidden = false; }
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; }
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; }
public void Play() { bool winner = false; bool reset = true; List <Card> dealerCards = new List <Card>(); List <Card> playerCards = new List <Card>(); Deck playingDeck = deck; //static int turn = 0; while (!winner) { if (reset) { playingDeck = new Deck(); dealerCards.Clear(); playerCards.Clear(); reset = false; } Console.Clear(); //Dealer Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("Dealer: \nCards: "); if (turn == 0 || turn == 2) { dealerCards.Add(DrawCard(playingDeck)); } PrintCards(dealerCards); Console.Write("\nDealer points: "); Console.Write(CalcValue(dealerCards).ToString()); Console.ResetColor(); Console.WriteLine("\n---------------------------------\n"); //Player Console.ForegroundColor = ConsoleColor.Blue; Console.Write("Player:\nCards: "); if (turn == 0 || turn == 1) { playerCards.Add(DrawCard(playingDeck)); if (turn == 0) { playerCards.Add(DrawCard(playingDeck)); } turn = 1; } PrintCards(playerCards); Console.Write("\nPlayer points: "); Console.WriteLine(CalcValue(playerCards).ToString()); Console.ResetColor(); string result = CalcResult(playerCards, dealerCards); if (result != null) { Console.WriteLine(result); Console.ResetColor(); Console.ReadKey(); reset = true; turn = 0; } else { Console.WriteLine("\nPress 's' to stay 'enter' to hit"); //char input = Console.ReadKey().KeyChar; bool stayTest = char.TryParse(Console.ReadLine(), out char input); if (input.Equals('S') || input.Equals('s')) { turn = 2; } if (input.Equals('R')) { reset = true; turn = 0; } } } }
public void DrawingTopCardsFromTwoShuffledDecksShouldBeDifferent() { var theDeck = new Deck(); theDeck.Shuffle(); var anotherDeck = new Deck(); anotherDeck.Shuffle(); var card1 = theDeck.Draw(); var card2 = anotherDeck.Draw(); card1.Should().NotBe(card2); }
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); } }
//test with only hit and stand actions private static void simpleBlackjack(int numberOfHands, int testInterval) { int[] x = { 150 }; int[] y = { 50 }; List<double> percs = new List<double>(); List<double> bas = new List<double>(); List<int> runNum = new List<int>(); //+9 card coupts //input, playersVal (17), dealersVal(10), playerHasAce(1), doubleFlag(1), spltFlag(1), actions(4) //var net = new Net.Net(32, x, 1); var playingNet = new Net.Net(41, x, 1); //9 card counds, 1 decks left, 3 actions (low, med, high) var bettingNet = new Net.Net(13, y, 1); Random r = new Random(); var eps = .7; //showPolicy(net); Deck deck = new Deck(6); deck.shuffleCards(); for (int i = 0; i < numberOfHands; i++) { if (i % testInterval == 0) { var perc = NNsimpleBasicStrategy(playingNet, 100000, 1.0, ref deck); //var bs = simpleBasicStrategy(100000); percs.Add(perc); //bas.Add(bs); runNum.Add(i); Console.WriteLine(i + ": " + perc); //set learning rate to perc NetCore.learningRate = -1.0f * (float)(perc); if (NetCore.learningRate < 0.0f) NetCore.learningRate = .001f; if (i < 1000000) NetCore.learningRate = .2f; Console.WriteLine(NetCore.learningRate); //showPolicy(net); } else { NNsimpleBasicStrategy(playingNet, 1, eps, ref deck, true); } //if(i % 100000 == 0) //showPolicy(net); } writeToFile(runNum, percs, bas); //save training net //Opens a file and serializes the object into it in binary format. Stream stream = File.Open("playingNet.xml", FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, playingNet); stream.Close(); }
public static void playGame() { Player player1 = new Player(); player1.cardSum = 0; player1.TotalGamesPlayed = 0; player1.WinCount = 0; //players init Dealer dealer1 = new Dealer(); dealer1.cardSum = 0; Random rng = new Random(); Deck freshDeck = new Deck(); freshDeck.makeDeck(); //randomize the cards for (int i = 0; i < freshDeck.newDeck.Count; i++) { var temp = freshDeck.newDeck[i]; int randomIndex = rng.Next(i, freshDeck.newDeck.Count); freshDeck.newDeck[i] = freshDeck.newDeck[randomIndex]; freshDeck.newDeck[randomIndex] = temp; } Boolean subMenu = true; while (subMenu) { playerTurn(); dealerTurn(); } void dealerTurn() { if (dealer1.cardSum == 0) { dealer1.cardSum = freshDeck.newDeck[0].CardValue + freshDeck.newDeck[1].CardValue; freshDeck.newDeck.RemoveAt(0); freshDeck.newDeck.RemoveAt(1); } else if (dealer1.cardSum == 21) { Console.WriteLine("Dealer Total is 21, You Lost"); } else if (dealer1.cardSum >= 17) { if (player1.cardSum > dealer1.cardSum) { Console.WriteLine("Dealer Total is " + dealer1.cardSum + " You Won!"); } else if (player1.cardSum > dealer1.cardSum) { Console.WriteLine("Dealer Total is " + dealer1.cardSum + " Tie!"); } else { //Recursive dealer1.cardSum += freshDeck.newDeck[0].CardValue; freshDeck.newDeck.RemoveAt(0); dealerTurn(); } } } void playerTurn() { player1.cardSum = freshDeck.newDeck[0].CardValue + freshDeck.newDeck[1].CardValue; freshDeck.newDeck.RemoveAt(0); freshDeck.newDeck.RemoveAt(1); if (player1.cardSum == 21) { Console.WriteLine("Your Total is 21, You Won"); } else { Console.WriteLine("You card sum is: " + player1.cardSum); Boolean keepGoing = true; while (keepGoing) { Console.WriteLine("Do you want to: \n1.hit. \n2.Stay(under construction)"); string respond = Console.ReadLine(); if (respond.Equals("1")) { player1.cardSum += freshDeck.newDeck[0].CardValue; freshDeck.newDeck.RemoveAt(0); if (player1.cardSum == 21) { Console.WriteLine("You got 21. You Won!"); keepGoing = false; } else if (player1.cardSum > 21) { Console.WriteLine("You got " + player1.cardSum + " Busted, you have lost!"); keepGoing = false; } else { Console.WriteLine("You card sum is: " + player1.cardSum); } } else if (respond.Equals("2")) { keepGoing = false; } } } } }