命名空间 WarCardGame {
public class Constants
{
public const int Ace = 14;
public const int Jack = 11;
public const int Queen = 12;
public const int King = 13;
public enum Symbol
{
Spades,
Hearts,
Clubs,
Diamonds,
}
}
public partial class Form2 : Form
{
private players allPlayers = new players();
Deck deck;
List<players> players;
game game;
public Form2( string player1Name, string player2Name)
{
InitializeComponent();
deck = new Deck();
btnDraw.BringToFront();
textBox1.Text = player1Name;
textBox2.Text = player2Name;
int halfDeckSize = deck.GetDeckList().Count / 2;
List<Card> player1Cards = new List<Card>(deck.GetDeckList().Take(halfDeckSize));
List<Card> player2Cards = new List<Card>(deck.GetDeckList().Skip(halfDeckSize));
players player1 = new players(player1Name, player1Cards);
players player2 = new players(player2Name, player2Cards);
players = new List<players> { player1, player2 };
game = new game(players);
}
private void btnDraw_Click(object sender, EventArgs e)
{
int Card = GetNextCard();
if (imageList1.Images != null && imageList1.Images.Count > 0)
{
int cardIndex = Card;
if (cardIndex >= 0 && cardIndex < imageList1.Images.Count)
{
pictureBox1.Image = global::WarCardGame.Resource1.getImage(cardIndex);
}
else
{
MessageBox.Show($"Invalid card index: {cardIndex}");
}
}
else
{
MessageBox.Show("ImageList is empty!");
}
pictureBox1.Image = null;
Label resultLabel = Label;
game.PlayRound(resultLabel, pictureBox1, player1Card, player2Card);
Label FinalResults = lblFinalResults;
game.PlayRound(FinalResults, pictureBox1, player1Card, player2Card);
game.DisplayPoints(lblplayer1Points, lblplayer2Points, lblFinalResults);
}
private int GetNextCard()
{
List<Card> deckList = deck.GetDeckList();
if (deckList.Count > 0)
{
Card nextCard = deckList[0];
deckList.RemoveAt(0);
return nextCard.GetNumber();
}
else
{
MessageBox.Show("The deck is empty!");
return -1;
}
}
private void Label_Click(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
class game
{
private Deck deck;
private List<players> players;
private int currentPlayerIndex;
public game(List<players> players)
{
this.players = players;
deck = new Deck();
currentPlayerIndex = 0;
}
public void DealInitialCards(PictureBox pictureBox1, PictureBox pictureBox2, PictureBox pictureBox3)
{
foreach (var player in players)
{
for (int i = 0; i < 5; i++)
{
Card dealtCard = deck.GetDeckList().First();
player.addCard(dealtCard);
dealtCard.IndexInHand = player.getcards().Count - 1;
deck.GetDeckList().RemoveAt(0);
}
}
UpdateHandVisual(pictureBox2, players[0]);
UpdateHandVisual(pictureBox3, players[1]);
}
private void UpdateHandVisual(PictureBox pictureBox, players player)
{
if (player.getcards().Count > 0)
{
Card currentPlayerCard = player.getcards()[player.indexInHand];
pictureBox.Image = global::WarCardGame.Resource1.getImage(currentPlayerCard.GetNumber());
}
else
{
pictureBox.Image = null;
}
pictureBox.Image = global::WarCardGame.Resource1.getImage(player.indexInHand);
}
public void PlayRound(Label resultLabel,PictureBox pictureBox1, PictureBox pictureBox2, PictureBox pictureBox3)
{
players currentPlayer = players[currentPlayerIndex];
players nextPlayer = players[(currentPlayerIndex + 1) % players.Count];
Card currentPlayerCard = currentPlayer.PlayCard();
Card nextPlayerCard = nextPlayer.PlayCard();
if (currentPlayerCard != null && nextPlayerCard != null)
{
pictureBox2.Image = global::WarCardGame.Resource1.getImage(currentPlayerCard.GetNumber());
pictureBox3.Image = global::WarCardGame.Resource1.getImage(nextPlayerCard.GetNumber());
if (currentPlayerCard.CompareTo(nextPlayerCard) > 0)
{
resultLabel.Text = $"{currentPlayer.getName()} wins the round!";
currentPlayer.addCard(nextPlayerCard);
currentPlayer.RoundsWon++;
}
else if (currentPlayerCard.CompareTo(nextPlayerCard) < 0)
{
resultLabel.Text = $"{nextPlayer.getName()} wins the round!";
nextPlayer.addCard(currentPlayerCard);
nextPlayer.RoundsWon++;
}
else
{
resultLabel.Text = "It's a tie!";
}
}
currentPlayerIndex = (currentPlayerIndex + 1) % players.Count;
pictureBox1.Image = null;
}
public void DisplayPoints(Label player1Points, Label player2Points, Label FinalResults)
{
foreach(var player in players)
{
int points = player.RoundsWon;
if (player == players[0])
player1Points.Text = $"{player.getName()}:{points} points";
else if (player == players[1])
player2Points.Text = $"{player.getName()}: {points}points";
}
if (players[0].RoundsWon > players[1].RoundsWon)
FinalResults.Text = $"{players[0].getName()} won!";
else if (players[0].RoundsWon < players[1].RoundsWon)
FinalResults.Text = $"{players[1].getName()} won!";
else
FinalResults.Text = "It's a tie!";
}
}
class players
{
string name;
List<Card> cards = new List<Card>();
public int indexInHand { get; set; }
public int RoundsWon { get; set; }
public List<players> PlayerList { get; set; }
public players()
{
}
public players(string name, List<Card> list)
{
PlayerList = new List<players>();
this.name = name;
cards = new List<Card>(list);
}
public void addCard(Card card)
{
cards.Add(card);
card.IndexInHand = cards.Count - 1;
this.indexInHand = card.IndexInHand;
}
public string getName()
{
return name;
}
public List<Card> getcards()
{
return cards;
}
public Card PlayCard()
{
if (cards.Count > 0)
{
Card playedCard = cards[0];
cards.RemoveAt(0);
return playedCard;
}
else
{
return null;
}
}
public partial class ChoosePlayerName : Form
{
public string Player1Name { get; private set; }
public string Player2Name { get; private set; }
public ChoosePlayerName()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void btnDone_Click(object sender, EventArgs e)
{
if (!(string.IsNullOrEmpty(textBox1.Text)) && !(string.IsNullOrEmpty(textBox2.Text)))
{
Player1Name = textBox1.Text;
Player2Name = textBox2.Text;
DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("Please enter names for both players.");
}
}
}
公共类卡:Constantants,IComparable {
int number;
Symbol symbol;
public Card(int number,Symbol symbol)
{
this.number = number;
this.symbol = symbol;
}
public int GetNumber()
{
return number;
}
public Symbol GetSymbol()
{
return symbol;
}
public int CompareTo(Card other)
{
int numberComparison = number.CompareTo(other.number);
if (numberComparison == 0)
{
return symbol.CompareTo(other.symbol);
}
return numberComparison;
}
public int IndexInHand { get; set; }
public static string CardToString(Card card)
{
return $"{card.number} of {card.symbol}";
}
}
public partial class FirstWindow : Form
{
//private List<string> cards;
//private game game;
public FirstWindow()
{
InitializeComponent();
}
static IEnumerable<Bitmap> GetImagesFromResources()
{
var resourceManager = WarCardGame.Properties.Resources.ResourceManager;
var resourceSet = resourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true);
var imageResources = resourceSet.Cast<DictionaryEntry>()
.Where(entry => entry.Value is Bitmap)
.Select(entry => (Bitmap)entry.Value);
return imageResources;
}
static void PrintImageListImages(ImageList imageList)
{
if (imageList != null)
{
Console.WriteLine($"Total Images in ImageList: {imageList.Images.Count}");
for (int i = 0; i < imageList.Images.Count; i++)
{
Image image = imageList.Images[i];
string imageName = GetImageName(image);
Console.WriteLine($"Image {i + 1}: {imageName}");
}
}
}
static string GetImageName(Image image)
{
return image?.Tag?.ToString() ?? $"Unnamed Image";
}
private void SetupImageList()
{
var imageResources = GetImagesFromResources();
int i = 0;
foreach (var bitmap in imageResources)
{
i++;
}
}
private void btnStart_Click(object sender, EventArgs e)
{
ChoosePlayerName choosePlayerName = new ChoosePlayerName();
if (choosePlayerName.ShowDialog() == DialogResult.OK)
{
Form2 form2 = new Form2(choosePlayerName.Player1Name, choosePlayerName.Player2Name);
form2.Show();
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
public class Deck
{
public List<Card> deckList = new List<Card>();
public Deck()
{
for (int i = 1; i <= 4; i++)
{
for (int j = 2; j <= 14; j++)
{
Constants.Symbol s = Constants.Symbol.Spades;
if (i == 2)
s = Constants.Symbol.Hearts;
if (i == 3)
s = Constants.Symbol.Clubs;
if (i == 4)
s = Constants.Symbol.Diamonds;
deckList.Add(new Card(j, s));
}
}
ShuffleDeck();
}
public List<Card> GetDeckList()
{
return deckList;
}
private void ShuffleDeck()
{
Random random_Shuffle = new Random();
for (int i = deckList.Count - 1; i > 0; i--)
{
int j = random_Shuffle.Next(i + 1);
Card temp = deckList[i];
deckList[i] = deckList[j];
deckList[j] = temp;
}
}
}
}
我期望该方法正确比较卡片并输出正确的获胜者。我的代码输出每轮的获胜者都是错误的。我希望我的游戏比较数字,如果它是一个符号,我在代码中写下每个符号的值,但我不知道为什么它不起作用
IComparable/IComparable<T>
界面用于订购商品。如果您想比较项目是否相等,您需要实现 IEquatable<T>
接口。
您可能还想重写
bool Equals(object obj)
方法,并实现 public static bool operator ==(Card left, Card right)
和 !=
运算符。
我还会考虑使用结构或记录而不是类,因为它们为您提供了自动相等实现,但您可能需要删除或使用
IndexInHand
执行其他操作。