2017-05-02 2 views
0

나는 주사위를 던져 주사위를 던져 점수를 얻는다. 현재 25 점에 도달하면이 플레이어가 승자라는 메시지가 나타납니다. 그러나 프로그램은 최대 50 자까지 올라갈 수 있습니다. 점수 한도에 도달하면 게임을 끝내고 싶지만 실제로는 잘 모르겠습니다. 그렇게하는 방법. 나는 do-while 루프가 필요하다고 생각하지만, 전체 포인트 제한이 다른 클래스에 있기 때문에 추가하는 방법을 모르겠습니다.주사위를위한 마무리 루프?

class Game 
{ 
    private static void Main(string[] args) ----- Main method: 
    { 
     if (gamemode == 1) 
     { 
       quickgame(); 
     } 

} 

    private static void quickgame() ---- Game carried out 
    { 
     Console.WriteLine("\nInstructions: Players take turns rolling all five dice and scoring for three-of-a-kind or better. \n\t  If a player only has two-of-a-kind, they may re-throw the remaining dice in an \n\t  attempt to improve the matching dice values. If no matching numbers are\n\t  rolled, a player scores 0. The first player to reach 25 points wins. "); 
     Console.WriteLine("\nScoring: 3-Of-A-Kind = 3 Points \n   4-Of-A-Kind = 6 Points \n   5-Of-A-Kind = 12 Points\n"); 

     Random RandomNum = new Random(); 
     Player[] player1 = new Player[5]; 
     Die[] myDie = new Die[5]; 


     for (int i = 0; i < 5; i++) 
     { 
      myDie[i] = new Dice_v4.Die(RandomNum); 
      player1[i] = new Dice_v4.Player(); 
     } 

     for (int i = 0; i < 2; i++) // Number of players 
     { 
      Console.Write("Enter Name for Player {0}:", i + 1); 
      string NewName = Console.ReadLine(); 
      player1[i].SetName(NewName); 
     } 

     Console.WriteLine("\nPress enter in turns to roll the five dice"); 
     Console.ReadLine(); 
     Console.WriteLine(); 


     for (int j = 1; j < 50; j++) 
     { 
      for (int i = 0; i < 2; i++) 
      { 

       myDie[i].roll(); 
       Console.WriteLine("{0} Rolled:{1} on the first dice", player1[i].GetName(), myDie[i].GetTopNumber()); 
       Console.WriteLine("{0} Rolled:{1} on the second dice", player1[i].GetName(), myDie[i].GetTopNumber1()); 
       Console.WriteLine("{0} Rolled:{1} on the third dice", player1[i].GetName(), myDie[i].GetTopNumber2()); 
       Console.WriteLine("{0} Rolled:{1} on the fourth dice", player1[i].GetName(), myDie[i].GetTopNumber3()); 
       Console.WriteLine("{0} Rolled:{1} on the fifth dice", player1[i].GetName(), myDie[i].GetTopNumber4()); 
       myDie[i].points();      
       Console.WriteLine("\t\t\t\t\tTotal Throws:{0}\n ------------------------------------------------------", j); 
       myDie[i].Totally(); 
       Console.ReadLine(); 

      } 
     } 

    } 

    } 

포인트 : 다음과 같은 속성에 주사위 클래스의 Totally() 방법을 변경하는

class Die 
{ 


    private int NumberTop1;     //attributes 
    private int NumberTop2; 
    private int NumberTop3; 
    private int NumberTop4; 
    private int NumberTop5; 

    int threepoints = 0; 
    int sixpoints = 0; 
    int twelvepoints = 0; 
    int TotalPoints = 0; 

    private Random RandomNumGenerator; 

    public Die(Random RandomGenerator)  // constructor 
    { 
     RandomNumGenerator = RandomGenerator;  // initialises random number 
    } 

    public int GetTopNumber() 
    { 
     return NumberTop1;   // Returns number on top 
    } 

    public int GetTopNumber1() 
    { 
     return NumberTop2; 
    } 

    public int GetTopNumber2() 
    { 
     return NumberTop3; 
    } 

    public int GetTopNumber3() 
    { 
     return NumberTop4; 
    } 

    public int GetTopNumber4() 
    { 
     return NumberTop5; 
    } 

    public void roll() 
    { 
     NumberTop1 = RandomNumGenerator.Next(1, 7); 
     NumberTop2 = RandomNumGenerator.Next(1, 7); 
     NumberTop3 = RandomNumGenerator.Next(1, 7); 
     NumberTop4 = RandomNumGenerator.Next(1, 7); 
     NumberTop5 = RandomNumGenerator.Next(1, 7); 

     List<int> diceValues = new List<int>(); 
     diceValues.Add(GetTopNumber()); 
     diceValues.Add(GetTopNumber1()); 
     diceValues.Add(GetTopNumber2()); 
     diceValues.Add(GetTopNumber3()); 
     diceValues.Add(GetTopNumber4()); 


     var duplicates = diceValues 
     .GroupBy(i => i) 
     .Where(g => g.Count() == 3) 
     .Select(g => g.Key); 
     foreach (var d in duplicates) 
     { 
      Console.BackgroundColor = ConsoleColor.Red; 
      Console.WriteLine("\n{0} Appeared three times --- 3 Points Awarded\n", d); 
      Console.BackgroundColor = ConsoleColor.Black; threepoints += 3; 
     } 

     var fourting = diceValues 
     .GroupBy(i => i) 
     .Where(g => g.Count() == 4) 
     .Select(g => g.Key); 
     foreach (var e in fourting) 
     { 
      Console.WriteLine("\n{0} Appeared four times --- 6 Points Awarded\n", e); 
      sixpoints += 6; 
     } 

     var fiveting = diceValues 
     .GroupBy(i => i) 
     .Where(g => g.Count() == 5) 
     .Select(g => g.Key); 
     foreach (var f in fiveting) 
     { 
      Console.WriteLine("\n{0} Appeared five times --- 12 Points Awarded\n", f); 
      twelvepoints += 12; 
     } 
    } 
public string points() 
    { 
     TotalPoints = threepoints + sixpoints + twelvepoints; 
     Console.WriteLine("\n\t\t\t\t\tTotal Score: {0}", TotalPoints); 

     return pointss; 
    } 



    public string Totally() 
    { 
     if (TotalPoints >= 25) 
     { 
      Console.WriteLine("This Player Won the game"); 

     } 
     return tots; 
    } 
} 
+0

Dice 클래스에서 '전체적으로'반환하기 전에 변수'tots '에 설정하는 값은 무엇입니까? Dice 클래스의'TotalPoints'에있는 값들을 어떻게 요약하고 있습니까? –

+0

나는 이해할 수 없다. 난 그저 작동 시키려고 노력하고 있었어. – Russsu

+0

당신이 그 논리에 대해 분명하지 않다면, 그 질문에 답하기가 매우 어렵습니다. 우리가 질문에 대답하고 그 논리를 얻을 수 없다고해도 요점은 없습니다. 전체 주사위 클래스를 공유 할 수 있습니까? –

답변

0

첫 번째 필요.

public class Dice 
{ 
    public int TotalScore 
    { 
     get { return TotalPoints; } 
    } 
} 

왜 50 번 반복해야하는지 잘 모르겠습니다.

다음 논리를 사용할 수 있습니다.

private static void quickgame() ---- Game carried out 
{ 
    // All other code.... 

    bool anyPlayerWon = false; 
    int totalThrows = 0; 
    while(!anyPlayerWon) 
    { 
     totalThrows += 1; 
     for (int i = 0; i < 2; i++) 
     { 
      myDie[i].roll(); 
      Console.WriteLine("{0} Rolled:{1} on the first dice", 
       player1[i].GetName(), myDie[i].GetTopNumber()); 
      Console.WriteLine("{0} Rolled:{1} on the second dice", 
       player1[i].GetName(), myDie[i].GetTopNumber1()); 
      Console.WriteLine("{0} Rolled:{1} on the third dice", 
       player1[i].GetName(), myDie[i].GetTopNumber2()); 
      Console.WriteLine("{0} Rolled:{1} on the fourth dice", 
       player1[i].GetName(), myDie[i].GetTopNumber3()); 
      Console.WriteLine("{0} Rolled:{1} on the fifth dice", 
       player1[i].GetName(), myDie[i].GetTopNumber4()); 
      myDie[i].points();      
      Console.WriteLine("\t\t\t\t\tTotal Throws:{0}\n ---------------- --------------------------------------", totalThrows); 
      var totalScore = myDie[i].TotalScore; 
      if(totalScore >= 25) 
      { 
       anyPlayerWon = true; 
       Console.WriteLine("This Player Won the game"); 
       break; 
      } 
     } 
    } 
} 
+0

정말 고마워요 !!! 그것은 작동합니다. 총 던지기는 더 이상 작동하지 않습니까? – Russsu

+0

show totalThrows ...에 대한 답변을 업데이트했습니다. –