using Chess; using System; using System.Windows; using System.Collections; namespace ChessPanel { public class Grid : IEnumerable { public Grid(int GridSize) { fields = new Field[GridSize, GridSize]; this.GridSize = GridSize; } //public Grid(Field[,] fields) //{ // this.GridSize = fields.GetLength(0); // this.fields = CopyMultiArray(fields); //} /// /// Creates a copy of the given array; /// /// /// /// private static T[,] CopyMultiArray(T[,] Array) { int[] dim = new int[] { Array.GetLength(0), Array.GetLength(1) }; T[,] Copy = new T[dim[0], dim[1]]; for (int x = 0; x < dim[0]; x++) for (int y = 0; y < dim[1]; y++) Copy[x, y] = Array[x, y]; return Copy; } //public Grid(Grid fields) : this(fields.fields) { } public Field[,] fields; public int GridSize; public Field this[Vector v] { get { return fields[(int)v.X, (int)v.Y]; } set { fields[(int)v.X, (int)v.Y] = value; } } internal Piece GetKing(PieceColor color) { foreach (Field item in this) { if (item.CurrentPiece?.color == color && item.CurrentPiece?.type == PieceType.King) return item.CurrentPiece; } throw new Exception("No King found"); } public Field this[int x, int y] { get { return fields[x, y]; } set { fields[x, y] = value; } } public Field this[double x, double y] { get { return fields[(int)x, (int)y]; } set { fields[(int)x, (int)y] = value; } } /// /// Adds x and y values to Vector v and gets the field at the position /// /// Vector /// X /// Y /// Field public Field this[Vector v, int x, int y] { get { return fields[x + (int)v.X, y + (int)v.Y]; } set { fields[x + (int)v.X, y + (int)v.Y] = value; } } public Field this[int x, int y, Vector v] { get { return fields[x + (int)v.X, y + (int)v.Y]; } set { fields[x + (int)v.X, y + (int)v.Y] = value; } } public Field this[Vector v, double x, double y] { get { return fields[(int)x + (int)v.X, (int)y + (int)v.Y]; } set { fields[(int)x + (int)v.X, (int)y + (int)v.Y] = value; } } public Field this[double x, double y, Vector v] { get { return fields[(int)x + (int)v.X, (int)y + (int)v.Y]; } set { fields[(int)x + (int)v.X, (int)y + (int)v.Y] = value; } } public Field GetFieldWithOffset(Field f, double x, double y) { int _x = (int)(f.location.X + x); int _y = (int)(f.location.Y + y); return fields[_x, _y]; } /// /// Checks if a field is endagered by a party /// /// Reference to the Field that should be checked /// Color of the attacking party /// Piece that virtually got moved /// Field that the movePiece virtually moved to /// public bool IsFieldVirtuallyEndagered(Field field, PieceColor color, Piece movePiece, Field targetPos) { foreach (Field item in this) { if (item.IsEmpty) continue; // Feld ist leer if (item == movePiece.field) continue; // Quellfeld muss nicht geprüft werden Piece p = item.CurrentPiece; if (p.color != color) continue; // Figur ist kein Gegner // Ask every Piece of the other party if it can attack the specified field if (p.EndageresFieldWithVirtualMove(this, field, movePiece, targetPos)) //if (item.currentPiece.GetValidTargetFields(movePiece, targetPos).ContainsKey(field)) return true; } return false; } IEnumerator IEnumerable.GetEnumerator() { return new FieldEnum(fields); } } // When you implement IEnumerable, you must also implement IEnumerator. public class FieldEnum : IEnumerator { public Field[,] field; // Enumerators are positioned before the first element // until the first MoveNext() call. int position = -1; int width; int height; public FieldEnum(Field[,] list) { field = list; width = list.GetLength(0); height = list.GetLength(1); } public bool MoveNext() { position++; return (position < field.Length); } public void Reset() { position = -1; } object IEnumerator.Current { get { return Current; } } public Field Current { get { try { return field[position % 8, position / 8]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } } }