AS 2019 go back and select a different piece

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Issue

At the moment once a piece is selected you can't undo and select another piece.

Select move has 2 while loops, one to choose a valid piece and one to choose a valid move. So once a piece is selected it can't go back to change your mind, the program will need then to enter a valid move before the game can continue.

Method

It will be straight forward to add a do..while loop around the current code. So find this:

 
private static int SelectMove(MoveRecord[] listOfMoves)
        {
            bool validPiece = false, validMove, found, endOfList;
            string piece = "", rowString, columnString;
            int index = 0, chosenPieceIndex;
            int newRow, newColumn;
            while (!validPiece)
            {

You should add the do part before the while:

 
private static int SelectMove(MoveRecord[] listOfMoves)
        {
            bool validPiece = false, validMove, found, endOfList;
            string piece = "", rowString, columnString;
            int index = 0, chosenPieceIndex;
            int newRow, newColumn;
            do // new do for do..while loop 
            {
                validPiece = false; // add this line also
                while (!validPiece)
                {

At the bottom of the method find the following code:

 
                    catch (Exception)
                    {
                        DisplayErrorCode(3);
                    }
                
            }
            return index;
        }

Add the following while before 'return index;':

 
                    catch (Exception)
                    {
                        DisplayErrorCode(3);
                    }
                
            }
            while (!validPiece || !validMove); // while for end of the do
            return index;
        }

Finally remove the 'while (!validMove)' while loop:

 
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;
                // while loop removed from here
                // and also a { removed from here
                    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;
        }