コード例 #1
0
ファイル: ChessBoardControl.cs プロジェクト: mygibbs/Chess
        /// <summary>
        /// Initialize the board.
        /// </summary>
        private void Initialize()
        {
            this.Board.GameEnded += (b, w) =>
                {
                    this.Repaint();
                    this.GameEnded.IfNotNull(a => a(b, w));
                };

            this.Squares = new ChessSquareControl[8, 8];

            for (int rank = 0; rank < 8; rank++)
            {
                int rankR = 7 - rank;

                this.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });

                Grid g = new Grid();
                Grid.SetRow(g, rankR);

                for (int file = 0; file < 8; file++)
                {
                    g.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });

                    ChessSquareControl sq = new ChessSquareControl(this, rank + 1, file + 1);
                    sq.MouseUp += this.SquareClicked;

                    Grid.SetColumn(sq, file);
                    g.Children.Add(sq);
                    this.Squares[rank, file] = sq;
                }

                this.Children.Add(g);
            }

            this.Repaint();
        }
コード例 #2
0
ファイル: ChessBoardControl.cs プロジェクト: mygibbs/Chess
        private void SquareClicked(object o, RoutedEventArgs ea)
        {
            if (!this.Turn)
            {
                this.LastClicked = null;
                this.Repaint();
                return;
            }

            ChessSquareControl sq = (ChessSquareControl)o;

            if (this.LastClicked == null)
            {
                if (sq.Square.Piece == null) return;
                if (sq.Square.Piece.Color != this.Board.Turn) return;

                Square[] moves = sq.Square.Piece.TotallyValidMoves.ToArray();

                if (moves.Length == 0) return;

                foreach (Square s in moves)
                {
                    this.Squares[s.Location.Rank - 1, Location.ConvertFile(s.Location.File) - 1].SetBackground(this.SelectBrushDark, this.SelectBrushLight);
                }

                this.LastClicked = sq;
            }
            else if (this.LastClicked == sq)
            {
                this.Repaint();
                this.LastClicked = null;
            }
            else
            {
                Move m = new Move(this.LastClicked.Square.Location, sq.Square.Location);
                if (this.Moved(this, m)) this.LastClicked.Square.To(sq.Square);

                this.Repaint();
                this.LastClicked = null;
            }
        }
コード例 #3
0
ファイル: ChessBoardControl.cs プロジェクト: mygibbs/Chess
 /// <summary>
 /// Clears the current selected square.
 /// </summary>
 public void ClearSelected()
 {
     this.LastClicked = null;
 }