Difference between revisions of "AS 2019 go back and select a different piece"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "Find the SelectMove procedure and use a Do While loop with the code private static int SelectMove(MoveRecord[] listOfMoves) { bool validPiece = false, va...")
 
Line 1: Line 1:
 
Find the SelectMove procedure and use a Do While loop with the code
 
Find the SelectMove procedure and use a Do While loop with the code
  
private static int SelectMove(MoveRecord[] listOfMoves)
+
<syntaxhighlight lang=c#>
 +
private static int SelectMove(MoveRecord[] listOfMoves)
 
         {
 
         {
 
             bool validPiece = false, validMove, found, endOfList;
 
             bool validPiece = false, validMove, found, endOfList;

Revision as of 15:49, 1 April 2019

Find the SelectMove procedure and use a Do While loop with the code

<syntaxhighlight lang=c#> private static int SelectMove(MoveRecord[] listOfMoves)

       {
           bool validPiece = false, validMove, found, endOfList;
           string piece = "", rowString, columnString;
           int index = 0, chosenPieceIndex;
           int newRow, newColumn;
           do
           {
               validPiece = false;
               while (!validPiece)
               {
                   found = false;
                   endOfList = false;
                   Console.Write("Which piece do you want to move? ");
                   piece = Console.ReadLine();
                   index = 0;
                   if (piece == "")
                   {
                       endOfList = true;
                   }
                   while (!found && !endOfList)
                   {
                       index++;
                       if (listOfMoves[index].Piece == piece)
                       {
                           found = true;
                       }
                       else if (listOfMoves[index].Piece == "")
                       {
                           endOfList = true;
                           DisplayErrorCode(1);
                       }
                   }
                   if (found)
                   {
                       validPiece = true;
                   }
               }
               chosenPieceIndex = index;
               validMove = false;
                   Console.Write("Which row do you want to move to? ");
                   rowString = Console.ReadLine();
                   Console.Write("Which column do you want to move to? ");
                   columnString = Console.ReadLine();
                   try
                   {
                       newRow = Convert.ToInt32(rowString);
                       newColumn = Convert.ToInt32(columnString);
                       found = false;
                       endOfList = false;
                       index = chosenPieceIndex - 1;
                       while (!found && !endOfList)
                       {
                           index++;
                           if (listOfMoves[index].Piece != piece)
                           {
                               endOfList = true;
                               DisplayErrorCode(2);
                           }
                           else if (listOfMoves[index].NewRow == newRow &&
                               listOfMoves[index].NewColumn == newColumn)
                           {
                               found = true;
                           }
                       }
                       validMove = found;
                   }
                   catch (Exception)
                   {
                       DisplayErrorCode(3);
                   }
               
           }
           while (!validPiece || !validMove);
           return index;
       }