/
Lasso.cs
150 lines (137 loc) · 5.4 KB
/
Lasso.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace buttonCowboy
{
class Lasso : Control
{
private const int BTN_SIZE = 70;
private const int BTN_SHIFT = 35;
private Button[] buttons;
private Point origin;
private int length;
private Cattle targetCattle;
private int[] blueIndex = new int[] { 1, 3, 5, 6, 7, 10 };
private bool inverseOrder = false;
public Lasso(Point origin, int length, ref Cattle targetCattles)
{
this.origin = origin;
this.length = length;
this.targetCattle = targetCattles;
buttons = new Button[length];
SuspendLayout();
for (int i = 0; i < length; ++i)
{
buttons[i] = new Button();
buttons[i].Location = new Point(origin.X - BTN_SHIFT * i, origin.Y - BTN_SHIFT * i);
buttons[i].Name = "btn" + i;
buttons[i].Size = new Size(BTN_SIZE, BTN_SIZE);
buttons[i].TabIndex = i;
buttons[i].UseVisualStyleBackColor = true;
buttons[i].Click += new EventHandler(lasso_Click);
if (isBlue(buttons[i].TabIndex))
{
buttons[i].BackColor = Color.Blue;
}
buttons[i].BringToFront();
Controls.Add(buttons[i]);
}
ResumeLayout(false);
}
public bool isBlue(int btnIndex)
{
for (int i = 0; i < blueIndex.Length; i++)
{
if (blueIndex[i] == btnIndex)
{
return true;
}
}
return false;
}
public bool isEnd(int index)
{
if (inverseOrder)
{
return index == 0;
}
else
{
return index == length - 1;
}
}
public void lasso_Click(object sender, EventArgs e)
{
Button clickedBtn = (Button)sender;
int turnOrigin = clickedBtn.TabIndex;
Point oldPrevLocation = new Point(buttons[turnOrigin].Location.X, buttons[turnOrigin].Location.Y);
int orderSign;
if (inverseOrder)
{
orderSign = -1;
}
else
{
orderSign = 1;
}
Point curLocation;
int i = turnOrigin;
while (!isEnd(i))
{
curLocation = new Point(buttons[i + orderSign].Location.X, buttons[i + orderSign].Location.Y);
Point prevLocation = new Point(buttons[i].Location.X, buttons[i].Location.Y);
if (isBlue(clickedBtn.TabIndex))
{//if blue
if (curLocation.X > oldPrevLocation.X & curLocation.Y < oldPrevLocation.Y)
{
buttons[i + orderSign].Location = new Point(prevLocation.X - BTN_SHIFT, prevLocation.Y - BTN_SHIFT);
}
else if (curLocation.X > oldPrevLocation.X & curLocation.Y > oldPrevLocation.Y)
{
buttons[i + orderSign].Location = new Point(prevLocation.X + BTN_SHIFT, prevLocation.Y - BTN_SHIFT);
}
else if (curLocation.X < oldPrevLocation.X & curLocation.Y > oldPrevLocation.Y)
{
buttons[i + orderSign].Location = new Point(prevLocation.X + BTN_SHIFT, prevLocation.Y + BTN_SHIFT);
}
else if (curLocation.X < oldPrevLocation.X & curLocation.Y < oldPrevLocation.Y)
{
buttons[i + orderSign].Location = new Point(prevLocation.X - BTN_SHIFT, prevLocation.Y + BTN_SHIFT);
}
}
else
{//if white
if (curLocation.X > oldPrevLocation.X & curLocation.Y < oldPrevLocation.Y)
{
buttons[i + orderSign].Location = new Point(prevLocation.X + BTN_SHIFT, prevLocation.Y + BTN_SHIFT);
}
else if (curLocation.X > oldPrevLocation.X & curLocation.Y > oldPrevLocation.Y)
{
buttons[i + orderSign].Location = new Point(prevLocation.X - BTN_SHIFT, prevLocation.Y + BTN_SHIFT);
}
else if (curLocation.X < oldPrevLocation.X & curLocation.Y > oldPrevLocation.Y)
{
buttons[i + orderSign].Location = new Point(prevLocation.X - BTN_SHIFT, prevLocation.Y - BTN_SHIFT);
}
else if (curLocation.X < oldPrevLocation.X & curLocation.Y < oldPrevLocation.Y)
{
buttons[i + orderSign].Location = new Point(prevLocation.X + BTN_SHIFT, prevLocation.Y - BTN_SHIFT);
}
}
oldPrevLocation = curLocation;
i = inverseOrder ? i - 1 : i + 1;
}
if (targetCattle.cattlesAreCovered())
{
gameoverMsg();
subtractHandlers();
}
++points;
updateGroupBox();
}
}
}