private void InitializeList() { for (int i = 0; i < List.Count; i++) { List[i] = new TriangleUiEl(); if (i < List.Count / 2) { List[i].Path = i % 2 == 0 ? CreateDownTriangle(new SolidColorBrush() { Color = Colors.Gray }) : CreateDownTriangle(new SolidColorBrush() { Color = Colors.Red }); } else { List[i].Path = i % 2 == 0 ? CreateUpTriangle(new SolidColorBrush() { Color = Colors.Gray }) : CreateUpTriangle(new SolidColorBrush() { Color = Colors.Red }); } List[i].StackPanel = i < List.Count / 2 ? new StackPanel() { VerticalAlignment = VerticalAlignment.Bottom } : new StackPanel() { }; List[i].Path.MouseLeftButtonDown += Triangle_Click; List[i].IsRed = i % 2 == 1; } }
private static void AddPack(TriangleUiEl triangle, Ellipse pack) { if (triangle.Ellipses.Count < 5) { triangle.StackPanel.Children.Add(pack); } else if (triangle.Ellipses.Count == 5) { if (triangle.StackPanel.VerticalAlignment == VerticalAlignment.Bottom) { AddTextForBottomTri(triangle, 6); } else { triangle.StackPanel.Children.Add(new TextBlock { Text = "6", TextAlignment = TextAlignment.Center, FontSize = 15 }); } } else { var textAmount = ImplementOptionsForGrid.GetChildOfType <TextBlock>(triangle.StackPanel); var amount = textAmount.Text; textAmount.Text = (int.Parse(amount) + 1).ToString(); } triangle.Ellipses.Add(pack); }
private static void AddTextForBottomTri(TriangleUiEl triangle, int amount) { var chidren = triangle.StackPanel.Children; triangle.StackPanel.Children.Clear(); triangle.StackPanel.Children.Add(new TextBlock { Text = "6", TextAlignment = TextAlignment.Center, FontSize = 15 }); for (int i = 0; i < chidren.Count; i++) { triangle.StackPanel.Children.Add(chidren[i]); } }
private void AddCheckers(int amount, SolidColorBrush checkerStyle, TriangleUiEl tr) { for (int i = 0; i < amount; i++) { var pack = new Ellipse { Fill = checkerStyle, MaxWidth = 40, MaxHeight = 40, Stretch = Stretch.UniformToFill }; pack.MouseLeftButtonDown += Triangle_Click;; tr.Ellipses.Add(pack); tr.StackPanel.Children.Add(pack); } }
private void RemovePack(UIElement pack, TriangleUiEl triangle) { triangle.Ellipses.Remove((Ellipse)pack); if (triangle.Ellipses.Count < 5) { triangle.StackPanel.Children.Remove(pack); } else if (triangle.Ellipses.Count == 5) { var textAmount = ImplementOptionsForGrid.GetChildOfType <TextBlock>(triangle.StackPanel); triangle.StackPanel.Children.Remove(textAmount); } else { var textAmount = ImplementOptionsForGrid.GetChildOfType <TextBlock>(triangle.StackPanel); var amount = textAmount.Text; textAmount.Text = (int.Parse(amount) - 1).ToString(); } }
private void Triangle_Click(object sender, MouseButtonEventArgs e) { if (MovingFromBar) { TriangleUiEl triangle = null; if (sender is Ellipse ellipse) { triangle = List.FirstOrDefault(s => s.Ellipses.Any(c => c.Equals(ellipse))); } if (sender is Path path) { triangle = List.FirstOrDefault(s => s.Path.Equals(path)); } var clickedTriangle = GetIndexForTriangle(triangle); if (BackgammonLogic.IsLegalFinalMove(clickedTriangle)) // trying to move to his color or empty slot { BackgammonLogic.GetOptionsForInitialSet().ForEach(num => { List[num].Path.Fill = List[num].IsRed ? GetColor(Colors.Red) : GetColor(Colors.Gray); }); BackgammonLogic.SetPlayerFinalMove(clickedTriangle, false); try { var pack = BackgammonLogic._RedPlayer.IsMyTurn ? Bar.RemoveRedChecker() : Bar.RemoveBlackChecker(); AddPack(triangle, pack); } catch { } } else if (BackgammonLogic.IsLegalFinalMoveEat(clickedTriangle)) // trying to eat. { BackgammonLogic.GetOptionsForInitialSet().ForEach(num => { List[num].Path.Fill = List[num].IsRed ? GetColor(Colors.Red) : GetColor(Colors.Gray); }); BackgammonLogic.SetPlayerFinalMoveEat(clickedTriangle); try { var pack = BackgammonLogic._RedPlayer.IsMyTurn ? Bar.RemoveRedChecker() : Bar.RemoveBlackChecker(); var packEaten = triangle.Ellipses.Last(); var ind = GetIndexForTriangle(triangle); RemovePack(packEaten, triangle); if (BackgammonLogic.GameBoard.Triangles[ind].CheckersColor == CheckerColor.Red) { Bar.AddRedChecker(packEaten); } else { Bar.AddBlackChecker(packEaten); } AddPack(triangle, pack); } catch { } } else { return; } MovingFromBar = false; } if (!MovingChecker) { TriangleUiEl triangle = null; if (sender is Path p) { triangle = List.FirstOrDefault(t => t.Path.Equals(p)); } if (sender is Ellipse ellipse) { triangle = List.FirstOrDefault(t => t.Ellipses.Any(c => c.Equals(ellipse))); } if (triangle == null) { return; } Current = triangle; int index = GetIndexForTriangle(triangle); if ((BackgammonLogic._RedPlayer.IsMyTurn && BackgammonLogic.GameBoard.GameBar.NumOfRedCheckers > 0) || (BackgammonLogic._BlackPlayer.IsMyTurn && BackgammonLogic.GameBoard.GameBar.NumOfBlackCheckers > 0)) { var ind = BackgammonLogic._RedPlayer.IsMyTurn ? -1 : 24; BackgammonLogic.SetPlayerInitialMove(ind); MovingFromBar = true; //BackgammonLogic.GetOptionsForBarOff().ForEach(num => { List[num].Path.Style = Styles.triangleCheck; }); return; } else if (BackgammonLogic.PlayerHasAvailableMoves()) { if (BackgammonLogic.GetOptionsForInitialMove(index).Count > 0) { BackgammonLogic.SetPlayerInitialMove(index); MovingChecker = true; BackgammonLogic.GetOptionsForInitialSet().ForEach(num => { List[num].Path.Fill = GetColor(Colors.Green); }); return; } } } else { TriangleUiEl triangle = null; if (sender is Ellipse ellipse) { triangle = List.FirstOrDefault(s => s.Ellipses.Any(c => c.Equals(ellipse))); } if (sender is Path path) { triangle = List.FirstOrDefault(s => s.Path.Equals(path)); } var clickedTriangle = GetIndexForTriangle(triangle); if (BackgammonLogic.PlayerInitialTriangleChoice == clickedTriangle) { MovingChecker = false; BackgammonLogic.GetOptionsForInitialSet().ForEach(num => { List[num].Path.Fill = List[num].IsRed ? GetColor(Colors.Red) : GetColor(Colors.Gray); }); return; } else if (BackgammonLogic.IsLegalFinalMove(clickedTriangle)) // trying to move to his color or empty slot { BackgammonLogic.GetOptionsForInitialSet().ForEach(num => { List[num].Path.Fill = List[num].IsRed ? GetColor(Colors.Red) : GetColor(Colors.Gray); }); BackgammonLogic.SetPlayerFinalMove(clickedTriangle, false); try { var pack = Current.Ellipses.Last(); RemovePack(pack, Current); AddPack(triangle, pack); } catch (Exception ex) { Debug.WriteLine(ex.Message); } } else if (BackgammonLogic.IsLegalFinalMoveEat(clickedTriangle)) // trying to eat. { BackgammonLogic.GetOptionsForInitialSet().ForEach(num => { List[num].Path.Fill = List[num].IsRed ? GetColor(Colors.Red) : GetColor(Colors.Gray); }); BackgammonLogic.SetPlayerFinalMoveEat(clickedTriangle); try { var pack = Current.Ellipses.Last(); RemovePack(pack, Current); var packEaten = triangle.Ellipses.Last(); RemovePack(packEaten, triangle); if (BackgammonLogic._BlackPlayer.IsMyTurn) { Bar.AddRedChecker(packEaten); } else { Bar.AddBlackChecker(packEaten); } AddPack(triangle, pack); } catch { } } else { return; } MovingChecker = false; } }
private int GetIndexForTriangle(TriangleUiEl triangle) { return(List.FindIndex((TriangleUiEl tr) => { return tr.Equals(triangle); })); }