我们的编码教授简介希望我们使用c#中的数组创建一个二十一点游戏。我们无法将“点击卡”的值添加到我们的初始总计中。当我们尝试多次击中时,它所做的就是替换第一张“击中的牌”,并且只将新牌加到初始牌中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Blackjack_Midterm
{
class Program
{
static void Main(string[] args)
{
//set up multidementional array for card values
string[,] cards = new string[52, 3]
{
{"Hearts", "Ace", "1"},
{"Diamonds", "Ace", "1"},
{"Clubs", "Ace", "1"},
{"Spades", "Ace", "1"},
{"Hearts", "Two", "2"},
{"Diamonds", "Two", "2"},
{"Clubs", "Two", "2"},
{"Spades", "Two", "2"},
{"Hearts", "Three", "3"},
{"Diamonds", "Three", "3"},
{"Clubs", "Three", "3"},
{"Spades", "Three", "3"},
{"Hearts", "Four", "4"},
{"Diamonds", "Four", "4"},
{"Clubs", "Four", "4"},
{"Spades", "Four", "4"},
{"Hearts", "Five", "5"},
{"Diamonds", "Five", "5"},
{"Clubs", "Five", "5"},
{"Spades", "Five", "5"},
{"Hearts", "Six", "6"},
{"Diamonds", "Six", "6"},
{"Clubs", "Six", "6"},
{"Spades", "Six", "6"},
{"Hearts", "Seven", "7"},
{"Diamonds", "Seven", "7"},
{"Clubs", "Seven", "7"},
{"Spades", "Seven", "7"},
{"Hearts", "Eight", "8"},
{"Diamonds", "Eight", "8"},
{"Clubs", "Eight", "8"},
{"Spades", "Eight", "8"},
{"Hearts", "Nine", "9"},
{"Diamonds", "Nine", "9"},
{"Clubs", "Nine", "9"},
{"Spades", "Nine", "9"},
{"Hearts", "Ten", "10"},
{"Diamonds", "Ten", "10"},
{"Clubs", "Ten", "10"},
{"Spades", "Ten", "10"},
{"Hearts", "Jack", "10"},
{"Diamonds", "Jack", "10"},
{"Clubs", "Jack", "10"},
{"Spades", "Jack", "10"},
{"Hearts", "Queen", "10"},
{"Diamonds", "Queen", "10"},
{"Clubs", "Queen", "10"},
{"Spades", "Queen", "10"},
{"Hearts", "King", "10"},
{"Diamonds", "King", "10"},
{"Clubs", "King", "10"},
{"Spades", "King", "10"},
};
//Title and Rules of game
Console.WriteLine("BlackJack with FlapJacks \n");
Console.WriteLine("Rules: \n");
Console.WriteLine(" - You are given two cards");
Console.WriteLine(" - The Dealer is given two cards (One Face-up, one Face-down)");
Console.WriteLine(" - The object of the game is to get closest to 21 without going over");
Console.WriteLine(" - If you go over 21, it's game over");
Console.WriteLine(" - If you receive 21 on the first two cards, automatic win (applies to both the dealer and player) \n");
Console.WriteLine("Press 'ENTER' to begin \n");
Console.ReadKey();
Boolean Broke = false;
int Chips;
Chips = 100;
while (Chips > 0)
{
Console.WriteLine("Remaining Chips: {0}", Chips);
Console.Write("Place your bet(1-{0}): ", Chips);
string bet = Console.ReadLine();
int Bet = Convert.ToInt16(bet);
Boolean Stand = false;
Console.WriteLine("\n");
//Deal Cards out to player and Dealer
Console.WriteLine("Dealing Cards \n");
Thread.Sleep(2000);
Console.WriteLine("(Player's Hand) \n");
//Set up a RNG to pick from the created arrays
//Set up RNG for each card
Random rnd = new Random();
int card1 = rnd.Next(52);
int card2 = rnd.Next(52);
int card3 = rnd.Next(52);
int card4 = rnd.Next(52);
Console.WriteLine(cards[card1, 1] + " of " + cards[card1, 0]);
card2 = rnd.Next(52);
Console.WriteLine(cards[card2, 1] + " of " + cards[card2, 0]);
int Value1 = Convert.ToInt16(cards[card1, 2]);
int Value2 = Convert.ToInt16(cards[card2, 2]);
int Total1 = Value1 + Value2;
Console.WriteLine("Total: " + Total1);
Console.WriteLine("\n");
Console.WriteLine("(Dealer's Hand) \n");
//Show the first card
card3 = rnd.Next(52);
Console.WriteLine(cards[card3, 1] + " of " + cards[card3, 0]);
//Second Card place holder
card4 = rnd.Next(52);
Console.WriteLine("[Hidden Card]");
//Convert the values to integers
//Add and show the total of the two cards
int Value3 = Convert.ToInt16(cards[card3, 2]);
int Value4 = Convert.ToInt16(cards[card4, 2]);
int Total2 = Value3;
Console.WriteLine("Total: " + Total2);
Console.WriteLine("\n");
while (Stand == false)
{
//Ask if Player wants to Hit or Stand
Console.Write("Hit(1) or Stand(2)?: ");
//Set up user input (string)
string GuessAsAString = Console.ReadLine();
//Convert String to an Int
int Answer = Convert.ToInt16(GuessAsAString);
Console.WriteLine("\n");
if (Answer == 1)
{
Stand = false;
//Continually add on to previous player total until "stand" or "bust"
card2 = rnd.Next(52);
Console.WriteLine(cards[card2, 1] + " of " + cards[card2, 0]);
int Value5 = Convert.ToInt16(cards [card2, 2]);
int Total3 = Total1 + Value5;
Console.WriteLine("Total: " + Total3);
Console.WriteLine("\n");
}
else if (Answer == 2)
{
Stand = true;
}
}
//Add cards to Dealer's hand until over 17 or bust
Console.WriteLine("Dealing Dealer's hand");
Thread.Sleep(1000);
Console.WriteLine(cards[card4, 1] + " of " + cards[card4, 0]);
}
Console.ReadKey();
}
}
}
答案 0 :(得分:1)
您似乎永远不会更新您的初始总变量:Total1
作为while (Stand == false)
循环的一部分。相反,您完全按照您的描述进行操作:仅将新卡值添加到初始值。
每次stand循环遍历时,您需要做的是不重新声明Total3
变量。将其声明移到循环之外。例如:
int Total3 = Total1; // Intialise the running total with the inital value from the deal
while (Stand == false)
{
...
Total3 = Total3 + Value5; // Add value of the newly dealt card to the running total.
Console.WriteLine("Total: " + Total3);
...
}
我还强烈建议您更好地重命名变量,以提高代码的可读性。命名变量Total1
,Total2
等并不是一种好的做法,这使得调试诸如您现在遇到的问题变得更加困难。让您的变量名称实际描述它们的用途,例如:而不是Total3
将其命名为runningTotal
。