2013-06-27 2 views
0

블랙 잭 게임 인 콘솔 앱을 빌드하려고합니다. BlackJackTable : TableGame 및 6 BettingSpots 있습니다. 나는 BettingSpots를 포함하는 배열을 원한다고 생각하고 있습니다. 하지만 BettingSpot []을 채우려는 중에 오류가 발생했습니다. 더 나은 디자인을 진행하는 방법에 대한 조언은 많은 도움이 될 것입니다.BlackJackTable 설정 시도, OO 디자인 설정 문제

public abstract class TableGame 
{ 
    // Can have 5-7 bettings spots. Has a dealer, players, Rules 
    public abstract void Rules(); 
    public abstract BettingSpot[] bettingSpotArray; 
    public Dealer dealer = new Dealer(); 

} 

public class BlackJackTable : TableGame 
{ 

    // A blackjack tablegame "has a" 6 BettingSpots available that a Player chooses to occupy. Have the Game ask the player which bettingspots they'd 
    // like to place a bet on. Bet amount must be the same for each. Use a try catch block to make sure TotalChips >= all bets. 
    public BlackJackTable(int tableNumber) 
    { 
     _tableNumber = tableNumber; 
    } 

    public override void BlackJackRules() 
    { } 

     BettingSpot spot1 = new BettingSpot(1); 
     BettingSpot spot2 = new BettingSpot(2); 
     BettingSpot spot3 = new BettingSpot(3); 
     BettingSpot spot4 = new BettingSpot(4); 
     BettingSpot spot5 = new BettingSpot(5); 
     BettingSpot spot6 = new BettingSpot(6); 

    public override BettingSpot[] bettingSpotArray = new BettingSpot[5]; 

    for (int i = 0; i < bettingSpotArray.Length; i++) 
    { 
     bettingSpotArray[i] = new BettingSpot[i+1]; 
    } 

    public void QueryPlayerForBettingSpots(BettingSpot[] bettingSpotArray) 
    { 
     int[] BettingSpotsAvailable = new BettingSpot[5]; 
     for (int idx = 0; idx < 5; idx++) 
      if (bettingSpotArray[idx] == 0) 
       BettingSpotsAvailable[idx] 

     Console.WriteLine("Place up to 3 bets on the following available BettingSpots: {0}", bettingSpotArray.Where<BettingSpot. 
    } 

} 

public class BettingSpot 
{ 
    protected decimal bet = 0; 
    public int _bettingSpotNumber; 
    // How many spots are on the Blackjack table will determine how many seats there will be. There are six betting spots allowed, 
    // so six bettingspots are created. THere are just 6 BettingSpots available and a player can 
    // occupy up to 3 BettingSpots at a time. A bettingspot "has a" bet. If no bet, put '0' in BettingSpotArray 

    public BettingSpot(int number) 
    { 
     _bettingSpotNumber = number; 
    } 

    public Player player 



    public decimal Bet 
    { 
     get 
     { 
      return bet; 
     } 
     set 
     { 
      bet = value; 

    } 

} 
+0

문제가 무엇인지 알 것 같지만 문제를 확인하기 위해 어떤 오류가 발생합니까? – Tim

답변

0

내가 당신의 코드에서 발견 한 몇 가지.

첫 번째로, 나는 당신이 클래스 본문 안에서 그것을하려고하기 때문에 bettingSpotArray[]을 채우는 데 어려움을 겪고 있다고 생각합니다. 메서드 나 속성의 본문에서이 작업을 수행해야합니다. 생성자 또는 Rules() 메서드를 제안합니다.

둘째, 추상 클래스에 코드를 정의하지 않으므로 추상 클래스를 인터페이스로 만들고 인터페이스 클래스를 게임 클래스에 구현하는 것이 더 쉬울 수 있습니다. 즉, 클래스는 자신이 갖고있는 것을 알 수 있습니다. 구현하지만 모든 것을 재정의 할 필요는 없습니다. 게임 클래스의 대다수에 대해 원하는 일종의 기본 로직이 있다면 그 기본 로직을 가진 추상 클래스를 사용하는 것에 동의하고 "특수"클래스는 필요에 따라이를 오버라이드합니다. 그러나 그것은 그것이 나의 그것에 관한 것이다. 귀하는 귀하의 질문에서 쉽게 볼 수없는 추상적 인 수업을 사용하기위한 귀하의 유효한 이유가있을 수 있습니다.

게시 한 코드가 실제로 컴파일 될지 잘 모르겠지만 이동하려는 위치로 이동할 수있는 코드 샘플을 제공 할 것입니다.

public class BlackJackTable : TableGame 
{ 

    // If other classes need access to this I'd set it up as public 
    // property, not a public field. If not, I'd set the field to 
    // private 
    public override BettingSpot[] bettingSpotArray = new BettingSpot[5]; 

    public BlackJackTable(int tableNumber) 
    { 

     // I don't see a _tableNumber field in your abstract class or your 
     // inheriting class - if you don't have that field you'll get an error 
     // in the compiler 
     _tableNumber = tableNumber; 
    } 

    // Your posted code had public override void BlackJackRules, but there is 
    // no BlackJackRules() method to override 
    public override void Rules() 
    { 

     // These are superfulous since you'll be using an array 
     // for the betting spots 
     //BettingSpot spot1 = new BettingSpot(1); 
     //BettingSpot spot2 = new BettingSpot(2); 
     //BettingSpot spot3 = new BettingSpot(3); 
     //BettingSpot spot4 = new BettingSpot(4); 
     //BettingSpot spot5 = new BettingSpot(5); 
     //BettingSpot spot6 = new BettingSpot(6); 

     // Now you can initialize your array 
     for (int i = 0; i < bettingSpotArray.Length; i++) 
     { 
      bettingSpotArray[i] = new BettingSpot[i+1]; 
     } 
    } 
} 

귀하의 QueryPlayerForBettingSpots는 사용자로부터 입력하라는 메시지를 표시하지만, 모든 입력을 허용하지 않는 한, 중 많은 이해가되지 않습니다. 아마도 그 방법을 끝내지 않았을 것입니까?

귀하의 전반적인 접근 방식은 올바른 접근 방식이지만 구현에 몇 가지 문제가 있다고 생각합니다 (위에서 언급 한 내용). 다행히도 프로젝트에서 앞으로 나아갈 때 도움이 될 것으로 기대합니다.