2012-11-19 20 views
0

我參與了我創建的Blackjack遊戲,並且我希望它是面向對象的。將項目轉化爲面向對象

我第一次編碼它時沒有考慮到這一點,但我希望它被分成不同的類。 我相信我需要將這些方法放在不同的類中,並適當地命名它們。 問題是,我該如何做到這一點?

我認爲這會像在每個方法前面放置「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); 
     } 

    } 
} 
} 
+0

你可以開始製作正確的卡片。你可以有一個'Card'類,它有'Suit'和'Value'這樣的'Properties'。然後,你可以有一個「Deck」類,它擁有所有的卡片,並提供'Shuffle'等'方法'來隨機化卡片。想想任何紙牌遊戲中涉及的對象,並考慮這些對象具有的屬性以及他們應該能夠做什麼或者您可能想要做什麼。 – Pete

回答

1

術語「面向對象」的概念方法是從我的角度來看待一個代碼問題,儘可能接近現實。我的意思是,如果你需要創建一個汽車的行爲,那麼你需要在類中封裝與這個「對象」相關的所有變量和函數,以便當你實例化它時,你實際上會在計算機內創建一個object「汽車」,它會以與真實物體相同的方式作出反應。

類之間的層次結構來自相同的概念。我們知道汽車是一輛汽車,但是你知道你不能製造汽車,因爲它是一個抽象的概念。所有車輛通用的變量和功能應位於abstract班級內,其他所有車輛/自行車/卡車均來自該班級。

在紙牌遊戲的情況下,每張卡片可以成爲一個對象,這將包含對他們的視覺的引用。所以,你會得到一個List<Card>允許你輕鬆地選擇,刪除或添加一個。該表也可能是一個對象,其中包含玩家的位置/ ID以及分配時牌的位置。

但是直接的「對象」方法並不總是可行的,因爲在現實生活中,不存在包含規則和概述遊戲步驟的「遊戲」對象。但是一旦你理解了OO編程的概念,你會發現它基本上是如何以及在哪裏放置變量和函數,以及多態性和乾淨的結構如何更容易地讀取和維護代碼。 OO編程並不是一個容易理解的術語,當人們開始將哲學與它混合在一起時,它會變得越來越難,沒有理由。我實際上給出了一個4小時的課,作爲對這個概念的介紹,所以不要指望在幾分鐘內抓住它。起初,試着把他們聽起來最合乎邏輯的地方放在那裏。

0

以下是基本步驟;

1)右鍵單擊項目/解決方案並選擇添加 - >類 2)爲該遊戲命名類似Game。 3)將Deal()等方法移入此類,並將它們聲明爲Public Deal()(或者在適用時爲私有方式,顯然它不適用於Deal)。 4)在你的主要創建一個遊戲實例Game game = new Game(); 5)調用方法與點運算符,即game.Deal();來處理遊戲。