Poab9200 6 Light Poster

Hello all, and happy fourth.

First off, I've been trying to create this little app for quite some time now and I've gotten pretty far as the code just "came" to me the other day. So I've gotten... I'd say about 80% done with this project but I am having most likely a huge problem with my WarBattle() Method(which is located in the War class).

Problems:
1. It seems that every time I run the game and the same face value card is drawn for both players they enter the WarBattle stage... Now this part isn't the problem(Enter WarBattle) its that every time I try to burn 3 cards of each players hand/deck it doesn't seem to return those cards of which were burned...

2. Now with the above problem stated I run the game in a while() loop so that I can test if the game will end... Eventually. But thus far I have yet to succede. I tried to run the test but I removed the WarBattle Function. So If the cards drawn were the same the cards were returned to the users deck. I ran this test for about 2 hours and still no end result. So I stopped it and I ran a benchmark test to see how many battles occured in certain time span(3 minutes was the testing length) I ran the test using the System.Diagnostics.StopWatch Namespace. The results came back to show that Approximatly(Averaged) 107.68 Million battles were conducted in that time span. The Diagnostics part of the test along with the removed WarBattle Functions has been opted out of my code so it won't be too confusing.

What I'm asking. Can you(anyone) help me solve the WarBattle() Method or the checks that go into the while() loop in the War Class.

Greatly appreciated!
--- Poab9200, Happy 4th!

using System;
using System.Collections.Generic;
using System.Text;

namespace new_theroy
{
    public enum Suit
    {
        Diamonds, Spades, Clubs, Hearts
    }

    public enum FaceValue
    {
        Two = 2,
        Three = 3,
        Four = 4,
        Five = 5,
        Six = 6,
        Seven = 7,
        Eight = 8,
        Nine = 9,
        Ten = 10,
        Jack = 11,
        Queen = 12,
        King = 13,
        Ace = 14
    }

    public class Card
    {
        // Members/Fields
        private readonly Suit suit;
        private readonly FaceValue faceVal;

        // Properties
        public Suit Suit { get { return suit; } }
        public FaceValue FaceVal { get { return faceVal; } }

        // Constructor
        public Card(Suit suit, FaceValue faceVal)
        {
            this.suit = suit;
            this.faceVal = faceVal;
        }

        // Override
        public override string ToString()
        {
            return "The " + faceVal.ToString() + " of " + suit.ToString();
        }

    }

    public class Deck
    {
        // Members/Fields
        private string sDeck;
        protected List<Card> cards = new List<Card>();

        // Properties
        public Card this[int position] { get { return (Card)cards[position]; } }
        public int Cards { get { return cards.Count; } }

        // Constructor
        public Deck()
        {
            foreach (Suit s in Enum.GetValues(typeof(Suit)))
            {
                foreach (FaceValue f in Enum.GetValues(typeof(FaceValue)))
                {
                    cards.Add(new Card(s, f));
                }
            }
        }

        // Public Methods
        public Card Draw()
        {
            Card card = cards[0];
            cards.RemoveAt(0);

            return card;
        }
        public void Shuffle()
        {
            Random random = new Random();

            for (int i = 0; i < cards.Count; i++)
            {
                int index1 = i;
                int index2 = random.Next(cards.Count);

                SwapCard(index1, index2);
            }
        }
        public void Shuffle(int SuffleAmount)
        {
            if (SuffleAmount == 0)
            {
                throw new ArgumentException("You must shuffle at least once");
            }
            else
            {
                Random random = new Random();

                for (int n = 0; n != SuffleAmount; n++)
                {

                    for (int i = 0; i < cards.Count; i++)
                    {
                        int index1 = i;
                        int index2 = random.Next(cards.Count);

                        SwapCard(index1, index2);
                    }

                }
            }
        }

        // Private Method
        private void SwapCard(int index1, int index2)
        {
            Card card = cards[index1];
            cards[index1] = cards[index2];
            cards[index2] = card;
        }

        // Override
        public override string ToString()
        {
            foreach (Card c in cards)
            {
                sDeck += string.Format("{0}\n", c);
            }

            return sDeck;
        }
    }

    public class Hand
    {
        // Members/Fields
        protected List<Card> cards = new List<Card>();
        // Properties
        public int NumCards { get { return cards.Count; } }
        public List<Card> Cards { get { return cards; } }

        // Public Method
        public bool ContainsCard(FaceValue item)
        {
            foreach (Card c in cards)
            {
                if (c.FaceVal == item)
                {
                    return true;
                }
            }
            return false;
        }
    }

    public class Player
    {
        // Members/Fields
        private string sDeck;
        private Deck curDeck;
        private List<Card> cards = new List<Card>();

        // Properties
        public Deck CurrentDeck { get { return curDeck; } set { curDeck = value; } }
        public int CardCount { get { return cards.Count; } }

        // Public Methods
        public void AddCard(Card c)
        {
            cards.Add(c);
        }
        public Card RemCard()
        {
            Card card = cards[0];
            cards.RemoveAt(0);
            return card;
        }

        // Override
        public override string ToString()
        {
            foreach (Card c in cards)
            {
                sDeck += string.Format("{0}\n", c);
            }
            return sDeck;
        }
    }

    public class Deal
    {
        // Members/Fields
        private Deck deck = new Deck();

        // Properties
        public Deck GetDeck { get { return deck; } }

        // Public Methods
        public void DealCards(Player p1, Player p2)
        {
            deck.Shuffle(10);
            for (int i = 0; i != 26; i++)
            {
                p1.AddCard(deck.Draw());
                p2.AddCard(deck.Draw());
            }
        }
    }

    public class War
    {
        // Members/Fields
        private Player player1 = new Player();
        private Player player2 = new Player();
        private Deal dealCards = new Deal();
        private int BattleCount = 0;
        private string sStatus = "";

        // Constructors
        public War()
        {
            dealCards.DealCards(player1, player2); // Start The Game
        }

        // Public Methods
        public void PlayGame()
        {
            while (player1.CardCount != 0 | player1.CardCount != 52)
            {
                Battle(player1, player2); // Battle

                BattleCount++; // Keep count of the number of battles

                if (sStatus == "!!! War !!!")
                {
                    sStatus = WarBattle(player1, player2); // 
                }

                if (player1.CardCount == 0)
                    Console.WriteLine("Player 2 Wins");
                if (player1.CardCount == 52)
                    Console.WriteLine("Player 1 Wins");
            }
        }

        // Private Methods
        private string Battle(Player p1, Player p2)
        {
            Card p1Card = p1.RemCard();
            Card p2Card = p2.RemCard();

            //Console.WriteLine(string.Format("\nPlayer 1 Draws : {0}", p1Card));
            //Console.WriteLine(string.Format("Player 2 Draws : {0}", p2Card));

            if (p1Card.FaceVal > p2Card.FaceVal)
            {
                p1.AddCard(p1Card);
                p1.AddCard(p2Card);
                return "Player 1 has won!";
            }
            else if (p2Card.FaceVal > p1Card.FaceVal)
            {
                p2.AddCard(p2Card);
                p2.AddCard(p1Card);
                return "Player 2 has won!";
            }
            else if (p1Card.FaceVal == p2Card.FaceVal)
            {
                // Were doing this until I can figure out a way to fix the War Method
                p1.AddCard(p1Card);
                p2.AddCard(p2Card);
                return "!!! War !!!";
            }
            else
            {
                return "eRR";
            }
        }
        private string WarBattle(Player p1, Player p2)
        {
            // Burn both player's cards
            Card p1Burn1 = p1.RemCard();
            Card p1Burn2 = p1.RemCard();
            Card p1Burn3 = p1.RemCard();

            Card p2Burn1 = p2.RemCard();
            Card p2Burn2 = p2.RemCard();
            Card p2Burn3 = p2.RemCard();

            // Draw the 'battle card'
            Card p1Card = p1.RemCard();
            Card p2Card = p2.RemCard();

            // Display whats going on
            Console.WriteLine(string.Format("\nPlayer 1 Burns 3 Cards and Draws : {0}", p1Card));
            Console.WriteLine(string.Format("Player 2 Burns 3 Cards and Draws : {0}", p2Card));

            if (p1Card.FaceVal > p2Card.FaceVal)
            {
                // First add the battle cards
                p1.AddCard(p1Card);
                p1.AddCard(p2Card);
                // Then add the burn cards
                p1.AddCard(p1Burn1);
                p1.AddCard(p1Burn2);
                p1.AddCard(p1Burn3);
                p1.AddCard(p2Burn1);
                p1.AddCard(p2Burn2);
                p1.AddCard(p2Burn3);
                return "Player 1 has won!";
            }
            else if (p2Card.FaceVal > p1Card.FaceVal)
            {
                // First add the battle cards
                p2.AddCard(p1Card);
                p2.AddCard(p2Card);
                // Then add the burn cards
                p2.AddCard(p2Burn1);
                p2.AddCard(p2Burn2);
                p2.AddCard(p2Burn3);
                p2.AddCard(p1Burn1);
                p2.AddCard(p1Burn2);
                p2.AddCard(p1Burn3);
                return "Player 2 has won!";
            }
            else if (p1Card.FaceVal == p2Card.FaceVal)
            {
                return "!!! War !!!";
            }
            else
            {
                return "eRR";
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            War w = new War();
            w.PlayGame();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.