Difference between revisions of "AS 2019 go back and select a different piece"
m (Admin moved page AS 2019 Re-Enter-Piece to AS 2019 go back and select a different piece without leaving a redirect: Better name) |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | =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: | ||
+ | |||
+ | <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; | ||
+ | while (!validPiece) | ||
+ | { | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | You should add the do part before the while: | ||
+ | |||
+ | <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 // new do for do..while loop | ||
+ | { | ||
+ | validPiece = false; // add this line also | ||
+ | while (!validPiece) | ||
+ | { | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | At the bottom of the method find the following code: | ||
+ | <syntaxhighlight lang=c#> | ||
+ | catch (Exception) | ||
+ | { | ||
+ | DisplayErrorCode(3); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | return index; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Add the following while before 'return index;': | ||
+ | |||
+ | <syntaxhighlight lang=c#> | ||
+ | catch (Exception) | ||
+ | { | ||
+ | DisplayErrorCode(3); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | while (!validPiece || !validMove); // while for end of the do | ||
+ | return index; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Finally remove the 'while (!validMove)' while loop: | ||
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
Line 8: | Line 69: | ||
int index = 0, chosenPieceIndex; | int index = 0, chosenPieceIndex; | ||
int newRow, newColumn; | int newRow, newColumn; | ||
− | do | + | do |
{ | { | ||
validPiece = false; | validPiece = false; | ||
Line 42: | Line 103: | ||
chosenPieceIndex = index; | chosenPieceIndex = index; | ||
validMove = false; | validMove = false; | ||
− | + | // while loop removed from here | |
+ | // and also a { removed from here | ||
Console.Write("Which row do you want to move to? "); | Console.Write("Which row do you want to move to? "); | ||
rowString = Console.ReadLine(); | rowString = Console.ReadLine(); | ||
Line 76: | Line 138: | ||
} | } | ||
− | while (!validPiece || !validMove); | + | while (!validPiece || !validMove); |
return index; | return index; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 16:25, 1 April 2019
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;
}