chess-board/ChessPanel/Pieces/King.cs

132 lines
6.2 KiB
C#

using Chess;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows;
namespace ChessPanel
{
public class King : Piece
{
public King(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()}");
}
internal override bool EndageresFieldWithVirtualMove(Grid fields, Field field, Piece movePiece, Field targetPos)
{
//#region Movement: 1 in every direction
//for (int x = -1; x < 2; x++)
//{
// for (int y = -1; y < 2; y++)
// {
// if ((x == 0 && y == 0) || field.location.X + x > 7 || field.location.Y + y > 7 || field.location.X + x < 0 || field.location.Y + y < 0)
// continue;
// if (!fields.IsFieldVirtuallyEndagered(fields[field.location, x, y], (this.color == PieceColor.Black ? PieceColor.White : PieceColor.Black), this, fields[field.location, x, y]))
// if ((fields[field.location, x, y].IsEmpty && fields[field.location, x, y] != targetPos) || fields[field.location, x, y] == movePiece.field)
// {
// if (fields[field.location, x, y] == field)
// return true;
// continue;
// }
// else
// {
// if (fields[field.location, x, y].currentPiece.color != color)
// if (fields[field.location, x, y] == field)
// return true;
// break;
// }
// }
//}
//#endregion
return false;
}
internal override Dictionary<Field, PieceTeam> GetValidTargetFields(Grid fields, MoveMode mode = MoveMode.Normal, bool dontTestKingDanger = false)
{
var targetFields = GetTargetFields(fields, field, this.color);
if (!dontTestKingDanger)
{
List<Field> remove = new List<Field>();
foreach (var virtualKinglocation in targetFields.Keys)
{
if (fields.IsFieldVirtuallyEndagered(virtualKinglocation, (this.color == PieceColor.Black ? PieceColor.White : PieceColor.Black), this, virtualKinglocation))
remove.Add(virtualKinglocation);
}
foreach (var item in remove)
{
targetFields.Remove(item);
}
}
return targetFields;
}
internal override Dictionary<Field, PieceTeam> GetTargetFields(Grid fields, Field field, PieceColor color)
{
Dictionary<Field, PieceTeam> targetFields = new Dictionary<Field, PieceTeam>();
#region Castling
if (!this.HasMoved && false) // Only Castle if King did not move
foreach (Field item in fields)
{
var itemLocation = item.location;
// Sucht nach Ally Turm und prüft, ob er sich schon bewegt hat
if (item.currentPiece != null && item.currentPiece.type == PieceType.Rook && !item.currentPiece.HasMoved && item.currentPiece.color == this.color)
{
if (itemLocation.X == 0 || itemLocation.X == 7)
{
bool castling = true;
for (int x = (itemLocation.X == 0 ? 1 : 5); (itemLocation.X == 0 ? x < 4 : x < 7); x++)
{
if (!fields[x, (int)field.location.Y].IsEmpty)
{
castling = false;
break;
}
}
if (castling) // Only Check endangered Fields if castling is generally possible
{
for (int x = (itemLocation.X == 0 ? 2 : 4); x < (itemLocation.X == 0 ? 5 : 7); x++) // König steht im Schach oder überschreitet bedrohtes Feld
{
if (fields.IsFieldVirtuallyEndagered(fields[x, field.location.Y], (this.color == PieceColor.Black ? PieceColor.White : PieceColor.Black), this, fields[x, field.location.Y]))
{
castling = false;
break;
}
}
if (castling)
{
targetFields.Add(fields[(itemLocation.X == 0 ? 2 : 6), itemLocation.Y], PieceTeam.Ally);
}
}
}
}
}
#endregion
#region Movement: 1 in every direction
for (int x = -1; x < 2; x++)
{
for (int y = -1; y < 2; y++)
{
if ((x == 0 && y == 0) || field.location.X + x > 7 || field.location.Y + y > 7 || field.location.X + x < 0 || field.location.Y + y < 0)
continue;
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);
continue;
}
}
}
#endregion
return targetFields;
}
} // CHECKMATE/CHECK-TEST MISSING
}