Difference between revisions of "AS 2019 DameMovement"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(ListPossibleMoves)
(ListPossibleMoves)
 
(One intermediate revision by the same user not shown)
Line 24: Line 24:
 
                 direction = -1;
 
                 direction = -1;
 
             }
 
             }
             for (int i = 1; i < NumberOfPieces + 1; i++)
+
             for (int i = 1; i < NumberOfPieces + 1; i++) // look for this loop
 
             {
 
             {
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 37: Line 37:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Now, copy the following:
+
Now, copy the following (it is within the for loop you found above):
  
 
<syntaxhighlight lang=c#>
 
<syntaxhighlight lang=c#>

Latest revision as of 08:09, 2 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++) // look for this loop
            {

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 (it is within the for loop you found above):

                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;