扑克游戏(c#)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Poker
{
    internal class Card
    {
        public int rank;
        public string suit;

        public Card(string suit, int rank)
        {
            this.rank = rank;
            this.suit = suit;
        }

        public override string ToString()
        {
            string new_rank = "";
            switch (this.rank)
            {
                case 14:
                    {
                        new_rank = "A";
                        break;
                    }
                case 11:
                    {
                        new_rank = "J";
                        break;
                    }
                case 12:
                    {
                        new_rank = "Q";
                        break;
                    }
                case 13:
                    {
                        new_rank = "K";
                        break;
                    }
                default:
                    {
                        new_rank = rank.ToString();
                        break;
                    }
            }
            return this.suit + new_rank;
        }

        //static void Main(string[] args)
        //{
        //    Card card = new Card("红桃", 13);
        //    Console.WriteLine(card);
        //}
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Poker
{
    internal class Poker
    {
        Card[] cards = new Card[20];
        string[] suits = { "红桃", "黑桃", "方片", "草花" };
        public Poker()
        {
            int idx = 0;
            for (int i = 0; i < suits.Length; i++)
            {
                for (int j = 10; j <= 14; j++)
                {
                    Card card = new Card(suits[i], j);
                    cards[idx++] = card;
                }
            }
        }

        public void output()
        {
            int idx = 0;
            for (int i = 0; i < suits.Length; i++)
            {
                for (int j = 10; j <= 14; j++)
                {
                    if (idx % 5 == 0 && idx != 0)
                    {
                        Console.WriteLine();
                    }
                    Console.Write(cards[idx++]);
                }
            }
        }
        public void suffle()
        {
            Random random = new Random();
            for(int i = 0; i < 20; i++)
            {
                int random_idx = random.Next(20);
                Card tmp = cards[i];
                cards[i] = cards[random_idx];
                cards[random_idx] = tmp;
            }
        }

        public List<Card> getOneHand()
        {
            this.suffle();
            List<Card> cardLst = new List<Card>();
            for(int i = 0; i < 5; i++)
            {
                cardLst.Add(cards[i]);
            }
            return cardLst;
        }

        public string getType(List<Card> hand)
        {
            List<int> rankLst = new List<int>();
            HashSet<int> rankSet = new HashSet<int>();
            HashSet<string> suitSet = new HashSet<string>();
            Dictionary<int, int> dic = new Dictionary<int, int>();
            foreach(Card card in hand)
            {
                rankLst.Add(card.rank);
                rankSet.Add(card.rank);
                suitSet.Add(card.suit);
                if (dic.ContainsKey(card.rank))
                {
                    int count = dic[card.rank];
                    count++;
                    dic[card.rank] = count;
                }
                else
                {
                    dic[card.rank] = 1;
                }
            }

            rankLst.Sort();
            bool isStraight = false;
            bool isSameColor = false;

            if (suitSet.Count() == 1)
            {
                isSameColor = true;
            }
            if(rankSet.Count() == 5 && rankLst[4] - rankLst[0] == 4)
            {
                isStraight = true;
            }

            if (isSameColor && isStraight)
            {
                return "同花顺";
            }
            if (isSameColor)
            {
                return "同花";
            }
            if (isStraight)
            {
                return "顺子";
            }

            if(rankSet.Count() == 4)
            {
                return "一对";
            }
            if(rankSet.Count() == 5)
            {
                return "杂牌";
            }
            if(rankSet.Count() == 2)
            {
                //32or41
                if (dic.ContainsValue(4))
                {
                    return "四条";
                }
                else
                {
                    return "满堂红";
                }
            }
            if(rankSet.Count() == 3)
            {
                //311or221
                if (dic.ContainsValue(3))
                {
                    return "311";
                }
                else
                {
                    return "两对";
                }
            }

            return "unknown";
        }

        public Card getCard(int idx)
        {
            return cards[idx];
        }
        //static void Main(string[] args)
        //{
        //    Poker poker = new Poker();
        //    poker.output();
        //    Console.WriteLine();
        //    poker.suffle();
        //    Console.WriteLine();
        //    List<Card> hand = poker.getOneHand();
        //    poker.output();
        //    Console.WriteLine();
        //    foreach (Card item in hand)
        //    {
        //        Console.Write(item+" ");
        //    }
        //    string type = poker.getType(hand);
        //    Console.WriteLine();
        //    Console.WriteLine(type);
        //}
    }
}

using System.Linq.Expressions;

namespace Poker
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            dic.Add("同花顺", 100);
            dic.Add("四条", 25);
            dic.Add("满堂红", 10);
            dic.Add("同花", 5);
            dic.Add("顺子", 4);
            dic.Add("311", 3);
            dic.Add("两对", 2);
            dic.Add("一对", 0);
            dic.Add("杂牌", -1);

            int score = 100;
            Poker poker = new Poker();
            while(score > 0)
            {
                score--;
                Console.WriteLine($"your score is {score}");
                List<Card> hand = poker.getOneHand();
                foreach(Card card in hand)
                {
                    Console.Write(card+" ");
                }
                string type = poker.getType(hand);
                Console.WriteLine(type);
                Console.WriteLine("\nplease input the card no what you want to change(1-5空格分割)\n" +
                    "input ok if you do not want to change any card\n" +
                    "input quit to end game");
                string cmd = Console.ReadLine();
                if (cmd.Equals("quit"))
                {
                    break;
                }
                if (!cmd.Equals(""))
                {
                    string[] arr = cmd.Split(' ');
                    for(int i = 0; i < arr.Length; i++)
                    {
                        int card_idx = int.Parse(arr[i]);
                        card_idx--;
                        hand.RemoveAt(card_idx);
                        Card card = poker.getCard(i + 5);
                        hand.Insert(card_idx, card);
                    }
                }

                foreach (Card card in hand)
                {
                    Console.Write(card + " ");
                }

                type = poker.getType(hand);
                Console.WriteLine(type);
                int prize = dic[type];
                Console.WriteLine($"you win {prize} score");
                score += prize;
                Console.WriteLine("-----------------------------");
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值