控制台二十一点c#,添加总数

本文关键字:添加 二十一 十一点 控制台 | 更新日期: 2023-09-27 18:13:31

我们的编程入门教授希望我们使用c#中的数组创建一个21点游戏。我们在将"命中卡"的值添加到初始总数时遇到了麻烦。当我们尝试多次命中时,它所做的只是替换第一张"命中卡",并只将新卡添加到初始总数中。

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();







}
}
}

控制台二十一点c#,添加总数

看起来你从来没有更新你的初始总变量: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);
    ...
}

我还强烈建议您更好地重命名变量,以提高代码的可读性。将变量命名为Total1Total2等并不是一个好的做法,而且会使调试问题变得更加困难,比如您现在遇到的问题。让你的变量名实际描述它们的用途,例如,将Total3改为runningTotal