Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Collections;
- using System.Collections.Generic;
- /*
- ToDo:
- - KI-Sonderfall: Bauern Umwandlung
- - Endzustanderkennung: Vorrübergehend auf Schlagen des Königs gesetzt
- - Onselect Rochade überdenken
- - Zustandsbaum: Unrealistische Zeit
- - Wertkalkulation bei Schlagen des Königs prüfen
- - Multithreading Problem gelöst. Nullreferece bei RateState
- */
- namespace ObscuritasSchach
- {
- public class Program
- {
- public class Vector2
- {
- public int Y;
- public int X;
- public static float Distance(Vector2 A, Vector2 B)
- {
- return (float)(Math.Sqrt(Math.Pow(A.X, 2) + Math.Pow(B.Y, 2)));
- }
- public Vector2(int x, int y)
- {
- X = x;
- Y = y;
- }
- }
- public class KI
- {
- public static List<List<State>> StateTree = new List<List<State>>();
- public static int[][] Root;
- public static Spieler Player;
- public KI cKI;
- public static bool TreeContainsField(int[][] Field)
- {
- for (int i = 0; i < StateTree.Count; i++)
- {
- for (int j = 0; j < StateTree[i].Count; j++)
- {
- for (int k = 0; k < StateTree[i][j].Feld.Length; k++)
- {
- for (int l = 0; l < StateTree[i][j].Feld[k].Length; l++)
- {
- if (StateTree[i][j].Feld[k][l] != Field[k][l])
- return false;
- }
- }
- }
- }
- return true;
- }
- private static object thisLock = new object();
- public static void BuildNode(State node, int pid, bool Turn, int level)
- {
- List<PFigur>[] FigurSets = new List<PFigur>[2];
- if (!node.isFinal)
- {
- FigurSets[0] = node.wFiguren;
- FigurSets[1] = node.sFiguren;
- int activeSet = Convert.ToInt32(((!Convert.ToBoolean(pid) || Turn) && (Convert.ToBoolean(pid) || !Turn)));
- foreach (PFigur Figur in FigurSets[activeSet])
- {
- try
- {
- Spieler.OnSelect(FigurSets[activeSet], FigurSets[activeSet].IndexOf(Figur), node.Feld);
- }
- catch (Exception)
- {
- Console.WriteLine(Figur.Klasse.ToString());
- }
- // j=x, i=y; Top Left Corner
- for (int i = 0; i < Figur.Movement.Field.Length; i++)
- {
- for (int j = 0; j < Figur.Movement.Field[i].Length; j++)
- {
- if (Figur.Movement.Field[i][j] != 0 && Figur.Movement.Field[i][j] != 5)
- {
- State child = new State(new Schachzug(Figur, new PFigur.Position(j, i), Figur.Movement.Field[i][j]), node, node.Feld, Turn == true ? pid : (1 - pid));
- if (child.isFinalState())
- {
- node.isFinal = true;
- }
- if (!TreeContainsField(child.Feld))
- {
- StateTree[level + 1].Add(child);
- node.Childs.Add(StateTree[level + 1].Last());
- }
- }
- }
- }
- }
- }
- return;
- }
- public static void FillStateTree(object param)
- {
- object[] args = new object[3];
- args = (object[])param;
- int pid = Convert.ToInt32(args[0]);
- int maxDepth = Convert.ToInt32(args[1]);
- State root = (State)args[2];
- StateTree.Add(new List<State> { root });
- bool Turn = true;
- List<PFigur>[] FigurSets = new List<PFigur>[2];
- for (int level = 0; level < maxDepth; level++, Turn = !Turn)
- {
- if ((level > 0 && StateTree[level - 1].Count != 0) || level == 0)
- {
- StateTree.Add(new List<State>());
- Task[] threads = new Task[StateTree[level].Count];
- for (int n = 0; n < threads.Length; n++)
- {
- State node = StateTree[level][n];
- threads[n] = Task.Run(() => BuildNode(node, pid, Turn, level));
- }
- try
- {
- Task.WaitAll(threads);
- }
- catch (AggregateException ae)
- {
- throw ae.Flatten();
- }
- }
- }
- }
- public class Schachzug
- {
- public PFigur Figur;
- public PFigur.Position newPos;
- public PFigur.Position oldPos;
- public int moveType;
- public Dictionary<PFigur.Position, int> XReplace = new Dictionary<PFigur.Position, int>();
- public Schachzug(PFigur figur, PFigur.Position pos, int movetype)
- {
- Figur = new PFigur(figur.Klasse, figur.position, figur.Color);
- oldPos = Figur.position;
- newPos = pos;
- moveType = movetype;
- switch (movetype)
- {
- case 3:
- XReplace.Add(new PFigur.Position(newPos.x, Figur.position.y), 0);
- break;
- case 4:
- XReplace.Add(new PFigur.Position(((newPos.x - Figur.position.x) / Math.Abs(newPos.x - Figur.position.x)) + Figur.position.x, Figur.position.y), Figur.ToClassIndex() - 3);
- break;
- }
- }
- public override string ToString()
- {
- int xPos = Convert.ToChar(Figur.position.x) + 65;
- int newxPos = Convert.ToChar(newPos.x) + 65;
- //return Figur.Klasse.ToString() + " von " + Convert.ToChar(xPos).ToString() + (8 - Figur.position.y).ToString() + " auf " + Convert.ToChar(newxPos).ToString() + (8 - newPos.y).ToString();
- return Figur.Klasse.ToString() +" " + Figur.Color.ToString() + " von (" + oldPos.x + ";" + oldPos.y + ") auf (" + newPos.x + ";" + newPos.y + ")";
- }
- }
- public class State
- {
- public int[][] Feld = new int[8][];
- public float Value;
- public State Parent;
- public List<State> Childs = new List<State>();
- public Schachzug Zug;
- public List<PFigur> wFiguren, sFiguren;
- public int playerID;
- public bool isFinal;
- public bool isFinalState()
- {
- if (wFiguren.Exists(x => x.Klasse == PFigur.Typ.König) && sFiguren.Exists(x => x.Klasse == PFigur.Typ.König))
- return false;
- else
- return true;
- }
- public State(Schachzug zug, State parent, int[][] prevField, int pid)
- {
- playerID = pid;
- Zug = zug;
- Parent = parent;
- wFiguren = parent.wFiguren;
- sFiguren = parent.sFiguren;
- for (int i = 0; i < prevField.Length; i++)
- {
- Feld[i] = new int[8];
- prevField[i].CopyTo(Feld[i], 0);
- }
- Feld[zug.Figur.position.y][zug.Figur.position.x] = 0;
- Feld[zug.newPos.y][zug.newPos.x] = zug.Figur.ToClassIndex();
- PFigur tEnemy = PFigur.IndexToFigur(Feld[zug.newPos.y][zug.newPos.x], zug.newPos);
- if (tEnemy.Color == PFigur.Farbe.weiß && wFiguren.Contains(tEnemy))
- wFiguren.Remove(tEnemy);
- else if (sFiguren.Contains(tEnemy))
- sFiguren.Remove(tEnemy);
- zug.Figur.position = zug.newPos;
- foreach (KeyValuePair<PFigur.Position, int> rep in Zug.XReplace)
- {
- Feld[rep.Key.y][rep.Key.x] = rep.Value;
- if (Feld[rep.Key.y][rep.Key.x] != 0)
- {
- tEnemy = PFigur.IndexToFigur(Feld[rep.Key.y][rep.Key.x], rep.Key);
- if (tEnemy.Color == PFigur.Farbe.weiß)
- wFiguren.Remove(tEnemy);
- else
- sFiguren.Remove(tEnemy);
- }
- }
- }
- public State(Schachzug zug, State parent, int pid, float value, int[][] refField, List<PFigur> wfiguren, List<PFigur> sfiguren)
- {
- playerID = pid;
- Zug = zug;
- Parent = parent;
- Value = value;
- for (int i = 0; i < refField.Length; i++)
- {
- Feld[i] = new int[8];
- refField[i].CopyTo(Feld[i], 0);
- }
- wFiguren = wfiguren;
- sFiguren = sfiguren;
- }
- }
- public static void WriteTreeCount()
- {
- while (true)
- {
- Console.SetCursorPosition(0, 0);
- try
- {
- Console.Write(System.Diagnostics.Process.GetCurrentProcess().Threads.Count);
- }
- catch (Exception) { }
- }
- }
- public int FigurAsTarget(PFigur Figur, List<PFigur> FigurSet, List<PFigur> OwnFigurSet, int[][] Feld)
- {
- int count = 0;
- foreach (PFigur Enemy in FigurSet)
- {
- if (FigurSet[0].Color == (PFigur.Farbe)Player.playerID)
- {
- if (Enemy.Movement[Figur.position.y, Figur.position.x] == 5)
- count++;
- }
- else
- {
- if (Math.Abs(Enemy.Movement[Figur.position.y, Figur.position.x] - 2.5f) == 0.5f)
- count++;
- }
- }
- return count;
- }
- public float RateState(State current)
- {
- float result = 0f, matValue = 0f, guarding = 0f, threatening = 0f;
- List<PFigur>[] FigurSets = new List<PFigur>[2];
- FigurSets[0] = current.wFiguren;
- FigurSets[1] = current.sFiguren;
- float[] FigurGuards = new float[FigurSets[Player.playerID].Count];
- float[] FigurThreats = new float[FigurSets[1 - Player.playerID].Count];
- List<PFigur> allFigures = FigurSets[0].Concat(FigurSets[1]).ToList();
- Task[] tasks = new Task[allFigures.Count];
- foreach (PFigur Figur in allFigures)
- {
- tasks[allFigures.IndexOf(Figur)] = Task.Run(() => Spieler.OnSelect(allFigures, allFigures.IndexOf(Figur), current.Feld));
- }
- try
- {
- Task.WaitAll(tasks);
- }
- catch (AggregateException ae)
- {
- throw ae.Flatten();
- }
- for (int i = 0; i < FigurSets[Player.playerID].Count; i++)
- {
- PFigur Figur = FigurSets[Player.playerID][i];
- matValue += Figur.Punkte;
- FigurGuards[i] += FigurAsTarget(Figur, FigurSets[Player.playerID], FigurSets[Player.playerID], current.Feld);
- FigurGuards[i] -= FigurAsTarget(Figur, FigurSets[1 - Player.playerID], FigurSets[Player.playerID], current.Feld);
- }
- for (int i = 0; i < FigurSets[1 - Player.playerID].Count; i++)
- {
- PFigur Figur = FigurSets[1 - Player.playerID][i];
- matValue -= Figur.Punkte;
- FigurThreats[i] += FigurAsTarget(Figur, FigurSets[1 - Player.playerID], FigurSets[1 - Player.playerID], current.Feld);
- FigurThreats[i] -= FigurAsTarget(Figur, FigurSets[Player.playerID], FigurSets[1 - Player.playerID], current.Feld);
- }
- guarding = FigurGuards.Join(FigurSets[Player.playerID], x => x, y => y.Punkte, (x, y) => ((x < -1) ? -1 : ((x > 1) ? 1 : x)) * y.Punkte).Sum();
- threatening = FigurThreats.Join(FigurSets[1 - Player.playerID], x => x, y => y.Punkte, (x, y) => ((x < -1) ? -1 : ((x > 1) ? 1 : x)) * y.Punkte).Sum();
- result = matValue + guarding - threatening;
- current.Value = result;
- //Console.WriteLine(guarding + " | " + threatening + " | " + matValue);
- // Gesamter Materialwert des Spielfeldes (+Eigene -Gegner) + Schutz der eigenen Figuren (+Punkte bei Schutz, -Punkte bei Bedrohung, Sonst 0), - Schutz der gegnerischen Figuren
- return result;
- }
- public KI(Spieler player)
- {
- Player = player;
- Root = new int[8][];
- for (int i = 0; i < Board.Schachfeld.Length; i++)
- {
- Root[i] = new int[8];
- Board.Schachfeld[i].CopyTo(Root[i], 0);
- }
- switch (Player.KITyp)
- {
- case Spieler.KIType.DynamicLearning:
- break;
- case Spieler.KIType.HardSearch:
- cKI = Player.assignedKI;
- Thread th = new Thread(new ParameterizedThreadStart(FillStateTree));
- //FillStateTree(Player.playerID);
- Thread th2 = new Thread(WriteTreeCount);
- th.Start();
- th2.Start();
- Console.ReadKey();
- break;
- case Spieler.KIType.StaticLearning:
- cKI = Player.assignedKI;
- int maxdepth = 1;
- Task th3 = Task.Run(() => FillStateTree(new object[] { Player.playerID, maxdepth, new State(null, null, Player.playerID, 0f, Root, Board.wFiguren, Board.sFiguren) }));
- try { th3.Wait(); }
- catch (AggregateException ae)
- {
- throw ae.Flatten();
- }
- List<Task> tasks = new List<Task>();
- List<State> states = new List<State>();
- for (int i = 0; i < StateTree.Count; i++)
- {
- for (int j = 0; j < StateTree[i].Count; j++)
- {
- states.Add(StateTree[i][j]);
- }
- }
- foreach (State state in states)
- tasks.Add(Task.Run(() => RateState(state)));
- try
- {
- Task.WaitAll(tasks.ToArray());
- }
- catch (AggregateException ae)
- {
- Console.Write(ae.StackTrace);
- }
- for (int i = 0; i < StateTree.Count; i++)
- {
- for (int j = 0; j < StateTree[i].Count; j++)
- {
- if (StateTree[i][j].Zug != null)
- Console.WriteLine(StateTree[i][j].Zug.ToString() + ": " + StateTree[i][j].Value + "|");
- }
- }
- Console.ReadKey();
- /*
- * Bewertungsfunktion:
- * - Figuren auf dem Feld
- * - Gegnerische Figuren bedroht
- * - Eigene Figuren bedroht
- * - Sicherung gegnerischer Figuren
- * - Sicherung eigener Figuren
- */
- break;
- }
- }
- }
- public class PFigur
- {
- public bool FirstTurn;
- public readonly int Punkte;
- public enum Farbe
- {
- weiß,
- schwarz
- }
- public Farbe Color;
- public enum Typ
- {
- Bauer,
- Turm,
- Springer,
- Läufer,
- Dame,
- König
- }
- public readonly Typ Klasse;
- public class Position
- {
- public int x;
- public int y;
- public Position(int X, int Y)
- {
- x = X;
- y = Y;
- }
- }
- public Position position;
- public class movement
- {
- public int[][] Field;
- public List<PFigur.Position> CountValues(int ValueMin, int ValueMax)
- {
- List<PFigur.Position> temp = new List<PFigur.Position>();
- for (int i = 0; i < 8; i++)
- {
- for (int j = 0; j < 8; j++)
- {
- if (Field[i][j] >= ValueMin && Field[i][j] <= ValueMin)
- {
- temp.Add(new PFigur.Position(j, i));
- }
- }
- }
- return temp;
- }
- public static implicit operator movement(int[][] field)
- {
- movement temp = new movement();
- temp.Field = field;
- return temp;
- }
- public static implicit operator int[][] (movement mov)
- {
- return mov.Field;
- }
- public int this[int i, int j]
- {
- get { return this.Field[i][j]; }
- set { this.Field[i][j] = (int)value; }
- }
- }
- public movement Movement;
- public static PFigur IndexToFigur(int index, Position indexPos)
- {
- Typ tTyp = (Typ)((index - 1) - Convert.ToInt32(index >= 0xA) * 9);
- Farbe tFarbe = (Farbe)(Convert.ToInt32(index >= 0xA));
- return new PFigur(tTyp, indexPos, tFarbe);
- }
- public int ToClassIndex()
- {
- if (Color == Farbe.weiß)
- {
- switch (Klasse)
- {
- case Typ.Bauer:
- return 0x1;
- case Typ.Dame:
- return 0x5;
- case Typ.König:
- return 0x6;
- case Typ.Läufer:
- return 0x4;
- case Typ.Springer:
- return 0x3;
- case Typ.Turm:
- return 0x2;
- default:
- return 0x0;
- }
- }
- else
- {
- switch (Klasse)
- {
- case Typ.Bauer:
- return 0xA;
- case Typ.Dame:
- return 0xE;
- case Typ.König:
- return 0xF;
- case Typ.Läufer:
- return 0xD;
- case Typ.Springer:
- return 0xC;
- case Typ.Turm:
- return 0xB;
- default:
- return 0x0;
- }
- }
- }
- /* INT-Werte für Movement Array:
- 0 - Keine Bewegung
- 1 - normale Bewegung
- 2 - Schlagen einer Figur
- 3 - Beiläufiges Schlagen einer Figur
- 4 - Rochade
- */
- public PFigur(Typ typ, Position pos, Farbe color)
- {
- Color = color;
- Movement = new int[8][] {
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 }
- };
- FirstTurn = true;
- Klasse = typ;
- position = pos;
- switch (typ)
- {
- case Typ.Bauer:
- Punkte = 1;
- break;
- case Typ.Turm:
- Punkte = 5;
- break;
- case Typ.Springer:
- Punkte = 3;
- break;
- case Typ.Läufer:
- Punkte = 3;
- break;
- case Typ.Dame:
- Punkte = 9;
- break;
- case Typ.König:
- Punkte = 100;
- break;
- default:
- throw new System.Exception("Der falsche Typ wurde eingegeben.");
- }
- }
- }
- public class Spieler
- {
- /* Definition der INT-Werte der Figuren: Hexadezimal
- Weiß:
- Bauer = 0x1
- Turm = 0x2
- Springer = 0x3
- Läufer = 0x4
- Königin = 0x5
- König = 0x6
- Schwarz:
- Bauer = 0xA
- Turm = 0xB
- Springer = 0xC
- Läufer = 0xD
- Königin = 0xE
- König = 0xF
- */
- public int playerID;
- public string Name;
- public KIType KITyp;
- public KI assignedKI;
- public enum KIType
- {
- HardSearch,
- StaticLearning,
- DynamicLearning
- }
- public static bool inCheck()
- {
- return false;
- }
- public static bool inCheck(PFigur.Position pos)
- {
- return false;
- }
- public static void OnSelect(List<PFigur> Pool, int selected, int[][] Feld)
- {
- int Faktor = 0;
- int[] Enemy = new int[2] { 0x0, 0x0 };
- if (Pool[0].Color == PFigur.Farbe.weiß)
- {
- Faktor = 1;
- Enemy = new int[] { 0xA, 0xF };
- }
- else
- {
- Faktor = -1;
- Enemy = new int[] { 0x1, 0x6 };
- }
- if (selected >= 0)
- {
- Pool[selected].Movement = new int[8][] {
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 },
- new int[8] { 0, 0, 0, 0, 0, 0, 0, 0 }
- };
- switch (Pool[selected].Klasse)
- {
- case PFigur.Typ.Bauer:
- try
- {
- if (Feld[Pool[selected].position.y - 1 * Faktor][Pool[selected].position.x] == 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y - 1 * Faktor, Pool[selected].position.x] = 1;
- }
- }
- catch (System.IndexOutOfRangeException)
- { }
- try
- {
- if (Feld[Pool[selected].position.y - 2 * Faktor][Pool[selected].position.x] == 0x0 && Pool[selected].FirstTurn)
- {
- Pool[selected].Movement[Pool[selected].position.y - 2 * Faktor, Pool[selected].position.x] = 1;
- }
- }
- catch (System.IndexOutOfRangeException)
- { }
- try
- {
- if (Feld[Pool[selected].position.y][Pool[selected].position.x + 1] >= Enemy[0] && Feld[Pool[selected].position.y][Pool[selected].position.x + 1] <= Enemy[1])
- {
- Pool[selected].Movement[Pool[selected].position.y - 1 * Faktor, Pool[selected].position.x + 1] = 3;
- }
- else if (Feld[Pool[selected].position.y][Pool[selected].position.x + 1] != 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y - 1 * Faktor, Pool[selected].position.x + 1] = 5;
- }
- }
- catch (System.IndexOutOfRangeException)
- { }
- try
- {
- if (Feld[Pool[selected].position.y][Pool[selected].position.x - 1] >= Enemy[0] && Feld[Pool[selected].position.y][Pool[selected].position.x - 1] <= Enemy[1])
- {
- Pool[selected].Movement[Pool[selected].position.y - 1 * Faktor, Pool[selected].position.x - 1] = 3;
- }
- else if (Feld[Pool[selected].position.y][Pool[selected].position.x - 1] != 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y - 1 * Faktor, Pool[selected].position.x - 1] = 5;
- }
- }
- catch (System.IndexOutOfRangeException)
- { }
- try
- {
- if (Feld[Pool[selected].position.y - 1 * Faktor][Pool[selected].position.x - 1] >= Enemy[0] && Feld[Pool[selected].position.y - 1 * Faktor][Pool[selected].position.x - 1] <= Enemy[1])
- {
- Pool[selected].Movement[Pool[selected].position.y - 1 * Faktor, Pool[selected].position.x - 1] = 2;
- }
- else if (Feld[Pool[selected].position.y - 1 * Faktor][Pool[selected].position.x - 1] != 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y - 1 * Faktor, Pool[selected].position.x - 1] = 5;
- }
- }
- catch (System.IndexOutOfRangeException)
- { }
- try
- {
- if (Feld[Pool[selected].position.y - 1 * Faktor][Pool[selected].position.x + 1] >= Enemy[0] && Feld[Pool[selected].position.y - 1 * Faktor][Pool[selected].position.x + 1] <= Enemy[1])
- {
- Pool[selected].Movement[Pool[selected].position.y - 1 * Faktor, Pool[selected].position.x + 1] = 2;
- }
- else if (Feld[Pool[selected].position.y - 1 * Faktor][Pool[selected].position.x + 1] != 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y - 1 * Faktor, Pool[selected].position.x + 1] = 5;
- }
- }
- catch (System.IndexOutOfRangeException)
- { }
- break;
- case PFigur.Typ.Dame:
- try
- {
- for (int i = Pool[selected].position.y + 1; i <= 7; i++)
- {
- if (Feld[i][Pool[selected].position.x] == 0x0)
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 1;
- }
- else if (Feld[i][Pool[selected].position.x] >= Enemy[0] && Feld[i][Pool[selected].position.x] <= Enemy[1])
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.y - 1; i >= 0; i--)
- {
- if (Feld[i][Pool[selected].position.x] == 0x0)
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 1;
- }
- else if (Feld[i][Pool[selected].position.x] >= Enemy[0] && Feld[i][Pool[selected].position.x] <= Enemy[1])
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x - 1; i >= 0; i--)
- {
- if (Feld[Pool[selected].position.y][i] == 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 1;
- }
- else if (Feld[Pool[selected].position.y][i] >= Enemy[0] && Feld[Pool[selected].position.y][i] <= Enemy[1])
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x + 1; i <= 7; i++)
- {
- if (Feld[Pool[selected].position.y][i] == 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 1;
- }
- else if (Feld[Pool[selected].position.y][i] >= Enemy[0] && Feld[Pool[selected].position.y][i] <= Enemy[1])
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x - 1, j = Pool[selected].position.y + 1; (i >= 0 && j <= 7); i--, j++)
- {
- if (Feld[j][i] == 0x0)
- {
- Pool[selected].Movement[j, i] = 1;
- }
- else if (Feld[j][i] >= Enemy[0] && Feld[j][i] <= Enemy[1])
- {
- Pool[selected].Movement[j, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[j, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x - 1, j = Pool[selected].position.y - 1; (i >= 0 && j >= 0); i--, j--)
- {
- if (Feld[j][i] == 0x0)
- {
- Pool[selected].Movement[j, i] = 1;
- }
- else if (Feld[j][i] >= Enemy[0] && Feld[j][i] <= Enemy[1])
- {
- Pool[selected].Movement[j, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[j, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x + 1, j = Pool[selected].position.y + 1; (i <= 7 && j <= 7); i++, j++)
- {
- if (Feld[j][i] == 0x0)
- {
- Pool[selected].Movement[j, i] = 1;
- }
- else if (Feld[j][i] >= Enemy[0] && Feld[j][i] <= Enemy[1])
- {
- Pool[selected].Movement[j, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[j, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x + 1, j = Pool[selected].position.y - 1; (i <= 7 && j >= 0); i++, j--)
- {
- if (Feld[j][i] == 0x0)
- {
- Pool[selected].Movement[j, i] = 1;
- }
- else if (Feld[j][i] >= Enemy[0] && Feld[j][i] <= Enemy[1])
- {
- Pool[selected].Movement[j, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[j, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- break;
- case PFigur.Typ.König:
- for (int i = 0; i < 8; i++)
- {
- for (int j = 0; j < 8; j++)
- {
- if (Vector2.Distance(new Vector2(Pool[selected].position.y, Pool[selected].position.x), new Vector2(i, j)) < 1.5)
- {
- if (Feld[i][j] == 0x0)
- Pool[selected].Movement[i, j] = 1;
- else if (Feld[i][j] >= Enemy[0] && Feld[i][j] <= Enemy[1])
- Pool[selected].Movement[i, j] = 2;
- else
- Pool[selected].Movement[i, j] = 5;
- }
- }
- }
- /* Rochade überdenken
- if (Pool[selected].FirstTurn)
- {
- for (int i = 0; i < Pool.Count; i++)
- {
- if (Pool[i].Klasse == PFigur.Typ.Turm && Pool[i].FirstTurn)
- {
- if (Pool[i].position.x == 0 && !inCheck(new PFigur.Position(Pool[selected].position.x, Pool[selected].position.y)) && !inCheck(new PFigur.Position(Pool[selected].position.x - 1, Pool[selected].position.y)) && !inCheck(new PFigur.Position(Pool[selected].position.x - 2, Pool[selected].position.y)))
- {
- if (Feld[Pool[selected].position.y][Pool[selected].position.x - 1] + Feld[Pool[selected].position.y][Pool[selected].position.x - 2] + Feld[Pool[selected].position.y][Pool[selected].position.x - 3] == 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y, Pool[selected].position.x - 2] = 4;
- }
- }
- else if (!inCheck(new PFigur.Position(Pool[selected].position.x, Pool[selected].position.y)) && !inCheck(new PFigur.Position(Pool[selected].position.x + 1, Pool[selected].position.y)) && !inCheck(new PFigur.Position(Pool[selected].position.x + 2, Pool[selected].position.y)))
- {
- if (Feld[Pool[selected].position.y][Pool[selected].position.x + 1] + Feld[Pool[selected].position.y][Pool[selected].position.x + 2] == 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y, Pool[selected].position.x + 2] = 4;
- }
- }
- }
- }
- }*/
- break;
- case PFigur.Typ.Läufer:
- try
- {
- for (int i = Pool[selected].position.x - 1, j = Pool[selected].position.y + 1; (i >= 0 && j <= 7); i--, j++)
- {
- if (Feld[j][i] == 0x0)
- {
- Pool[selected].Movement[j, i] = 1;
- }
- else if (Feld[j][i] >= Enemy[0] && Feld[j][i] <= Enemy[1])
- {
- Pool[selected].Movement[j, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[j, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x - 1, j = Pool[selected].position.y - 1; (i >= 0 && j >= 0); i--, j--)
- {
- if (Feld[j][i] == 0x0)
- {
- Pool[selected].Movement[j, i] = 1;
- }
- else if (Feld[j][i] >= Enemy[0] && Feld[j][i] <= Enemy[1])
- {
- Pool[selected].Movement[j, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[j, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x + 1, j = Pool[selected].position.y + 1; (i <= 7 && j <= 7); i++, j++)
- {
- if (Feld[j][i] == 0x0)
- {
- Pool[selected].Movement[j, i] = 1;
- }
- else if (Feld[j][i] >= Enemy[0] && Feld[j][i] <= Enemy[1])
- {
- Pool[selected].Movement[j, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[j, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x + 1, j = Pool[selected].position.y - 1; (i <= 7 && j >= 0); i++, j--)
- {
- if (Feld[j][i] == 0x0)
- {
- Pool[selected].Movement[j, i] = 1;
- }
- else if (Feld[j][i] >= Enemy[0] && Feld[j][i] <= Enemy[1])
- {
- Pool[selected].Movement[j, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[j, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- break;
- case PFigur.Typ.Springer:
- for (int i = 0; i < 8; i++)
- {
- for (int j = 0; j < 8; j++)
- {
- if (Vector2.Distance(new Vector2(Pool[selected].position.y, Pool[selected].position.x), new Vector2(i, j)) == Math.Sqrt(5))
- {
- if (Feld[i][j] == 0x0)
- Pool[selected].Movement[i, j] = 1;
- else if (Feld[i][j] >= Enemy[0] && Feld[i][j] <= Enemy[1])
- Pool[selected].Movement[i, j] = 2;
- else
- Pool[selected].Movement[i, j] = 5;
- }
- }
- }
- break;
- case PFigur.Typ.Turm:
- try
- {
- for (int i = Pool[selected].position.y + 1; i <= 7; i++)
- {
- if (Feld[i][Pool[selected].position.x] == 0x0)
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 1;
- }
- else if (Feld[i][Pool[selected].position.x] >= Enemy[0] && Feld[i][Pool[selected].position.x] <= Enemy[1])
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.y - 1; i >= 0; i--)
- {
- if (Feld[i][Pool[selected].position.x] == 0x0)
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 1;
- }
- else if (Feld[i][Pool[selected].position.x] >= Enemy[0] && Feld[i][Pool[selected].position.x] <= Enemy[1])
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[i, Pool[selected].position.x] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x - 1; i >= 0; i--)
- {
- if (Feld[Pool[selected].position.y][i] == 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 1;
- }
- else if (Feld[Pool[selected].position.y][i] >= Enemy[0] && Feld[Pool[selected].position.y][i] <= Enemy[1])
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- try
- {
- for (int i = Pool[selected].position.x + 1; i <= 7; i++)
- {
- if (Feld[Pool[selected].position.y][i] == 0x0)
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 1;
- }
- else if (Feld[Pool[selected].position.y][i] >= Enemy[0] && Feld[Pool[selected].position.y][i] <= Enemy[1])
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 2;
- throw new System.Exception();
- }
- else
- {
- Pool[selected].Movement[Pool[selected].position.y, i] = 5;
- throw new System.Exception();
- }
- }
- }
- catch (System.Exception) { }
- break;
- }
- }
- }
- public static void OnSelect(List<PFigur> Pool, int selected)
- {
- OnSelect(Pool, selected, Board.Schachfeld);
- }
- public static void OnSelect(object param)
- {
- object[] args = (object[])param;
- OnSelect((List<PFigur>)args[0], (int)args[1], (int[][])args[2]);
- }
- public Spieler(int ID, KIType type, string pName)
- {
- Name = pName;
- Punkte = 0;
- playerID = ID;
- KITyp = type;
- assignedKI = new KI(this);
- }
- public List<PFigur> FigurSet;
- public int Punkte;
- public bool Schach;
- }
- public class Spielfeld
- {
- public Spieler P1, P2;
- public int activeTurn;
- public int[][] Schachfeld = new int[8][];
- public List<PFigur> wFiguren;
- public List<PFigur> sFiguren;
- public Spielfeld()
- {
- Schachfeld = new int[8][] {
- new int[8] { 0xB, 0xC, 0xD, 0xE, 0xF, 0xD, 0xC, 0xB },
- new int[8] { 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA },
- new int[8] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
- new int[8] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
- new int[8] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
- new int[8] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
- new int[8] { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 },
- new int[8] { 0x2, 0x3, 0x4, 0x5, 0x6, 0x4, 0x3, 0x2 }
- };
- wFiguren = new List<PFigur>();
- for (int i = 0; i < 8; i++)
- wFiguren.Add(new PFigur(PFigur.Typ.Bauer, new PFigur.Position(i, 6), PFigur.Farbe.weiß));
- wFiguren.Add(new PFigur(PFigur.Typ.Turm, new PFigur.Position(0, 7), PFigur.Farbe.weiß));
- wFiguren.Add(new PFigur(PFigur.Typ.Springer, new PFigur.Position(1, 7), PFigur.Farbe.weiß));
- wFiguren.Add(new PFigur(PFigur.Typ.Läufer, new PFigur.Position(2, 7), PFigur.Farbe.weiß));
- wFiguren.Add(new PFigur(PFigur.Typ.König, new PFigur.Position(4, 7), PFigur.Farbe.weiß));
- wFiguren.Add(new PFigur(PFigur.Typ.Dame, new PFigur.Position(3, 7), PFigur.Farbe.weiß));
- wFiguren.Add(new PFigur(PFigur.Typ.Turm, new PFigur.Position(7, 7), PFigur.Farbe.weiß));
- wFiguren.Add(new PFigur(PFigur.Typ.Springer, new PFigur.Position(6, 7), PFigur.Farbe.weiß));
- wFiguren.Add(new PFigur(PFigur.Typ.Läufer, new PFigur.Position(5, 7), PFigur.Farbe.weiß));
- sFiguren = new List<PFigur>();
- for (int i = 0; i < 8; i++)
- sFiguren.Add(new PFigur(PFigur.Typ.Bauer, new PFigur.Position(i, 1), PFigur.Farbe.schwarz));
- sFiguren.Add(new PFigur(PFigur.Typ.Turm, new PFigur.Position(0, 0), PFigur.Farbe.schwarz));
- sFiguren.Add(new PFigur(PFigur.Typ.Springer, new PFigur.Position(1, 0), PFigur.Farbe.schwarz));
- sFiguren.Add(new PFigur(PFigur.Typ.Läufer, new PFigur.Position(2, 0), PFigur.Farbe.schwarz));
- sFiguren.Add(new PFigur(PFigur.Typ.König, new PFigur.Position(4, 0), PFigur.Farbe.schwarz));
- sFiguren.Add(new PFigur(PFigur.Typ.Dame, new PFigur.Position(3, 0), PFigur.Farbe.schwarz));
- sFiguren.Add(new PFigur(PFigur.Typ.Turm, new PFigur.Position(7, 0), PFigur.Farbe.schwarz));
- sFiguren.Add(new PFigur(PFigur.Typ.Springer, new PFigur.Position(6, 0), PFigur.Farbe.schwarz));
- sFiguren.Add(new PFigur(PFigur.Typ.Läufer, new PFigur.Position(5, 0), PFigur.Farbe.schwarz));
- }
- }
- public static Spielfeld Board;
- static void Main(string[] args)
- {
- Board = new Spielfeld();
- Board.activeTurn = 0;
- Board.P1 = new Spieler(0, Spieler.KIType.StaticLearning, "Player 1");
- Board.P1 = new Spieler(1, Spieler.KIType.StaticLearning, "Player 2");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement