提问人:Jasem Khan 提问时间:11/18/2023 更新时间:11/18/2023 访问量:70
我的卡片即使在打印后也会继续随机化。C语言中的高牌抽奖游戏#
My cards keep randomizing even after they have been printed. High Card Draw game in C#
问:
我正在尝试编写一个高牌抽奖游戏,玩家会得到一张初始牌,然后可以继续抽牌,直到他们决定坚持使用最近一张牌。但出于某种原因,我的初始牌和之前发的牌不断被随机化。当您运行代码时,您可以看到这种情况发生,它会为您提供一张初始卡。一旦你选择抽奖,你的初始卡片在打印后也会被改变。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
Console.WriteLine("Welcome to High Card Draw Game!");
// Get the number of players
int numPlayers;
do
{
Console.Write("Enter the number of players (2-4): ");
} while (!int.TryParse(Console.ReadLine(), out numPlayers) || numPlayers < 2 || numPlayers > 4);
// Initialize the game
HighCardDrawGame game = new HighCardDrawGame(numPlayers);
// Play the game
game.PlayGame();
Console.WriteLine("Game over! Final Winner: " + game.GetFinalWinner());
}
}
class HighCardDrawGame
{
private List<Player> players;
private Deck deck;
private const int NumRounds = 5;
public HighCardDrawGame(int numPlayers)
{
deck = new Deck();
players = new List<Player>();
// Create players and get their names
for (int i = 0; i < numPlayers; i++)
{
Console.Write($"Enter name for Player {i + 1}: ");
string playerName;
do
{
playerName = Console.ReadLine();
} while (!IsAlphabetic(playerName));
Player player = new Player(playerName);
players.Add(player);
// Deal one card to each player at the start
Card initialCard = deck.DrawCard();
player.SetInitialCard(initialCard); // Set the initial card
player.DrawCard(initialCard);
Console.WriteLine($"{player.Name}, your initial card is: {initialCard}");
}
}
public void PlayGame()
{
for (int round = 1; round <= NumRounds; round++)
{
Console.WriteLine($"\nRound {round}");
// Allow players to draw or stick
foreach (var player in players)
{
Console.WriteLine($"{player.Name}, it's your turn.");
while (true)
{
Console.Write($"Would you like to draw again or stick with your current card? (Enter 'Draw' or 'Stick'): ");
string choice = Console.ReadLine().ToLower();
if (choice == "draw")
{
Card drawnCard = deck.DrawCard();
player.DrawCard(drawnCard);
Console.WriteLine($"{player.Name}, you drew: {drawnCard}");
}
else if (choice == "stick")
{
break; // Break the loop if the player chooses to stick
}
else
{
Console.Write("Invalid choice. ");
}
}
}
// Display hands
foreach (var player in players)
{
Console.WriteLine($"{player.Name}'s hand: {player.Hand}");
}
// Determine the round winner
Player roundWinner = players.OrderByDescending(p => p.Hand.GetHighestRank()).First();
roundWinner.IncrementRoundWins();
Console.WriteLine($"Round {round} winner: {roundWinner.Name} with {roundWinner.Hand}");
}
// Display initial cards
Console.WriteLine("\nInitial Cards:");
foreach (var player in players)
{
Console.WriteLine($"{player.Name}'s initial card: {player.InitialCard}");
}
}
public string GetFinalWinner()
{
// Determine the final winner
Player finalWinner = players.OrderByDescending(p => p.RoundWins).First();
return $"{finalWinner.Name} with {finalWinner.RoundWins} round wins";
}
private static bool IsAlphabetic(string input)
{
return input.All(char.IsLetter);
}
}
class Player
{
public string Name { get; }
public Hand Hand { get; private set; }
public int RoundWins { get; private set; }
public Card InitialCard { get; private set; } // New property to store the initial card
public Player(string name)
{
Name = name;
Hand = new Hand();
RoundWins = 0;
}
public void DrawCard(Card card)
{
Hand.Clear(); // Remove existing card
Hand.AddCard(card);
}
public void SetInitialCard(Card card)
{
InitialCard = card;
}
public void IncrementRoundWins()
{
RoundWins++;
}
}
class Deck
{
private List<Card> cards;
private Random random;
private bool hasBeenShuffled;
public Deck()
{
cards = GenerateDeck();
random = new Random();
Shuffle(); // Shuffle the deck after generation
}
public Card DrawCard()
{
if (cards.Count == 0)
{
Console.WriteLine("Out of cards! The game cannot continue.");
Environment.Exit(0);
}
Card card = cards[0];
cards.RemoveAt(0);
return card;
}
private void Shuffle()
{
if (!hasBeenShuffled)
{
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;
}
hasBeenShuffled = true; // Set the flag to indicate that the deck has been shuffled
}
}
private List<Card> GenerateDeck()
{
List<Card> deck = new List<Card>();
foreach (var suit in Enum.GetValues(typeof(Suit)))
{
foreach (var rank in Enum.GetValues(typeof(Rank)))
{
deck.Add(new Card((Suit)suit, (Rank)rank));
}
}
return deck;
}
}
class Card
{
public Suit Suit { get; }
public Rank Rank { get; }
public Card(Suit suit, Rank rank)
{
Suit = suit;
Rank = rank;
}
public override string ToString()
{
return $"{Rank} of {Suit}";
}
}
class Hand
{
private List<Card> cards;
public Hand()
{
cards = new List<Card>();
}
public void AddCard(Card card)
{
cards.Add(card);
}
public void Clear()
{
cards.Clear();
}
public Rank GetHighestRank()
{
return cards.Max(card => card.Rank);
}
public override string ToString()
{
return string.Join(", ", cards);
}
}
enum Suit
{
Spades,
Hearts,
Diamonds,
Clubs
}
enum Rank
{
Ace = 1,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack = 11,
Queen = 12,
King = 13
}
我尝试了所有事情,但无法解决这个问题。我希望初始牌和已发牌在发牌后永远停止更改。如果你们能帮我解决这个问题,我将不胜感激!
答: 暂无答案
评论
Shuffle
I want the initial card and the dealt cards to stop changing forever once they are dealt.
我不知道这意味着什么。对我来说,问题并不明显。程序似乎有效。InitialCard
Hand
player.DrawCard(..)
Hand
player.DrawCard(...)
AddToHand.(...)