c# - 平局后如何拿起第四张牌?
问题描述
我有这个纸牌战争游戏;游戏开始时,每个玩家翻转 1 张牌,拥有最高牌的人获胜。如果平局,接下来的三张牌面朝下打出,第四张牌面朝上,谁拥有最高的牌获胜。游戏一直进行到 1 名玩家拥有所有牌;但是,我无法正确完成它,每次我在“让翻转卡片”语句之后按回车时它都会崩溃,并且我无法让玩家获得第四张卡片,因为当使用第三个变量r3
,它同样崩溃。
using System;
using System.Collections.Generic;
namespace PTCB12WARGAME2
{
class Card
{
public string Name;
public int Value;
public string Suit;
public override string ToString() { return string.Format("{0} of {1}", Name, Suit); }
}
class DeckOfCards
{
static void Main(string[] args)
{
List<Card> DeckOfCards = new List<Card>();
var names = new[] { "Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
var suits = new[] { "Hearts", "Clubs", "Diamonds", "Spades" };
var cards = new List<Card>();
// Populate our deck of cards with two nested loops
foreach (var suit in suits)
{
// We use a 'for' loop here instead of 'foreach' so we can use the index to
// populate the Value (which is always one more than the index of the name)
for (int i = 0; i < names.Length; i++)
{
cards.Add(new Card { Name = names[i], Value = i + 1, Suit = suit });
}
}
//Display deck of 52 cards
Console.WriteLine("Each player is flipping over 1 card and whomever has the highest card wins");
Console.ReadLine();
for (int i = 0; i < DeckOfCards.Count; i++)
{
Console.WriteLine(DeckOfCards[i]);
}
Console.WriteLine();
// create just ONCE the random object
Random random = new Random();
List<Card> deckOfPlayer1 = new List<Card>();
List<Card> deckOfPlayer2 = new List<Card>();
do
{
Console.WriteLine("Let's flip cards");
string userAnswer = Console.ReadLine();
if (userAnswer == "quit")
break;
// pick a random card for player 1
int r1;
r1 = random.Next(0, DeckOfCards.Count);
Card c1 = DeckOfCards[r1];
deckOfPlayer1.Add(c1);
// pick a random card for player 2
int r2;
r2 = random.Next(0, DeckOfCards.Count);
Card c2 = DeckOfCards[r2];
deckOfPlayer2.Add(c2);
Console.WriteLine("Player1 has: {0}", c1);
Console.WriteLine("Player2 has: {0}", c2);
// now compare the cards
if (c1.Value == c2.Value )
{
Console.WriteLine("Its a tie! Next three cards in deck are played face down, " +
"4th card face up, whoever has the highest card wins");
int r3;
r3 = random.Next(0, DeckOfCards.Count);
Card c3 = DeckOfCards[3];
deckOfPlayer1.Add(c3);
Console.WriteLine(deckOfPlayer1[3]);
Console.WriteLine(deckOfPlayer2[3]);
if (c1.Value > c2.Value && c2.Value != 1)
{
deckOfPlayer1.Add(c2);
deckOfPlayer2.Remove(c2);
Console.WriteLine("Player1 wins");
}
else
{
Console.WriteLine("Player2 wins");
deckOfPlayer2.Add(c1);
deckOfPlayer1.Remove(c1);
}
}
else if (c1.Value == 1 || (c1.Value > c2.Value && c2.Value != 1))
{
deckOfPlayer1.Add(c2);
deckOfPlayer2.Remove(c2);
Console.WriteLine("Player1 wins");
}
else
{
Console.WriteLine("Player2 wins");
deckOfPlayer2.Add(c1);
deckOfPlayer1.Remove(c1);
}
} while (deckOfPlayer1.Count < 52 || (deckOfPlayer2.Count < 52));
}
}
}
解决方案
首先,请学习如何使用交互式调试。如果您只知道“它崩溃了”,那么您将很难找到并消除代码中的错误。这是您可以开始的一个教程。
在这种情况下,问题可能是由于您使用了两个列表:
List<Card> DeckOfCards = new List<Card>(); // One here
var names = new[] { "Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
var suits = new[] { "Hearts", "Clubs", "Diamonds", "Spades" };
var cards = new List<Card>(); // And one here
您正在用 填充第二个cards.Add
,但您正在尝试在其他任何地方访问第一个,例如使用这些行:
r1 = random.Next(0, DeckOfCards.Count);
Card c1 = DeckOfCards[r1];
删除其中一个列表,用另一个实例替换它,您的代码将有更好的运行机会(免责声明:我没有编译它来检查其他问题)
推荐阅读
- javascript - 我正在为我的学校项目创建一个样机电影网站。我遇到了一些与“选择”表单和 JavaScript 代码相关的问题
- ruby-on-rails - 如果未上传载波图像,则添加默认图像
- c++ - 应用程序在 Windows 10 上的 boost::filesystem::exist 和 CoInitializeSecurity 调用上自行启动
- selenium - 无法从网站 (goibibo.com) 中选择城市名称
- ionic-framework - 在 Ionic 1 应用程序中创建页面的应用程序快捷方式
- android - 如何在 Retrofit 中使用 execute
- c++ - 如何使用变体和使用处理复杂的标头递归
- bash - 如何在 ssh 脚本中获取嵌套的 sftp 以等待密码提示
- javascript - How do I make values update automatically if user clicks on another option?
- android - 数据无法存储在实时数据库中