-
Notifications
You must be signed in to change notification settings - Fork 140
Open
Description
// Form1.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
namespace GeographyQuizGame
{
public partial class Form1 : Form
{
private List questions = new List();
private int currentIndex = 0;
private int correctCount = 0;
private Stopwatch timer = new Stopwatch();
public Form1()
{
InitializeComponent();
questionTypeComboBox.SelectedIndexChanged += QuestionTypeComboBox_SelectedIndexChanged;
addButton.Click += AddButton_Click;
editButton.Click += EditButton_Click;
deleteButton.Click += DeleteButton_Click;
questionListBox.SelectedIndexChanged += QuestionListBox_SelectedIndexChanged;
submitAnswerButton.Click += SubmitAnswerButton_Click;
showAnswersButton.Click += ShowAnswersButton_Click;
restartButton.Click += RestartButton_Click;
exitButton.Click += (s, e) => Close();
questionTypeComboBox.SelectedIndex = 0;
UpdateInputVisibility();
}
private void QuestionTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateInputVisibility();
}
private void UpdateInputVisibility()
{
multipleChoiceGroup.Visible = questionTypeComboBox.SelectedIndex == 0;
trueFalseGroup.Visible = questionTypeComboBox.SelectedIndex == 1;
openEndedGroup.Visible = questionTypeComboBox.SelectedIndex == 2;
}
private void AddButton_Click(object sender, EventArgs e)
{
Question q = CreateQuestionFromInputs();
if (q != null)
{
questions.Add(q);
RefreshQuestionList();
}
}
private void EditButton_Click(object sender, EventArgs e)
{
if (questionListBox.SelectedIndex >= 0)
{
Question q = CreateQuestionFromInputs();
if (q != null)
{
questions[questionListBox.SelectedIndex] = q;
RefreshQuestionList();
}
}
}
private void DeleteButton_Click(object sender, EventArgs e)
{
if (questionListBox.SelectedIndex >= 0)
{
questions.RemoveAt(questionListBox.SelectedIndex);
RefreshQuestionList();
}
}
private void RefreshQuestionList()
{
questionListBox.Items.Clear();
foreach (var q in questions)
questionListBox.Items.Add(q.Text);
}
private void QuestionListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (questionListBox.SelectedIndex >= 0)
{
LoadQuestionToForm(questions[questionListBox.SelectedIndex]);
}
}
private void LoadQuestionToForm(Question q)
{
questionTextBox.Text = q.Text;
if (q is MultipleChoiceQuestion mcq)
{
questionTypeComboBox.SelectedIndex = 0;
mcOption1.Text = mcq.Options[0];
mcOption2.Text = mcq.Options[1];
mcOption3.Text = mcq.Options[2];
mcOption4.Text = mcq.Options[3];
mcCorrectOption.SelectedIndex = mcq.CorrectIndex;
}
else if (q is TrueFalseQuestion tfq)
{
questionTypeComboBox.SelectedIndex = 1;
trueRadio.Checked = tfq.Answer;
falseRadio.Checked = !tfq.Answer;
}
else if (q is OpenEndedQuestion oeq)
{
questionTypeComboBox.SelectedIndex = 2;
acceptedAnswersTextBox.Text = string.Join(", ", oeq.AcceptedAnswers);
}
}
private Question CreateQuestionFromInputs()
{
string text = questionTextBox.Text.Trim();
if (string.IsNullOrEmpty(text)) return null;
switch (questionTypeComboBox.SelectedIndex)
{
case 0:
if (mcCorrectOption.SelectedIndex < 0) return null;
return new MultipleChoiceQuestion(text,
new string[] { mcOption1.Text, mcOption2.Text, mcOption3.Text, mcOption4.Text },
mcCorrectOption.SelectedIndex);
case 1:
return new TrueFalseQuestion(text, trueRadio.Checked);
case 2:
var answers = acceptedAnswersTextBox.Text.Split(',').Select(a => a.Trim().ToLower()).ToList();
return new OpenEndedQuestion(text, answers);
}
return null;
}
private void StartQuiz()
{
if (questions.Count == 0)
{
MessageBox.Show("No questions available.");
return;
}
currentIndex = 0;
correctCount = 0;
resultLabel.Text = "";
timer.Restart();
LoadQuestion();
}
private void LoadQuestion()
{
if (currentIndex >= questions.Count)
{
timer.Stop();
resultLabel.Text = $"Correct: {correctCount}/{questions.Count}\nTime: {timer.Elapsed.Minutes}m {timer.Elapsed.Seconds}s";
return;
}
var q = questions[currentIndex];
playQuestionLabel.Text = q.Text;
playAnswerPanel.Controls.Clear();
Control answerControl = q.GetAnswerControl();
answerControl.Location = new System.Drawing.Point(0, 0);
playAnswerPanel.Controls.Add(answerControl);
}
private void SubmitAnswerButton_Click(object sender, EventArgs e)
{
if (currentIndex < questions.Count)
{
var q = questions[currentIndex];
bool correct = q.CheckAnswer(playAnswerPanel.Controls[0]);
if (correct) correctCount++;
currentIndex++;
LoadQuestion();
}
}
private void ShowAnswersButton_Click(object sender, EventArgs e)
{
string msg = string.Join("\n\n", questions.Select(q => $"{q.Text}\nAnswer: {q.GetCorrectAnswer()}"));
MessageBox.Show(msg, "Correct Answers");
}
private void RestartButton_Click(object sender, EventArgs e)
{
StartQuiz();
}
private void falseRadio_CheckedChanged(object sender, EventArgs e)
{
}
}
public abstract class Question
{
public string Text { get; set; }
public Question(string text) => Text = text;
public abstract Control GetAnswerControl();
public abstract bool CheckAnswer(Control c);
public abstract string GetCorrectAnswer();
}
public class MultipleChoiceQuestion : Question
{
public string[] Options { get; set; }
public int CorrectIndex { get; set; }
public MultipleChoiceQuestion(string text, string[] options, int correctIndex) : base(text)
{
Options = options;
CorrectIndex = correctIndex;
}
public override Control GetAnswerControl()
{
var panel = new Panel();
for (int i = 0; i < Options.Length; i++)
{
var radio = new RadioButton { Text = Options[i], Location = new System.Drawing.Point(0, 25 * i), Tag = i };
panel.Controls.Add(radio);
}
return panel;
}
public override bool CheckAnswer(Control c)
{
foreach (RadioButton rb in c.Controls)
{
if (rb.Checked && (int)rb.Tag == CorrectIndex)
return true;
}
return false;
}
public override string GetCorrectAnswer() => Options[CorrectIndex];
}
public class TrueFalseQuestion : Question
{
public bool Answer { get; set; }
public TrueFalseQuestion(string text, bool answer) : base(text) => Answer = answer;
public override Control GetAnswerControl()
{
var panel = new Panel();
var trueRB = new RadioButton { Text = "True", Location = new System.Drawing.Point(0, 0), Tag = true };
var falseRB = new RadioButton { Text = "False", Location = new System.Drawing.Point(0, 25), Tag = false };
panel.Controls.Add(trueRB);
panel.Controls.Add(falseRB);
return panel;
}
public override bool CheckAnswer(Control c)
{
foreach (RadioButton rb in c.Controls)
{
if (rb.Checked && (bool)rb.Tag == Answer)
return true;
}
return false;
}
public override string GetCorrectAnswer() => Answer ? "True" : "False";
}
public class OpenEndedQuestion : Question
{
public List<string> AcceptedAnswers { get; set; }
public OpenEndedQuestion(string text, List<string> answers) : base(text) => AcceptedAnswers = answers;
public override Control GetAnswerControl()
{
return new TextBox { Width = 300 };
}
public override bool CheckAnswer(Control c)
{
string input = (c as TextBox).Text.Trim().ToLower();
return AcceptedAnswers.Contains(input);
}
public override string GetCorrectAnswer() => string.Join(", ", AcceptedAnswers);
}
}
Metadata
Metadata
Assignees
Labels
No labels