169 lines
5.6 KiB
C#
169 lines
5.6 KiB
C#
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);
|
|
//}
|
|
/// <summary>
|
|
/// Creates a copy of the given array;
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="Array"></param>
|
|
/// <returns></returns>
|
|
private static T[,] CopyMultiArray<T>(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; } }
|
|
/// <summary>
|
|
/// Adds x and y values to Vector v and gets the field at the position
|
|
/// </summary>
|
|
/// <param name="v">Vector</param>
|
|
/// <param name="x">X</param>
|
|
/// <param name="y">Y</param>
|
|
/// <returns>Field</returns>
|
|
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];
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Checks if a field is endagered by a party
|
|
/// </summary>
|
|
/// <param name="field">Reference to the Field that should be checked</param>
|
|
/// <param name="color">Color of the attacking party</param>
|
|
/// <param name="movePiece">Piece that virtually got moved</param>
|
|
/// <param name="targetPos">Field that the movePiece virtually moved to</param>
|
|
/// <returns></returns>
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |