private void CreateBoard() { LettersOfChessboard1.RowDefinitions.Add(new RowDefinition()); LettersOfChessboard2.RowDefinitions.Add(new RowDefinition()); NumbersOfChessboard1.ColumnDefinitions.Add(new ColumnDefinition()); NumbersOfChessboard2.ColumnDefinitions.Add(new ColumnDefinition()); for (int i = 0; i < SizeBoard; i++) { Board.RowDefinitions.Add(new RowDefinition()); Board.ColumnDefinitions.Add(new ColumnDefinition()); LettersOfChessboard1.ColumnDefinitions.Add(new ColumnDefinition()); LettersOfChessboard2.ColumnDefinitions.Add(new ColumnDefinition()); NumbersOfChessboard1.RowDefinitions.Add(new RowDefinition()); NumbersOfChessboard2.RowDefinitions.Add(new RowDefinition()); } _sizeCell = Convert.ToInt32(Board.ActualHeight / SizeBoard); bool isEven = false; for (int row = 0; row < SizeBoard; row++) { for (int col = 0; col < SizeBoard; col++) { if (!isEven) { Button button = new Button(); button.Width = button.Height = _sizeCell; button.Background = Brushes.Bisque; button.Foreground = Brushes.Transparent; button.Style = (Style)FindResource("EnableButtonStyle"); button.Padding = new Thickness(0); Grid.SetRow(button, row); Grid.SetColumn(button, col); Board.Children.Add(button); BoardCell cell = new BoardCell(); cell.Button = button; cell.Col = col; cell.Row = row; cell.Background = Brushes.Bisque; _boardCells.Add(cell); isEven = !isEven; } else { Button button = new Button(); button.Width = button.Height = _sizeCell; button.Background = Brushes.Black; button.Style = (Style)FindResource("ButtonStyle"); button.Padding = new Thickness(0); button.Click += ButtonOnClick; Grid.SetRow(button, row); Grid.SetColumn(button, col); Board.Children.Add(button); BoardCell cell = new BoardCell(); cell.Button = button; cell.Col = col; cell.Row = row; cell.Background = Brushes.Black; _boardCells.Add(cell); isEven = !isEven; } } isEven = !isEven; } foreach (var grid in new [] { LettersOfChessboard1, LettersOfChessboard2 }) { for (int col = 0, l = 'A'; col < SizeBoard; col++) { Label block = new Label() { Content = Convert.ToChar(l++).ToString(), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Center, Width = 30, FontSize = 14, FontFamily = new FontFamily("Segoe Script") }; Grid.SetRow(block, 0); Grid.SetColumn(block, col); grid.Children.Add(block); } } foreach (var grid in new[] { NumbersOfChessboard1, NumbersOfChessboard2 }) { for (int row = 0; row < SizeBoard; row++) { Label block = new Label() { Content = (row + 1).ToString(), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, Height = 30, FontSize = 14, FontFamily = new FontFamily("Segoe Script") }; Grid.SetRow(block, row); Grid.SetColumn(block, 0); grid.Children.Add(block); } } }
public bool IsWall( BoardCell element) => Constants.Walls.Contains(element);
private async void ConversationStart() { try { byte[] buffer = new byte[1024]; int readBytes; string message; while (!_tockenSource.Token.IsCancellationRequested) { readBytes = await Task.Factory.FromAsync(_clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, null, null), result => _clientSocket.EndReceive(result)); message = Encoding.GetEncoding(1251).GetString(buffer, 0, readBytes); string[] arr = message.Split('$'); foreach (string msg in arr) { //Log.Text += $"{msg}\r\n"; if (Regex.IsMatch(msg, @"info:(?<InfoMessage>.*)")) { Status.Text = Regex.Match(msg, @"info:(?<InfoMessage>.*)").Groups["InfoMessage"].Value; } else if (Regex.IsMatch(msg, @"Checker\(Row:(?<Row>\d*);Col:(?<Col>\d*);Color:(?<Color>.*)\)")) { StartGameMenuItem.Header = "Отмена"; Match match = Regex.Match(msg, @"Checker\(Row:(?<Row>\d*);Col:(?<Col>\d*);Color:(?<Color>.*)\)"); int row = Convert.ToInt32(match.Groups["Row"].Value); int col = Convert.ToInt32(match.Groups["Col"].Value); BoardCell boardCell = _boardCells.FirstOrDefault(b => b.Row == row && b.Col == col); if (boardCell != null) { Uri uri = new Uri( match.Groups["Color"].Value.Contains("Black") ? "pack://*****:*****@"set_checker_color:(?<CheckerColor>.*)")) { string type = Regex.Match(msg, @"set_checker_color:(?<CheckerColor>.*)") .Groups["CheckerColor"] .Value; _color = type.Contains("Black") ? CheckerColor.Black : CheckerColor.White; } else if (Regex.IsMatch(msg, @"your_turn:(?<Turn>.*)")) { _yourTurn = Regex.Match(msg, @"your_turn:(?<Turn>.*)").Groups["Turn"].Value.Contains("True"); Title = _yourTurn ? "Ваш ход" : "Ход соперника"; } else if (Regex.IsMatch(msg, @"IlluminationStroke\(Row:(?<Row>\d*);Col:(?<Col>\d*)\)")) { Match match = Regex.Match(msg, @"IlluminationStroke\(Row:(?<Row>\d*);Col:(?<Col>\d*)\)"); int row = Convert.ToInt32(match.Groups["Row"].Value); int col = Convert.ToInt32(match.Groups["Col"].Value); BoardCell cell = _boardCells.FirstOrDefault(c => c.Row == row && c.Col == col); if (cell != null) { cell.Button.Background = _backlightСolor; } } else if (Regex.IsMatch(msg, @"Old\[Row:(?<OldRow>\d*);Col:(?<OldCol>\d*)\]New\[Row:(?<NewRow>\d*);Col:(?<NewCol>\d*)\]")) { Match match = Regex.Match(msg, @"Old\[Row:(?<OldRow>\d*);Col:(?<OldCol>\d*)\]New\[Row:(?<NewRow>\d*);Col:(?<NewCol>\d*)\]"); int oldRow = Convert.ToInt32(match.Groups["OldRow"].Value); int oldCol = Convert.ToInt32(match.Groups["OldCol"].Value); BoardCell c1 = _boardCells.FirstOrDefault(c => c.Row == oldRow && c.Col == oldCol); if (c1 != null) { int newRow = Convert.ToInt32(match.Groups["NewRow"].Value); int newCol = Convert.ToInt32(match.Groups["NewCol"].Value); BoardCell c2 = _boardCells.FirstOrDefault(c => c.Row == newRow && c.Col == newCol); if (c2 != null) { c2.Button.Content = c1.Button.Content; c1.Button.Content = null; } } } else if (Regex.IsMatch(msg, @"remove_checker\(Row:(?<Row>\d*);Col:(?<Col>\d*)\)")) { Match match = Regex.Match(msg, @"remove_checker\(Row:(?<Row>\d*);Col:(?<Col>\d*)\)"); int row = Convert.ToInt32(match.Groups["Row"].Value); int col = Convert.ToInt32(match.Groups["Col"].Value); BoardCell cell = _boardCells.FirstOrDefault(c => c.Row == row && c.Col == col); if (cell != null) { cell.Button.Content = null; } } else if (Regex.IsMatch(msg, @"BacklightStroke\(Row:(?<Row>\d*);Col:(?<Col>\d*);Clear:(?<Clear>\d*)\)")) { Match match = Regex.Match(msg, @"BacklightStroke\(Row:(?<Row>\d*);Col:(?<Col>\d*);Clear:(?<Clear>\d*)\)"); int row = Convert.ToInt32(match.Groups["Row"].Value); int col = Convert.ToInt32(match.Groups["Col"].Value); int clear = Convert.ToInt32(match.Groups["Clear"].Value); BoardCell cell = _boardCells.FirstOrDefault(c => c.Row == row && c.Col == col); if (cell != null) { if (clear == 1) { ClearBacklightStroke(); } cell.Button.BorderBrush = Brushes.Red; cell.Button.BorderThickness = new Thickness(3); } } else if (Regex.IsMatch(msg, @"Account\(White:(?<White>\d*);Black:(?<Black>\d*)\)")) { Match match = Regex.Match(msg, @"Account\(White:(?<White>\d*);Black:(?<Black>\d*)\)"); int white = Convert.ToInt32(match.Groups["White"].Value); int black = Convert.ToInt32(match.Groups["Black"].Value); YourCheckerCount.Content = white.ToString(); OponentCheckerCount.Content = black.ToString(); } else if (Regex.IsMatch(msg, @"set_oponent_nickname\((?<NickName>.*);Type:(?<Type>\d*)\)")) { Match match = Regex.Match(msg, @"set_oponent_nickname\((?<NickName>.*);Type:(?<Type>\d*)\)"); int type = Convert.ToInt32(match.Groups["Type"].Value); if (type == 0) { YourNickName.Content = "Пользователь: " + match.Groups["NickName"].Value; } else { NickNameOponent.Content = "Пользователь: " + match.Groups["NickName"].Value; } } else if (Regex.IsMatch(msg, @"GameEnd\((?<Result>.*)\)")) { string result = Regex.Match(msg, @"GameEnd\((?<Result>.*)\)").Groups["Result"].Value; if (result.Equals("win")) { await this.ShowMessageAsync("Результат партии", "Поздравляем Вы выйграли!"); } else if (result.Equals("loss")) { await this.ShowMessageAsync("Результат партии", "Увы Вы проиграли!"); } GameOver(); } else if (Regex.IsMatch(msg, @"change_checker_type\(Row:(?<Row>\d*);Col:(?<Col>\d*);Color:(?<Color>.*)\)")) { Match match = Regex.Match(msg, @"change_checker_type\(Row:(?<Row>\d*);Col:(?<Col>\d*);Color:(?<Color>.*)\)"); int row = Convert.ToInt32(match.Groups["Row"].Value); int col = Convert.ToInt32(match.Groups["Col"].Value); BoardCell cell = _boardCells.FirstOrDefault(c => c.Row == row && c.Col == col); if (cell != null) { Uri uri = new Uri( match.Groups["Color"].Value.Contains("Black") ? "pack://*****:*****@"block\(Row:(?<Row>\d*);Col:(?<Col>\d*);Color:(?<Color>.*)\)")) { Match match = Regex.Match(msg, @"block\(Row:(?<Row>\d*);Col:(?<Col>\d*);Color:(?<Color>.*)\)"); int row = Convert.ToInt32(match.Groups["Row"].Value); int col = Convert.ToInt32(match.Groups["Col"].Value); BoardCell cell = _boardCells.FirstOrDefault(c => c.Row == row && c.Col == col); if (cell != null) { List <BoardCell> cells = _boardCells.Where(b => b.Button.Content != null).ToList(); if (cells != null) { List <BoardCell> blockCells = new List <BoardCell>(); foreach (BoardCell boardCell in cells) { Image image = boardCell.Button.Content as Image; if (image != null) { BitmapImage bitmapImage = image.Source as BitmapImage; if (bitmapImage != null) { if (match.Groups["Color"].Value.Contains("Black")) { if ((bitmapImage.UriSource.Equals(new Uri("pack://application:,,,/Images/BlackCrown.png")) || bitmapImage.UriSource.Equals(new Uri("pack://application:,,,/Images/BlackChecker.png"))) && boardCell != cell) { blockCells.Add(boardCell); } } else if (match.Groups["Color"].Value.Contains("White")) { if ((bitmapImage.UriSource.Equals(new Uri("pack://application:,,,/Images/WhiteCrown.png")) || bitmapImage.UriSource.Equals(new Uri("pack://application:,,,/Images/WhiteChecker.png"))) && boardCell != cell) { blockCells.Add(boardCell); } } } } } foreach (BoardCell boardCell in blockCells) { boardCell.Button.IsEnabled = false; } } } } else if (msg.Contains("clear_blocking")) { foreach (BoardCell cell in _boardCells) { if (cell.Background != Brushes.Bisque) { cell.Button.IsEnabled = true; } } } //else if (msg.Contains("ClearBacklightStroke")) //{ // ClearBacklightStroke(); //} } } } catch (Exception e) { Status.Text = e.Message; } }
/// <summary> /// Returns count of elements with type specified near /// </summary> /// <param name="layer"></param> /// <param name="point"></param> /// <param name="element"></param> /// <returns></returns> public int CountNear(Layer layer, Point point, BoardCell element) { var elements = this.GetNear(layer, point); return(elements.Count(x => x == element)); }