Skip to main content

That's a lot of code to look through. I'll focus on a section of it for the time being. In the BishopPiece class, all of the loops getting valid moves for a direction are similar. Each loop can be replaced with a method call, with parameters handling the differences. Here is the method:

private List<Tuple<int, int>> GetValidTurnsInADirection(
    Tuple<int, int> currentPosition, int xDirection, int yDirection)
{
    List<Tuple<int, int>> validMoves = new List<Tuple<int, int>>();
    Tuple<int, int> newPosition = new Tuple<int, int>(
            currentPosition.Item1 + xDirection, currentPosition.Item2 + yDirection);
    while (!IsOutOfBounds(newPosition) && !WillCollideWithAlly(newPosition, PieceColor))
    {
        validMoves.Add(rightUpDiagonalnewPosition);
        if (WillCollideWithEnemy(newPosition, PieceColor).Item1)
        {
            break;
        }
        newPosition = new Tuple<int, int>(
            newPosition.Item1 + xDirection, newPosition.Item2 + yDirection);
    }
}

GetValidTurns() can then be written like this:

protected override List<Tuple<int, int>> GetValidTurns()
{
    List<Tuple<int, int>> rightUpMoves = GetValidTurnsInADirection(CurrentPosition, 1, 1);
    List<Tuple<int, int>> leftUpMoves = GetValidTurnsInADirection(CurrentPosition, 1, -1);
    List<Tuple<int, int>> rightDownMoves = GetValidTurnsInADirection(CurrentPosition, -1, 1);
    List<Tuple<int, int>> leftDownMoves = GetValidTurnsInADirection(CurrentPosition, -1, -1);
    List<Tuple<int, int>> validMoves = rightUpMoves
        .Concat(leftUpMoves)
        .Concat(rightDownMoves)
        .Concat(leftDownMoves)
        .ToList();
    return validMoves;
}

You can also use that helper method for getting the valid turns for a rook.

That's a lot of code to look through. I'll focus on a section of it for the time being. In the BishopPiece class, all of the loops getting valid moves for a direction are similar. Each loop can be replaced with a method call, with parameters handling the differences. Here is the method:

private List<Tuple<int, int>> GetValidTurnsInADirection(
    Tuple<int, int> currentPosition, int xDirection, int yDirection)
{
    List<Tuple<int, int>> validMoves = new List<Tuple<int, int>>();
    Tuple<int, int> newPosition = new Tuple<int, int>(
            currentPosition.Item1 + xDirection, currentPosition.Item2 + yDirection);
    while (!IsOutOfBounds(newPosition) && !WillCollideWithAlly(newPosition, PieceColor))
    {
        validMoves.Add(rightUpDiagonal);
        if (WillCollideWithEnemy(newPosition, PieceColor).Item1)
        {
            break;
        }
        newPosition = new Tuple<int, int>(
            newPosition.Item1 + xDirection, newPosition.Item2 + yDirection);
    }
}

GetValidTurns() can then be written like this:

protected override List<Tuple<int, int>> GetValidTurns()
{
    List<Tuple<int, int>> rightUpMoves = GetValidTurnsInADirection(CurrentPosition, 1, 1);
    List<Tuple<int, int>> leftUpMoves = GetValidTurnsInADirection(CurrentPosition, 1, -1);
    List<Tuple<int, int>> rightDownMoves = GetValidTurnsInADirection(CurrentPosition, -1, 1);
    List<Tuple<int, int>> leftDownMoves = GetValidTurnsInADirection(CurrentPosition, -1, -1);
    List<Tuple<int, int>> validMoves = rightUpMoves
        .Concat(leftUpMoves)
        .Concat(rightDownMoves)
        .Concat(leftDownMoves)
        .ToList();
    return validMoves;
}

You can also use that helper method for getting the valid turns for a rook.

That's a lot of code to look through. I'll focus on a section of it for the time being. In the BishopPiece class, all of the loops getting valid moves for a direction are similar. Each loop can be replaced with a method call, with parameters handling the differences. Here is the method:

private List<Tuple<int, int>> GetValidTurnsInADirection(
    Tuple<int, int> currentPosition, int xDirection, int yDirection)
{
    List<Tuple<int, int>> validMoves = new List<Tuple<int, int>>();
    Tuple<int, int> newPosition = new Tuple<int, int>(
            currentPosition.Item1 + xDirection, currentPosition.Item2 + yDirection);
    while (!IsOutOfBounds(newPosition) && !WillCollideWithAlly(newPosition, PieceColor))
    {
        validMoves.Add(newPosition);
        if (WillCollideWithEnemy(newPosition, PieceColor).Item1)
        {
            break;
        }
        newPosition = new Tuple<int, int>(
            newPosition.Item1 + xDirection, newPosition.Item2 + yDirection);
    }
}

GetValidTurns() can then be written like this:

protected override List<Tuple<int, int>> GetValidTurns()
{
    List<Tuple<int, int>> rightUpMoves = GetValidTurnsInADirection(CurrentPosition, 1, 1);
    List<Tuple<int, int>> leftUpMoves = GetValidTurnsInADirection(CurrentPosition, 1, -1);
    List<Tuple<int, int>> rightDownMoves = GetValidTurnsInADirection(CurrentPosition, -1, 1);
    List<Tuple<int, int>> leftDownMoves = GetValidTurnsInADirection(CurrentPosition, -1, -1);
    List<Tuple<int, int>> validMoves = rightUpMoves
        .Concat(leftUpMoves)
        .Concat(rightDownMoves)
        .Concat(leftDownMoves)
        .ToList();
    return validMoves;
}

You can also use that helper method for getting the valid turns for a rook.

Source Link

That's a lot of code to look through. I'll focus on a section of it for the time being. In the BishopPiece class, all of the loops getting valid moves for a direction are similar. Each loop can be replaced with a method call, with parameters handling the differences. Here is the method:

private List<Tuple<int, int>> GetValidTurnsInADirection(
    Tuple<int, int> currentPosition, int xDirection, int yDirection)
{
    List<Tuple<int, int>> validMoves = new List<Tuple<int, int>>();
    Tuple<int, int> newPosition = new Tuple<int, int>(
            currentPosition.Item1 + xDirection, currentPosition.Item2 + yDirection);
    while (!IsOutOfBounds(newPosition) && !WillCollideWithAlly(newPosition, PieceColor))
    {
        validMoves.Add(rightUpDiagonal);
        if (WillCollideWithEnemy(newPosition, PieceColor).Item1)
        {
            break;
        }
        newPosition = new Tuple<int, int>(
            newPosition.Item1 + xDirection, newPosition.Item2 + yDirection);
    }
}

GetValidTurns() can then be written like this:

protected override List<Tuple<int, int>> GetValidTurns()
{
    List<Tuple<int, int>> rightUpMoves = GetValidTurnsInADirection(CurrentPosition, 1, 1);
    List<Tuple<int, int>> leftUpMoves = GetValidTurnsInADirection(CurrentPosition, 1, -1);
    List<Tuple<int, int>> rightDownMoves = GetValidTurnsInADirection(CurrentPosition, -1, 1);
    List<Tuple<int, int>> leftDownMoves = GetValidTurnsInADirection(CurrentPosition, -1, -1);
    List<Tuple<int, int>> validMoves = rightUpMoves
        .Concat(leftUpMoves)
        .Concat(rightDownMoves)
        .Concat(leftDownMoves)
        .ToList();
    return validMoves;
}

You can also use that helper method for getting the valid turns for a rook.