Intersecting rectangles from array - c#

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < pala.cantLetras; i++)
{ guiones[i] = new Label();
guiones[i].Text = "_";
guiones[i].Font = new Font("Berlin Sans FB Demi", 25);
guiones[i].Size = new Size(18, 37);
guiones[i].Name = "guion" + i;
guiones[i].ForeColor = Color.Black;
guiones[i].BackColor = Color.Transparent;
guiones[i].Location = new Point(x, 341);
this.Controls.Add(guiones[i]);
recguion[i] = guiones[i].Bounds;
recguion[i] = new Rectangle();
//recguion[i].Location = new Point(x, 341);
x = x + 50;
}
for (int i = 0; i < pala.cantLetras; i++)
{
labels[i] = new Label();
labels[i].Size = new Size(25, 55);
labels[i].Name = "label" + i;
labels[i].Text = pala.palabra[i].ToString();
labels[i].Font = new Font("Berlin Sans FB Demi", 20);
labels[i].ForeColor = Color.Black;
labels[i].BackColor = Color.Transparent;
labels[i].Location = new Point(y, 165);
this.Controls.Add(labels[i]);
posRandom[i] = y;
reclabel[i] = labels[i].Bounds;
reclabel[i] = new Rectangle();
// reclabel[i].Location = new Point(y, 165);
y = y + 40;
}
}
I need to know when reclabel[] intersects the recguion[] corresponding to that number.
Ex: reclabel[1] Intersects recguion[1] but only that one, if it intersects another it has to say that it's wrong.
The rectangles have inside(or that's what I a'm trying) labels[] and guiones[]
This is what I have tryied but it doesnt work.
private void intersecta()
{
int cont = 0;
for (int i = 0; i < pala.cantLetras; i++)
{
for (int j = 0; j < pala.cantLetras; j++)
{
if (i==j)
{
Rectangle intersect = Rectangle.Intersect(reclabel[i], recguion[j]);
if (intersect != Rectangle.Empty)
{
MessageBox.Show("Intersection!");
cont++;
}
}
if (cont != 0)
{
i = pala.cantLetras - 1;
j = pala.cantLetras - 1;
}
}
}
}
Thank you!

There is no need for a nested loop. Just loop through one array and check both rectangles at that index with .IntersectsWith. My apologies if there are any syntax errors, I don't have access to Visual Studio at the moment.
For(int i = 0; i < Array1.Length; i++)
{
if(Array1[i].IntersectsWith(Array2[i]))
{
//Intersected
}
}
But also, as Andrew pointed out, you have a serious problem here:
reclabel[i] = new Rectangle();
You are just overwriting all your data with a new instance (of a different type!).

Related

Delete rectangle annotations from Chart

I have a list of signals in a listview. When the user checks one, the values of the signals are being plotted on the chart. Moreover there is a vertical annotation which the user can drag across the graph and see the values for every x value. Each signal has one rectangle annotation that shows the Y value.
My problem is that when the user checks a new signal then the old rectangle annotations do not disappear.
Here is what I mean :
enter image description here
Here is my code so far :
List<RectangleAnnotation> anno = new List<RectangleAnnotation>();
List<Series> checkedItems = new List<Series>();
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (listView1.FocusedItem != null)
{
double xFactor = 0.03;
double yFactor = 0.02;
CA = chart1.ChartAreas[0];
if (e.NewValue == CheckState.Checked)
{
anno.Clear();
Series s12 = new Series();
s12 = chart1.Series.Add((listView1.Items[e.Index].Text).ToString());
s12.ChartType = SeriesChartType.Line;
s12.MarkerStyle = MarkerStyle.Circle; // make the points stand out
s12.MarkerSize = 3;
checkedItems.Add(s12);
for (int i = 0; i < chart1.Series.Count - 1; i++)
{
anno.Add(new RectangleAnnotation());
anno[i].AxisX = CA.AxisX;
anno[i].IsSizeAlwaysRelative = false;
anno[i].Width = 20 * xFactor;
anno[i].Height = 8 * yFactor;
// VA.Name = "myRect";
anno[i].LineColor = Color.Black;
anno[i].BackColor = Color.Black;
anno[i].AxisY = CA.AxisY;
anno[i].Y = -anno[i].Height;
// RA[i].X = VA.X - RA[i].Width / 2;
anno[i].Text = "Hello";
anno[i].ForeColor = Color.Black;
anno[i].Font = new System.Drawing.Font("Arial", 8f);
anno[i].Text = String.Format("{0:0.00}", 0);
chart1.Annotations.Add(anno[i]);
}
for (int r = 0; r < num_rows; r++)
{
DataPoint dp = new DataPoint();
dp.SetValueXY(r, values[r, listView1.Items.IndexOf(listView1.Items[e.Index])]);
// chart1.Series[checkedListBox1.Items[e.Index].ToString()].Points.Add(dp);
s12.Points.AddXY(r, values[r, listView1.Items.IndexOf(listView1.Items[e.Index])]);
}
}
}
}
private void chart1_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e)
{
int pt1 = (int)e.NewLocationX;
int i = 0;
foreach (var signal in checkedItems) {
double val = signal.Points[pt1].YValues[0];
foreach (var sim in signal.Points[pt1].YValues)
{
anno[i].Y = sim + 0.5;
}
if (sender == VA) anno[i].X = VA.X - anno[i].Width / 2;
anno[i].Text = String.Format("{0:0.00}", val);
i++;
Console.WriteLine(anno.Count);
}
I have thought of adding
chart1.Annotations.clear();
But it deletes all Annotations including the vertical. I only want to delete the rectangle annotations.

Drawing Lines in Autoscroll form c#

I just want to transform the points when scrolling. I already use the e.Graphics.TranslateTransform() but it only works on drawing. The point of the line wont change the offset.
private void Form1_Scroll(object sender, ScrollEventArgs e)
{
Point scrollOffset = this.AutoScrollPosition;
for (int lk = 0; lk < Pt1.Count; lk++)
{
Pt1[lk] = new Point(Pt1[lk].X + scrollOffset.X, Pt1[lk].Y + scrollOffset.Y);
Pt2[lk] = new Point(Pt2[lk].X + scrollOffset.X, Pt2[lk].Y + scrollOffset.Y);
}
this.Invalidate();
this.Update();
this.Refresh();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Point scrollOffset = this.AutoScrollPosition;
e.Graphics.TranslateTransform(scrollOffset.X, scrollOffset.Y);
e.Graphics.Clear(this.BackColor);
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Pen greenPen = new Pen(Color.Green,1);
greenPen.DashPattern = new float[] { 5.0F, 5.0F, 5.0F, 5.0F };
Pen red = new Pen(Color.Red,1);
red.DashPattern = new float[] { 5.0F, 5.0F, 5.0F, 5.0F };
Pen Grey = new Pen(Color.Gray,1);
Grey.DashPattern = new float[] { 5.0F, 5.0F, 5.0F, 5.0F };
#region "Draw Line using cursor"
Pen SolidGreen = new Pen(Color.Green, 1);
Pen SolidGrey = new Pen(Color.Gray, 1);
Pen Red = new Pen(Color.Red, 1);
for (int pl = 0; pl < Pt1j.Count; pl++)
{
PointF[] pointsj = new PointF[]
{
new PointF(Pt2j[pl].X, Pt2j[pl].Y),
new PointF(Pt3j[pl].X , Pt3j[pl].Y),
new PointF(Pt4j[pl].X , Pt4j[pl].Y),
new PointF(Pt5j[pl].X , Pt5j[pl].Y),
};
e.Graphics.DrawLine(Pens.Green, Pt1j[pl].X , Pt1j[pl].Y, Pt2j[pl].X, Pt2j[pl].Y);
e.Graphics.DrawCurve(Pens.Green, pointsj);
e.Graphics.DrawLine(Pens.Green, Pt5j[pl].X , Pt5j[pl].Y, Pt6j[pl].X, Pt6j[pl].Y);
}
for (int pl = 0; pl < Pt1DashedGray.Count; pl++)
{
e.Graphics.DrawLine(Grey, Pt1DashedGray[pl], Pt2DashedGray[pl]);
PointF[] pointsdhg = new PointF[]
{
new PointF(Pt2DashedGray[pl].X, Pt2DashedGray[pl].Y),
new PointF(Pt3DashedGray[pl].X, Pt3DashedGray[pl].Y),
new PointF(Pt4DashedGray[pl].X, Pt4DashedGray[pl].Y),
new PointF(Pt5DashedGray[pl].X, Pt5DashedGray[pl].Y),
};
e.Graphics.DrawCurve(Grey, pointsdhg);
e.Graphics.DrawLine(Grey, Pt5DashedGray[pl], Pt6DashedGray[pl]);
}
for (int pl = 0; pl < Pt1SolidGray.Count; pl++)
{
e.Graphics.DrawLine(SolidGrey, Pt1SolidGray[pl], Pt2SolidGray[pl]);
PointF[] pointsdhg = new PointF[]
{
new PointF(Pt2SolidGray[pl].X, Pt2SolidGray[pl].Y),
new PointF(Pt3SolidGray[pl].X, Pt3SolidGray[pl].Y),
new PointF(Pt4SolidGray[pl].X, Pt4SolidGray[pl].Y),
new PointF(Pt5SolidGray[pl].X, Pt5SolidGray[pl].Y),
};
e.Graphics.DrawCurve(SolidGrey, pointsdhg);
e.Graphics.DrawLine(SolidGrey, Pt5SolidGray[pl], Pt6SolidGray[pl]);
}
for (int pl = 0; pl < Pt1DashGreen.Count; pl++)
{
e.Graphics.DrawLine(greenPen, Pt1DashGreen[pl], Pt2DashGreen[pl]);
PointF[] pointsdhg = new PointF[]
{
new PointF(Pt2DashGreen[pl].X, Pt2DashGreen[pl].Y),
new PointF(Pt3DashGreen[pl].X, Pt3DashGreen[pl].Y),
new PointF(Pt4DashGreen[pl].X, Pt4DashGreen[pl].Y),
new PointF(Pt5DashGreen[pl].X, Pt5DashGreen[pl].Y),
};
e.Graphics.DrawCurve(greenPen, pointsdhg);
e.Graphics.DrawLine(greenPen, Pt5DashGreen[pl], Pt6DashGreen[pl]);
}
for (int i = 0; i < Pt1.Count; i++)
{
e.Graphics.DrawLine(SolidGreen, Pt1[i], Pt2[i]);
}
for (int l = 0; l < DashedGreen1.Count; l++)
{
e.Graphics.DrawLine(greenPen, DashedGreen1[l], DashedGreen2[l]);
}
for (int k = 0; k < SolidGrey1.Count; k++)
{
e.Graphics.DrawLine(SolidGrey, SolidGrey1[k], SolidGrey2[k]);
}
for (int j = 0; j < DashedGrey1.Count; j++)
{
e.Graphics.DrawLine(Grey, DashedGrey1[j], DashedGrey2[j]);
}
if (IsDrawing)
{
e.Graphics.DrawLine(Red, NewPt1.X, NewPt1.Y, NewPt2.X, NewPt2.Y);
}
if (Draw_Dashed_Green)
{
e.Graphics.DrawLine(red, DashedGreenPT1, DashedGreenPT2);
}
if (Draw_Solid_Grey)
{
e.Graphics.DrawLine(Red, SolidGreyPT1, SolidGreyPT2);
}
if (Draw_dashed_Grey)
{
e.Graphics.DrawLine(red, DashedGreyPT1, DashedGreyPT2);
}
}
Need Help
Here is my code so far.
BDW, The point of the line is stored in the list from.
The Drawn lines in Paint event also include the line jumpers which has 6 points on it to make a jumps.
So the only problem is I would like to change the Points of the lines as I use the scroll bar.
Thanks for answering my question.

How do i access these arrays so that i can multiply inputted values in the diagonal?

If I inputted 5, it will generate a square matrix by 5 (5x5)
How do I multiply 1, 7, 13, 19, 25?
Is there any applicable algorithm for my code so I could multiply the diagonals or do i need to rewrite a new one?
public partial class Form1 : Form {
public int e = 0;
int Row = 0;
int Column = 0;
int YAxisPosition = 0;
int XAxisPosition = 0;
int Counter = 0;
int PositionalValue = 0;
TextBox[] MyTextBoxDimA = new TextBox[999999];
TextBox tbRow = new TextBox();
Button MyButton = new Button();
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
AutoScroll = true;
WindowState = System.Windows.Forms.FormWindowState.Maximized;
//GENERATING THE SIZE BUTTON
tbRow = new TextBox();
tbRow.Text = "5";
tbRow.Size = new Size(100, 10);
tbRow.Location = new Point(0, 0);
Controls.Add(tbRow);
//GENERATE MATRIX BUTTON
MyButton = new Button();
MyButton.Text = "GENERATE MATRIX";
MyButton.Size = new Size(200, 25);
MyButton.Click += new EventHandler(MyButton_Click);
MyButton.Location = new Point(0, 30);
Controls.Add(MyButton);
}
public void MyButton_Click(object sender, EventArgs ee) {
//CODE FOR GENERATING MATRIX A
e = 1;
PositionalValue = 1;
Counter = 1;
//POSITION
YAxisPosition = 60;
XAxisPosition = 0;
Row = Convert.ToInt32(tbRow.Text);
Column = Convert.ToInt32(tbRow.Text);
while (Row >= e) {
while (Column >= Counter) {
MyTextBoxDimA[PositionalValue] = new TextBox();
MyTextBoxDimA[PositionalValue].Location =
new Point(XAxisPosition, YAxisPosition); //coordinates (start)
MyTextBoxDimA[PositionalValue].Size = new Size(70, 10);
MyTextBoxDimA[PositionalValue].Text = Convert.ToString(PositionalValue);
Controls.Add(MyTextBoxDimA[PositionalValue]);
XAxisPosition = XAxisPosition + 100;
PositionalValue++;
Counter++;
}
YAxisPosition = YAxisPosition + 50;
Counter = 1;
e++;
XAxisPosition = 0;
}
}
}
If you are looking for an algorithm:
static void Main(string[] args)
{
int n = 5;
int ans = 1;
int current = 1;
for (int i = 1; i <= n; i++)
{
ans = ans * current;
current += n + 1;
}
Console.WriteLine(ans);
}
Just loop from zero to the size of the matrix. The index will be used to get each diagonal value (this code assumes that each textbox contains an integer and is not blank):
int matrixSize = 5;
int product = 1;
for (int i = 0; i < matrixSize; i++)
{
product *= int.Parse(MyTextBoxDimA[i,i].Text);
}

dynamically adding controls under loop in C#

I am developing a windows application where I want to create some controls dynamically inside a loop.
The code I am trying is
private Label newLabel = new Label();
private int txtBoxStartPosition = 100;
private int txtBoxStartPositionV = 25;
for (int i = 0; i < 7; i++)
{
newLabel.Location = new System.Drawing.Point(txtBoxStartPosition, txtBoxStartPositionV);
newLabel.Size = new System.Drawing.Size(70, 40);
newLabel.Text = i.ToString();
panel1.Controls.Add(newLabel);
txtBoxStartPositionV += 30;
}
This code is generating only one Label with text 7 but I want to create 8 Lables with their respective texts, how can I do this?
In your loop you are essentially updating properties of the very same Label. If you want create a new one on each step, move creation of the object inside the loop:
private Label newLabel;
for (int i = 0; i < 7; i++)
{
newLabel = new Label();
...
By the way if you want 8 labels - your for should iterate 8 times, not 7, as it does now:
for (int i = 0; i < 8; i++)
Try this:
private int txtBoxStartPosition = 100;
private int txtBoxStartPositionV = 25;
for (int i = 0; i < 7; i++)
{
newLabel = new Label();
newLabel.Location = new System.Drawing.Point(txtBoxStartPosition, txtBoxStartPositionV);
newLabel.Size = new System.Drawing.Size(70, 40);
newLabel.Text = i.ToString();
panel1.Controls.Add(newLabel);
txtBoxStartPositionV += 30;
}
You need to put the line private Label newLabel = new Label(); in the for loop.
private int txtBoxStartPosition = 100;
private int txtBoxStartPositionV = 25;
for (int i = 0; i < 7; i++)
{
Label newLabel = new Label();
newLabel.Location = new System.Drawing.Point(txtBoxStartPosition, txtBoxStartPositionV);
newLabel.Size = new System.Drawing.Size(70, 40);
newLabel.Text = i.ToString();
panel1.Controls.Add(newLabel);
txtBoxStartPositionV += 30;
}

button event with validation and actions

I need help with the Check button. After a user adds all the 42 numbers in the textbox and enter a number from 0-9 in the "Enter number" area and clicks on the start button, next what he should do is run through the array of labels with the red label or lblCovece and he should collect the same values like the number enters before. And after he clicks on the Check button, the programme should first validate if the value that is selected with the red label is the same as the number entered. If is valid the label should turn green and than the result should appear in the label lblResultE(the result for example should be like this: if the number entered is 2, the result it is 2+2+2...)and if is not valid in the lblResultE we take out 10 points. That's what i did by now with some help.:) thank you.
namespace Seminarska
{
public partial class Form1 : Form
{
private Label l,l2,lblCovece,l4,lblResultE;
private Button bUp, bRight, bLeft, bDown, bCheck,bStart, bReset;
private TextBox txtVnes, txtGoal;
private Label[] pole;
public Form1()
{
InitializeComponent();
l2 = new Label();
l2.Text = " Enter one number";
l2.Location = new Point(230, 200);
l2.AutoSize = true;
l4 = new Label();
l4.Text = "Score";
l4.Location = new Point(240, 260);
l4.AutoSize = true;
lblResultE = new Label();
lblResultE.Location = new Point(350, 260);
lblResultE.AutoSize = true;
bLeft = new Button();
bLeft.Location = new Point(0, 250);
bLeft.Width=75;
bLeft.Height = 25;
bLeft.Text = "LEFT";
bCheck = new Button();
bCheck.Location = new Point(75, 250);
bCheck.Width = 75;
bCheck.Height = 25;
bCheck.Text = "Check";
bRight = new Button();
bRight.Location = new Point(150, 250);
bRight.Width = 75;
bRight.Height = 25;
bRight.Text = "RIGHT";
bUp = new Button();
bUp.Location = new Point(75, 220);
bUp.Width = 75;
bUp.Height = 25;
bUp.Text = "UP";
bDown = new Button();
bDown.Location = new Point(75, 280);
bDown.Width = 75;
bDown.Height = 25;
bDown.Text = "DOWN";
bStart = new Button();
bStart.Location = new Point(240, 165);
bStart.Width = 75;
bStart.Height = 25;
bStart.Text = "START";
bReset = new Button();
bReset.Location = new Point(320, 165);
bReset.Width = 75;
bReset.Height = 25;
bReset.Text = "RESET";
txtVnes = new TextBox();
txtVnes.Location = new Point(240, 10);
txtVnes.Width = 160;
txtVnes.Height = 130;
txtVnes.Multiline = true;
txtGoal = new TextBox();
txtGoal.Width = 75;
txtGoal.Height = 25;
txtGoal.Location = new Point(330, 200);
lblCovece = new Label();
lblCovece.Location = new Point(160,165);
lblCovece.Width = 20;
lblCovece.Height = 20;
lblCovece.TextAlign = ContentAlignment.MiddleCenter;
lblCovece.Text = "O";
lblCovece.BackColor = Color.FromArgb(255, 0, 0);
int a = 0;
pole = new Label[42];
this.Controls.Add(lblCovece);
for (int i = 1; i <= 6; i++)
{
for (int j = 1; j <= 7; j++)
{
l = new Label();
l.Name = "label" + i.ToString() + j.ToString();
l.Text = "Z";
l.Width = 20;
l.Height = 20;
l.TextAlign = ContentAlignment.MiddleCenter;
l.Parent = this;
l.BackColor = Color.FromArgb(100, 149, 237);
l.Location = new Point(10 + (j - 1) * 25, 15 + (i - 1) * 25);
pole[a] = l;
this.Controls.Add(l);
a++;
}
}
this.Width = 460;
this.Height = 380;
this.Controls.Add(l2);
this.Controls.Add(l4);
this.Controls.Add(lblResultE);
this.Controls.Add(lblTimeE);
this.Controls.Add(bStart);
this.Controls.Add(bReset);
this.Controls.Add(txtGoal);
this.Controls.Add(txtVnes);
this.Controls.Add(bUp);
this.Controls.Add(bLeft);
this.Controls.Add(bRight);
this.Controls.Add(bDown);
this.Controls.Add(bCheck);
bStart.Click+=new EventHandler(bStart_Click);
bUp.Click+=new EventHandler(bUp_Click);
bDown.Click+=new EventHandler(bDown_Click);
bLeft.Click+=new EventHandler(bLeft_Click);
bRight.Click+=new EventHandler(bRight_Click);
bCheck.Click+=new EventHandler(bZemaj_Click);
bReset.Click+=new EventHandler(bReset_Click);
}
private void bStart_Click(object sender, EventArgs e)
{
string Str = txtGoal.Text.Trim();
int Num;
bool isNum = int.TryParse(Str, out Num);
if (isNum && Str.Length == 1)
{
string[] ts = txtVnes.Text.Split(
new string[] { "\r\n" },
StringSplitOptions.RemoveEmptyEntries);
int row = 0;
for (int i = 0; i < ts.Length && row < 6; i++)
{
if (LineIsValid(ts[i]))
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = ts[i][2 * col].ToString();
}
row++;
}
}
for (; row < 6; row++)
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = "Z";
}
}
}
else
{
MessageBox.Show("Invalid Input");
}
}
private static Regex regex = new Regex(#"^(\s)*(\d ){6}\d(\s)*$");
private static bool LineIsValid(string line)
{
return regex.IsMatch(line);
}
private void bReset_Click(object sender, EventArgs e)
{
txtVnes.Clear();
string[] ts = txtVnes.Text.Split(new string[] { "\r\n" },
StringSplitOptions.RemoveEmptyEntries);
int row = 0;
for (int i = 0; i < ts.Length && row < 6; i++)
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = "Z";
pole[row * 7 + col].BackColor=Color.FromArgb(100, 149, 237);
}
row++;
}
for (; row < 6; row++)
{
for (int col = 0; col < 7; col++)
{
pole[row * 7 + col].Text = "Z";
pole[row * 7 + col].BackColor = Color.FromArgb(100, 149, 237);
}
}
txtGoal.Clear();
lblCovece.Location=new Point(160,165);
}
private void bUp_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X, lblCovece.Location.Y -
25);
}
private void bDown_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X, lblCovece.Location.Y +
25);
}
private void bLeft_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X - 25,
lblCovece.Location.Y);
}
private void bRight_Click(object sender, EventArgs e)
{
lblCovece.Location = new Point(lblCovece.Location.X + 25,
lblCovece.Location.Y);
}
private void bCheck_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
What makes your programm complicated and difficult to understand, is that you mix game logic with display logic.
I suggest you to redesign your game. It could look something like this:
public enum State
{
Empty, // Displays "Z"
Neutral, // Blue
Good, // Green
Bad // Red
}
public class Square
{
public int Number { get; set; }
public State State { get; set; }
}
public class Game
{
public const int Width = 7, Height = 6;
public Game()
{
Board = new Square[Width, Height];
}
public event EventHandler GameChanged;
public Square[,] Board { get; private set; }
public int CurrentX { get; private set; }
public int CurrentY { get; private set; }
public void Reset()
{
for (int x = 0; x < Width; x++) {
for (int y = 0; y < Height; y++) {
Board[x, y].State = State.Empty; // Always displayed in blue as "Z"
}
}
OnGameChanged();
}
public void MoveLeft()
{
if (CurrentX > 0) {
CurrentX--;
OnGameChanged();
}
}
public void MoveRight()
{
if (CurrentX < Width - 1) {
CurrentX++;
OnGameChanged();
}
}
// and so on
private void OnGameChanged()
{
EventHandler eh = GameChanged;
if (eh != null) {
eh(this, EventArgs.Empty);
}
}
}
In the form I would define pole to be a matrix as well (like the board). I show only a few relevant parts of the form code, to give you an idea of what I mean:
public class Form1 : Form
{
private Game game;
private Label[,] pole;
public Form1()
{
game = new Game();
game.GameChanged += new EventHandler(Game_GameChanged);
pole = new Label[Game.Width, Game.Height];
// Intialize pole.
// ...
}
void Game_GameChanged(object sender, EventArgs e)
{
for (int x = 0; x < Game.Width; x++) {
for (int y = 0; y < Game.Height; y++) {
Square square = game.Board[x, y];
Label label = pole[x,y];
switch (square.State) {
case State.Empty:
label.BackColor = Color.Blue;
label.Text = "Z";
break;
case State.Neutral:
label.BackColor = Color.Blue;
label.Text = square.Number.ToString();
break;
case State.Good:
label.BackColor = Color.Green;
label.Text = square.Number.ToString();
break;
case State.Bad:
label.BackColor = Color.Red;
label.Text = square.Number.ToString();
break;
default:
break;
}
}
}
// Place lblCovece according to game.CurrentX and game.CurrentY
// ...
}
private void bReset_Click(object sender, EventArgs e)
{
game.Reset();
}
private void bLeft_Click(object sender, EventArgs e)
{
game.MoveLeft();
}
private void bRight_Click(object sender, EventArgs e)
{
game.MoveRight();
}
}
Note how the Game class tells the form when changes happen through the event GameChanged. The form then updates the game display. In the game class, you can now concentrate on the game logic and do not have to deal with buttons and labels. You can also work with logical coordinates in the range [0..6] and [0..5] of the game board. This is easier than working with pixels. You delegate all the pixel calculations to the form.
My example is not complete, but when you try to implement it, you will see that it will be much easier to think about how the logic of the game should work.
Add an int score;
private void bCheck_Click(object sender, EventArgs e)
{
bool found = false;
foreach (Label l in pole)
{
if (l.Location == lblCovece.Location && l.Text == txtGoal.Text)
{
l.BackColor = Color.Green;
score += int.Parse(l.Text);
lblResultE.Text = score.ToString();
found = true;
}
}
if (!found)
{
score -= 10;
lblResultE.Text = score.ToString();
}
}

Categories

Resources