2015-12-22 208 views
1

所以我想用C#在控制檯應用程序中創建一個dartgame。C#控制檯應用程序Dartgame

這是據我已經ATM得到:

class Program 
{ 
    static void Main(string[] args) 
    { 
     Game gameOn = new Game(); 
     gameOn.PlayGame(); 
    } 

    class Game 
    { 
     private List<Players> playerList = new List<Players>(); 

     public void AddPlayers(string name) 
     { 
      Players names = new Players(name); 
      playerList.Add(names); 
     } 

     public void PlayGame() 
     { 
      Console.WriteLine("Välkommen till Dartspelet! Tryck valfri knapp för att fortsätta..."); 
      Console.ReadLine(); 
      Console.WriteLine("Skriv in antal spelare. Ni kommer också att möta en Dator."); 
      Console.WriteLine(); 
      int players = Convert.ToInt32(Console.ReadLine()); 

      for (int i = 0; i < players; i++) 
      { 
       Console.Write("Skriv in spelarens namn: "); 
       string playersNames = Console.ReadLine(); 
       AddPlayers(playersNames); 
      } 

      Console.WriteLine(); 
      Console.WriteLine("Spelet har börjat!"); 
      Console.WriteLine(); 

       foreach (Players name in playerList) 
       { 

        Console.WriteLine("Datorn börjar att kasta... Var god vänta..."); 
        System.Threading.Thread.Sleep(2000); 
        Random rng = new Random(); 
        int ranAttempt1 = rng.Next(0, 21); 
        int ranAttempt2 = rng.Next(0, 21); 
        int ranAttempt3 = rng.Next(0, 21); 
        Attempts result = new Attempts(ranAttempt1, ranAttempt2, ranAttempt3); 

        Console.WriteLine("Datorn Fick " + result.GetScore() + " på 3 kast."); 
        Console.ReadLine(); 

        Console.WriteLine(name + "s Tur! "); 

        Console.WriteLine("Skriv in Poäng mellan 0-20 för kast 1:"); 
        int attempt1 = Int32.Parse(Console.ReadLine()); 

        Console.WriteLine("Skriv in Poäng mellan 0-20 för kast 2:"); 
        int attempt2 = Int32.Parse(Console.ReadLine()); 

        Console.WriteLine("Skriv in Poäng mellan 0-20 för kast 3:"); 
        int attempt3 = Int32.Parse(Console.ReadLine()); 

        Attempts result1 = new Attempts(attempt1, attempt2, attempt3); 

        Console.WriteLine(name + " Fick " + result1.GetScore()); 

      } 
      Console.ReadLine(); 
     } 
    } 

    class Attempts 
    { 
     private int attempt1; 
     private int attempt2; 
     private int attempt3; 

     public Attempts(int attempt1 = 0, int attempt2 = 0, int attempt3 = 0) 
     { 
      this.attempt1 = attempt1; 
      this.attempt2 = attempt2; 
      this.attempt3 = attempt3; 
     } 

     public int GetScore() 
     { 
      return attempt1 + attempt2 + attempt3; 
     } 
    } 
    class Players 
    { 
     private string Name { get; set; } 
     public List<Attempts> attempts = new List<Attempts>(); 


     public Players(string name = "") 
     { 
      Name = name; 
     } 

     public override string ToString() 
     { 
      return Name; 
     } 
    } 
} 

我需要周圍產生foreach循環時的球員之一已經達到了分數301以上,它將結束while循環幫助。我還需要一種方法將比分保持在列表中或類似的東西中。但是,我被困住了,所以任何幫助都很受歡迎! :)

很遺憾,如果代碼是有點亂

提前感謝!

回答

1

那麼,你已經有玩家嘗試的列表。你可以:

  1. 添加一個while循環周圍的foreach(玩家...):
  2. 追加的嘗試,它的完成之後。
  3. 經過每個玩家的嘗試後,計算總分。
  4. 打印的分數並退出,如果總分> 301

    while(true) 
    { 
    
        foreach(Players name in playerList) 
        { 
        // existing code here 
    
        // you should encapsulate attempts with getter and setter, 
        // but according to your Players class this will work. 
        name.attempts.add(result1); 
    
        // now sum up the total results for the player 
        // exercise for the reader. 
        int totalResults = player.getTotalScore(); 
    
        if(totalResults > 301) 
        { 
         Console.WriteLine("Player " + name.getName() + " won the game!"); 
         Environment.exit(0); 
        } 
        } 
    } 
    
+0

謝謝!我只是有點困惑,我應該把這個方法:player.getTotalScore();? @Kyle Fransham – oOMelon

+0

由於您有玩家對象的嘗試列表,您可以使用 int getTottalScore(){ int sum = 0; foreach(嘗試嘗試att){ sum + = att.GetScore(); } } 總結所有的嘗試。 –

+0

啊我在那個方向想着。我似乎有點修補了一下。代碼工作正常,但if語句永遠不會因爲某種原因而被解僱,無論用戶獲得多少點。 while循環只是繼續前進。感謝您的幫助!這是非常appriciated! @Kyle Fransham – oOMelon