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と、配布されたときにカードがどこに行くかが含まれています。

しかし、実際の生活では、ルールとゲームのステップの概要を含む「ゲーム」オブジェクトのようなものは存在しないため、直接的な「オブジェクト」アプローチが常に可能であるとは限りません。しかし、オブジェクト指向プログラミングの概念を理解すると、基本的には変数と関数をどこにどのように配置するか、そしてポリモーフィズムとクリーンな構造によってコードの読み取りと保守がはるかに簡単になることがわかります。

オブジェクト指向プログラミングは、理解するのがそれほど簡単な用語ではありません。人々が哲学をそれと混ぜ始めると、理由もなく難しくなります。私は実際にこの概念の紹介として4時間のクラスを提供しているので、すべてを数分で理解することを期待しないでください。最初は、最も論理的な場所にあると思われる場所に配置してみてください。

于 2012-11-19T01:08:38.663 に答える
0

基本的な手順は次のとおりです。

1)プロジェクト/ソリューションを右クリックし、[追加]-> [クラス]を選択します。2)Gameゲームのようにクラスに適切な名前を付けます。3)のようなメソッドDeal()をこのクラスに移動し、それらをPublic Deal()(または、適切な場合はプライベート、明らかにDeal用ではない)として宣言します。4)メインで5)でゲームインスタンスを作成します。Game game = new Game(); ドット演算子を使用してメソッドを呼び出します。つまりgame.Deal();、ゲームを処理します。

于 2012-11-19T00:54:49.670 に答える