public static void MoveFeagure(Point newPosition, Chessman chessman) { RemoveFeagure(chessman); Chessman enemy; if (_board.TryGetValue(newPosition, out enemy)) { if (enemy.Color == chessman.Color) { throw new Exception("Cannibalism"); } _board.Remove(newPosition); } AddFeagure(newPosition, chessman); }
static bool SomebodyCanSave(Chessman king, Point safaPoint) { var saviors = _board.Values.Where( _ => _.Color == king.Color && _.Type != FeagureType.King && _.CanGo(safaPoint)).ToList(); saviors = saviors.Where(_ => !IsKingUnderAttack(king.Color, safaPoint, _, false)).ToList(); Debug.WriteLine("*****BEGING Saviors ******"); foreach (var savior in saviors) { Debug.WriteLine("{0} {1}", savior.Type, savior.CurrentPosition); } Debug.WriteLine("*****END Saviors ******"); return(saviors.Any()); }
public static Chessman GetKingAttacker(Color color, Point?exceptPoint, Chessman chessman, bool showMessage = true) { var king = _board.Values.First(_ => _.Type == FeagureType.King && _.Color == color); var added = false; Chessman removedChessman = null; //TODO if (chessman != null) { _board.Remove(chessman.CurrentPosition); } if (exceptPoint.HasValue && chessman != null) { if (_board.ContainsKey(exceptPoint.Value)) { removedChessman = _board[exceptPoint.Value]; _board.Remove(exceptPoint.Value); } _board.Add(exceptPoint.Value, chessman); added = true; } var enemies = _board.Values.Where(_ => _.Color != color && _.CurrentPosition != exceptPoint).Where(_ => _.CanHit(king.CurrentPosition)).ToList(); foreach (var enemy in enemies) { Debug.WriteLine("{0} {1}", enemy.Color, enemy.Type); } if (added) { _board.Remove(exceptPoint.Value); } if (chessman != null) { _board.Add(chessman.CurrentPosition, chessman); } if (removedChessman != null) { _board.Add(exceptPoint.Value, removedChessman); } if (enemies.Any()) { if (enemies.Count > 1) { throw new FatalityException { WinColor = ColorHelper.GetOpposite(color) } } ; if (showMessage) { MessageBox.Show("Your king need to run!"); } return(enemies.First()); } return(null); }
public static void RemoveFeagure(Chessman chessman) { var entry = _board.First(_ => _.Value == chessman); _board.Remove(entry.Key); }
public static bool IsKingUnderAttack(Color color, Point?exceptPoint, Chessman chessman, bool showMessage = true) { var attacker = GetKingAttacker(color, exceptPoint, chessman, showMessage); return(attacker != null); }
public static void AddFeagure(Point point, Chessman chessman) { _board.Add(point, chessman); }