private void handleBoxMouseDown(object sender, MouseButtonEventArgs e) { if (Game.Latrunculi.ActualPlayer != null) { return; } var box = sender as Box.Box; if (box == null) { return; } if (ActivePosition == box.Position) { ActivePosition = null; return; } var targetState = Game.Latrunculi.Desk.GetState(box.Position); if (targetState == Game.Latrunculi.HistoryManager.ActualPlayer) { ActivePosition = box.Position; return; } if (ActivePosition != null && targetState == ChessBoxState.Empty) { try { Game.Latrunculi.Rules.Move(Game.Latrunculi.HistoryManager.ActualPlayer, new Move(ActivePosition, box.Position)); Game.TryTurn(); } catch (Exception exception) { MessageBox.Show(exception.Message, "Chyba při vykonávání tahu"); }; ActivePosition = null; return; } }
private void printCheckBox(ChessBoxPosition position) { var lastChange = historyManager.LastStep?.Changes.Find(change => change.Position.Index == position.Index); var state = desk.PlayingDesk[position.X, position.Y]; ConsoleColor?color = null; ConsoleColor?backgroundColor = null; string symbol; if (lastChange != null) { if (lastChange.IsCaptured == true) { backgroundColor = ConsoleColor.Red; } else if (lastChange.NewState == ChessBoxState.Empty) { backgroundColor = ConsoleColor.DarkGray; } else if (lastChange.NewState == state) { color = ConsoleColor.Green; } } switch (state) { case ChessBoxState.Black: symbol = "b"; break; case ChessBoxState.White: symbol = "w"; break; case ChessBoxState.Empty: default: symbol = " "; break; } Program.WriteColored($" {symbol} ", color, backgroundColor); }
public Move Turn(LatrunculiApp latrunculi, ChessBoxState player, CancellationToken ct = default) { Console.Write("Váš tah (start cíl): "); string line = Console.ReadLine().Trim().ToLower(); if (commandManager.CheckCommand(line)) { return(null); } var moveMatch = Regex.Match(line, @"^(?<from>[a-zA-Z][1-9])\s*(?<to>[a-zA-Z][1-9])$"); if (moveMatch.Success) { ChessBoxPosition from = ChessBoxPosition.FromString(deskSize, moveMatch.Groups["from"].Value); ChessBoxPosition to = ChessBoxPosition.FromString(deskSize, moveMatch.Groups["to"].Value); return(new Move(from, to)); } else { throw new InvalidCastException("Je vyžadován tah ve formátu start cíl, např. E4 E5"); } }
private string getCheckBox(LatrunculiApp latrunculi, ChessBoxPosition position) { var lastChange = latrunculi.HistoryManager.LastStep?.Changes.Find(change => change.Position.Index == position.Index); var state = latrunculi.Desk.PlayingDesk[position.X, position.Y]; string symbol; switch (state) { case ChessBoxState.Black: symbol = "b"; break; case ChessBoxState.White: symbol = "w"; break; case ChessBoxState.Empty: default: symbol = " "; break; } if (lastChange != null) { if (lastChange.IsCaptured == true) { symbol = "X"; } else if (lastChange.NewState == ChessBoxState.Empty) { symbol = "_"; } else if (lastChange.NewState == state) { symbol = symbol.ToUpper(); } } return(symbol); }
private IEnumerable <Move> getValidMovesFromPosition(ChessBoxPosition position) { return(from ChessBoxPosition newPosition in position.GetNeighbors() where desk.GetState(newPosition) == ChessBoxState.Empty select new Move(position, newPosition)); }
private IEnumerable <ChessBoxPosition> getNeighborsByPlayer(ChessBoxState player, ChessBoxPosition position, NeighborDirection direction = NeighborDirection.All) { return(from ChessBoxPosition neighborPosition in position.GetNeighbors(direction) where desk.GetState(neighborPosition) == player select neighborPosition); }
private IEnumerable <ChessBoxPosition> getEnemiesToRemoveByDirection(ChessBoxState actualPlayer, ChessBoxPosition position, NeighborDirection direction) { var enemyPlayer = getEnemyPlayer(actualPlayer); foreach (var enemy in getNeighborsByPlayer(enemyPlayer, position, direction)) { if (enemy.IsCorner && getNeighborsByPlayer(actualPlayer, enemy).Count() == 1 || getNeighborsByPlayer(actualPlayer, enemy, direction).Count() == 1) { yield return(enemy); } } }