简体   繁体   中英

C# application/game, best approach

I want to create a game something like "Who wants to be millionaire?", where you answer questions by clicking one of the three buttons. I'm kind of noob in c# so I don't know what would be the best approach to do this. I was thinking of making the loop and then creating objects (question - object). But how would I connect form buttons with these new objects? Or maybe there is a better way to accomplish this? Thank you all for suggestions!

First of all: Unless you need your application to run on other platforms than Windows you should look into Windows Presentation Foundation, it's the successor of Winforms.

Regarding how to implement this a simple solution would be to have a CurrentQuestion property containing an instance of a Question class with Answers inside.

class Question
{
     public string QuestionText {get;}
     public Answer AnswerA { get; }
     public Answer AnswerB { get; }
     public Answer AnswerC { get; }
     public Answer AnswerD { get; }
     public Answer ChosenAnswer { get;set;}
}

class Answer
{
    public string AnswerText { get; }
}

Each buttons Text-property binds to an answer in the CurrentQuestion and when you click the button the answer is chosen and the next question can be processed.

I will presume that you will do this in windows Forms or WPF (Windows Presentation Foundation) - an application running on a Windows PC and not a web application.

What I would do is to prepare a complete model (or object) that would have the question and the correct answer which I would use to load a view - a bunch of controls (TextBoxes, Labels, Buttons etc) that would present the information.

Since both Windows Forms and WPF support the notion of events , I would hook up handlers the click events of the three button and take action on a click of any of those buttons. And since I loaded the view with the correct answer, I wouldn't have to load anything on the click event which means I could determine if the answer would be correct immediately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM