Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将项目转化为面向对象_C#_Oop - Fatal编程技术网

C# 将项目转化为面向对象

C# 将项目转化为面向对象,c#,oop,C#,Oop,我制作了一个21点游戏,我希望它是面向对象的 我第一次编写代码时没有考虑到这一点,但我希望它被划分为不同的类。 我认为我需要将这些方法放在不同的类中,并适当地命名它们。 问题是我如何做到这一点 我原以为这就像在每个方法前面加上“public”一样简单,但它不起作用 我该怎么做 如果您需要我的源代码,请参阅: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac

我制作了一个21点游戏,我希望它是面向对象的

我第一次编写代码时没有考虑到这一点,但我希望它被划分为不同的类。 我认为我需要将这些方法放在不同的类中,并适当地命名它们。 问题是我如何做到这一点

我原以为这就像在每个方法前面加上“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);
        }

    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间21点
{
/// 
///“玩家卡”将储存玩家的卡。玩家最多可以持有11张卡。
///“hitOrStand”询问玩家是想击球还是站位。
/// 
///“得分”卡计算球员在该轮的得分。
///“cardCount”增加,玩家可以在手中持有多张牌。
///“scoreDealer”卡统计经销商在该轮的得分。
/// 
///“新卡”用于随机化玩家的卡。
/// 
班级计划
{
静态随机新卡=新随机();
静态积分=0,信用卡计数=1,积分经销商=0;
静态字符串[]playerCards=新字符串[11];
静态字符串hitOrStand=“”;
/// 
///主要方法
/// 
///Main方法通过执行“Start()”开始游戏
/// 
静态void Main(字符串[]参数)
{
Start();
}
/// 
///启动方法
/// 
///开始方法让玩家知道他们正在玩21点。
///然后它给玩家发两张牌(因为在21点游戏中,你从两张牌开始)。
///它告诉玩家当时他们的分数是多少。
///最后,它通过Game()方法(位于下面)询问玩家是想打(继续并获得一张新卡)还是站(停止)。
/// 
静态void Start()
{
scoredaler=newCard.Next(15,22);
playerCards[0]=Deal();
playerCards[1]=交易();
做
{
Console.WriteLine(“欢迎!经销商为您提供“+playerCards[0]+”和“+playerCards[1]+”\n存储:“+score+”\n您想击球还是站立?”);
hitOrStand=Console.ReadLine().ToLower();
}而(!hitOrStand.Equals(“hit”)和&!hitOrStand.Equals(“stand”);
游戏();
}
/// 
///博弈方法
/// 
///游戏方法检查玩家是否击中或站立。
///如果玩家确实命中,则执行该方法(命中方法)。
///但如果玩家确实站着,它会检查他们的分数是否大于庄家的分数,以及分数是否等于/小于21。
///如果上面的陈述是真的,它会祝贺并公布庄家的分数。然后它会询问玩家是否想再次比赛。
/// 
///但是,如果上面的说法是错误的,它会告诉玩家他们输了,显示庄家的分数,并询问玩家是否想再次比赛。
/// 
静态无效游戏()
{
如果(hitOrStand.Equals(“hit”))
{
命中();
}
否则如果(hitOrStand.Equals(“stand”))
{

如果(score>scoredaler&&score,以下是基本步骤

1) 右键单击项目/解决方案并选择Add->Class 2) 为游戏适当地命名类,如
Game
。 3) 将像
Deal()
这样的方法移动到此类中,并将它们声明为
Public Deal()
(或者在适当的时候声明为private,显然它不适用于Deal)。 4) 在主界面中,使用
game game=new game();

5) 使用点运算符调用方法,即
game.Deal();
来处理游戏。

术语“面向对象”的概念方法是,在我看来,以尽可能接近现实的观点来处理代码问题

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

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

在纸牌游戏中,每张牌都可以成为一个对象,其中包含对其视觉效果的引用。因此,您将获得一个
列表
,允许您轻松拾取、移除或添加一张。该表也可以是一个对象,其中包含玩家的位置/id以及分发时牌的去向

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


OO编程并不是一个很容易理解的术语,当人们开始将哲学与它混合在一起时,它会毫无理由地变得更难。实际上,我会给大家上4小时的课,作为对这个概念的介绍,所以不要期望在几分钟内就把它全部讲清楚。首先,试着把东西放在听起来最符合逻辑的地方。

你可以从制作合适的卡片开始。你可以有一个
Card
类,该类具有
属性,如
Suit
值。然后你可以有一个
Deck
类,该类保存所有的卡片,并提供
方法,如
Shuffle
,将卡片随机化。想想一个游戏中涉及的对象纽约纸牌游戏