chess-board/ChessPanel/Pieces/Bishop.cs

144 lines
5.8 KiB
C#
Raw Normal View History

2017-11-16 18:06:29 +01:00
using Chess;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows;
namespace ChessPanel
{
public class Bishop : Piece
{
public Bishop(PieceColor color, Vector position, Grid fields) : base(position, fields)
{
type = (PieceType)Enum.Parse(typeof(PieceType), this.GetType().Name);
this.color = color;
this.image = (Image)ChessPanel.Properties.Resources.ResourceManager.GetObject($"{this.type.ToString()}{this.color.ToString()}");
}
private static bool AddFieldToTargetFields(Grid fields, Field field, int x, int y, Dictionary<Field, PieceTeam> targetFields, PieceColor color)
{
if (fields[field.location, x, y].IsEmpty)
{
targetFields.Add(fields[field.location, x, y], PieceTeam.Ally);
}
else
{
if (fields[field.location, x, y].CurrentPiece?.color != color)
targetFields.Add(fields[field.location, x, y], PieceTeam.Enemy);
return true;
}
return false;
}
internal override Dictionary<Field, PieceTeam> GetTargetFields(Grid fields, Field field, PieceColor color)
{
return StaticGetTargetFields(fields, field, color);
}
internal static Dictionary<Field, PieceTeam> StaticGetTargetFields(Grid fields, Field field, PieceColor color)
{
Dictionary<Field, PieceTeam> targetFields = new Dictionary<Field, PieceTeam>();
#region nach RechtsOben
for (int xy = 1; xy <= 7; xy++)
{
if (CheckOutOfBounds(field, xy, xy) || AddFieldToTargetFields(fields, field, xy, xy, targetFields, color))
break;
}
#endregion
// LinksOben
for (int xy = 1; xy <= 7; xy++)
{
if (CheckOutOfBounds(field, -xy, xy) || AddFieldToTargetFields(fields, field, -xy, xy, targetFields, color))
break;
}
// RechtsUnten
for (int xy = 1; xy <= 7; xy++)
{
if (CheckOutOfBounds(field, xy, -xy) || AddFieldToTargetFields(fields, field, xy, -xy, targetFields, color))
break;
}
// LinksUnten
for (int xy = 1; xy <= 7; xy++)
{
if (CheckOutOfBounds(field, -xy, -xy) || AddFieldToTargetFields(fields, field, -xy, -xy, targetFields, color))
break;
}
return targetFields;
}
private static bool CheckOutOfBounds(Field field, int x, int y)
{
return field.location.X + x > 7 || (int)field.location.Y + y > 7
|| field.location.X + x < 0 || (int)field.location.Y + y < 0;
}
/// <summary>
///
/// </summary>
/// <param name="fields">Global Field Grid</param>
/// <param name="field">Field that should be checked for endager</param>
/// <param name="movePiece">Piece that should be moved</param>
/// <param name="targetPos">Target of the moved piece</param>
/// <returns></returns>
internal override bool EndageresFieldWithVirtualMove(Grid fields, Field possibleEndageredField, Piece movePiece, Field targetPos)
{
return StaticEndageresFieldWithVirtualMove(fields, this.field, possibleEndageredField, movePiece, targetPos, color);
}
internal static bool StaticEndageresFieldWithVirtualMove(Grid fields, Field thisField, Field possibleEndageredField, Piece movePiece, Field targetPos, PieceColor color)
{
var skipField = movePiece.field;
for (int xy = 1; xy <= 7; xy++) // nach RechtsOben
{
if (thisField.location.X + xy > 7 || thisField.location.Y + xy > 7)
break;
var possibleTargetField = fields.GetFieldWithOffset(thisField, xy, xy);
if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt))
return true;
if (breakIt)
break;
}
for (int xy = 1; xy <= 7; xy++)
{
if (thisField.location.X - xy < 0 || thisField.location.Y + xy > 7) // LinksOben
break;
var possibleTargetField = fields.GetFieldWithOffset(thisField, -xy, xy);
if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt))
return true;
if (breakIt)
break;
}
for (int xy = 1; xy <= 7; xy++)
{
if (thisField.location.X + xy > 7 || thisField.location.Y - xy < 0) // RechtsUnten
break;
var possibleTargetField = fields.GetFieldWithOffset(thisField, xy, -xy);
if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt))
return true;
if (breakIt)
break;
}
for (int xy = 1; xy <= 7; xy++)
{
if (thisField.location.X - xy < 0 || thisField.location.Y - xy < 0) // LinksUnten
break;
var possibleTargetField = fields.GetFieldWithOffset(thisField, -xy, -xy);
if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt))
return true;
if (breakIt)
break;
}
return false;
}
} // FINALIZED
}