public void UpdateBoardFromGlobalState() { //var vision = GameRules.GetVision(_globalState, _globalState.WhiteTurn, new VisionRules { ViewMoveFields = false, ViewRange = 1 }); string blackKingField = _globalState.GetWhiteKingPos(); string whiteKingField = _globalState.GetBlackKingPos(); foreach (Grid child in this.BoardGrid.Children) { bool a = PosIsWhiteBoardField(child.Name); if (a) { child.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x8F, 0xFF, 0xFF)); } else { //child.Background = new Brush("#FF6F6F6F"); child.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x6F, 0x8F, 0x8F)); } ApplyFieldStateToGrid(child, _globalState.GetFieldAt(child.Name)); if (_globalState.CanSeeField(_clientIsPlayer, child.Name)) { child.Opacity = 1; foreach (UIElement underChiled in child.Children) { underChiled.Visibility = Visibility.Visible; } if (child.Name == _globalState.Selected) { var border = new Border(); border.BorderBrush = Brushes.Red; border.BorderThickness = new Thickness(2, 2, 2, 2); //You can specify here which borders do you want child.Children.Add(border); } else if (_globalState.IsLegalMove(child.Name)) { var border = new Border(); border.BorderBrush = Brushes.Red; border.BorderThickness = new Thickness(3, 3, 3, 3); //You can specify here which borders do you want child.Children.Add(border); } else if (_lastMoveFrom == child.Name || _lastMoveTo == child.Name) { var border = new Border(); border.BorderBrush = Brushes.SeaGreen; border.BorderThickness = new Thickness(3, 3, 3, 3); //You can specify here which borders do you want child.Children.Add(border); } if (_globalState.GetLegalMovesFromField(child.Name).Contains(whiteKingField) && _globalState.CanSeeField(_clientIsPlayer, whiteKingField)) { //messageBox.AppendText("YGetLegalMovesFromField" + "\n"); var border = new Border(); border.BorderBrush = Brushes.Gold; border.BorderThickness = new Thickness(3, 3, 3, 3); //You can specify here which borders do you want child.Children.Add(border); Grid kingGrid = (Grid)BoardGrid.FindName(whiteKingField); border = new Border(); border.BorderBrush = Brushes.Gold; border.BorderThickness = new Thickness(3, 3, 3, 3); //You can specify here which borders do you want kingGrid.Children.Add(border); } else if (_globalState.GetLegalMovesFromField(child.Name).Contains(blackKingField) && _globalState.CanSeeField(_clientIsPlayer, blackKingField)) { //messageBox.AppendText("YGetLegalMovesFromField" + "\n"); var border = new Border(); border.BorderBrush = Brushes.Gold; border.BorderThickness = new Thickness(3, 3, 3, 3); //You can specify here which borders do you want child.Children.Add(border); Grid kingGrid = (Grid)BoardGrid.FindName(blackKingField); border = new Border(); border.BorderBrush = Brushes.Gold; border.BorderThickness = new Thickness(3, 3, 3, 3); //You can specify here which borders do you want kingGrid.Children.Add(border); } } else { child.Visibility = Visibility.Visible; child.Opacity = 0.15; foreach (UIElement underChiled in child.Children) { underChiled.Visibility = Visibility.Hidden; } } } _killedPices.Sort(); foreach (var pice in _killedPices) { AddPiceToGrid(KilledPicesGrid, pice); } }
private void Field_PreviewMouseDown(object sender, MouseButtonEventArgs e) { //sender if (e.LeftButton == MouseButtonState.Pressed) { Grid fieldGrid = (Grid)sender; Field clickState = _globalState.GetFieldAt(fieldGrid.Name); //Early escape not player pice if (_globalState.Selected == null) { if (_globalState.WhiteTurn ? _clientIsPlayer == ClientIsPlayer.Black : _clientIsPlayer == ClientIsPlayer.White) { return; } if ((_globalState.WhiteTurn && Field.HasWhitePice(clickState)) || (!_globalState.WhiteTurn && Field.HasBlackPice(clickState))) { _globalState.Selected = fieldGrid.Name; //_legalMoves = GameRules.GetLegalMoves(_globalState, new FieldState(_globalState.Selected, clickState)); ClearBoard(); UpdateBoardFromGlobalState(); } } else { if (_globalState.IsLegalMove(fieldGrid.Name)) { //(var name, var extraFieldList) = _legalMoves.Find((a) => a.Item1 == fieldGrid.Name); WriteTextNonInvoke("I did move!"); //Pices killedPice = _globalState.DoMoveTo(fieldGrid.Name); //if (killedPice != Pices.Non) _killedPices.Add(killedPice); ChessCom.MatchEvent matchEvent = ChessCom.MatchEvent.Non; //if (killedPice == Pices.WhiteKing) { matchEvent = ChessCom.MatchEvent.BlackWin; } //if (killedPice == Pices.BlackKing) { matchEvent = ChessCom.MatchEvent.WhiteWin; } if (_matchStream?.RequestStream != null) { int timespan; if (_globalState.WhiteTurn) { timespan = (int)(_whiteStaticTimeLeft - _whiteTimeSpan).TotalSeconds; } else { //WriteTextInvoke($"_blackTimeSpan {_blackTimeSpan.TotalSeconds}, _blackStaticTimeLeft {_blackStaticTimeLeft.TotalSeconds}, diff sec {(_blackStaticTimeLeft + _blackTimeSpan).Seconds}"); timespan = (int)(_blackStaticTimeLeft - _blackTimeSpan).TotalSeconds; } _myLastMove = new ChessCom.Move { From = _globalState.Selected, To = fieldGrid.Name, Timestamp = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(DateTime.UtcNow), SecSpent = timespan }; _matchStream.RequestStream.WriteAsync(new ChessCom.MovePacket { AskingForDraw = false, CheatMatchevent = matchEvent, DoingMove = true, MatchToken = _matchToken, Usertoken = _userdata.Usertoken, Move = _myLastMove, }); } //_legalMoves.Clear(); _globalState.Selected = null; //ClearBoard(); //UpdateBoardFromGlobalState(); } // } } else if (e.RightButton == MouseButtonState.Pressed) { //_legalMoves.Clear(); _globalState.Selected = null; ClearBoard(); UpdateBoardFromGlobalState(); } }