-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8Puzzle_Ai.cs
277 lines (238 loc) · 7.54 KB
/
8Puzzle_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Rania Salem Aloun 115571
class node
{
public static int generatedStates = 0;
public static int expandedStates = 0;
public int[] state = new int[9];
int[] goal = new int[9] { 0,1,2,3,3,4,4,4,8 };
public node[] children = new node[4];
public node parent;
//public int space = 0; //zero
public node(int[] State)
{
for (int i = 0; i < 9; i++)
{
this.state[i] = State[i];
}
}
public void printPuzzle()
{
int index = searchForZeroLocation();
char[] tempArr = new char[9];
for (int i = 0; i < 9; i++)
{
tempArr[i] = Convert.ToChar(state[i] + '0');
}
tempArr[index] = ' ';
Console.WriteLine(tempArr[0] + " " + tempArr[1] + " " + tempArr[2]);
Console.WriteLine(tempArr[3] + " " + tempArr[4] + " " + tempArr[5]);
Console.WriteLine(tempArr[6] + " " + tempArr[7] + " " + tempArr[8]);
Console.WriteLine("");
}
public bool goalTest()
{
int flag = 0; //flag to find if the test fails
for (int i = 0; i < 9; i++)
{
if (this.state[i] != this.goal[i])
{
flag = 1;
break;
}
}
if (flag == 0) //all are equal
{
Console.WriteLine("Puzzle is solved!");
return true;
}
else
{
return false;
}
}
public int searchForZeroLocation()
{
int location = 10;
int target = 0;
for (int i = 0; i < 9; i++)
{
if (state[i] == target)
{
location = i;
}
}
return location;
}
public void Up()
{
int[] arrayUp = new int[9];
for (int i = 0; i < 9; i++)
{
arrayUp[i] = state[i];
}
int index = searchForZeroLocation();
arrayUp[index] = arrayUp[index - 3];
arrayUp[index - 3] = 0;
children[0] = new node(arrayUp);
children[0].parent = this;
}
public void down()
{
int[] arrayDown = new int[9];
for (int i = 0; i < 9; i++)
{
arrayDown[i] = state[i];
}
int index = searchForZeroLocation();
arrayDown[index] = arrayDown[index + 3];
arrayDown[index + 3] = 0;
children[1] = new node(arrayDown);
children[1].parent = this;
}
public void Right()
{
int[] arrayRight = new int[9];
for (int i = 0; i < 9; i++)
{
arrayRight[i] = state[i];
}
int index = searchForZeroLocation();
arrayRight[index] = arrayRight[index + 1];
arrayRight[index + 1] = 0;
children[2] = new node(arrayRight);
children[2].parent = this;
}
public void Left()
{
int[] arrayLeft = new int[9];
for (int i = 0; i < 9; i++)
{
arrayLeft[i] = state[i];
}
int index = searchForZeroLocation();
arrayLeft[index] = arrayLeft[index - 1];
arrayLeft[index - 1] = 0;
children[3] = new node(arrayLeft);
children[3].parent = this;
}
public void Expand()
{
expandedStates++;
int location = searchForZeroLocation();
if (location != 0 && location != 1 && location != 2)
{
Up();
generatedStates++;
}
if (location != 6 && location != 7 && location != 8)
{
down();
generatedStates++;
}
if (location != 2 && location != 5 && location != 8)
{
Right();
generatedStates++;
}
if (location != 0 && location != 3 && location != 6)
{
Left();
generatedStates++;
}
}
}
namespace Ai
{
class Program
{
static bool duplication(List<node> x, int[] arr)
{
for(int i=0; i<x.Count; i++)
{
if (x[i].state.SequenceEqual(arr))
return true;
}
return false;
}
static void Main(string[] args)
{
int[] arr = new int[9] { 1, 0, 2, 3, 4, 5, 6, 7, 8};
Console.WriteLine("Welcome to the 8-Puzzle AI solver!");
Console.Write("Enter initial state of puzzle: ");
string inititial_State = Console.ReadLine();
if (inititial_State.Length != 9)
{
Console.WriteLine("Error, missing values");
}
else
{
for (int i = 0; i < 9; i++)
{
arr[i] = (int)Char.GetNumericValue(inititial_State[i]);
}
node Leaf = new node(arr);
Leaf.printPuzzle();
Leaf.goalTest();
List<node> listOfObjects = new List<node>();
listOfObjects.Add(Leaf);
int flag = 0; //to catch the goal
node ListPointer;
int count = 0;
node goalPointer = new node(arr);
while (flag == 0)
{
ListPointer = listOfObjects[count]; //leaf at begining
ListPointer.Expand();
for (int i = 0; i < 4; i++)
{
if (ListPointer.children[i] != null)
{
if (ListPointer.children[i].goalTest())
{
flag = 1;
goalPointer = ListPointer.children[i];
break;
}
else
{
if (duplication(listOfObjects, ListPointer.children[i].state) == false)
{
listOfObjects.Add(ListPointer.children[i]);
}
}
}
}
count++;
}
int flag2 = 0;
List<node> goalList = new List<node>();
while (flag2==0)
{
goalList.Add(goalPointer);
goalPointer = goalPointer.parent;
if (goalPointer.parent == null)
{
flag2 = 1;
//goalPointer.printPuzzle();
break;
}
}
Console.WriteLine("Path to Solution:");
for (int i=goalList.Count-1; i>=0; i--)
{
goalList[i].printPuzzle();
}
Console.WriteLine("Number of generated states = " + node.generatedStates);
Console.WriteLine("Number of expanded states = " + node.expandedStates);
string end;
Console.WriteLine("Press enter to close...");
Console.ReadLine();
}
}
}
}