input from user to know pattern of movement.
Good evening teacher. I was trying to get the program to tell me how the king moved. But i believe there is a better and effective way of writing the code. Please kindly take a look at it and trim it how possible or a better way of thought in writing it. Thanks.
{
string[] a = {"A","B","C","D","E","F","G","H"};
string pos;
int posI;
int posS = 1;
string pos2;
int pos2I;
int pos2S = 1;
string Step = "";
int steps = 0;
do
{
Console.WriteLine("Enter position of the king A-H 1-8: ");
pos = Console.ReadLine().ToUpper();
}
while (pos.Length != 2 ||
!(pos[0] >= 'A' && pos[0] <= 'H') ||
!(pos[1] >= '1' && pos[1] <= '8'));
{
Console.WriteLine("You entered {0}", pos);
posI = int.Parse(pos[1].ToString());
}
for (int i = 0; i < 8; i++)
{
if (a[i] == pos[0].ToString())
{
posS = i + 1;
}
}
do
{
Console.WriteLine("Enter position where the king need to be A-H 1-8: ");
pos2 = Console.ReadLine().ToUpper();
}
while (pos2.Length != 2 ||
!(pos2[0] >= 'A' && pos2[0] <= 'H') ||
!(pos2[1] >= '1' && pos2[1] <= '8'));
{
Console.WriteLine("You entered {0}", pos2);
pos2I = int.Parse(pos2[1].ToString());
}
for (int j = 0; j < 8; j++)
{
if (a[j] == pos2[0].ToString())
{
pos2S = j + 1;
}
}
while (steps >= 0)
{
// to show how many steps taken right up.
if (posS < pos2S & posI < pos2I)
{
steps += 1;
posS += 1;
posI += 1;
Step += ",RU";
}
// to show how many steps taken left up.
else if (posS > pos2S & posI < pos2I)
{
steps += 1;
posS -= 1;
posI += 1;
Step += ",LU";
}
// to show how many steps taken right down.
else if (posS < pos2S & posI > pos2I)
{
steps += 1;
posS += 1;
posI -= 1;
Step += ",RD";
}
// to show how many steps taken left down.
else if (posS > pos2S & posI > pos2I)
{
steps += 1;
posS -= 1;
posI -= 1;
Step += ",LD";
}
// to show how many steps taken up.
else if (posS == pos2S & posI < pos2I)
{
steps += 1;
posI += 1;
Step += ",U";
}
// to show how many steps taken down.
else if (posS == pos2S & posI > pos2I)
{
steps += 1;
posI -= 1;
Step += ",D";
}
else if (posS < pos2S & posI == pos2I)
{
steps += 1;
posS += 1;
Step += ",R";
}
else if (posS > pos2S & posI == pos2I)
{
steps += 1;
posS -= 1;
Step += ",L";
}
else if (posS == pos2S & posI == pos2I)
{
Step += ".";
Console.WriteLine("{0}{1}", steps, Step);
break;
}
else
{
Console.WriteLine("Error!");
break;
}
}
Console.ReadLine();