Ejemplo n.º 1
0
        /// <summary>
        /// Places a bet. Several possibilities depending on the round, argument and player money.
        /// </summary>
        /// <param name="s">The amount to be bet, a number or "all".</param>
        /// <returns>(S, A) with S indicating if the bet can be correctly placed and A the amount bet if S is true.</returns>
        public (bool success, int amount) PlaceBet(string s)
        {
            (bool success, int amount)res = (false, 0);
            int    min        = Math.Max(CurrentMaxBet, ante);
            string normalized = s.Trim().ToLower();

            if (normalized.Equals("all"))
            {
                res.success = true;
                res.amount  = currentPlayer.CurrentBet + currentPlayer.Money;
                GameControler.DisplayAllIn(currentPlayer.Name);
            }
            else
            {
                bool ok = int.TryParse(normalized, out int bet);
                if (ok && bet <= currentPlayer.CurrentBet + currentPlayer.Money && bet >= min)
                {
                    res = (true, bet);
                }
            }
            if (res.success)
            {
                int match = Math.Min(res.amount - currentPlayer.CurrentBet, currentPlayer.Money);
                currentPlayer.Spend(match);
                pot += match;
                currentPlayer.CurrentBet = res.amount;
            }
            if (currentPlayer.PlayerType == PlayerType.AI)
            {
                GameControler.DisplayAIBets(currentPlayer.Name, res.amount);
            }
            return(res);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Folds. The current player becomes out of the deal.
 /// </summary>
 public void FoldBet()
 {
     currentPlayer.PlayerStatus = PlayerStatus.OutDeal;
     if (currentPlayer.PlayerType == PlayerType.AI)
     {
         GameControler.DisplayAIFolds(currentPlayer.Name);
     }
 }
Ejemplo n.º 3
0
 private void automatic()
 {
     if (theRules.Round.ToString().Equals("Ante"))
     {
         GameControler.Interpret("ante");
     }
     else if (theRules.Round.ToString().Equals("DealCardDown") || theRules.Round.ToString().Equals("DealCardUp"))
     {
         GameControler.Interpret("next");
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Deals one card face up.
        /// </summary>
        /// <returns>The card that has been dealt from the deck.</returns>
        public Card DealCardUp()
        {
            Card c = deck.Pop();

            currentPlayer.AddCard(c);
            if (currentPlayer.PlayerType == PlayerType.AI)
            {
                GameControler.DisplayAIReceives(currentPlayer.Name, c);
            }
            return(c);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Method called to start a new deal.
        /// </summary>
        public void NewDeal()
        {
            if (gameOver)
            {
                return;
            }
            foreach (Player p in players)
            {
                if (p.Money <= 0 && p.PlayerStatus != PlayerStatus.OutGame)
                {
                    Loss(p);
                }
            }
            if (players.FindAll(p => p.PlayerStatus == PlayerStatus.OutGame).Count >= players.Count - 1)
            {
                Winner = players.Find(p => p.PlayerStatus != PlayerStatus.OutGame);
                GameControler.GameOver();
                return;
            }
            pot = 0;
            if (deal > 0 && anteDoubles)
            {
                ante *= 2;
            }
            currentRound = 0;
            deck.Clear();
            commonCards.Clear();
            List <Card> randomOrderCards = Card.Deck.OrderBy(c => rng.Next(int.MaxValue)).ToList();

            for (int i = Card.Deck.Count; i > 0; i--)
            {
                deck.Push(randomOrderCards[rng.Next(0, i)]);
                randomOrderCards.Remove(deck.Peek());
            }
            foreach (Player p in players)
            {
                if (p.PlayerStatus == PlayerStatus.OutDeal)
                {
                    p.PlayerStatus = PlayerStatus.In;
                }
                p.Hand.Clear();
                p.CurrentBet = 0;
                if (p.Money <= 0 && p.PlayerStatus != PlayerStatus.OutGame)
                {
                    Loss(p);
                }
            }
            currentPlayer = players.Find(p => p.PlayerStatus != PlayerStatus.OutGame);
            deal++;
            GameControler.DisplayNewDeal(players.FindAll(p => p.PlayerStatus == PlayerStatus.In));
            Prompt();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Checks the bet for the current player. The action is always available for players that are still in.
        /// </summary>
        /// <returns>The amount spent by the player and added to the pot (can be 0 when the player is all in).</returns>
        public int CheckBet()
        {
            int res = Math.Min(CurrentMaxBet - currentPlayer.CurrentBet, currentPlayer.Money);

            currentPlayer.Spend(res);
            currentPlayer.CurrentBet = CurrentMaxBet;
            pot += res;
            if (currentPlayer.PlayerType == PlayerType.AI)
            {
                GameControler.DisplayAIChecks(currentPlayer.Name);
            }
            return(res);
        }
Ejemplo n.º 7
0
 private void Victory(Player player)
 {
     if (gameOver)
     {
         return;
     }
     player.Gain(pot);
     GameControler.DisplayDealWon(player.Name, pot);
     if (OtherPlayers.All(p => p.Money <= 0 || p.PlayerStatus == PlayerStatus.OutGame))
     {
         Winner = player;
         GameControler.GameOver();
     }
 }
Ejemplo n.º 8
0
 private void Loss(Player currentPlayer)
 {
     if (gameOver)
     {
         return;
     }
     currentPlayer.PlayerStatus = PlayerStatus.OutGame;
     GameControler.DisplayLoss(currentPlayer.Name);
     if (players.FindAll(p => p.PlayerStatus != PlayerStatus.OutGame).Count <= 1)
     {
         Winner = players.Find(p => p.PlayerStatus != PlayerStatus.OutGame);
         GameControler.GameOver();
     }
 }
Ejemplo n.º 9
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            RuleSet theRules;
            string  fileName = "";

            QuestionCustom qs = new QuestionCustom();

            if (qs.ShowDialog() == DialogResult.Yes)
            {
                OpenFileDialog changeRules = new OpenFileDialog
                {
                    Filter           = "Fichier xml|*.xml",
                    Title            = "Choose a File",
                    InitialDirectory = "../"
                };
                if (changeRules.ShowDialog() == DialogResult.OK)
                {
                    fileName = changeRules.FileName;
                }
                else
                {
                    fileName = "../../rules.xml";
                }
            }
            else
            {
                fileName = "../../rules.xml";
            }


            theRules = new RuleSet(fileName);

            // 2 - SetupView

            ExampleSetupView setupView = new ExampleSetupView(theRules);

            setupView.TopMost = true;
            if (setupView.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            GameView theView = new GameView(theRules);

            GameControler.Initialize(theRules, theView);

            Application.Run(theView);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Places the amount of the ante to start the deal, or all money if the player has not enough.
        /// The current player may lose if they do not have money after the ante is placed.
        /// </summary>
        /// <returns>The actual amount payed as ante by the player.</returns>
        public int PlaceNecessaryAnte()
        {
            int ante = Math.Min(this.ante, currentPlayer.Money);

            currentPlayer.Spend(ante);
            pot += ante;
            if (currentPlayer.PlayerType == PlayerType.AI)
            {
                GameControler.DisplayAIPlacesAnte(currentPlayer.Name, ante);
            }
            if (currentPlayer.Money <= 0)
            {
                Loss(currentPlayer);
                return(-1);
            }
            return(ante);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Method called to make the player act.
 /// If the player is human, the controller is used to display the possible actions and to wait on
 /// the input of the player.
 /// If the player is an AI, automatic play is engaged.
 /// </summary>
 public void Prompt()
 {
     if (gameOver)
     {
         return;
     }
     if (currentPlayer == null)
     {
         GameControler.GameOver();
     }
     GameControler.DisplayInitialPrompt("Game: " + gameName +
                                        ", Player: " + currentPlayer.Name + ", round= " + theRounds[currentRound]);
     if (currentPlayer.PlayerType == PlayerType.AI)
     {
         AutoPlay(currentPlayer);
     }
     else
     {
         GameControler.DisplayPossibleActions(PossibleActions(theRounds[currentRound]));
     }
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Draw some cards, facing up, depending on how many cards where discarded, and remove the discards.
        /// </summary>
        /// <returns>The list of drawn cards, null if there are no discards.</returns>
        public List <Card> DrawCardsUp()
        {
            int cardsToDraw = currentPlayer.Hand.FindAll(c => c.Discard).Count;

            if (cardsToDraw <= 0)
            {
                return(null);
            }
            List <Card> drawn = new List <Card>();

            for (int i = 0; i < cardsToDraw; i++)
            {
                Card c = deck.Pop();
                currentPlayer.AddCard(c);
                drawn.Add(c);
                if (currentPlayer.PlayerType == PlayerType.AI)
                {
                    GameControler.DisplayAIReceives(currentPlayer.Name, c);
                }
            }
            currentPlayer.RemoveDiscards();
            return(drawn);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Method called when a player's actions are finished for the current round, passing to the
        /// next player (if there are players left in this round), the next round (if there are still
        /// rounds left in this deal), the next deal (if there are still players in the game with money),
        /// or possibly ending the game.
        /// </summary>
        public void NextPlayerRoundOrDeal()
        {
            if (gameOver)
            {
                return;
            }
            if (players.FindAll(p => p.PlayerStatus == PlayerStatus.In).Count <= 1) // Last standing
            {
                Victory(players.Find(p => p.PlayerStatus == PlayerStatus.In));
                CheckOnly = false;
                if (!gameOver)
                {
                    NewDeal();
                }
                return;
            }
            if (Round == Rule.DealCardCommon) // Common - only first player then next round
            {
                CheckOnly = false;
                currentRound++;
                currentPlayer = players.Find(p => p.PlayerStatus == PlayerStatus.In);
                Prompt();
                return;
            }
            Player np = players.Find(p => p.PlayerStatus == PlayerStatus.In && players.IndexOf(p) > players.IndexOf(currentPlayer));

            if (np != null) // Still a player in this round
            {
                currentPlayer = np;
                Prompt();
                return;
            }
            List <Player> remainingPlayers = players.FindAll(p => p.PlayerStatus == PlayerStatus.In);

            if (Round == Rule.Bet && !remainingPlayers.All(i => i.CurrentBet == CurrentMaxBet)) // Betting incomplete
            {
                CheckOnly     = true;
                currentPlayer = players.Find(p => p.PlayerStatus == PlayerStatus.In);
                Prompt();
                return;
            }
            if (currentRound < theRounds.Count - 1 && theRounds[currentRound + 1] != Rule.Showdown) // Still rounds to perform
            {
                CheckOnly = false;
                currentRound++;
                currentPlayer = players.Find(p => p.PlayerStatus == PlayerStatus.In);
                Prompt();
                return;
            }
            if (theRounds[currentRound + 1] == Rule.Showdown || Round == Rule.Showdown) // This is it.
            {
                List <(List <Card> cards, Player player)> remainingHands = new List <(List <Card>, Player)>();
                foreach (Player p in remainingPlayers)
                {
                    List <Card> hand = new List <Card>(p.Hand);
                    if (commonCards != null && commonCards?.Count > 0)
                    {
                        hand.AddRange(commonCards);
                    }
                    remainingHands.Add((hand, p));
                }
                Player winner = Card.Showdown(remainingHands);
                if (winner == null)
                {
                    remainingPlayers.ForEach(o => o.Gain(pot / remainingPlayers.Count));
                    GameControler.DisplayDraw();
                }
                else
                {
                    GameControler.DisplayWinningHand(remainingHands.Find(cp => cp.player == winner).cards);
                    Victory(winner);
                }
            }
            CheckOnly = false;
            if (!gameOver)
            {
                NewDeal();
            }
        }
Ejemplo n.º 14
0
 private void ante_Click(object sender, System.EventArgs e)
 {
     GameControler.Interpret("ante");
 }
Ejemplo n.º 15
0
 private void next_Click(object sender, System.EventArgs e)
 {
     GameControler.Interpret("next");
 }
Ejemplo n.º 16
0
 private void discardCardToolStripMenuItem_Click(object sender, EventArgs e)
 {
     GameControler.Interpret("discard " + selectedCard.code());
     Refresh();
 }
Ejemplo n.º 17
0
 private void check_Click(object sender, EventArgs e)
 {
     GameControler.Interpret("check");
 }
Ejemplo n.º 18
0
 private void status_Click(object sender, EventArgs e)
 {
     GameControler.Interpret("status");
 }