这是我正在使用的卡片,我很困惑为什么经销商在指示时没有打中。我已经重新排列了代码,尝试了不同的方法,但是它似乎仍然无法正常工作。如果有人可以帮助我解决这个C#脚本,我将非常感激。
using System;
using System.Collections.Generic;
public class MainClass
{
public static int PlayerTotal;
public static int DealerTotal;
static Random random = new Random();
public static List<int> cards = new List<int>();
public static List<int> hand = new List<int>();
public static List<int> DealerHand = new List<int>();
public static bool GameOver = false;
public static void Main (string[] args)
{
// Players hand gets created
for (int i = 2; i <= 11; i++)
{
cards.Add(i);
}
for (int i = 0; i < 2; i++)
{
int index = random.Next(cards.Count);
hand.Add(cards[index]);
}
foreach (int a in hand)
{
Console.WriteLine("{0}", a);
PlayerTotal = PlayerTotal + a;
if(PlayerTotal == 22 || PlayerTotal == 21)
{
Console.WriteLine("You won!");
GameOver = true;
}
}
Console.WriteLine("Your hand total is " + PlayerTotal);
// Dealer hand being created
for(int i = 0; i < 2; i++)
{
int index = random.Next(cards.Count);
DealerHand.Add(cards[index]);
}
foreach(int a in DealerHand)
{
Console.WriteLine("{0}", a);
DealerTotal = DealerTotal + a;
if(DealerTotal == 22 || DealerTotal == 21)
{
Console.WriteLine("The dealer won!");
GameOver = true;
}
}
Console.WriteLine("The dealer hand total is " + DealerTotal);
// Player choice to hit or stay
while (PlayerTotal < 21 && GameOver == false)
{
Console.WriteLine("Do you want to hit or stand? h/s");
string choice = Console.ReadLine();
if (choice == "h")
{
PlayerHit();
}
else if (choice == "s")
{
Console.WriteLine("You stood");
}
}
while (DealerTotal < 21 && GameOver == false )
{
if(DealerTotal <= 16)
{
DealerHit();
}
else
{
Console.WriteLine("The dealer stood");
}
}
}
public static void PlayerHit()
{
for (int i = 0; i < 1; i++)
{
int index = random.Next(cards.Count);
int hitCard = cards[index];
hand.Add(hitCard);
PlayerTotal = PlayerTotal + hitCard;
Console.WriteLine("You got a " + hitCard);
Console.WriteLine("Your new total is " + PlayerTotal);
if(PlayerTotal > 21)
{
Console.WriteLine("You lost!");
GameOver = true;
}
else if (PlayerTotal == 21)
{
Console.WriteLine("You won!");
GameOver = true;
}
}
}
public static void DealerHit()
{
for (int i = 0; i < 1; i++)
{
int index = random.Next(cards.Count);
int DealerHitCard = cards[index];
DealerHand.Add(DealerHitCard);
DealerTotal = DealerTotal + DealerHitCard;
int DealerHitCardNew = DealerHitCard;
Console.WriteLine("The dealer got a " + DealerHitCardNew);
Console.WriteLine("The dealers total is now " + DealerTotal);
if (DealerTotal > 21)
{
Console.WriteLine("You Won!");
GameOver = true;
}
else if (DealerTotal == 21)
{
Console.WriteLine("You Lost!");
GameOver = true;
}
}
}
}
任何帮助将不胜感激。我的学校为我们设置了作业,必须创建一种应用程序,我认为这样做很有趣。
答案 0 :(得分:0)
此行:
while(PlayerTotal < 21 && GameOver == false) {
只有当您破产或得到21时,这才是正确的。如果您站着,则永远不会退出此循环。在Stand选项中添加一个休息时间:
} else if(choice == "s"){
Console.WriteLine("You stood");
break;
}
不幸的是,您在以下循环中遇到了相同的问题:
while(DealerTotal < 21 && GameOver == false ) {
在正确的块上添加break
,以便在经销商站立时退出循环。最后,在玩家和发牌人都站稳后,您将需要添加赢/输逻辑。