我的代码创建了一个二十一点游戏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Crappy_Card_Game
{
class Program
{
public static string ans = null;
public static string randomans = null;
public static int card_value;
public static string[] Deck = new string[37];
public static int[] Deck_value = new int[37];
public static string[] Player_Hand = new string[10];
public static int[] Player_HandValue = new int[10];
public static string[] Dealer_Hand = new string[10];
public static int[] Dealer_HandValue = new int[10];
public static int playerhandvalue;
public static int dealerhandvalue;
public static int deckvalue;
static void Main(string[] args)
{
int GamesPlayed = 0;
string again = null;
do
{
ShuffleCards();
DealingCards();
PlayersTurn();
DealersTurn();
Console.WriteLine();
if (dealerhandvalue > playerhandvalue && dealerhandvalue <= 21)
{
Console.WriteLine("Dealer Wins!");
}
else if (dealerhandvalue == playerhandvalue)
{
Console.WriteLine("Draw!");
}
else if (playerhandvalue <= 21 && playerhandvalue > dealerhandvalue)
{
Console.WriteLine("Player Wins!");
}
else if (playerhandvalue > 21 && dealerhandvalue < 21)
{
Console.WriteLine("Dealer Wins!");
}
else if (playerhandvalue < 22 && dealerhandvalue > 21)
{
Console.WriteLine("Player Wins!");
}
Console.WriteLine("Would you like another game? (yes/no)");
again = Console.ReadLine().ToLower();
Console.Clear();
GamesPlayed++;
ClearAllArrays(); // Apart from Deck and DeckValue
if (GamesPlayed == 9 || GamesPlayed > 9)
{
Array.Clear(Deck, 0, Deck.Length);
Array.Clear(Deck_value, 0, Deck_value.Length);
}
} while (again == "yes");
}
static void Playing_Cards()
{
card_value = 0;
switch (randomans)
{
case "Ace":
card_value = 13;
break;
case "1":
card_value = 1;
break;
case "2":
card_value = 2;
break;
case "3":
card_value = 3;
break;
case "4":
card_value = 4;
break;
case "5":
card_value = 5;
break;
case "6":
card_value = 6;
break;
case "7":
card_value = 7;
break;
case "8":
card_value = 8;
break;
case "9":
card_value = 9;
break;
case "Jack":
card_value = 10;
break;
case "Queen":
card_value = 11;
break;
case "King":
card_value = 12;
break;
}
}
static void Picking()
{
Random rnd = new Random();
int PickNumber = rnd.Next(0, 12);
string[] Card_Inventory = new string[13];
string[] Card_Suit = new string[4];
Card_Inventory[0] = "Ace";
Card_Inventory[1] = "1";
Card_Inventory[2] = "2";
Card_Inventory[3] = "3";
Card_Inventory[4] = "4";
Card_Inventory[5] = "5";
Card_Inventory[6] = "6";
Card_Inventory[7] = "7";
Card_Inventory[8] = "8";
Card_Inventory[9] = "9";
Card_Inventory[10] = "Jack";
Card_Inventory[11] = "Queen";
Card_Inventory[12] = "King";
string PickNumberName = Card_Inventory[PickNumber];
int PickSuit = rnd.Next(0, 3);
Card_Suit[1] = "Heart";
Card_Suit[2] = "Spade";
Card_Suit[3] = "Diamond";
Card_Suit[0] = "Club";
string PickSuitName = Card_Suit[PickSuit];
ans = PickNumberName + " " + PickSuitName;
randomans = PickNumberName;
}
static void ShuffleCards()
{
int check = 0;
int i = 0;
while (i < 37)
{
Picking();
Playing_Cards();
while (check < 37)
{
if (ans == Deck[check])
{
Picking();
Playing_Cards();
check = 0;
}
check++;
}
check = 0;
Deck[i] = ans;
Deck_value[i] = card_value;
i++;
}
}
static void ShowDeck()
{
for (int i = 1; i < Deck.Length; i++)
{
Console.WriteLine(Deck[i]);
}
Console.WriteLine();
Console.WriteLine("Press Enter to Continue..");
Console.ReadLine();
}
static void DealingCards()
{
int i = 1;
do
{
deckvalue = i;
i++;
} while (Deck[i] == null);
Player_Hand[0] = Deck[deckvalue];
Player_HandValue[0] = Deck_value[deckvalue];
Deck[deckvalue] = null;
Deck_value[deckvalue] = 0;
deckvalue++;
Player_Hand[1] = Deck[deckvalue];
Player_HandValue[1] = Deck_value[deckvalue];
Deck[deckvalue] = null;
Deck_value[deckvalue] = 0;
deckvalue++;
Dealer_Hand[0] = Deck[deckvalue];
Dealer_HandValue[0] = Deck_value[deckvalue];
Deck[deckvalue] = null;
Deck_value[deckvalue] = 0;
deckvalue++;
Dealer_Hand[1] = Deck[deckvalue];
Dealer_HandValue[1] = Deck_value[deckvalue];
Deck[deckvalue] = null;
Deck_value[deckvalue] = 0;
deckvalue++;
Console.WriteLine("Dealers Hand: " + Dealer_Hand[0] + " (" + Dealer_HandValue[0] + ") , ? (?) Total: " + Dealer_HandValue[0]);
Console.WriteLine();
Console.WriteLine("Your Cards: " + Player_Hand[0] + " (" + Player_HandValue[0] + ") , " + Player_Hand[1] + " (" + Player_HandValue[1] + ") Total:" + (Player_HandValue[0] + Player_HandValue[1]));
}
static void PlayersTurn()
{
int playervalue = 2;
playerhandvalue = 0;
bool round = true;
do
{
Console.WriteLine();
Console.WriteLine("Hit or Stand?");
string answer = Console.ReadLine().ToLower();
switch (answer)
{
case "hit":
Player_Hand[playervalue] = Deck[deckvalue];
Player_HandValue[playervalue] = Deck_value[deckvalue];
Deck[deckvalue] = null;
Deck_value[deckvalue] = 0;
deckvalue++;
playervalue++;
playerhandvalue = 0;
Console.Clear();
Console.WriteLine("Dealers Hand: " + Dealer_Hand[0] + " (" + Dealer_HandValue[0] + ") , ? (?) Total: " + Dealer_HandValue[0]);
Console.WriteLine();
Console.WriteLine("Your Hand");
for (int i = 0; i < Player_Hand.Length && Player_Hand[i] != null; i++)
{
Console.Write(Player_Hand[i] + " ");
Console.Write("(" + Player_HandValue[i] + ") , ");
}
for (int i = 0; i < Player_HandValue.Length; i++)
{
playerhandvalue += Player_HandValue[i];
}
if (playerhandvalue > 21)
{
Console.WriteLine("Total:" + playerhandvalue + " BUST!");
round = false;
}
else if (playerhandvalue == 21)
{
Console.WriteLine("Total:" + playerhandvalue + " BLACKJACK!");
round = false;
}
else
{
Console.Write("Total: " + playerhandvalue);
}
break;
case "stand":
Console.Clear();
Console.WriteLine("Dealers Hand: " + Dealer_Hand[0] + " (" + Dealer_HandValue[0] + ") , ? (?) Total: " + Dealer_HandValue[0]);
Console.WriteLine();
Console.WriteLine("Your hand: ");
playerhandvalue = 0;
for (int i = 0; i < Player_Hand.Length; i++)
{
playerhandvalue += Player_HandValue[i];
}
for (int i = 0; i < Player_Hand.Length && Player_Hand[i] != null; i++)
{
Console.Write(Player_Hand[i] + " ");
Console.Write("(" + Player_HandValue[i] + ") , ");
}
if (playerhandvalue > 21)
{
Console.WriteLine("Total:" + playerhandvalue + " BUST!");
round = false;
}
else if (playerhandvalue == 21)
{
Console.WriteLine("Total:" + playerhandvalue + "BLACKJACK!");
}
else
{
Console.Write("Total:" + playerhandvalue);
}
Console.WriteLine();
round = false;
break;
}
} while (round == true);
}
static void DealersTurn()
{
int daelervalue = 2;
bool round = true;
string result = null;
dealerhandvalue = 0;
for (int i = 0; i < Dealer_HandValue.Length; i++)
{
dealerhandvalue += Dealer_HandValue[i];
}
do
{
if (dealerhandvalue < playerhandvalue && playerhandvalue <= 21)
{
result = "hit";
}
else if (dealerhandvalue == playerhandvalue && dealerhandvalue < 16)
{
result = "hit";
}
else if (dealerhandvalue > 21)
{
result = "stand";
}
else if (playerhandvalue > 21)
{
result = "stand";
}
else if (dealerhandvalue > playerhandvalue)
{
result = "stand";
}
switch (result)
{
case "hit":
Dealer_Hand[daelervalue] = Deck[deckvalue];
Dealer_HandValue[daelervalue] = Deck_value[deckvalue];
Deck[deckvalue] = null;
Deck_value[deckvalue] = 0;
daelervalue++;
deckvalue++;
dealerhandvalue = 0;
for (int i = 0; i < Dealer_HandValue.Length && Dealer_HandValue[i] != 0; i++)
{
dealerhandvalue += Dealer_HandValue[i];
}
break;
case "stand":
Task.Delay(2000);
dealerhandvalue = 0;
for (int i = 0; i < Dealer_HandValue.Length; i++)
{
dealerhandvalue += Dealer_HandValue[i];
}
Console.Clear();
Console.WriteLine("Your Hand");
for (int i = 0; i < Player_Hand.Length && Player_Hand[i] != null; i++)
{
Console.Write(Player_Hand[i] + " ");
Console.Write("(" + Player_HandValue[i] + ") , ");
}
Console.Write(" Toatl " + playerhandvalue);
Console.WriteLine();
Console.Write("Dealers Hand: ");
for (int i = 0; i < Dealer_Hand.Length && Dealer_Hand[i] != null; i++)
{
Console.Write(Dealer_Hand[i] + " ");
Console.Write("(" + Dealer_HandValue[i] + ") , ");
}
if (dealerhandvalue > 21)
{
Console.WriteLine("Total:" + dealerhandvalue + " BUST!");
}
else if (dealerhandvalue == 21)
{
Console.WriteLine("Total:" + dealerhandvalue + " BLACKJACK!");
}
else
{
Console.WriteLine("Total:" + dealerhandvalue);
}
round = false;
break;
}
} while (round == true);
}
static void ClearAllArrays()
{
Array.Clear(Player_Hand, 0, Player_Hand.Length);
Array.Clear(Dealer_Hand, 0, Dealer_Hand.Length);
Array.Clear(Dealer_HandValue, 0, Dealer_HandValue.Length);
}
}
}
当我将阵列Deck和Deck_Value的大小增加到37以上时,问题出现在Shuffle Cards中。此外,检查没有多次卡被选中,控制台似乎崩溃并且没有继续执行代码。 希望对此有所帮助,谢谢。