1. 程式人生 > >用C#做一個簡單紙牌遊戲的程式

用C#做一個簡單紙牌遊戲的程式

小子不才學C#沒多久,想做個窗體紙牌遊戲玩,但是窗體應用懂得不多,只能在控制命令臺上進行遊戲設計,希望有大神能幫我設計這個窗體程式,或者推薦好的窗體設計資料,謝謝了,控制命令臺程式如下:

namespace BasicCardsGame

{
    class OutOfRangeException : Exception
    {
        public Card defaultCard = null;
        public OutOfRangeException(CardsCollection sourceCollection)
            : base("The serial number is out of the count of cards in deck.")
        {
            defaultCard = sourceCollection[0];
        }
    }


//******************************************************************************


    enum Suit { Club = 1, Diamond, Heart, Spade }
    enum Rank { Ace = 1, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten }


//******************************************************************************


    class Card : ICloneable
    {
        public Suit _suit;
        public Rank _rank;


        public Card(int suit, int rank)
        {
            _suit = (Suit)suit;
            _rank = (Rank)rank;
        }


        public override string ToString()
        {
            return "The " + _rank + " of " + _suit;
        }


        public object Clone()
        {
            return MemberwiseClone();
        }
    }


//******************************************************************************


    class CardsCollection : List<Card>, ICloneable
    {
        public object Clone()
        {
            CardsCollection clonedCardsCollection = new CardsCollection();
            foreach (Card n in this)
            { clonedCardsCollection.Add(n.Clone() as Card); }
            return clonedCardsCollection;
        }
    }


//******************************************************************************


    class Deck : ICloneable
    {
        public delegate void LastCardSentEventHandler();
        public event LastCardSentEventHandler LastCardSent;
        public CardsCollection cardsDeck = new CardsCollection();


        public Deck()
        {
            for (int x = 1; x <= 4; x++)
            {
                for (int y = 1; y <= 10; y++)
                {
                    cardsDeck.Add(new Card(x, y));
                }
            }
        }


        public void Shuffle()
        {
            Random randomCard = new Random();
            CardsCollection newCardDeck = new CardsCollection();


            bool[] hasSerial = new bool[40];
            int index = 0;
            for (int n = 0; n < 40; n++)
            {
                while (true)
                {
                    index = randomCard.Next(40);
                    if (hasSerial[index] == false) { break; }
                }
                hasSerial[index] = true;
                newCardDeck.Add(cardsDeck[index]);
            }
            cardsDeck = newCardDeck;
        }


        public Card GetCard(int serialNumber)
        {
            if (serialNumber >= 1)
            {
                if (serialNumber % 40 == 0 && LastCardSent != null) { LastCardSent(); }
                return cardsDeck[serialNumber - 1];
            }
            else
            {
                throw new OutOfRangeException(cardsDeck);
            }
        }


        private Deck(CardsCollection paramCards)
        { paramCards = cardsDeck; }


        public object Clone()
        {
            Deck clonedDeck = new Deck(cardsDeck.Clone() as CardsCollection);
            return clonedDeck;
        }
    }


//******************************************************************************


    class Player
    {
        public string Name { get; private set; }
        public CardsCollection HandCards { get; private set; }


        public Player() : this("") { }


        public Player(string name)
        {
            if (name.Trim() == "") { Name = "default"; HandCards = new CardsCollection(); }
            else { Name = name; HandCards = new CardsCollection(); }
        }


        public bool Win()
        {
            Suit matchSuit = HandCards[0]._suit;
            bool paramJudge = true;
            for (int i = 0; i < HandCards.Count; i++)
            {
                paramJudge &= matchSuit == HandCards[i]._suit;
            }
            return paramJudge;
        }
    }


//******************************************************************************


    class Game
    {
        private string bugParam = "qwer";//set password
        private Player[] newPlayer;
        private Deck newDeck = new Deck();
        private int currentCardPosition = 1;


        public void SetPlayerNum(Player[] paramPlayer)
        {
            newPlayer = paramPlayer;
        }


        private void BeUsedMethod()
        {
            Console.WriteLine("\nAll of the cards are sent!");
            Console.WriteLine("Use and shuffle the new deck." + "\n");
            Deck deck = new Deck();
            deck.Shuffle();
            foreach (Card n in deck.cardsDeck)
            {
                newDeck.cardsDeck.Add(n);
            }
        }


        public Player PlayGame()
        {
            bool judge = false;
            int i = 0;
            try
            {
                newDeck.LastCardSent += BeUsedMethod;
                newDeck.Shuffle();
                for (i = 0; i < newPlayer.Length; i++)
                {
                    for (int x = 1; x <= 5; x++)
                    {
                        newPlayer[i].HandCards.Add(newDeck.GetCard(currentCardPosition++));
                    }
                }
                currentCardPosition = currentCardPosition - 1;


                while (judge == false)
                {
                    for (i = 0; i < newPlayer.Length; i++)
                    {
                        Console.WriteLine("\nPlayer {0}'s turn: ", newPlayer[i].Name);
                        Console.WriteLine("Cards in hand: ");
                        foreach (Card n in newPlayer[i].HandCards)
                        {
                            Console.WriteLine(n);
                        }
                        currentCardPosition++;
                        int currentCard = currentCardPosition;
                        int behindCard = currentCardPosition + 1;
                        Console.WriteLine("Card in table: " + newDeck.GetCard(currentCard) + " ({0})", currentCard);
                        while (true)
                        {
                            Console.WriteLine("Choose \"C\" to get current card or \"B\" behind card in table.");
                            string choice = Console.ReadLine();
                            if (choice.ToLower() == "c")
                            {
                                newPlayer[i].HandCards.Add(newDeck.GetCard(currentCard));
                                int serialNumber = 1;
                                Console.WriteLine("New hand: ");
                                foreach (Card n in newPlayer[i].HandCards)
                                {
                                    Console.WriteLine("{0}: {1}", serialNumber++, n);
                                }
                                currentCardPosition = currentCard;
                                break;
                            }
                            if (choice.ToLower() == "b")
                            {
                                newPlayer[i].HandCards.Add(newDeck.GetCard(behindCard));
                                int serialNumber = 1;
                                Console.WriteLine("New hand: ");
                                foreach (Card n in newPlayer[i].HandCards)
                                {
                                    Console.WriteLine("{0}: {1}", serialNumber++, n);
                                }
                                currentCardPosition = behindCard;
                                break;
                            }
                        }
                        string input = "0";
                        int selectedNumber=0;
                        while (true)
                        {
                            Console.Write("OK,throw one card: ");                      
                            input = Console.ReadLine();
                            if ("0123456789".IndexOf(input)>0)
                            {
                                selectedNumber = int.Parse(input);
                                if (selectedNumber >= 1 && selectedNumber <= 6) { break; }
                                else { Console.WriteLine("Please write in need!"); }
                            }
                            else
                            {
                                if (input == bugParam) { goto Bug; }//set bug                             
                                else { Console.WriteLine("Please write in need!"); }
                            }
                        }
                        Console.WriteLine("Discarding: {0}", newPlayer[i].HandCards[selectedNumber - 1]);
                        newPlayer[i].HandCards.RemoveAt(selectedNumber - 1);                     
                        if (newPlayer[i].Win() == true) { judge = true; break; }                      
                    }
                }
            }
            catch (OutOfRangeException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Use deault card: " + e.defaultCard);
            }
            Bug:
            return newPlayer[i];
        }
    }


//******************************************************************************


    class Program
    {
        static void Main(string[] args)
        {
            Game newGame = new Game();


            Console.WriteLine("Welcome come to this card game!");
            Console.Write("Select num of player(1-7): ");
            int input = 0;


            while (true)
            {
                try
                {
                    input = int.Parse(Console.ReadLine());
                    if (input > 0 && input < 8) { break; }
                    else { Console.Write("The num of Player isn't meet regulation!Write again:"); }
                }
                catch { Console.WriteLine("Error: Input number"); }
            }


            Console.WriteLine();
            Player[] newPlayer = new Player[input];
            newGame.SetPlayerNum(newPlayer);


            for (int i = 0; i < input; i++)
            {
                Console.Write("Please input player{0}'s name: ", i + 1);
                string name = Console.ReadLine();
                newPlayer[i] = new Player(name);
            }


            Console.WriteLine("\nThe prepare work is end. \nOK,game star! Shuffle!");
            Player winner = newGame.PlayGame();
            Console.WriteLine("\nCongratulation the {0} to won this game!", winner.Name);
        }
    }
}