0

我在自己创建的二十一点游戏上工作,我希望它是面向对象的。

我第一次编码它时没有考虑到这一点,但我希望它被分成不同的类。我相信我需要将方法放在各种类中并适当地命名它们。问题是我该怎么做?

我认为这就像在每个方法前面都加上“public”一样简单,但它没有奏效。

我该怎么做呢?

如果你想要我的源代码,这里是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackJack
{

/// <summary>
/// 'playerCards' will store the player's cards. The maximum of cards a player can hold is 11.
/// 'hitOrStand' asks the player if they want to hit or stand.
/// 
/// 'score' cardCounts the player's score for that round.
/// 'cardCount' increases so that the player can hold multiple cards in their hand.
/// 'scoreDealer' cardCounts the dealer's score for that round.
/// 
/// 'newCard' works to randomize the player's cards.
/// </summary>

class Program
{
    static Random newCard = new Random();
    static int score = 0, cardCount = 1, scoreDealer = 0;
    static string[] playerCards = new string[11];
    static string hitOrStand = "";

    /// <summary>
    /// MAIN METHOD
    /// 
    /// The Main method starts the game by executing 'Start()'
    /// </summary>

    static void Main(string[] args)
    {
        Start();
    }

    /// <summary>
    /// START METHOD
    /// 
    /// The Start method lets the player know that they are playing Blackjack.
    /// Then it deals the player two cards (because in Blackjack, you start with two cards).
    /// It tells the player what their score score is at that moment.
    /// Finally, it asks the player if they want to hit (continue and get a new card), or stand (stop) through the Game() method (which lies below this).
    /// </summary>

    static void Start()
    {
        scoreDealer = newCard.Next(15, 22);
        playerCards[0] = Deal();
        playerCards[1] = Deal();
        do
        {
            Console.WriteLine("Welcome! The dealer gives you " + playerCards[0] + " and " + playerCards[1] + ". \nScore: " + score + ".\nWould you like to hit or stand?");
            hitOrStand = Console.ReadLine().ToLower();
        } while (!hitOrStand.Equals("hit") && !hitOrStand.Equals("stand"));
        Game();
    }

    /// <summary>
    /// GAME METHOD
    /// 
    /// The Game method checks if the player did hit or if the player did stand.
    /// If the player did hit, that method (the Hit method) is executed.
    /// But if the player did stand, it checks if their score is larger than the dealer's AND if the score equals/less than 21.
    /// If the above statement is TRUE, it will congratulate, and reveal the dealer's score. Then it'll ask if the player wants to play again.
    /// 
    /// However, if the above is FALSE, it will tell the player that they lost, reveal the dealer's score, and ask if the player wants to play again.
    /// </summary>

    static void Game()
    {
        if (hitOrStand.Equals("hit"))
        {
            Hit();
        }
        else if (hitOrStand.Equals("stand"))
        {
            if (score > scoreDealer && score <= 21)
            {
                Console.WriteLine("\nCongratulations! You won! The dealer's score was " + scoreDealer + ".\nWould you like to play again? y/n");
                PlayAgain();
            }
            else if (score < scoreDealer)
            {
                Console.WriteLine("\nYou lost! The dealer's score was " + scoreDealer + ".\nWould you like to play again? y/n");
                PlayAgain();
            }

        }
        Console.ReadLine();
    }

    /// <summary>
    /// DEAL METHOD
    /// 
    /// The Deal method creates a random number between 1 and 14.
    /// Then that int switches and assigns a value to Card.
    /// Depending on its result, it will add to the amount on score.
    /// Lastly, it'll return the string Card.
    /// 
    /// Below, all the cards that are available can be viewed.
    /// </summary>

    static string Deal()
    {
        string Card = "";
        int cards = newCard.Next(1, 14);
        switch (cards)
        {
            case 1: Card = "2"; score += 2;
                break;
            case 2: Card = "3"; score += 3;
                break;
            case 3: Card = "4"; score += 4;
                break;
            case 4: Card = "5"; score += 5;
                break;
            case 5: Card = "6"; score += 6;
                break;
            case 6: Card = "7"; score += 7;
                break;
            case 7: Card = "8"; score += 8;
                break;
            case 8: Card = "9"; score += 9;
                break;
            case 9: Card = "10"; score += 10;
                break;
            case 10: Card = "Jack"; score += 10;
                break;
            case 11: Card = "Queen"; score += 10;
                break;
            case 12: Card = "King"; score += 10;
                break;
            case 13: Card = "Ace"; score += 11;
                break;
            default: Card = "2"; score += 2;
                break;
        }
        return Card;
    }

    /// <summary>
    /// HIT METHOD
    /// 
    /// The Hit method adds another card to the player's hand, as they demanded (when they decided to hit instead of stand).
    /// Then it checks if the player still holds an amount less than 21, got Blackjack (21), or Busted (received an amount over 21).
    /// 
    /// If the amount is less than 21, the player may continue, and they will be asked if they'd like to hit or stand.
    /// </summary>

    static void Hit()
    {
        cardCount += 1;
        playerCards[cardCount] = Deal();
        Console.WriteLine("\nYou were dealed a " + playerCards[cardCount] + ".\nYour new score is " + score + ".");
        if (score.Equals(21))
        {
            Console.WriteLine("\nBlackjack! Congratulations! The dealer's score was " + scoreDealer + ".\nWould you like to play again? y/n");
            PlayAgain();
        }
        else if (score > 21)
        {
            Console.WriteLine("\nYou busted, and lost. The dealer's score was " + scoreDealer + ".\nWould you like to play again? y/n");
            PlayAgain();
        }
        else if (score < 21)
        {
            do
            {
                Console.WriteLine("\nWould you like to hit or stand?");
                hitOrStand = Console.ReadLine().ToLower();
            } while (!hitOrStand.Equals("hit") && !hitOrStand.Equals("stand"));
            Game();
        }
    }

    /// <summary>
    /// PLAYAGAIN METHOD
    /// 
    /// This method simply asks the player if they want to play again, or not.
    /// If the player replies with a 'y', it will tell the player to press enter in order to restart the game.
    /// 
    /// If the player replies with an 'n', it will tell the player to press enter in order to close the game.
    /// </summary>

    static void PlayAgain()
    {
        string playAgain = "";
        do
        {
            playAgain = Console.ReadLine().ToLower();
        } while (!playAgain.Equals("y") && !playAgain.Equals("n"));
        if (playAgain.Equals("y"))
        {
            Console.WriteLine("\nPress Enter to play again.");
            Console.ReadLine();
            Console.Clear();
            scoreDealer = 0;
            cardCount = 1;
            score = 0;
            Start();
        }
        else if (playAgain.Equals("n"))
        {
            Console.WriteLine("\nThank you for playing. \nPress Enter to close the game.");
            Console.ReadLine();
            Environment.Exit(0);
        }

    }
}
}
4

2 回答 2

1

从我的角度来看,“面向对象”一词的概念方法是用尽可能接近现实的观点来解决代码问题。

我的意思是,如果您必须创建汽车的行为,您将在一个类中封装与该“对象”相关的所有变量和函数,这样当您实例化它时,您实际上会创建计算机内部的object“汽车”,其反应方式与真实物体的反应方式相同。

类之间的层次结构来自相同的概念。我们知道汽车是交通工具,但你知道你不能创造交通工具,因为它是一个抽象的概念。所有车辆通用的变量和函数都应该在abstract该类中,其他所有汽车/自行车/卡车都从该类派生。

在纸牌游戏的情况下,每张纸牌都可以成为一个对象,其中包含对其视觉效果的引用。因此,您将获得一个List<Card>允许您轻松选择、删除或添加一个。桌子也可以是一个对象,它包含玩家的位置/id 以及卡片分发时的去向。

但是直接的“对象”方法并不总是可行的,因为在现实生活中,没有包含规则和概述游戏步骤的“游戏”对象之类的东西。但是,一旦您理解了 OO 编程的概念,您就会发现它基本上就是如何以及在何处放置变量和函数,以及多态性和干净的结构如何使代码更易于阅读和维护。

OO 编程不是一个容易理解的术语,当人们开始将哲学与它混在一起时,它就会无缘无故地变得更难。实际上,我给了一个 4 小时的课程作为对这个概念的介绍,所以不要指望在几分钟内掌握所有内容。一开始,试着把这些东西放在听起来最合乎逻辑的地方。

于 2012-11-19T01:08:38.663 回答
0

以下是基本步骤;

1) 右键单击​​项目/解决方案并选择 Add->Class 2)Game为游戏命名适当的类。3)将方法Deal()移到此类中并将它们声明为Public Deal()(或在适当时为私有,显然它不适用于Deal)。4)在你的主创建一个游戏实例Game game = new Game(); 5)用点运算符调用方法,即game.Deal();处理游戏。

于 2012-11-19T00:54:49.670 回答