从我以前的帖子更新
由于这是我第一次使用列表和我的第二个月编程,请不要认为我是一个彻头彻尾的白痴。
我的问题是试图阻止用户点击(将该位置更改为新矩形)在所有方向上的1个区块超过他/她之前点击的区块。
到目前为止我的代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Quoridor
{
public partial class Form1 : Form
{
private static List<RectangleAndPenBlack> recListBlack = new List
<RectangleAndPenBlack>();
private static List<RectangleAndBrushRed> recListRed = new List<RectangleAndBrushRed>();
private static List<RectangleAndBrushGreen> recListGreen =
recListBlack.Cast<RectangleAndBrushGreen>().ToList();
private static Size recSize = new Size(50, 50);
private bool isFirstRectanglePlaced = false;
private int previousIndex;
private int rectanglesPlaced;
RectangleAndBrushGreen recGreen = new RectangleAndBrushGreen();
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.Fixed3D;
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 18; y++)
{
Point recPoint = new Point(x * 50, y * 50);
recListBlack.Add(new RectangleAndPenBlack
{
blackRectangle = new Rectangle(recPoint, recSize),
});
}
Application.DoEvents();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (RectangleAndPenBlack rpb in recListBlack)
{
e.Graphics.DrawRectangle(rpb.blackPen, rpb.blackRectangle);
}
foreach (RectangleAndBrushRed rpr in recListRed)
{
e.Graphics.FillRectangle(rpr.redBrush, rpr.redRectangle);
}
foreach (RectangleAndBrushGreen rpg in recListGreen)
{
e.Graphics.FillRectangle(rpg.greenBrush, rpg.greenRectangle);
}
}
private void overrideRectangle(MouseEventArgs e)
{
foreach (RectangleAndPenBlack rpb in recListBlack.ToList())
{
if (rpb.blackRectangle.Contains(e.Location) && e.Button == MouseButtons.Left)
{
recListBlack.Remove(rpb);
recListRed.Add(new RectangleAndBrushRed
{
redRectangle = new Rectangle(rpb.blackRectangle.Location, recSize)
});
isFirstRectanglePlaced = true;
Invalidate();
}
}
foreach (RectangleAndPenBlack rpb in recListBlack.ToList())
{
if (rpb.blackRectangle.Contains(e.Location) && e.Button == MouseButtons.Right
&& rectanglesPlaced <= 1)
{
previousIndex = recListBlack.IndexOf(rpb);
recListBlack.Remove(rpb);
recListGreen.Add(new RectangleAndBrushGreen
{
greenRectangle = new Rectangle(rpb.blackRectangle.Location, recSize)
});
Invalidate();
}
else if (rpb.blackRectangle.Contains(e.Location) && e.Button ==
MouseButtons.Right && rectanglesPlaced <= 1)
{
if (checkIfMoveIsValid(rpb, e))
{
previousIndex = recListBlack.IndexOf(rpb);
recListBlack.Remove(rpb);
recListGreen.Add(new RectangleAndBrushGreen
{
greenRectangle = new Rectangle(rpb.blackRectangle.Location, recSize)
});
Invalidate();
}
else
{
}
}
}
}
public Boolean checkIfMoveIsValid(RectangleAndPenBlack rpb, MouseEventArgs e)
{
if ((rpb.blackRectangle.Location) == e.Location)
{
return true;
}
else
{
return false;
}
}
private void mouseClick(object sender, MouseEventArgs e)
{
overrideRectangle(e);
rectanglesPlaced += 1;
}
}
}
班级:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Quoridor
{
class RectangleAndBrushRed
{
public Rectangle redRectangle { get; set; }
public Brush redBrush = Brushes.Red;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Quoridor
{
public class RectangleAndPenBlack
{
public Rectangle blackRectangle { get; set; }
public Pen blackPen = Pens.Black;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Quoridor
{
public class RectangleAndBrushGreen
{
public Rectangle greenRectangle { get; set; }
public Brush greenBrush = Brushes.Green;
}
}
如果您需要澄清,请随时提出! :d