[프로그램]

C# 포커 게임의 예시 소스 코드

Blackberrys 2023. 6. 26. 21:09
반응형


C# 포커 게임의 예시 소스 코드.
이 코드는 간단한 콘솔 기반 게임으로, 사용자와 컴퓨터 간에 5장의 카드를 받고, 각각의 패를 평가하여 승자를 결정합니다. 이 예시를 기반으로 원하는 대로 게임을 확장하고 개선할 수 있습니다.
 
using System;
using System.Collections.Generic;

namespace PokerGame
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the Poker Game!");

            // 덱을 초기화하고 섞습니다.
            Deck deck = new Deck();
            deck.Shuffle();

            // 플레이어와 컴퓨터에게 5장의 카드를 나눠줍니다.
            List<Card> playerHand = deck.DealHand(5);
            List<Card> computerHand = deck.DealHand(5);

            // 플레이어와 컴퓨터의 패를 출력합니다.
            Console.WriteLine("Your Hand:");
            PrintHand(playerHand);

            Console.WriteLine("Computer's Hand:");
            PrintHand(computerHand);

            // 플레이어와 컴퓨터의 패를 평가하고 승자를 결정합니다.
            HandEvaluator evaluator = new HandEvaluator();
            int playerScore = evaluator.EvaluateHand(playerHand);
            int computerScore = evaluator.EvaluateHand(computerHand);

            Console.WriteLine("Your Hand Rank: " + evaluator.GetRankName(playerScore));
            Console.WriteLine("Computer's Hand Rank: " + evaluator.GetRankName(computerScore));

            if (playerScore > computerScore)
            {
                Console.WriteLine("Congratulations! You win!");
            }
            else if (playerScore < computerScore)
            {
                Console.WriteLine("Sorry, you lose. Computer wins.");
            }
            else
            {
                Console.WriteLine("It's a tie!");
            }

            Console.ReadLine();
        }

        static void PrintHand(List<Card> hand)
        {
            foreach (Card card in hand)
            {
                Console.WriteLine(card.Rank + " of " + card.Suit);
            }
            Console.WriteLine();
        }
    }

    // 카드 클래스
    class Card
    {
        public string Suit { get; set; }
        public string Rank { get; set; }
    }

    // 덱 클래스
    class Deck
    {
        private List<Card> cards;

        public Deck()
        {
            cards = new List<Card>();
            string[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" };
            string[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };

            foreach (string suit in suits)
            {
                foreach (string rank in ranks)
                {
                    Card card = new Card();
                    card.Suit = suit;
                    card.Rank = rank;
                    cards.Add(card);
                }
            }
        }

        public void Shuffle()
        {
            Random random = new Random();
            int n = cards.Count;
            while (n > 1)
            {
                n--;
                int k = random.Next(n + 1);
                Card value = cards[k];
                                cards[k] = cards[n];
                cards[n] = value;
            }
        }

        public List<Card> DealHand(int numCards)
        {
            List<Card> hand = new List<Card>();
            for (int i = 0; i < numCards; i++)
            {
                hand.Add(cards[i]);
                cards.RemoveAt(i);
            }
            return hand;
        }
    }

    // 패 평가기 클래스
    class HandEvaluator
    {
        // 패 평가 로직을 구현합니다. 이 예시에서는 간단히 패의 크기(카드 숫자)를 반환합니다.
        public int EvaluateHand(List<Card> hand)
        {
            return hand.Count;
        }

        // 패의 크기(카드 숫자)를 기반으로 패 랭크의 이름을 반환합니다.
        public string GetRankName(int rank)
        {
            return rank.ToString();
        }
    }
}
 
이 소스 코드를 실행하면 콘솔에 "Welcome to the Poker Game!"이 출력되고, 플레이어와 컴퓨터 각각에게 5장의 카드가 나눠집니다. 그 후, 플레이어와 컴퓨터의 패를 출력하고, 각각의 패를 평가하여 승자를 결정합니다. 최종 결과는 승자 메시지가 출력됩니다.
이 예시 코드는 매우 간단한 포커 게임의 기본 구조만을 보여주기 위한 것입니다. 실제 포커 게임은 더 많은 규칙과 로직을 가지고 있으며, 이 예시를 기반으로 게임을 확장하고 개선할 수 있습니다.