This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
/
ai.cs
182 lines (158 loc) · 3.9 KB
/
ai.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Squarification
{
public class ai
{
public static Form1 baseform;
#region ai
public static void performTurn(game g, gamepanel gp)
{
if (g.completeGame)
return;
Thread.Sleep(100);
Application.DoEvents();
//get list of possible moves
var possible = new List<area>();
bool sacrifice = false;
retry:
foreach (area a in g.lines)
{
if (a.isSet)
continue;
//push wins to the front and exit
int setedges = checkFilledBoxBlankEdges(a);
if (setedges == 4)
{
possible.Clear();
possible.Insert(0, a);
break;
}
//try not to sacrifice this square unless no other option, or no sac = push
if ((setedges == 3&& sacrifice) || setedges <= 2)
{
int n = 0;
if (possible.Count > 0)
{
var R = new Random();
n = R.Next()%possible.Count;
}
possible.Insert(n,a);
}
}
//if there are no possible moves, but we are still here, it means we have to sacrifice
if (possible.Count == 0)
{
//if sac already true, then error
if (sacrifice)
{
MessageBox.Show("Error in ai");
return;
}
sacrifice = true;
goto retry;
}
clickOnArea(possible[0], g, gp);
}
#endregion
#region player
private static bool checkEndGame(game g)
{
for (int y = 0; y < g.height; y++) for (int x = 0; x < g.width; x++)
{
area a = g.boxes[y][x].lineup;
if (a.isAreaUsedInGame && a.isSet == false)
return false;
a = g.boxes[y][x].linedown;
if (a.isAreaUsedInGame && a.isSet == false)
return false;
a = g.boxes[y][x].lineleft;
if (a.isAreaUsedInGame && a.isSet == false)
return false;
a = g.boxes[y][x].lineright;
if (a.isAreaUsedInGame && a.isSet == false)
return false;
}
return true;
}
public static void clickOnArea(area a, game g, gamepanel gp)
{
if (a.isAreaUsedInGame == false || a.isSet || a.parents[0].thisBox == a)
return;
a.isSet = true;
a.refpanel.BackColor = g.players[0].colour;
//check boxes
bool goodmove = false;
foreach (var v in a.parents)
{
if (v.thisBox.isAreaUsedInGame == false || v.thisBox.isSet)
continue;
bool val = checkAndSetFilledBox(v, g);
if (val)
goodmove = true;
}
//if the box gets set, that means it was a filled square- the player gets another turn
if (goodmove == false)
{
g.requeuePlayers();
gp.currentplayercolour.BackColor = g.players[0].colour;
}
else
{
gamecontroller.updateScoreTable(g, gp);
}
//check end game
if (checkEndGame(g))
{
gamecontroller.endGame(g,gp);
}
if (g.players[0].isAI)
{
Thread.Sleep(100);
performTurn(g, gp);
}
}
private static bool checkAndSetFilledBox(box b, game g)
{
//dont check is set so we can change later on if required via abilities or whatever
if (b.thisBox.isAreaUsedInGame == false)
return false;
if (b.lineup.isSet && b.lineleft.isSet &&
b.lineright.isSet && b.linedown.isSet)
{
b.thisBox.refpanel.BackColor = g.players[0].colour;
b.thisBox.isSet = true;
g.players[0].score++;
return true;
}
return false;
}
private static int checkFilledBoxBlankEdges(area proposedLine)
{
int setedgemax = 0;
int setedge = 0;
foreach (box b in proposedLine.parents)
{
if (b.thisBox.isAreaUsedInGame==false)
continue;
setedge = 0;
if (b.lineup.isSet || proposedLine == b.lineup)
setedge++;
if (b.linedown.isSet || proposedLine == b.linedown)
setedge++;
if (b.lineleft.isSet || proposedLine == b.lineleft)
setedge++;
if (b.lineright.isSet || proposedLine == b.lineright)
setedge++;
if (setedgemax == -1 || setedgemax < setedge)
setedgemax = setedge;
}
return setedgemax;
}
#endregion
}
}