chess-board/ChessPanel/Pieces/Rook.cs

142 lines
6.1 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 Rook : Piece
{
public Rook(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 Dictionary<Field, PieceTeam> GetValidTargetFields(Grid fields, MoveMode mode = MoveMode.Any, bool dontTestKingDanger = false)
//{
// var targetFields = GetTargetFields(fields, field, this.color);
// if (!dontTestKingDanger)
// {
// Field kinglocation = fields.GetKing(this.color).field;
// foreach (var item in targetFields)
// if (fields.IsFieldVirtuallyEndagered(kinglocation, (this.color == PieceColor.Black ? PieceColor.White : PieceColor.Black), this, item.Key))
// targetFields.Remove(item.Key);
// }
// return targetFields;
//}
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>();
for (int x = (int)field.location.X - 1; x >= 0; x--) // nach Links
{
if (fields[x, field.location.Y].IsEmpty)
{
targetFields.Add(fields[x, field.location.Y], PieceTeam.Ally);
}
else
{
if (fields[x, (int)field.location.Y].currentPiece.color != color)
targetFields.Add(fields[x, field.location.Y], PieceTeam.Enemy);
break;
}
}
for (int x = (int)field.location.X + 1; x <= 7; x++) // nach Rechts
{
if (fields[x, (int)field.location.Y].IsEmpty)
{
targetFields.Add(fields[x, field.location.Y], PieceTeam.Ally);
}
else
{
if (fields[x, (int)field.location.Y].currentPiece.color != color)
targetFields.Add(fields[x, field.location.Y], PieceTeam.Enemy);
break;
}
}
for (int y = (int)field.location.Y - 1; y >= 0; y--) // nach Unten
{
if (fields[(int)field.location.X, y].IsEmpty)
{
targetFields.Add(fields[field.location.X, y], PieceTeam.Ally);
}
else
{
if (fields[(int)field.location.X, y].currentPiece.color != color)
targetFields.Add(fields[field.location.X, y], PieceTeam.Enemy);
break;
}
}
for (int y = (int)field.location.Y + 1; y <= 7; y++) // nach Oben
{
if (fields[(int)field.location.X, y].IsEmpty)
{
targetFields.Add(fields[field.location.X, y], PieceTeam.Ally);
}
else
{
if (fields[(int)field.location.X, y].currentPiece.color != color)
targetFields.Add(fields[field.location.X, y], PieceTeam.Enemy);
break;
}
}
return targetFields;
}
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)
{
if (possibleEndageredField.location.X != thisField.location.X && possibleEndageredField.location.Y != thisField.location.Y) // Field can not be reached horizontally nor vertically
return false;
for (int x = (int)thisField.location.X - 1; x >= 0; x--) // nach Links
{
var possibleTargetField = fields[x, thisField.location.Y];
if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt))
return true;
if (breakIt)
break;
}
for (int x = (int)thisField.location.X + 1; x <= 7; x++) // nach Rechts
{
var possibleTargetField = fields[x, thisField.location.Y];
if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt))
return true;
if (breakIt)
break;
}
for (int y = (int)thisField.location.Y - 1; y >= 0; y--) // nach Oben
{
var possibleTargetField = fields[thisField.location.X, y];
if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt))
return true;
if (breakIt)
break;
}
for (int y = (int)thisField.location.Y + 1; y <= 7; y++) // nach Unten
{
var possibleTargetField = fields[thisField.location.X, y];
if (CheckFieldCanBeReached(possibleEndageredField, targetPos, movePiece, possibleTargetField, color, out bool breakIt))
return true;
if (breakIt)
break;
}
return false;
}
} // FINALIZED
}