Difference between revisions of "AS 2019 DameMovement"
(Created page with "=Issue= Dame pieces can be created and should move to the first row of the board, however if no space exists on this row the Dame will be left at the bottom row. This would m...") |
(→ListPossibleMoves) |
||
Line 134: | Line 134: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | You need to change the | + | You need to change the line: |
+ | <syntaxhighlight lang=c#> | ||
+ | newRow = currentRow + direction; | ||
+ | </syntaxhighlight> | ||
+ | to: | ||
+ | <syntaxhighlight lang=c#> | ||
+ | newRow = currentRow - direction; | ||
+ | </syntaxhighlight> |
Revision as of 21:09, 1 April 2019
Issue
Dame pieces can be created and should move to the first row of the board, however if no space exists on this row the Dame will be left at the bottom row. This would make it impossible to move, and would therefore be stuck. It therefore makes sense to make the Dame piece move a single diagonal square in any direction.
ListPossibleMoves
The ListPossibleMoves method will create a list of moves, we therefore need to add some code to this method. Find the for loop which cycles through each piece:
private static void ListPossibleMoves(string[,] board, int[,] playersPieces, string nextPlayer, MoveRecord[] listOfMoves)
{
int direction, numberOfMoves = 0; ;
int currentColumn, leftColumn, rightColumn;
int jumpLeftColumn, jumpRightColumn;
int currentRow, newRow, jumpRow;
string piece;
if (nextPlayer == "a")
{
direction = 1;
}
else
{
direction = -1;
}
for (int i = 1; i < NumberOfPieces + 1; i++)
{
Inside this for loop, there are 4 if statements which use ValidMove & ValidJump. Directly under these add a new if statement:
if (playersPieces[i, Dame] == 1)
{
}
Now, copy the following:
newRow = currentRow + direction;
leftColumn = currentColumn - 1;
rightColumn = currentColumn + 1;
if (ValidMove(board, newRow, leftColumn))
{
Console.WriteLine(piece + " can move to " + newRow + " , " + leftColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = newRow;
listOfMoves[numberOfMoves].NewColumn = leftColumn;
listOfMoves[numberOfMoves].CanJump = false;
}
if (ValidMove(board, newRow, rightColumn))
{
Console.WriteLine(piece + " can move to " + newRow + " , " + rightColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = newRow;
listOfMoves[numberOfMoves].NewColumn = rightColumn;
listOfMoves[numberOfMoves].CanJump = false;
}
jumpRow = currentRow + direction + direction;
jumpLeftColumn = currentColumn - 2;
jumpRightColumn = currentColumn + 2;
if (ValidJump(board, playersPieces, piece, jumpRow, jumpLeftColumn))
{
Console.WriteLine(piece + " can jump to " + jumpRow + " , " + jumpLeftColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = jumpRow;
listOfMoves[numberOfMoves].NewColumn = jumpLeftColumn;
listOfMoves[numberOfMoves].CanJump = true;
}
if (ValidJump(board, playersPieces, piece, jumpRow, jumpRightColumn))
{
Console.WriteLine(piece + " can jump to " + jumpRow + " , " + jumpRightColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = jumpRow;
listOfMoves[numberOfMoves].NewColumn = jumpRightColumn;
listOfMoves[numberOfMoves].CanJump = true;
}
Now paste it into the new if statement:
if (playersPieces[i, Dame] == 1)
{
newRow = currentRow - direction; //change to the minus sign
leftColumn = currentColumn - 1;
rightColumn = currentColumn + 1;
if (ValidMove(board, newRow, leftColumn))
{
Console.WriteLine(piece + " can move to " + newRow + " , " + leftColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = newRow;
listOfMoves[numberOfMoves].NewColumn = leftColumn;
listOfMoves[numberOfMoves].CanJump = false;
}
if (ValidMove(board, newRow, rightColumn))
{
Console.WriteLine(piece + " can move to " + newRow + " , " + rightColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = newRow;
listOfMoves[numberOfMoves].NewColumn = rightColumn;
listOfMoves[numberOfMoves].CanJump = false;
}
jumpRow = currentRow + direction + direction;
jumpLeftColumn = currentColumn - 2;
jumpRightColumn = currentColumn + 2;
if (ValidJump(board, playersPieces, piece, jumpRow, jumpLeftColumn))
{
Console.WriteLine(piece + " can jump to " + jumpRow + " , " + jumpLeftColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = jumpRow;
listOfMoves[numberOfMoves].NewColumn = jumpLeftColumn;
listOfMoves[numberOfMoves].CanJump = true;
}
if (ValidJump(board, playersPieces, piece, jumpRow, jumpRightColumn))
{
Console.WriteLine(piece + " can jump to " + jumpRow + " , " + jumpRightColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = jumpRow;
listOfMoves[numberOfMoves].NewColumn = jumpRightColumn;
listOfMoves[numberOfMoves].CanJump = true;
}
}
You need to change the line:
newRow = currentRow + direction;
to:
newRow = currentRow - direction;