public scrLevel1()
{
InitializeComponent(GetRightToLeft());
InitializePictures();
DisplayRandomPicture();
}
private void InitializePictures()
{
// Initialize pictures with images and answers
pictures = new List<Picture>
{
new(new string[] { @"D:4Picsdocumentbark1.jpg", @"D:4Picsdocumentbark2.jpg", @"D:4Picsdocumentbark3.jpg", @"D:4Picsdocumentbark4.jpg" }, "BARK"),
new(new string[] { @"D:4Picsdocumentcomputer1.jpg", @"D:4Picsdocumentcomputer2.jpg", @"D:4Picsdocumentcomputer3.jpg", @"D:4Picsdocumentcomputer4.jpg" }, "COMPUTER"),
new Picture(new string[] { @"D:4Picsdocumentfirm1.jpg", @"D:4Picsdocumentfirm2.jpg", @"D:4Picsdocumentfirm3.jpg", @"D:4Picsdocumentfirm4.jpg" }, "FIRM"),
new Picture(new string[] { @"D:4Picsdocumenthero1.jpg", @"D:4Picsdocumenthero2.jpg", @"D:4Picsdocumenthero3.jpg", @"D:4Picsdocumenthero4.jpg" }, "HERO"),
new Picture(new string[] { @"D:4Picsdocumentnavigate1.jpg", @"D:4Picsdocumentnavigate2.jpg", @"D:4Picsdocumentnavigate3.jpg", @"D:4Picsdocumentnavigate4.jpg" }, "NAVIGATE"),
new Picture(new string[] { @"D:4Picsdocumentpollution1.jpg", @"D:4Picsdocumentpollution2.jpg", @"D:4Picsdocumentpollution3.jpg", @"D:4Picsdocumentpollution4.jpg" }, "POLLUTED"),
new Picture(new string[] { @"D:4Picsdocumentschool1.jpg", @"D:4Picsdocumentschool2.jpg", @"D:4Picsdocumentschool3.jpg", @"D:4Picsdocumentschool4.jpg" }, "SCHOOL"),
new Picture(new string[] { @"D:4Picsdocumenttriple1.jpg", @"D:4Picsdocumenttriple2.jpg", @"D:4Picsdocumenttriple3.jpg", @"D:4Picsdocumenttriple4.jpg" }, "TRIPLE"),
// Add more pictures here
};
}
public class Picture
{
public string[] ImagePaths { get; }
public string CorrectAnswer { get; }
public Picture(string[] imagePaths, string correctAnswer)
{
ImagePaths = imagePaths;
CorrectAnswer = correctAnswer;
}
}
private void DisplayRandomPicture()
{
position = 0;
// Randomly select a picture from the list
Random random = new Random();
currentPicture = pictures[random.Next(pictures.Count)];
answer = currentPicture.CorrectAnswer;
// Display images of the current picture
pic1.ImageLocation = currentPicture.ImagePaths[0];
pic2.ImageLocation = currentPicture.ImagePaths[1];
pic3.ImageLocation = currentPicture.ImagePaths[2];
pic4.ImageLocation = currentPicture.ImagePaths[3];
// Define the alphabet
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Initialize the charArray with enough size
charArray = new char[20];
//put the letters of answers involved in the letter button
for (int i = 0; i < answer.Length; i = i + 1)
{
charArray[i] = answer[i];
}
// Randomize more letters for the answer keys
foreach (char letter in alphabet)
{
random = new Random();
for (int i = answer.Length; i < 12; i++)
{
charArray[i] = alphabet[random.Next(alphabet.Length)];
}
}
//Shuffle the letters
for (int i = 11; i > -1; i--)
{
int j = random.Next(i + 1);
var temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
}
// Set the text of the button key to character of charArray
key1.Text = charArray[0].ToString();
key2.Text = charArray[1].ToString();
key3.Text = charArray[2].ToString();
key4.Text = charArray[3].ToString();
key5.Text = charArray[4].ToString();
key6.Text = charArray[5].ToString();
key7.Text = charArray[6].ToString();
key8.Text = charArray[7].ToString();
key9.Text = charArray[8].ToString();
key10.Text = charArray[9].ToString();
key11.Text = charArray[10].ToString();
key12.Text = charArray[11].ToString();
AnsButton = new Button[answer.Length];
if (answer.Length == 3)
{
for (int i = 0; i < answer.Length; i++)
{
AnsButton[i] = new Button
{
Name = "keyButton{i}",
Text = "",
Size = new Size(47, 48),
Location = new Point(269 + (i * 53), 495),
BackColor = Color.Black,
Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
ForeColor = Color.White
};
this.Controls.Add(AnsButton[i]);
}
}
else if (answer.Length == 4)
{
for (int i = 0; i < answer.Length; i++)
{
AnsButton[i] = new Button
{
Name = "KeyButton{i}",
Text = "",
Size = new Size(47, 48),
Location = new Point(238 + (i * 53), 495),
BackColor = Color.Black,
Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
ForeColor = Color.White
};
this.Controls.Add(AnsButton[i]);
}
}
else if (answer.Length == 5)
{
for (int i = 0; i < answer.Length; i++)
{
AnsButton[i] = new Button
{
Name = $"keyButton{i}",
Text = "",
Size = new Size(47, 48),
Location = new Point(215 + (i * 53), 495),
BackColor = Color.Black,
Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
ForeColor = Color.White
};
this.Controls.Add(AnsButton[i]);
}
}
else if (answer.Length == 6)
{
for (int i = 0; i < answer.Length; i++)
{
AnsButton[i] = new Button
{
Name = $"keyButton{i}",
Text = "",
Size = new Size(47, 48),
Location = new Point(184 + (i * 53), 495),
BackColor = Color.Black,
Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
ForeColor = Color.White
};
this.Controls.Add(AnsButton[i]);
}
}
else if (answer.Length == 7)
{
for (int i = 0; i < answer.Length; i++)
{
AnsButton[i] = new Button
{
Name = $"keyButton{i}",
Text = "",
Size = new Size(47, 48),
Location = new Point(161 + (i * 53), 495),
BackColor = Color.Black,
Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
ForeColor = Color.White
};
this.Controls.Add(AnsButton[i]);
}
}
else if (answer.Length == 8)
{
for (int i = 0; i < answer.Length; i++)
{
AnsButton[i] = new Button
{
Name = "keyButton{i}",
Text = "",
Size = new Size(47, 48),
Location = new Point(138 + (i * 53), 495),
BackColor = Color.Black,
Font = new Font("Franklin Gothic Book", 12F, FontStyle.Bold),
ForeColor = Color.White
};
this.Controls.Add(AnsButton[i]);
}
}
ansinput = "";
key1.Click += key_Click;
key2.Click += key_Click;
key3.Click += key_Click;
key4.Click += key_Click;
key5.Click += key_Click;
key6.Click += key_Click;
key7.Click += key_Click;
key8.Click += key_Click;
key9.Click += key_Click;
key10.Click += key_Click;
key11.Click += key_Click;
key12.Click += key_Click;
CheckAnswer();
}
private void key_Click(object sender, EventArgs e)
{
if (position < AnsButton.Length)
{
Button clickedButton = sender as Button;
if (clickedButton != null)
{
AnsButton[position].Text = clickedButton.Text;
ansinput = ansinput + clickedButton.Text;
clickedButton.ForeColor = Color.Black;
position++;
}
}
}
private void CheckAnswer()
{
if (ansinput == answer)
{
MessageBox.Show("Correct!");
// Display a new picture
DisplayRandomPicture();
// Clear the answer input
ClearAnswerInput();
}
else if (ansinput != answer)
{
MessageBox.Show("Incorrect. Try again.");
// Reset the position and clear the answer input
position = 0;
ClearAnswerInput();
}
}
private void ClearAnswerInput()
{
// Clear the text of answer buttons
foreach (Button button in AnsButton)
{
button.Text = "";
}
// Reset the position
position = 0;
}
I expected it to display random pictures, allow players to click on the keybutton, clicked buttons are showed in the ansbuttons. When all the ansbuttons are filled, use the function “Checkanswers” to check the answer and show the message box. If the answer is correct, display the new one. If not, redo the question. But this code checks the answer before display the question.
New contributor
Tăng Hoàng Xuân Hạnh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.