我正在为我的编程课制作一款名为“Lingo”的游戏。我已经完成了大部分工作。但是我们应该在表单类中包含尽可能少的代码。所以我必须将大部分内容转移到另一个班级。关于我应该如何做的任何想法?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
namespace Lingo
{
public partial class Form1 : Form
{
Random r = new Random();
AboutBox1 about = new AboutBox1();
int gissningar, x, score, highcsore;
Label[] labels = new Label[25];
string ord;
public Form1()
{
InitializeComponent();
gissningar = 0;
x = 0;
score = 1000;
highcsore = 0;
labels[0] = label1;
labels[1] = label2;
labels[2] = label3;
labels[3] = label4;
labels[4] = label5;
labels[5] = label6;
labels[6] = label7;
labels[7] = label8;
labels[8] = label9;
labels[9] = label10;
labels[10] = label11;
labels[11] = label12;
labels[12] = label13;
labels[13] = label14;
labels[14] = label15;
labels[15] = label16;
labels[16] = label17;
labels[17] = label18;
labels[18] = label19;
labels[19] = label20;
labels[20] = label21;
labels[21] = label22;
labels[22] = label23;
labels[23] = label24;
labels[24] = label25;
String[] ordLista = File.ReadAllLines(("words.txt").ToString());
ord = ordLista[r.Next(r.Next(ordLista.Length))];
ord = ord.ToUpper();
labels[0].Text = Convert.ToString(ord[0]);
}
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
about.Show();
}
private void addWordsToolStripMenuItem_Click(object sender, EventArgs e)
{
new Form2().Show();
}
private void button1_Click(object sender, EventArgs e)
{
string ordet = ord;
string svar = textBox1.Text.ToUpper();
if (Regex.IsMatch(svar, "^[a-zA-Z]{5}$"))
{
if (x <= 20)
{
if (svar == ord)
for (int i = 0; i <= 4; i++)
{
labels[i + x].Text = Convert.ToString(svar[i]);
labels[i + x].BackColor = Color.Green;
highcsore = score;
}
else
{
gissningar++;
score = score / gissningar;
for (int i = 0; i <= 4; i++)
{
labels[i + x].BackColor = Color.Red;
}
for (int i = 0; i <= 4; i++)
{
labels[i + x].Text = Convert.ToString(svar[i]);
if (svar[i] == ord[i])
{
labels[i + x].BackColor = Color.Green;
ordet = ordet.Insert(i, "?");
ordet = ordet.Remove(i + 1, 1);
if (x < 20)
{
labels[x + 5].Text = Convert.ToString(ord[0]);
labels[i + x + 5].Text = Convert.ToString(svar[i]);
}
}
}
for (int i = 0; i <= 4; i++)
{
for (int k = 0; k <= 4; k++)
{
if (svar[i] == ordet[k])
{
labels[i + x].BackColor = Color.Orange;
svar = svar.Insert(i, "#");
svar = svar.Remove(i + 1, 1);
}
}
}
}
//kod för att jämföra chars
x = x + 5;
}
else
{
MessageBox.Show("Du förlorade! \r\n Ordet var: " + ord);
}
}
else
{
MessageBox.Show("Ordet måste ha 5 bokstäver");
}
}
}
}