AS 2019 TakePiece
Issues
I have completed this improvement, and there are many sections to make this work.I think it is unlikely to be asked to do this, but you need to:
- Add to the MoveRecord Struct (CanTake boolean, TakeRow & TakeColumn)
- Duplicate ValidJump and create ValidTake
- Add loads to ListPossibleMoves
- Changes to MakeMove
Changes to Struct
Find the current MoveRecord struct:
struct MoveRecord
{
public string Piece;
public int NewRow;
public int NewColumn;
public bool CanJump;
}
Add the following variables to get:
struct MoveRecord
{
public string Piece;
public int NewRow;
public int NewColumn;
public bool CanJump;
public bool CanTake;
public int TakeRow;
public int TakeColumn;
}
ValidTake
Create the following method by duplicating valid jump, then change to this:
private static bool ValidTake(string[,] board, int[,] playersPieces, string piece, int newRow, int newColumn)
{
bool valid = false;
string middlePiece = "";
string player, oppositePiecePlayer, middlePiecePlayer;
int index, currentRow, currentColumn, middlePieceRow, middlePieceColumn;
player = piece[0].ToString().ToLower();
index = Convert.ToInt32(piece.Substring(1));
if (player == "a")
{
oppositePiecePlayer = "b";
}
else
{
oppositePiecePlayer = "a";
}
if (newRow >= 0 && newRow < BoardSize &&
newColumn >= 0 && newColumn < BoardSize)
{
if (board[newRow, newColumn] == Space)
{
currentRow = playersPieces[index, Row];
currentColumn = playersPieces[index, Column];
middlePieceRow = (currentRow + newRow) / 2;
middlePieceColumn = (currentColumn + newColumn) / 2;
middlePiece = board[middlePieceRow, middlePieceColumn];
middlePiecePlayer = middlePiece[0].ToString().ToLower();
if (middlePiecePlayer == oppositePiecePlayer && middlePiecePlayer != " ") //change this
{
valid = true;
}
}
}
return valid;
}
ListPossibleMoves
The for loop cycles through each piece, you will see inside this loop are several if statements. The last 2 check if any valid jumps are possible. So find this code:
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;
}
Copy these two if statements and paste directly underneath, and change ValidJump to ValidTake:
if (ValidTake(board, playersPieces, piece, jumpRow, jumpLeftColumn))
{
Console.WriteLine(piece + " can take piece and go to " + jumpRow + " , " + jumpLeftColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = jumpRow;
listOfMoves[numberOfMoves].NewColumn = jumpLeftColumn;
listOfMoves[numberOfMoves].CanJump = true;
listOfMoves[numberOfMoves].CanTake = true;
listOfMoves[numberOfMoves].TakeRow = currentRow + direction;
listOfMoves[numberOfMoves].TakeColumn = jumpLeftColumn + 1;
}
if (ValidTake(board, playersPieces, piece, jumpRow, jumpRightColumn))
{
Console.WriteLine(piece + " can take piece and go to " + jumpRow + " , " + jumpRightColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = jumpRow;
listOfMoves[numberOfMoves].NewColumn = jumpRightColumn;
listOfMoves[numberOfMoves].CanJump = true;
listOfMoves[numberOfMoves].CanTake = true;
listOfMoves[numberOfMoves].TakeRow = currentRow + direction;
listOfMoves[numberOfMoves].TakeColumn = jumpRightColumn - 1;
}
The code above will also set the CanTake, TakeRow, and TakeColumn if a take is possible.
Finally, we need to not list any moves for a taken piece. So again in ListPossibleMoves, the if statements to check ValidMove, ValidJump, and Valid take can be wrapped in an if statement to check if the Row & Column are set to -1:
if (currentRow != -1 && currentColumn!=-1)
{ piece = nextPlayer + i;
currentRow = playersPieces[i, Row];
currentColumn = playersPieces[i, Column];
if (playersPieces[i, Dame] == 1)
{
piece = piece.ToUpper();
}
newRow = currentRow + direction;
leftColumn = currentColumn - 1;
rightColumn = currentColumn + 1;
if (currentRow != -1 && 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;
}
if (ValidTake(board, playersPieces, piece, jumpRow, jumpLeftColumn))
{
Console.WriteLine(piece + " can take piece and go to " + jumpRow + " , " + jumpLeftColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = jumpRow;
listOfMoves[numberOfMoves].NewColumn = jumpLeftColumn;
listOfMoves[numberOfMoves].CanJump = true;
listOfMoves[numberOfMoves].CanTake = true;
listOfMoves[numberOfMoves].TakeRow = currentRow + direction;
listOfMoves[numberOfMoves].TakeColumn = jumpLeftColumn + 1;
}
if (ValidTake(board, playersPieces, piece, jumpRow, jumpRightColumn))
{
Console.WriteLine(piece + " can take piece and go to " + jumpRow + " , " + jumpRightColumn);
numberOfMoves++;
listOfMoves[numberOfMoves].Piece = piece;
listOfMoves[numberOfMoves].NewRow = jumpRow;
listOfMoves[numberOfMoves].NewColumn = jumpRightColumn;
listOfMoves[numberOfMoves].CanJump = true;
listOfMoves[numberOfMoves].CanTake = true;
listOfMoves[numberOfMoves].TakeRow = currentRow + direction;
listOfMoves[numberOfMoves].TakeColumn = jumpRightColumn - 1;
}
Make Move
Change the if statement:
if (jumping)
{
middlePieceRow = (currentRow + newRow) / 2;
middlePieceColumn = (currentColumn + newColumn) / 2;
middlePiece = board[middlePieceRow, middlePieceColumn];
Console.WriteLine("jumped over " + middlePiece);
}
to this:
if (taking)
{
middlePieceRow = (currentRow + newRow) / 2;
middlePieceColumn = (currentColumn + newColumn) / 2;
middlePiece = board[middlePieceRow, middlePieceColumn];
board[middlePieceRow, middlePieceColumn]=" ";
opponentsPieces[Convert.ToInt32(middlePiece.Substring(1)), Row] = -1;
opponentsPieces[Convert.ToInt32(middlePiece.Substring(1)), Column] = -1;
Console.WriteLine("Taken " + middlePiece);
}
else if (jumping)
{
middlePieceRow = (currentRow + newRow) / 2;
middlePieceColumn = (currentColumn + newColumn) / 2;
middlePiece = board[middlePieceRow, middlePieceColumn];
Console.WriteLine("jumped over " + middlePiece);
}