Added initial code commit

This commit is contained in:
2017-11-16 18:06:29 +01:00
parent 368cb29eaf
commit dd63e0adf5
19 changed files with 1762 additions and 695 deletions

View File

@ -0,0 +1,232 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using ChessPanel;
using System.Linq;
using System.Windows;
namespace Chess
{
public enum PieceColor { White, Black };
public enum PieceTeam { Enemy, Ally };
public enum PieceType { King, Queen, Bishop, Rook, Knight, Pawn };
public enum MoveMode { Normal, Attack, Movement };
/// <summary>
/// Figur
/// </summary>
public abstract class Piece
{
public PieceColor color;
public PieceType type;
public Dictionary<Field, PieceTeam> validTargetSpots;
public Image image;
public bool HasMoved = false;
public Field field;
public bool EnPassantPossible = false; // Only in Pawn Class. Signs that this Piece has virtually not jumped two pieces, but one, reflective meaning: This.Figure can be attacked using Enpassant if this.type is Pawn
public override string ToString()
{
return type.ToString();
}
public Piece(Vector position, Grid fields)
{
this.field = fields[position];
fields[position].CurrentPiece = this;
}
public Dictionary<Field, PieceTeam> RefreshValidTargetSpots(Grid fields, MoveMode mode = MoveMode.Normal)
{
this.validTargetSpots = this.GetValidTargetFields(fields, mode);
return validTargetSpots;
}
internal void RemoveTargetFieldsWhereKingIsEndangered(Grid fields, Dictionary<Field, PieceTeam> targetFields)
{
Field ownKingLocation = fields.GetKing(this.color).field;
foreach (var virtualTargetField in targetFields)
{
if (fields.IsFieldVirtuallyEndagered(ownKingLocation, (this.color == PieceColor.Black ? PieceColor.White : PieceColor.Black), this, virtualTargetField.Key))
targetFields.Remove(virtualTargetField.Key);
}
}
/// <summary>
/// Tests if the Move will leave your King endangered.
/// </summary>
/// <param name="fields">GameField</param>
/// <param name="pieceLocation">Location of Piece to move</param>
/// <param name="simulatedLocation">Location to move Piece to</param>
/// <param name="color">Color of Piece to move</param>
/// <returns>Bool</returns>
/* internal static bool MoveWillEndangerOwnKing(Grid fields, Vector pieceLocation, Vector simulatedLocation, PieceColor color)
{
// fields[simulateLocation].currentPiece = fields[pieceLocation].currentPiece;
// fields[pieceLocation].currentPiece = null;
// Check if own king is endangered
return FieldIsEndangered(fields, simulatedLocation, kingLocation, color, dontTestKingDanger: true);
}*/
/// <summary>
/// Checks if a specific field can be reached by the figure while a virtual move is simulated
/// </summary>
/// <param name="movePiece">Piece that virtually got moved</param>
/// <param name="targetPos">Field that the movePiece virtually moved to</param>
/// <param name="possibleEndageredField">Field that should be checked</param>
/// <param name="fields">Current Game Grid</param>
/// <returns></returns>
internal abstract bool EndageresFieldWithVirtualMove(Grid fields, Field possibleEndageredField, Piece movePiece, Field targetPos);
internal void Move(Field newPieceField)
{
this.field.CurrentPiece = null; // Remove piece from previous position
this.field = newPieceField; // Set pieces location to new field
newPieceField.CurrentPiece = this; // Set new fields piece to this
}
/// <summary>
/// Tests if the Move will leave your movedPiece endangered.
/// </summary>
/// <param name="fields">GameField</param>
/// <param name="pieceLocation">Location of Piece to move</param>
/// <param name="testLocation">Location to move Piece to</param>
/// <param name="color">Color of Piece to move</param>
/// <returns>Bool</returns>
//internal static bool MoveWillEndangerPiece(Grid fields, Vector pieceLocation, Vector testLocation, PieceColor color)
//{
// return false;
// //Grid simulatedField = fields;
// //simulatedField[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = null;
// //return FieldIsEndangered(fields simulatedField, testLocation, color);
//}
/// <summary>
/// Tests if your King is currently endangered.
/// </summary>
/// <param name="fields">GameField</param>
/// <param name="color">Kings color</param>
/// <returns></returns>
//private static bool KingIsEndangered(Grid fields, PieceColor color) => FieldIsEndangered(fields, GetKingLocation(fields, color), color, dontTestKingDanger: true);
/// <summary>
/// Tests if the testLocation is endangered by an enemy.
/// </summary>
/// <param name="fields">GameField</param>
/// <param name="color">Ally Color</param>
/// <param name="testLocation">Location to test</param>
/// <returns>Bool</returns>
//private static bool FieldIsEndangered(Grid fields, Vector simulatedLocation, Vector testLocation, PieceColor color, bool dontTestKingDanger = false)
//{
// foreach (Field field in fields)
// {
// if (field.location != simulatedLocation
// && field.currentPiece != null
// && field.currentPiece.color != color
// && field.currentPiece.GetValidTargetFields(fields, dontTestKingDanger: dontTestKingDanger).ContainsKey(testLocation))
// return true;
// }
// return false;
//}
/*internal static bool IsEndangeredLocationForPiece(Grid fields, Vector pieceLocation, Vector testLocation, PieceColor color)
{
bool returnValue = false;
Piece p = fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece;
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = null;
foreach (var field in fields)
{
if (field.currentPiece != null) if (field.currentPiece.color != color) if (field.currentPiece.GetValidTargetFields(fields, field.location, true).ContainsKey(testLocation))
{
returnValue = true;
break;
}
}
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = p;
return returnValue;
}*/
/* internal static bool MoveWillEndangerKing(Grid fields, Vector pieceLocation, Vector pieceMovedLocation, PieceColor color) // This does not test if it is a valid move spot
{
bool returnValue = false;
Piece p1 = fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece;
Piece p2 = fields[(int)pieceMovedLocation.X, (int)pieceMovedLocation.Y].currentPiece;
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = null;
fields[(int)pieceMovedLocation.X, (int)pieceMovedLocation.Y].currentPiece = p1;
returnValue = KingIsEndangered(fields, color);
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = p1;
fields[(int)pieceMovedLocation.X, (int)pieceMovedLocation.Y].currentPiece = p2;
return returnValue;
}*/
/* private static bool KingIsEndangered(Grid fields, PieceColor color)
{
Vector kingLocation = GetKingLocation(fields, color);
foreach (var field in fields)
{
if (field.currentPiece == null || field.currentPiece.color != color)
continue;
if (field.currentPiece.GetValidTargetFields(fields, field.location, dontTestKingDanger: true).ContainsKey(kingLocation))
{
return true;
}
}
return false;
}*/
/// <summary>
/// Searches for the King of the given color.
/// </summary>
/// <param name="fields">GameField</param>
/// <param name="color">Kings color</param>
/// <returns>King location as Vector</returns>
private static Vector GetKingLocation(Grid fields, PieceColor color)
{
foreach (Field field in fields)
if (!field.IsEmpty && field.currentPiece.type == PieceType.King && field.currentPiece.color == color)
return field.location;
throw new Exception("King not found!");
}
virtual internal Dictionary<Field, PieceTeam> GetValidTargetFields(Grid fields, MoveMode mode = MoveMode.Normal, bool dontTestKingDanger = false)
{
var targetFields = GetTargetFields(fields, field, this.color);
if (!dontTestKingDanger)
{
Field kinglocation = fields.GetKing(this.color).field;
List<Field> remove = new List<Field>();
foreach (var item in targetFields.Keys)
{
if (fields.IsFieldVirtuallyEndagered(kinglocation, (this.color == PieceColor.Black ? PieceColor.White : PieceColor.Black), this, item))
remove.Add(item);
}
foreach (var item in remove)
{
targetFields.Remove(item);
}
}
return targetFields;
}
internal abstract Dictionary<Field, PieceTeam> GetTargetFields(Grid fields, Field field, PieceColor color);
internal static bool FieldIsVirtuallyEmpty(Field targetPos, Field skipField, Field possibleTargetField)
{
return (possibleTargetField.IsEmpty && possibleTargetField != targetPos)
|| possibleTargetField == skipField;
}
internal static bool CheckFieldCanBeReached(Field possibleEndageredField, Field targetPos, Piece movePiece, Field possibleTargetField, PieceColor color, out bool breakIt)
{
breakIt = false;
var skipField = movePiece.field;
if (FieldIsVirtuallyEmpty(targetPos, skipField, possibleTargetField))
{
if (possibleTargetField == possibleEndageredField)
return true;
}
else
{
var checkPiece = possibleTargetField == targetPos ? movePiece : possibleTargetField.CurrentPiece;
if (checkPiece.color != color && possibleTargetField == possibleEndageredField)
return true;
breakIt=true;
}
return false;
}
//internal abstract Dictionary<Field, PieceTeam> GetValidTargetFields(Grid fields, MoveMode mode = MoveMode.Any, bool dontTestKingDanger = false);
}
}

View File

@ -0,0 +1,226 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using ChessPanel;
using System.Linq;
using System.Windows;
namespace Chess
{
public enum PieceColor { White, Black };
public enum PieceTeam { Enemy, Ally };
public enum PieceType { King, Queen, Bishop, Rook, Knight, Pawn };
public enum MoveMode { Any, Attack, Movement };
/// <summary>
/// Figur
/// </summary>
public abstract class Piece
{
public PieceColor color;
public PieceType type;
public Image image;
public bool HasMoved = false;
public Field field;
private Vector virtualPosition;//?
public bool EnPassantPossible = false; // Only in Pawn Class. Signs that this Piece has virtually not jumped two pieces, but one, reflective meaning: This.Figure can be attacked using Enpassant if this.type is Pawn
public override string ToString()
{
return type.ToString();
}
public Piece(Vector position, Grid fields)
{
this.field = fields[position];
fields[position].CurrentPiece = this;
}
internal void removeTargetFieldsWhereKingIsEndangered(Grid fields, Dictionary<Field, PieceTeam> targetFields)
{
Field ownKingLocation = fields.GetKing(this.color).field;
foreach (var virtualTargetField in targetFields)
{
if (fields.IsFieldVirtuallyEndagered(ownKingLocation, (this.color == PieceColor.Black ? PieceColor.White : PieceColor.Black), this, virtualTargetField.Key))
targetFields.Remove(virtualTargetField.Key);
}
}
/// <summary>
/// Tests if the Move will leave your King endangered.
/// </summary>
/// <param name="fields">GameField</param>
/// <param name="pieceLocation">Location of Piece to move</param>
/// <param name="simulatedLocation">Location to move Piece to</param>
/// <param name="color">Color of Piece to move</param>
/// <returns>Bool</returns>
/* internal static bool MoveWillEndangerOwnKing(Grid fields, Vector pieceLocation, Vector simulatedLocation, PieceColor color)
{
// fields[simulateLocation].currentPiece = fields[pieceLocation].currentPiece;
// fields[pieceLocation].currentPiece = null;
// Check if own king is endangered
return FieldIsEndangered(fields, simulatedLocation, kingLocation, color, dontTestKingDanger: true);
}*/
/// <summary>
/// Checks if a specific field can be reached by the figure while a virtual move is simulated
/// </summary>
/// <param name="movePiece">Piece that virtually got moved</param>
/// <param name="targetPos">Field that the movePiece virtually moved to</param>
/// <param name="possibleEndageredField">Field that should be checked</param>
/// <param name="fields">Current Game Grid</param>
/// <returns></returns>
internal abstract bool EndageresFieldWithVirtualMove(Grid fields, Field possibleEndageredField, Piece movePiece, Field targetPos);
internal void Move(Field newPieceField)
{
this.field.CurrentPiece = null; // Remove piece from previous position
this.field = newPieceField; // Set pieces location to new field
newPieceField.CurrentPiece = this; // Set new fields piece to this
}
/// <summary>
/// Tests if the Move will leave your movedPiece endangered.
/// </summary>
/// <param name="fields">GameField</param>
/// <param name="pieceLocation">Location of Piece to move</param>
/// <param name="testLocation">Location to move Piece to</param>
/// <param name="color">Color of Piece to move</param>
/// <returns>Bool</returns>
//internal static bool MoveWillEndangerPiece(Grid fields, Vector pieceLocation, Vector testLocation, PieceColor color)
//{
// return false;
// //Grid simulatedField = fields;
// //simulatedField[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = null;
// //return FieldIsEndangered(fields simulatedField, testLocation, color);
//}
/// <summary>
/// Tests if your King is currently endangered.
/// </summary>
/// <param name="fields">GameField</param>
/// <param name="color">Kings color</param>
/// <returns></returns>
//private static bool KingIsEndangered(Grid fields, PieceColor color) => FieldIsEndangered(fields, GetKingLocation(fields, color), color, dontTestKingDanger: true);
/// <summary>
/// Tests if the testLocation is endangered by an enemy.
/// </summary>
/// <param name="fields">GameField</param>
/// <param name="color">Ally Color</param>
/// <param name="testLocation">Location to test</param>
/// <returns>Bool</returns>
//private static bool FieldIsEndangered(Grid fields, Vector simulatedLocation, Vector testLocation, PieceColor color, bool dontTestKingDanger = false)
//{
// foreach (Field field in fields)
// {
// if (field.location != simulatedLocation
// && field.currentPiece != null
// && field.currentPiece.color != color
// && field.currentPiece.GetValidTargetFields(fields, dontTestKingDanger: dontTestKingDanger).ContainsKey(testLocation))
// return true;
// }
// return false;
//}
/*internal static bool IsEndangeredLocationForPiece(Grid fields, Vector pieceLocation, Vector testLocation, PieceColor color)
{
bool returnValue = false;
Piece p = fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece;
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = null;
foreach (var field in fields)
{
if (field.currentPiece != null) if (field.currentPiece.color != color) if (field.currentPiece.GetValidTargetFields(fields, field.location, true).ContainsKey(testLocation))
{
returnValue = true;
break;
}
}
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = p;
return returnValue;
}*/
/* internal static bool MoveWillEndangerKing(Grid fields, Vector pieceLocation, Vector pieceMovedLocation, PieceColor color) // This does not test if it is a valid move spot
{
bool returnValue = false;
Piece p1 = fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece;
Piece p2 = fields[(int)pieceMovedLocation.X, (int)pieceMovedLocation.Y].currentPiece;
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = null;
fields[(int)pieceMovedLocation.X, (int)pieceMovedLocation.Y].currentPiece = p1;
returnValue = KingIsEndangered(fields, color);
fields[(int)pieceLocation.X, (int)pieceLocation.Y].currentPiece = p1;
fields[(int)pieceMovedLocation.X, (int)pieceMovedLocation.Y].currentPiece = p2;
return returnValue;
}*/
/* private static bool KingIsEndangered(Grid fields, PieceColor color)
{
Vector kingLocation = GetKingLocation(fields, color);
foreach (var field in fields)
{
if (field.currentPiece == null || field.currentPiece.color != color)
continue;
if (field.currentPiece.GetValidTargetFields(fields, field.location, dontTestKingDanger: true).ContainsKey(kingLocation))
{
return true;
}
}
return false;
}*/
/// <summary>
/// Searches for the King of the given color.
/// </summary>
/// <param name="fields">GameField</param>
/// <param name="color">Kings color</param>
/// <returns>King location as Vector</returns>
private static Vector GetKingLocation(Grid fields, PieceColor color)
{
foreach (Field field in fields)
if (!field.IsEmpty && field.currentPiece.type == PieceType.King && field.currentPiece.color == color)
return field.location;
throw new Exception("King not found!");
}
virtual internal 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;
List<Field> remove = new List<Field>();
foreach (var item in targetFields.Keys)
{
if (fields.IsFieldVirtuallyEndagered(kinglocation, (this.color == PieceColor.Black ? PieceColor.White : PieceColor.Black), this, item))
remove.Add(item);
}
foreach (var item in remove)
{
targetFields.Remove(item);
}
}
return targetFields;
}
internal abstract Dictionary<Field, PieceTeam> GetTargetFields(Grid fields, Field field, PieceColor color);
internal static bool FieldIsVirtuallyEmpty(Field targetPos, Field skipField, Field possibleTargetField)
{
return (possibleTargetField.IsEmpty && possibleTargetField != targetPos)
|| possibleTargetField == skipField;
}
internal static bool checkFieldCanBeReached(Field possibleEndageredField, Field targetPos, Piece movePiece, Field possibleTargetField, PieceColor color, out bool breakIt)
{
breakIt = false;
var skipField = movePiece.field;
if (FieldIsVirtuallyEmpty(targetPos, skipField, possibleTargetField))
{
if (possibleTargetField == possibleEndageredField)
return true;
}
else
{
var checkPiece = possibleTargetField == targetPos ? movePiece : possibleTargetField.CurrentPiece;
if (checkPiece.color != color && possibleTargetField == possibleEndageredField)
return true;
breakIt=true;
}
return false;
}
//internal abstract Dictionary<Field, PieceTeam> GetValidTargetFields(Grid fields, MoveMode mode = MoveMode.Any, bool dontTestKingDanger = false);
}
}

143
ChessPanel/Pieces/Bishop.cs Normal file
View File

@ -0,0 +1,143 @@
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
}

132
ChessPanel/Pieces/King.cs Normal file
View File

@ -0,0 +1,132 @@
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
}

View File

@ -0,0 +1,98 @@
using Chess;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows;
namespace ChessPanel
{
public class Knight : Piece
{
public Knight(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)
{
for (int a = -1; a < 2; a += 2)
{
for (int b = -2; b < 3; b += 4)
{
if (!(a + (int)field.location.X > 7 || b + (int)field.location.Y < 0 || a + (int)field.location.X < 0 || b + (int)field.location.Y > 7))
{
if ((fields[field.location,a,b].IsEmpty && fields[field.location, a, b] != targetPos) || fields[field.location, a, b] == movePiece.field)
{
if (fields[field.location, a, b] == field)
return true;
continue;
}
else
{
if (fields[field.location, a, b].CurrentPiece?.color != color)
if (fields[field.location, a, b] == field)
return true;
break;
}
}
if (!(b + (int)field.location.X > 7 || a + (int)field.location.Y < 0 || b + (int)field.location.X < 0 || a + (int)field.location.Y > 7))
{
if ((fields[field.location, b, a].IsEmpty && fields[field.location, b, a] != targetPos) || fields[field.location, b, a] == movePiece.field)
{
if (fields[field.location, b, a] == field)
return true;
continue;
}
else
{
if (fields[field.location, b, a].CurrentPiece?.color != color)
if (fields[field.location, b, a] == field)
return true;
break;
}
}
}
}
return false;
}
internal override Dictionary<Field, PieceTeam> GetTargetFields(Grid fields, Field field, PieceColor color)
{
Dictionary<Field, PieceTeam> targetFields = new Dictionary<Field, PieceTeam>();
for (int a = -1; a < 2; a += 2)
{
for (int b = -2; b < 3; b += 4)
{
if (!(a + (int)field.location.X > 7 || b + (int)field.location.Y < 0 || a + (int)field.location.X < 0 || b + (int)field.location.Y > 7))
{
if (fields[field.location,a,b].IsEmpty)
{
targetFields.Add(fields[field.location, a, b], PieceTeam.Ally);
}
else
{
if (fields[field.location, a, b].CurrentPiece?.color != color)
targetFields.Add(fields[field.location, a, b], PieceTeam.Enemy);
}
}
if (!(b + (int)field.location.X > 7 || a + (int)field.location.Y < 0 || b + (int)field.location.X < 0 || a + (int)field.location.Y > 7))
{
if (fields[field.location, b, a].IsEmpty)
{
targetFields.Add(fields[field.location, b, a], PieceTeam.Ally);
}
else
{
if (fields[field.location, b, a].CurrentPiece?.color != color)
targetFields.Add(fields[field.location, b, a], PieceTeam.Enemy);
}
}
}
}
return targetFields;
}
} // FINALIZED
}

80
ChessPanel/Pieces/Pawn.cs Normal file
View File

@ -0,0 +1,80 @@
using Chess;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows;
namespace ChessPanel
{
public class Pawn : Piece
{
public Pawn(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) // Pawn does not care about virtually moved pieces, if field is in range it can attack
{
int direction = this.color == PieceColor.White ? -1 : 1;
#region Attack
for (int x = -1; x < 2; x += 2)
{
if (field.location.Y + (direction) > 7 || field.location.Y + (direction) < 0 || field.location.X + x > 7 || field.location.X + x < 0)
continue;
if (fields[field.location, x, direction] == field)
return true;
}
#endregion
return false;
}
internal override Dictionary<Field, PieceTeam> GetTargetFields(Grid fields, Field field, PieceColor color)
{
MoveMode mode = MoveMode.Normal;
Dictionary<Field, PieceTeam> targetFields = new Dictionary<Field, PieceTeam>();
int direction = this.color == PieceColor.White ? -1 : 1;
#region Movement
if (mode == MoveMode.Normal || mode == MoveMode.Movement)
for (int y = 1; y < 1 + (HasMoved ? 1 : 2); y++)
{
if (field.location.Y + (direction * y) > 7 || field.location.Y + (direction * y) < 0)
break;
if (fields[field.location, 0, (direction * y)].IsEmpty)
targetFields.Add(fields[field.location, 0, (direction * y)], PieceTeam.Ally);
else
break;
}
#endregion
#region Attack
if (mode == MoveMode.Normal || mode == MoveMode.Attack)
{
#region EnPassant
if (field.location.Y == (this.color == PieceColor.White ? 3 : 4))
for (int x = -1; x < 2; x += 2)
if (field.location.X + x <= 7 && field.location.X + x >= 0)
if (fields[field.location, x, 0].CurrentPiece != null
&& fields[field.location, x, 0].CurrentPiece.type == PieceType.Pawn
&& fields[field.location, x, 0].CurrentPiece.color != this.color
&& fields[field.location, x, 0].CurrentPiece.EnPassantPossible)
targetFields.Add(fields[field.location, x, direction], PieceTeam.Enemy);
#endregion
for (int x = -1; x < 2; x += 2)
{
if (field.location.Y + (direction) > 7 || field.location.Y + (direction) < 0 || field.location.X + x > 7 || field.location.X + x < 0)
continue;
if (fields[field.location, x, direction].CurrentPiece != null && fields[field.location, x, direction].CurrentPiece.color != this.color)
{
targetFields.Add(fields[field.location, x, direction], PieceTeam.Enemy);
}
}
#endregion
}
return targetFields;
}
}
}

View File

@ -0,0 +1,35 @@
using Chess;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows;
namespace ChessPanel
{
public class Queen : Piece
{
public Queen(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 possibleEndageredField, Piece movePiece, Field targetPos)
{
return Rook.StaticEndageresFieldWithVirtualMove(fields, this.field, possibleEndageredField, movePiece, targetPos, color)
|| Bishop.StaticEndageresFieldWithVirtualMove(fields, this.field, possibleEndageredField, movePiece, targetPos, color);
}
internal override Dictionary<Field, PieceTeam> GetTargetFields(Grid fields, Field field, PieceColor color)
{
Dictionary<Field, PieceTeam> targetFields = Rook.StaticGetTargetFields(fields, field, this.color); // Take Rook fields
targetFields = targetFields.Concat(Bishop.StaticGetTargetFields(fields, field, this.color) // Add Bishop; Rook + Bishop = Queen
.Where(kvp => !targetFields.ContainsKey(kvp.Key)))
.OrderBy(c => c.Value)
.ToDictionary(c => c.Key, c => c.Value);
return targetFields;
}
} // FINALIZED
}

141
ChessPanel/Pieces/Rook.cs Normal file
View File

@ -0,0 +1,141 @@
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
}