2017-08-01 55 views
-4

我開始學習一種c#語言。我創建了一個簡單易用的控制檯應用程序,它沒有太多的工作。那麼你可以檢查它。它關於足球(足球)比賽。我真的想重新編碼到OOP使用類和方法,但我不知道從哪裏開始。在我的應用程序中,您可以從兩個團隊中選擇一個,然後隨機檢查結果。但我喜歡增加一個選項,你可以從1到更多的團隊中進行選擇。說實話,一開始可能只有4支隊伍。我知道我需要使用類來編寫相同的代碼塊。對於我來說,理解OOP依然很困難。如果有人能以最簡單的方式重新編寫我的代碼,我希望我能從中獲得更多。由於如何重構我的代碼到OOP

class Program 
{ 
    static void Main(string[] args) 
    { 
     int inputGames = 0; 
     string userInput; 
     while (true) 
     { 
      Console.Write("Please select your team: Type '1' for FC Barcelona or '2' for Real Madrid: "); 
      userInput = Console.ReadLine(); 

      if (userInput == "1") 
      { 
       Console.Write("Your select FC Barcelona. How many games you want to play against Real Madrid?: "); 
       inputGames = int.Parse(Console.ReadLine()); 
       break; 
      } 
      else if (userInput == "2") 
      { 
       Console.Write("Your select Real Madrid. How many games you want to play against FC Barcelona?: "); 
       inputGames = int.Parse(Console.ReadLine()); 
       break; 
      } 
      else 
      { 
       Console.Write("Wrong input! Try Again."); 
       continue; 
      } 
     } 



     int game = 0; 
     //string clubA = "FC Barcelona"; 
     //string clubB = "Real Madrid"; 
     int teamAwins = 0; 
     int teamBwins = 0; 
     int teamAlose = 0; 
     int teamBlose = 0; 
     int teamAdraw = 0; 
     int teamBdraw = 0; 
     int teamApoints = 0; 
     int teamBpoints = 0; 
     int teamAgoles = 0; 
     int teamBgoles = 0; 

     while (game < inputGames) 
     { 

      int teamA = 0; 
      int teamB = 0; 
      int teamAresult = 0; 
      int teamBresult = 0; 

      int TeamADefense = 8; 
      int TeamAMidfield = 9; 
      int TeamAAttack = 10; 
      int TeamAMentality = 8; 

      int TeamBDefense = 9; 
      int TeamBMidfield = 8; 
      int TeamBAttack = 9; 
      int TeamBMentality = 9; 


      Random Num = new Random(); 
      int RandomTADefense = Num.Next(TeamADefense - 5, TeamADefense + 1); 
      int RandomTAMidfield = Num.Next(TeamAMidfield - 5, TeamAMidfield + 1); 
      int RandomTAAttack = Num.Next(TeamAAttack - 5, TeamAAttack + 1); 
      int RandomTAMentality = Num.Next(TeamAMentality - 5, TeamAMentality + 1); 

      int RandomTBDefense = Num.Next(TeamBDefense - 5, TeamBDefense + 1); 
      int RandomTBMidfield = Num.Next(TeamBMidfield - 5, TeamBMidfield + 1); 
      int RandomTBAttack = Num.Next(TeamBAttack - 5, TeamBAttack + 1); 
      int RandomTBMentality = Num.Next(TeamBMentality - 5, TeamBMentality + 1); 

      //Console.WriteLine("FC Barcelona - Defense: {0}", RandomTADefense); 
      //Console.WriteLine("FC Barcelona - Midfield: {0}", RandomTAMidfield); 
      //Console.WriteLine("FC Barcelona - Attack: {0}", RandomTAAttack); 
      //Console.WriteLine("FC Barcelona - Mentality: {0}", RandomTAMentality); 
      //Console.WriteLine("Real Madrid - Defense: {0}", RandomTBDefense); 
      //Console.WriteLine("Real Madrid - Midfield: {0}", RandomTBMidfield); 
      //Console.WriteLine("Real Madrid - Attack: {0}", RandomTBAttack); 
      //Console.WriteLine("Real Madrid - Mentality: {0}", RandomTBMentality); 


      if (RandomTADefense > RandomTBDefense) 
      { 
       teamA++; 
      } 
      else 
      { 
       teamB++; 
      } 
      if (RandomTAMidfield > RandomTBMidfield) 
      { 
       teamA++; 
      } 

      else 
      { 
       teamB++; 
      } 
      if (RandomTAAttack > RandomTBAttack) 
      { 
       teamA++; 
      } 

      else 
      { 
       teamB++; 
      } 
      if (RandomTAMentality > RandomTBMentality) 
      { 
       teamA++; 
      } 

      else 
      { 
       teamB++; 
      } 
      Random result = new Random(); 

      if(teamA > teamB) 
      { 
       teamAwins++; 
       teamBlose++; 
       teamApoints += 3; 
       if (teamA == 4) 
       { 
        int winner = result.Next(4, 7); 
        teamAresult = winner; 
        teamBresult = winner - result.Next(4, winner); 
        teamAgoles += winner; 
        teamBgoles += teamBresult; 
       } 
       else if (teamA == 3) 
       { 
        int winner = result.Next(3, 5); 
        teamAresult = winner; 
        teamBresult = winner - result.Next(2, winner); 
        teamAgoles += winner; 
        teamBgoles += teamBresult; 
       } 
       else if (teamA == 2) 
       { 
        int winner = result.Next(1, 3); 
        teamAresult = winner; 
        teamBresult = winner - result.Next(1, winner); 
        teamAgoles += winner; 
        teamBgoles += teamBresult; 
       } 
      }else if (teamB > teamA) 
      { 
       teamBwins++; 
       teamAlose++; 
       teamBpoints += 3; 
       if (teamB == 4) 
       { 
        int winner = result.Next(4, 7); 
        teamBresult = winner; 
        teamAresult = winner - result.Next(4, winner); 
        teamBgoles += winner; 
        teamAgoles += teamAresult; 
       } 
       else if (teamB == 3) 
       { 
        int winner = result.Next(3, 5); 
        teamBresult = winner; 
        teamAresult = winner - result.Next(2, winner); 
        teamBgoles += winner; 
        teamAgoles += teamAresult; 
       } 
       else if (teamB == 2) 
       { 
        int winner = result.Next(2, 3); 
        teamBresult = winner; 
        teamAresult = winner - result.Next(1, winner); 
        teamBgoles += winner; 
        teamAgoles += teamAresult; 
       } 
      } 

      else 
      { 
       teamAdraw++; 
       teamBdraw++; 
       teamApoints++; 
       teamBpoints++; 
       int winner = result.Next(2, 5); 
       teamAresult = winner - teamA; 
       teamBresult = winner - teamB; 
       teamAgoles += teamAresult; 
       teamBgoles += teamBresult; 
      } 


      game++; 
      // Console.WriteLine("\nFC Barcelona {0} - {1} Real Madrid", teamA, teamB); 
      Console.WriteLine("{0}. FC Barcelona {1} - {2} Real Madrid",game, teamAresult , teamBresult); 

      Console.ReadLine(); 

     } 

     if (userInput == "1") 
     { 
      Console.WriteLine("FC Barcelona - Points: {0}, Wins: {1}, Draws: {2}, Losses: {3}, Goles: {4}", teamApoints, teamAwins, teamAdraw, teamAlose, teamAgoles); 
     } 
     else 
     { 
      Console.WriteLine("Real Madrid - Points: {0}, Wins: {1}, Draws: {2}, Losses: {3}, Goles: {4}", teamBpoints, teamBwins, teamBdraw, teamBlose, teamBgoles); 
     } 






     Console.ReadLine(); 
    } 
} 

}

+3

'如果有人能以最簡單的方式重新獲得我的代碼'。不,這不是*代碼重構*,*爲我寫一個代碼*,*做我的作業*或*代碼翻譯*網站 –

+0

在你的代碼中,你有幾個「共同」的東西,看起來他們會是很好的候選人。例如。你有團隊A和團隊B,所有這些都具有相同的屬性。他們將成爲轉換爲「團隊」對象的理想人選,並擁有一些管理它們的屬性/方法。 – AndrewP

+0

堆棧溢出不是「重構我的代碼」網站。 – EJoshuaS

回答

1

//我不能寫完整的代碼給你,但會盡量給它會幫助你的整體結構,因爲它看起來像你已經知道編碼只是尋找構建代碼以遵循OOPS的初步幫助。

//You can have an Enum for team types as 


public enum TeamType 
{ 
    Barcelona, RealMadrid // This can be extended at any point of time 
} 

//You can create a class as 

Class Team{ 
    //Properties 
    public int ID{get; set;} 
    public string Name{get; set;} 
    public TeamType Type{get; set;} 
    public int InputGames{get; set;} 

    //Methods 
    //write your methods here 
} 

// You can write you program here to use these as 
class Program 
{ 
    static void Main(string[] args) 
    { 
     //Program would be written here 
     //If one team is needed, you will create one object of Team class. 
     //Similarily you can create as many number of Team object as many 
     //teams you need 
    } 
} 
+0

謝謝。對我來說還是外語,但我會嘗試。 – VoytecTheDon