I really need some help
I must build 8-Puzzle program. I use Microsoft Visual Studio 2005 and use C# language to write a program. My program didn't have an error but it cannot work properly. It can't go to the Goal state. I use heuristic functions which are Manhatta Distance and Missing Place Valu by I check the Manhatta first and choose the minimum one,if the results are the same i'll check the Missing Place Value and also choose the minimum one. Could you help me to complete it. Thank you so much in advance.
This is my source below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Puzzle
{
public partial class Form1 : Form
{
private string[,] arrPuzzle = new string[3, 3] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "" }, };
private string[,] arrGoal = new string[3, 3] { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "" }, };
private int clickRow;
private int clickColumn;
private int count_step = 0;
private int count_runstep = 0;
private int p2x;
private int p2y;
public Form1()
{
InitializeComponent();
}
public Boolean check_blank()
{
//Check Left
if (clickColumn > 0 && arrPuzzle[clickRow, clickColumn - 1].Equals(""))
{
arrPuzzle[clickRow, clickColumn - 1] = arrPuzzle[clickRow, clickColumn];
arrPuzzle[clickRow, clickColumn] = "";
return true;
}
//Check Right
if (clickColumn < 2 && arrPuzzle[clickRow, clickColumn + 1].Equals(""))
{
arrPuzzle[clickRow, clickColumn + 1] = arrPuzzle[clickRow, clickColumn];
arrPuzzle[clickRow, clickColumn] = "";
return true;
}
//CheckUp
if (clickRow > 0 && arrPuzzle[clickRow - 1, clickColumn].Equals(""))
{
arrPuzzle[clickRow - 1, clickColumn] = arrPuzzle[clickRow, clickColumn];
arrPuzzle[clickRow, clickColumn] = "";
return true;
}
//CheckDown
if (clickRow < 2 && arrPuzzle[clickRow + 1, clickColumn].Equals(""))
{
arrPuzzle[clickRow + 1, clickColumn] = arrPuzzle[clickRow, clickColumn];
arrPuzzle[clickRow, clickColumn] = "";
return true;
}
else
return false;
}
private void restore()
{
button1.Text = arrPuzzle[0, 0];
button2.Text = arrPuzzle[0, 1];
button3.Text = arrPuzzle[0, 2];
button4.Text = arrPuzzle[1, 0];
button5.Text = arrPuzzle[1, 1];
button6.Text = arrPuzzle[1, 2];
button7.Text = arrPuzzle[2, 0];
button8.Text = arrPuzzle[2, 1];
button9.Text = arrPuzzle[2, 2];
}
private void reset()
{
arrPuzzle[0, 0] = "1";
arrPuzzle[0, 1] = "2";
arrPuzzle[0, 2] = "3";
arrPuzzle[1, 0] = "4";
arrPuzzle[1, 1] = "5";
arrPuzzle[1, 2] = "6";
arrPuzzle[2, 0] = "7";
arrPuzzle[2, 1] = "8";
arrPuzzle[2, 2] = "";
restore();
}
//private void chk_current()
//{
// int i = 0;
// int j = 0;
// for (i = 0; i <= 2; i++)
// {
// for (j = 0; j <= 2; j++)
// {
// if (arrPuzzle[i, j].Equals(""))
// {
// chk_distance(i, j);
// }
// }
// }
//}
private void cmp_goal(string a)
{
p2x = 0;
p2y = 0;
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
if (arrGoal[i, j].Equals(a))
{
p2x = i;
p2y = j;
}
}
}
}
private int missing_place()
{
int count_missing = 0;
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
if (!(arrPuzzle[i, j] == arrGoal[i, j]))
{
count_missing = count_missing + 1;
}
}
}
return count_missing;
}
//Manhattan Distance
private void chk_distance()
{
int dist = 0;
int dist2 = 0;
int dist3 = 0;
int dist4 = 0;
string temp = "";
string point1 = "";
string point2 = "";
string point3 = "";
string point4 = "";
int miss1 = 0;
int miss2 = 0;
int miss3 = 0;
int miss4 = 0;
int x = 0;
int y = 0;
int i = 0;
int j = 0;
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
{
if (arrPuzzle[i, j].Equals(""))
{
x = i;
y = j;
}
}
}
//array[0,0]
if (x == 0 && y == 0)
{
point1 = arrPuzzle[x, y + 1];
arrPuzzle[x, y] = point1;
temp = arrPuzzle[x, y];
point1 = "";
cmp_goal(temp);
dist = Math.Abs(x - p2x) + Math.Abs(y - p2y);
miss1 = missing_place();
point2 = arrPuzzle[x + 1, y];
arrPuzzle[x, y] = point2;
temp = arrPuzzle[x, y];
point2 = "";
cmp_goal(temp);
dist2 = Math.Abs(x - p2x) + Math.Abs(y - p2y);
miss2 = missing_place();
if (dist < dist2)
{
arrPuzzle[x, y + 1] = "";
arrPuzzle[x, y] = button2.Text;
restore();
}
else if (dist2 < dist)
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button4.Text;
restore();
}
else if (dist == dist2)
{
if (miss1 < miss2)
{
arrPuzzle[x, y + 1] = "";
arrPuzzle[x, y] = button2.Text;
restore();
}
else if (miss2 < miss1)
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button4.Text;
restore();
}
else
{
Random r = new Random();
int a;
a = r.Next(0, 2);
if (a == 0)
{
arrPuzzle[x, y + 1] = "";
arrPuzzle[x, y] = button2.Text;
restore();
}
else
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button4.Text;
restore();
}
}
}
}
//array[0,1]
else if (x == 0 && y == 1)
{
point1 = arrPuzzle[x, y - 1];
point2 = arrPuzzle[x + 1, y];
point3 = arrPuzzle[x, y + 1];
arrPuzzle[x, y] = point1;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist = Math.Abs(x - p2x) + Math.Abs((y - 1) - p2y);
miss1 = missing_place();
arrPuzzle[x, y] = point2;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist2 = Math.Abs((x + 1) - p2x) + Math.Abs(y - p2y);
miss2 = missing_place();
arrPuzzle[x, y] = point3;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist3 = Math.Abs(x - p2x) + Math.Abs((y + 1) - p2y);
miss3 = missing_place();
if (dist < dist2 && dist < dist3)
{
arrPuzzle[x, y - 1] = "";
arrPuzzle[x, y] = button1.Text;
restore();
}
else if (dist2 < dist && dist2 < dist3)
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button5.Text;
restore();
}
else if (dist3 < dist && dist3 < dist2)
{
arrPuzzle[x, y + 1] = "";
arrPuzzle[x, y] = button3.Text;
restore();
}
else
{
if (miss1 < miss2 && miss1 < miss3)
{
arrPuzzle[x, y - 1] = "";
arrPuzzle[x, y] = button1.Text;
restore();
}
else if (miss2 < miss1 && miss2 < miss3)
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button5.Text;
restore();
}
else if (miss3 < miss1 && miss3 < miss2)
{
arrPuzzle[x, y + 1] = "";
arrPuzzle[x, y] = button3.Text;
restore();
}
else
{
Random r = new Random();
int a;
a = r.Next(0, 3);
if (a == 0)
{
arrPuzzle[x, y - 1] = "";
arrPuzzle[x, y] = button1.Text;
restore();
}
else if (a == 1)
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button5.Text;
restore();
}
else
{
arrPuzzle[x, y + 1] = "";
arrPuzzle[x, y] = button3.Text;
restore();
}
}
}
}
//array[0,2]
else if (x == 0 && y == 2)
{
point1 = arrPuzzle[x, y - 1];
point2 = arrPuzzle[x + 1, y];
arrPuzzle[x, y] = point1;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist = Math.Abs(x - p2x) + Math.Abs((y - 1) - p2y);
miss1 = missing_place();
textBox2.Text = dist.ToString();
textBox3.Text = miss1.ToString();
arrPuzzle[x, y] = point2;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist2 = Math.Abs((x + 1) - p2x) + Math.Abs(y - p2y);
miss2 = missing_place();
if (dist < dist2)
{
arrPuzzle[x, y - 1] = "";
arrPuzzle[x, y] = button2.Text;
restore();
}
else if (dist2 < dist)
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button6.Text;
restore();
}
else
{
if (miss1 < miss2)
{
arrPuzzle[x, y - 1] = "";
arrPuzzle[x, y] = button2.Text;
restore();
}
else if (miss2 < miss1)
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button6.Text;
restore();
}
else
{
Random r = new Random();
int a;
a = r.Next(0, 2);
if (a == 0)
{
arrPuzzle[x, y - 1] = "";
arrPuzzle[x, y] = button2.Text;
restore();
}
else
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button6.Text;
restore();
}
}
}
}
//array[1,0]
else if (x == 1 && y == 0)
{
point1 = arrPuzzle[x - 1, y];
point2 = arrPuzzle[x, y + 1];
point3 = arrPuzzle[x + 1, y];
arrPuzzle[x, y] = point1;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist = Math.Abs((x - 1) - p2x) + Math.Abs(y - p2y);
miss1 = missing_place();
arrPuzzle[x, y] = point2;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist2 = Math.Abs(x - p2x) + Math.Abs((y + 1) - p2y);
miss2 = missing_place();
arrPuzzle[x, y] = point3;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist3 = Math.Abs((x + 1) - p2x) + Math.Abs(y - p2y);
miss3 = missing_place();
if (dist < dist2 && dist < dist3)
{
arrPuzzle[x - 1, y] = "";
arrPuzzle[x, y] = button1.Text;
restore();
}
else if (dist2 < dist && dist2 < dist3)
{
arrPuzzle[x, y + 1] = "";
arrPuzzle[x, y] = button5.Text;
restore();
}
else if (dist3 < dist && dist3 < dist2)
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button7.Text;
restore();
}
else
{
if (miss1 < miss2 && miss1 < miss3)
{
arrPuzzle[x - 1, y] = "";
arrPuzzle[x, y] = button1.Text;
restore();
}
else if (miss2 < miss1 && miss2 < miss3)
{
arrPuzzle[x, y + 1] = "";
arrPuzzle[x, y] = button5.Text;
restore();
}
else if (miss3 < miss1 && miss3 < miss2)
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button7.Text;
restore();
}
else
{
Random r = new Random();
int a;
a = r.Next(0, 3);
if (a == 0)
{
arrPuzzle[x - 1, y] = "";
arrPuzzle[x, y] = button1.Text;
restore();
}
else if (a == 1)
{
arrPuzzle[x, y + 1] = "";
arrPuzzle[x, y] = button5.Text;
restore();
}
else
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button7.Text;
restore();
}
}
}
}
//array[1,1]
else if (x == 1 && y == 1)
{
point1 = arrPuzzle[x - 1, y];
point2 = arrPuzzle[x, y - 1];
point3 = arrPuzzle[x, y + 1];
point4 = arrPuzzle[x + 1, y];
arrPuzzle[x, y] = point1;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist = Math.Abs((x - 1) - p2x) + Math.Abs(y - p2y);
miss1 = missing_place();
arrPuzzle[x, y] = point2;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist2 = Math.Abs(x - p2x) + Math.Abs((y - 1) - p2y);
miss2 = missing_place();
arrPuzzle[x, y] = point3;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist3 = Math.Abs(x - p2x) + Math.Abs((y + 1) - p2y);
miss3 = missing_place();
arrPuzzle[x, y] = point4;
temp = arrPuzzle[x, y];
cmp_goal(temp);
dist4 = Math.Abs((x + 1) - p2x) + Math.Abs(y - p2y);
miss4 = missing_place();
if (dist < dist2 && dist < dist3 && dist < dist4)
{
arrPuzzle[x - 1, y] = "";
arrPuzzle[x, y] = button2.Text;
restore();
}
else if (dist2 < dist && dist2 < dist3 && dist2 < dist4)
{
arrPuzzle[x, y - 1] = "";
arrPuzzle[x, y] = button4.Text;
restore();
}
else if (dist3 < dist && dist3 < dist2 && dist3 < dist4)
{
arrPuzzle[x, y + 1] = "";
arrPuzzle[x, y] = button6.Text;
restore();
}
else if (dist4 < dist && dist4 < dist2 && dist4 < dist3)
{
arrPuzzle[x + 1, y] = "";
arrPuzzle[x, y] = button8.Text;
restore();
}
else
{
if (miss1 < miss2 && miss1 < miss3 && miss1 < miss4)
{
arrPuzzle[x - 1, y] = "";
arrPuzzle[x, y] = button2.Text;
restore();
}
else if (miss2 < miss1 && miss2 < miss3 && miss2 < miss4)
&n
|