4

プログラムに300のチャレンジを追加しようとしていますが、CompletionValue.IsChecked=falseの場合にのみ表示します。

このプログラムを作成していた場合。チャレンジをどのように保存しますか?私はスイッチを使用していますが、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";
    }
4

4 に答える 4

6

あなたの挑戦はお互いにひどく似ていますね?これは、共通のデータ構造を抽出し、各課題を1つのデータとして表す場合です。

チャレンジを統一的に表現し、特定のチャレンジIDのチャレンジデータに基づいてUIを設定します。

データを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;
}

これは、宣言型プログラミングの一形態と見なすことができます。プログラムの重要な部分であるチャレンジ自体は、命令ステートメント(UIプロパティの設定)から非常に単純なデータ宣言に変換されています。

この変換を行うことで、各チャレンジをチェックして、すべてのパーツが入力されていることを確認することもできます。次に、300の課題ごとに、タイトル、質問、リソース、回答などが設定されていることを確認します。

于 2011-09-29T21:36:38.893 に答える
4

課題はデータベースまたはファイルに保存できます。乱数を使用していて、チャレンジを1つだけ表示しているようです。DBは次のようなものにすることができます

ChallengeId, DifficultyValue, TitleValue ... 

ChallengeIdはquestionId番号になります。したがって、生成された乱数に応じて、特定のChallengeIdと関連データを選択できます。

于 2011-09-29T21:15:19.803 に答える
2

実際に調べる必要があるのは、カプセル化とポリモーフィックコードです。同様のプロパティを単一のクラスにカプセル化することで、「チャレンジ」全体をより適切に表現でき、何度も入力する必要のある部分を再利用できるように.Text = "..."なると()、将来のコーディングライフが無限に広がります。より良い。確かに、Challenge以下に示すように、エンティティのリストをコーディングすることさえ面白くありません。いつかどこかにそのデータを入力する必要があります。_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);
    }

}
于 2011-09-29T21:26:54.827 に答える
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;
        }
    }
}

ChallengeクラスをFactoryクラス内のネストされたクラスにしたことに注意してください。これを行うことの良い点は、チャレンジのコンストラクターをprivate作成できることです(つまり、ファクトリメソッド以外では「無効な」タイプのチャレンジを作成できません。これを行うことの悪い点は、チャレンジをさらに修飾する必要があることです。クラスには、クラスを含むクラスをプレフィックスとして付けます。つまりChallengeFactory、この場合は、追加の修飾子を使用する価値があると思います。

最終的に、コードですべての課題を定義することを計画している場合は、どこかでスイッチを作成する必要があります。他の人が言っているように、外部データソース(データベースなど)で課題を定義し、読み取り、解析、および単一のメソッドを使用することで、作成する必要のあるコード(したがってスイッチ)の量を大幅に減らすことができます。インスタンスを作成しChallengeます。

于 2011-09-30T00:59:36.607 に答える