2011-09-29 71 views
4

我想將300個挑戰添加到我的程序中,但只顯示它們,如果CompletionValue.IsChecked = false;如何避免300箱的開關塊?

如果你創建這個程序。你將如何存儲挑戰? 我正在使用一個開關,但有300個案例是矯枉過正,有沒有更好的方法?

上提高代碼的任何建議十分讚賞。 我對此有點新鮮。

Random rand = new Random(); 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
     AnswerValue.Visibility = Visibility.Collapsed; 
     Load(); 

    } 


    private void Load() 
    { 
     int random = rand.Next(1, 4); 
     switch (random) 
     { 
      case 1: 
       Challenge1(); 
       break; 
      case 2: 
       Challenge2(); 
       break; 
      case 3: 
       Challenge3(); 
       break; 
     } 
    } 

    private void Challenge1() 
    { 
     DifficultyValue.Text = "20%"; 
     CompletionValue.IsChecked = false; 
     TitleValue.Text = "Chicken or Egg?"; 
     QuestionValue.Text = "Can you answer the ancient question: Which came first the chicken of the egg?"; 
     bmp.UriSource = new Uri("Images/Challenge1.png", UriKind.Relative); 
     ImageValue.Source = bmp; 
     ImageValue.Visibility = Visibility.Visible; 
     ResourceValue.Text = "Resource: Brain Games"; 
     AnswerValue.Text = "The Egg. According to paleontologists, reptiles and dinosaurs existed long before birds and chickens. Fossilized eggs dating back on hundred millions years have been uncovered. Thus it can be said that eggs came before chickens."; 

    } 

    private void Challenge2() 
    { 
     DifficultyValue.Text = "25%"; 
     CompletionValue.IsChecked = false; 
     TitleValue.Text = "Halving Seven"; 
     QuestionValue.Text = "Can you prove that seven is half of twelve?"; 
     bmp.UriSource = new Uri("Images/Challenge2.png", UriKind.Relative); 
     ImageValue.Source = bmp; 
     ImageValue.Visibility = Visibility.Visible; 
     ResourceValue.Text = "Resource: Yahoo Questions"; 
     AnswerValue.Text = "Roman numeral for 12 - XII \n Cut the roman numeral in half. you will get VII, which is 7."; 

    } 

    private void Challenge3() 
    { 
     DifficultyValue.Text = "25%"; 
     CompletionValue.IsChecked = false; 
     TitleValue.Text = "Three-coin flip"; 
     QuestionValue.Text = "You ask a friend about probability, and he tells you the following: The odds of three tossed coins turning up all heads or all tails is one in two, that is, fifty-fifty. That’s because anytime you toss three coins, at least two must match, either two heads or two tails. So that means the third coin—which is equally likely to be heads or tails—determines the odds.」 Is your friend right? If not, what are the odds of three tossed coins turning up all heads or all tails?"; 
     bmp.UriSource = new Uri("Images/Challenge3.png", UriKind.Relative); 
     ImageValue.Source = bmp; 
     ImageValue.Visibility = Visibility.Visible; 
     ResourceValue.Text = "Resource: Brain Games"; 
     AnswerValue.Text = "Answer will be available soon"; 
    } 

回答

6

你的挑戰看起來非常相似,對嗎?在這種情況下,您想要提取出常見的數據結構,並將每項挑戰都表示爲一項數據。

隨着您的挑戰統一表示,設置好UI基於一個特別的挑戰ID挑戰數據。

總是可以將數據移動到XML文件,JSON文件或數據庫,而是先看看簡單的C#解決方案適用於您:

// Note: This example is simplified for readability 

// Here is the common data structure that can represent all challenges 
private class Challenge 
{ 
    public string Title { get; set; } 
    public string Question { get; set; } 
    public string ImagePath { get; set; } 
} 

// All of the challenges are defined right here, as simple data 
private Challenge[] _allChallenges = new Challenge[] 
{ 
    new Challenge 
    { 
     Title = "Chicken or Egg?", 
     Question = "Can you answer the ancient question: Which came first the chicken of the egg?", 
     ImagePath = "Images/Challenge1.png", 
    }, 
    new Challenge 
    { 
     Title = "Halving Seven?", 
     Question = "Can you prove that seven is half of twelve?", 
     ImagePath = "Images/Challenge1.png", 
    }, 
} 

// Choosing challenges is as simple as indexing into the array 
private void Load() 
{ 
    int random = rand.Next(1, 4); 
    Challenge chosenChallenge = _allChallenges[random]; 
    LoadChallenge(chosenChallenge); 
} 

// Setting up the UI for a challenge means extracting information from the data structure 
private void LoadChallenge(Challenge chosenChallenge) 
{ 
    TitleValue.Text = chosenChallenge.Title; 
    QuestionValue.Text = chosenChallenge.Text; 
    bmp.UriSource = new Uri(chosenChallenge.ImagePath, UriKind.Relative); 
    ImageValue.Source = bmp; 
    ImageValue.Visibility = Visibility.Visible; 
} 

你可以認爲這是一種形式declarative programming。程序中的一個重要部分,挑戰本身已經從命令性語句(設置UI屬性)轉換爲非常簡單的數據聲明。

通過進行這種轉換,你甚至可以檢查每一個挑戰,以確保所有的部件都填寫。然後,您可以確定標題,問題,資源,答案等是針對您的每個300挑戰設定的。

4

您可以將挑戰保存在數據庫或文件中。我確實看到你正在使用一個隨機數並只顯示1個挑戰。 DB可以是類似於

ChallengeId, DifficultyValue, TitleValue ... 

ChallengeId將成爲問題ID號。因此,根據生成的隨機數,您可以選擇特定的ChallengeId和相關數據。

+0

+1必須在這裏同意。對300個挑戰進行編碼似乎是一個糟糕的主意。 –

2

什麼你應該看看是封裝和多態代碼。通過封裝你喜歡的屬性變成一個類,你有代表「挑戰」作爲一個整體,並能重複使用,你必須輸入一遍又一遍(.Text = "...")的部分將讓你未來的編碼生活的更好的方法無限更好。當然,即使對實體列表進行編碼,如下所示,也不會很有趣,您必須在某個時間輸入數據。我們將考慮這個編碼練習,您可以輕鬆地修改下面的代碼以從數據庫或序列化文件填充_challenges

public class Challenge 
{ 
    public int Id {get;set;} 
    public int Difficulty {get;set;} 
    public bool IsCompleted {get;set;} 
    public string Title {get;set;} 
    public string Question {get;set;} 
    public string Answer {get;set;} 
} 

public class MainPage 
{ 
    private List<Challenge> _challenges; 
    private Random rand = new Random(); 
    public MainPage() 
    { 
     _challenges = new List<Challenge> { 
      new Challenge { 
        Id = 1, 
        Difficulty = 20, 
        Title = "What came first?", 
        Question = "The chicken or the egg?", 
        Answer = "The egg." }, 
      new Challenge { 
        Id = 2, 
        Difficulty = 30, 
        Title = "Make 7 from 12?", 
        Question = "Can you prove 7 is half of 12?", 
        Answer = "VII" }}; 
    } 

    public void LoadChallenge(Challenge challenge) 
    { 
     Difficulty.Test = String.Format("%{0}", callenge.Difficulty); 
     Completeted.Value = challenge.IsCompleted; 
     Title.Test = challenge.Title; 
     // etc 
    } 

    public void StartNewChallenge() 
    { 
     Challenge nextChallenge = null; 
     while(nextChallenge == null) 
     { 
      var nextId = rand.Next(1,2); 
      nextChallenge = _challenges 
       .Where(x => x.Id == nextId && !x.IsCompleted) 
       .SingleOrDefault(); // default to null if completed == true 
     } 
     LoadChallenge(nextChallenge); 
    } 

} 
1

另一種替代方案可能是某種工廠方法:

MyForm.cs

public class MyForm 
{ 
    Random rand = new Random(); 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
     AnswerValue.Visibility = Visibility.Collapsed; 
     Load(); 
    } 

    private void Load() 
    { 
     int random = rand.Next(1, 4); 
     DisplayChallenge(ChallengeFactory.GetChallenge(random)); 
    } 

    private void DisplayChallenge(ChallengeFactory.Challenge challengeToDisplay) 
    { 
     DifficultyValue.Text = String.Format("{0}%", challengeToDisplay.Difficulty); 
     CompletionValue.IsChecked = challengeToDisplay.IsChecked; 
     TitleValue.Text = challengeToDisplay.Title; 
     QuestionValue.Text = challengeToDisplay.Question; 
     ImageValue.Source = challengeToDisplay.ImageSource; 
     ImageValue.Visibility = challengeToDisplay.Visible; 
     ResourceValue.Text = challengeToDisplay.ResourceValue; 
     AnswerValue.Text = challengeToDisplay.Answer; 
    } 
} 

ChallengeFactory。CS

public static class ChallengeFactory 
{ 
    public class Challenge 
    { 
     public int Difficulty { get; set; } 
     public bool IsChecked { get; set; } 
     public string Title { get; set; } 
     public string Question { get; set; } 
     public Uri ImageSource { get; set; } 
     public bool Visible { get; set; } 
     public string ResourceValue { get; set; } 
     public string Answer { get; set; } 

     private Challenge(int difficulty, bool isChecked, string title, string question, Uri imageSource, bool visible, string resourceValue, string answer) 
     { 
      // assign each of the arguments to the instance properties 
     } 
    } 

    public static Challenge GetChallenge(int challengeNumber) 
    { 
     switch(challengeNumber) 
     { 
      case 1: 
       return new Challenge(20, false, "Chicken or Egg?", "Can you answer the ancient question: Which came first the chicken of the egg?", new Uri("Images/Challenge1.png", UriKind.Relative), true, "Resource: Brain Games", "The Egg..."); 
      break; 
      case 2: 
       // new challenge for challenge #2 
      break; 
      case 3: 
       // new challenge for challenge #3 
      break; 
     } 
    } 
} 

請注意,我已經做了挑戰類工廠類的內部嵌套類。這樣做的好處是您可以製作挑戰private的構造函數(這意味着您不能通過工廠方法之外的任何其他方法創建「無效」類型的挑戰。這樣做的壞處是您必須進一步限定條件由前綴它挑戰類與它的包含類,也就是ChallengeFactory。我認爲這是值得在這種情況下,額外的預選賽。

最後,我認爲有,如果你打算定義創建一個開關某處你被卡住在代碼中遇到的所有挑戰正如其他人所說的那樣,通過在外部數據源(如數據庫)中定義挑戰並擁有單一代碼,您可以顯着減少需要編寫的代碼量(以及交換機)方法來讀取,解析,並創建一個Challenge實例。