Black Jack
Voici un exemple simplifié de la structure de base d'un jeu de blackjack en C#. Cela couvre le mélange des cartes, la distribution des cartes aux joueurs et au croupier, ainsi que le calcul des scores :
csharp
Copy code
using System;
using System.Collections.Generic;
public enum CardSuit
{
Hearts,
Diamonds,
Clubs,
Spades
}
public enum CardValue
{
Ace = 1,
Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
Jack, Queen, King
}
public class Card
{
public CardSuit Suit { get; }
public CardValue Value { get; }
public Card(CardSuit suit, CardValue value)
{
Suit = suit;
Value = value;
}
}
public class Deck
{
private List<Card> cards;
private Random rand;
public Deck()
{
cards = new List<Card>();
rand = new Random();
// Initialize the deck with 52 cards
foreach (CardSuit suit in Enum.GetValues(typeof(CardSuit)))
{
foreach (CardValue value in Enum.GetValues(typeof(CardValue)))
{
cards.Add(new Card(suit, value));
}
}
}
public void Shuffle()
{
// Shuffle the deck
for (int i = cards.Count - 1; i > 0; i--)
{
int j = rand.Next(0, i + 1);
Card temp = cards[i];
cards[i] = cards[j];
cards[j] = temp;
}
}
public Card DealCard()
{
// Deal a card from the top of the deck
Card card = cards[0];
cards.RemoveAt(0);
return card;
}
}
public class BlackjackGame
{
private Deck deck;
private List<Card> playerHand;
private List<Card> dealerHand;
public BlackjackGame()
{
deck = new Deck();
deck.Shuffle();
playerHand = new List<Card>();
dealerHand = new List<Card>();
}
public void DealInitialCards()
{
// Deal two cards to the player and one card to the dealer
playerHand.Add(deck.DealCard());
dealerHand.Add(deck.DealCard());
playerHand.Add(deck.DealCard());
dealerHand.Add(deck.DealCard());
}
public int CalculateScore(List<Card> hand)
{
// Calculate the score of a hand
int score = 0;
int numAces = 0;
foreach (Card card in hand)
{
if (card.Value == CardValue.Ace)
{
numAces++;
score += 11; // Assume Ace is worth 11 initially
}
else if (card.Value == CardValue.Jack || card.Value == CardValue.Queen || card.Value == CardValue.King)
{
score += 10;
}
else
{
score += (int)card.Value;
}
}
// Adjust the score for Aces
while (score > 21 && numAces > 0)
{
score -= 10; // Change Ace value from 11 to 1
numAces--;
}
return score;
}
}
class Program
{
static void Main(string[] args)
{
BlackjackGame game = new BlackjackGame();
game.DealInitialCards();
// Add more game logic here...
}
}
Cet exemple met en œuvre les bases du jeu de blackjack en C#, y compris la définition des cartes, la création d'un jeu de cartes, la distribution des cartes aux joueurs et au croupier, ainsi que le calcul des scores des mains. Vous pouvez étendre ce code pour inclure des fonctionnalités supplémentaires telles que le jeu réel, la gestion des mises, etc.
!!! CODE: OCFYGRBzVslQ7vtIRWsZGA
Aucun commentaire:
Enregistrer un commentaire