Beispiel #1
0
        private void DrawFigures()
        {
            labelWhite.Visible = Game.CurrentPlayer.Color == ChessColor.WHITE;
            labelBlack.Visible = Game.CurrentPlayer.Color == ChessColor.BLACK;
            for (int i = 0; i < dgvBoard.RowCount; i++)
            {
                for (int j = 0; j < dgvBoard.ColumnCount; j++)
                {
                    dgvBoard.Rows[i].Cells[j].Value = string.Empty;
                }
            }
            List <ChessFigure> whiteFigures = Game.WhitePlayer.Figures.Where(f => !f.IsKilled).ToList();
            List <ChessFigure> blackFigures = Game.BlackPlayer.Figures.Where(f => !f.IsKilled).ToList();

            foreach (var whiteFigure in whiteFigures)
            {
                ChessField       field   = whiteFigure.CurrentField;
                Tuple <int, int> indexes = field.GetIndexes();
                string           symbol  = whiteFigure.GetSymbol().ToString();
                dgvBoard.Rows[7 - indexes.Item1].Cells[indexes.Item2].Value = symbol;
            }
            foreach (var blackFigure in blackFigures)
            {
                ChessField       field   = blackFigure.CurrentField;
                Tuple <int, int> indexes = field.GetIndexes();
                string           symbol  = blackFigure.GetSymbol().ToString();
                dgvBoard.Rows[7 - indexes.Item1].Cells[indexes.Item2].Value = symbol;
            }
        }
Beispiel #2
0
        private ChessFigure GetFigure()
        {
            string selectedText = comboFrom.Text;

            if (selectedText.Contains("Roque") || selectedText.Contains("Decision"))
            {
                return(null);
            }

            int         row          = int.Parse(selectedText.Substring(selectedText.Length - 1));
            char        column       = selectedText.ToCharArray()[selectedText.Length - 2];
            ChessField  field        = Game.Board.GetField(row, column);
            ChessFigure figureToMove = null;

            foreach (var figure in Game.CurrentPlayer.Figures)
            {
                if (figure.CurrentField == field)
                {
                    figureToMove = figure;
                    break;
                }
            }
            return(figureToMove);
        }
Beispiel #3
0
        private void buttonMove_Click(object sender, EventArgs e)
        {
            ChessFigure figureToMove = GetFigure();

            if (figureToMove == null)
            {
                if (comboFrom.Text.Contains("Give In"))
                {
                    MessageBox.Show("You lose!");
                    GameBegins();
                    return;
                }
                if (comboFrom.Text.Contains("Draw"))
                {
                    DialogResult result = MessageBox.Show("Other player, do you want to draw?", "Draw proposal",
                                                          MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        MessageBox.Show("Draw!");
                        GameBegins();
                    }
                    return;
                }

                ChessKing        king  = Game.CurrentPlayer.Figures.OfType <ChessKing>().First();
                List <ChessRook> rooks = Game.CurrentPlayer.Figures.OfType <ChessRook>().ToList();
                ChessRook        rook  = null;
                if (comboFrom.Text == "Long Roque")
                {
                    rook = rooks.First(r => r.CurrentField.Column == 'A');
                }
                if (comboFrom.Text == "Short Roque")
                {
                    rook = rooks.First(r => r.CurrentField.Column == 'H');
                }
                if (rook == null)
                {
                    return;
                }
                rook.MakeRoque(Game.Board, king, Game.CurrentMoveNumber);
            }
            else
            {
                char[]      columnAndRow = comboTo.Text.ToCharArray();
                ChessField  field        = Game.Board.GetField(int.Parse(columnAndRow[1].ToString()), columnAndRow[0]);
                ChessPlayer enemy        = Game.CurrentPlayer.Color == ChessColor.WHITE ? Game.BlackPlayer : Game.WhitePlayer;
                figureToMove.Move(field, enemy, Game.Board, Game.CurrentMoveNumber, Game.CurrentPlayer.Figures);
            }

            bool check = Check();
            bool mate  = CheckMate();

            Game.ChangePlayer();
            DrawFigures();
            PrepareFigures();
            if (mate)
            {
                MessageBox.Show("MATE!");
                GameBegins();
            }
            else if (check)
            {
                MessageBox.Show("CHECK!");
            }
        }
Beispiel #4
0
        private void PopulateFields(List <Piece> pieces)
        {
            BoardGrid.Children.RemoveRange(0, BoardGrid.Children.Count);             //Remove all children

            #region Bindings & Field population

            _chessFields = new List <ChessField>();
            bool darkerColor = false;

            for (int y = 1; y <= Helper.BoardSize; y++)
            {
                darkerColor = !darkerColor;                 //Change color
                for (int x = 1; x <= Helper.BoardSize; x++)
                {
                    //Create field
                    ChessField field;
                    if (darkerColor)
                    {
                        field = new ChessField(x, y, Helper.DarkerFieldsColor);
                    }
                    else
                    {
                        field = new ChessField(x, y, Helper.ClearerFieldsColor);
                    }

                    //Each field is a single grid
                    Grid fieldGrid = new Grid();
                    fieldGrid.SetValue(Grid.RowProperty, Helper.BoardSize - y);
                    fieldGrid.SetValue(Grid.ColumnProperty, x - 1);

                    //Each grid contains a rectangle (the color of the field)
                    Rectangle rectangleField = new Rectangle();
                    rectangleField.HorizontalAlignment = HorizontalAlignment.Stretch;
                    rectangleField.Stroke            = Brushes.Black;
                    rectangleField.VerticalAlignment = VerticalAlignment.Stretch;
                    rectangleField.DataContext       = field;
                    rectangleField.SetBinding(Rectangle.FillProperty, "ColorProperty");                     //Bind the color property

                    //Each grid also contains an image (the image of the piece or just an empty image)
                    Image imageField = new Image();
                    imageField.MouseDown += Field_Clicked;                                                             //Click event

                    var pieceOnField = pieces.FirstOrDefault(piece => piece.Position.X == x && piece.Position.Y == y); //Get piece on field
                    if (pieceOnField != null)
                    {
                        imageField.Source = pieceOnField.GetImage();                         //Set image if there was any piece on the field
                    }
                    else
                    {
                        imageField.Source = Helper.EmptyImage;                         //If no piece on the field --> Empty image source
                    }

                    imageField.HorizontalAlignment = HorizontalAlignment.Stretch;
                    imageField.VerticalAlignment   = VerticalAlignment.Stretch;
                    imageField.Tag = x + "," + y;                     //Set the image tag (needed for the click event)

                    //Add dynamically created rectangle and image to field-grid
                    fieldGrid.Children.Add(rectangleField);
                    fieldGrid.Children.Add(imageField);

                    BoardGrid.Children.Add(fieldGrid);           //Add field-grid to the main-grid in the XAML

                    _chessFields.Add(field);                     //Add to list --> needed for the initialization of the board
                    darkerColor = !darkerColor;                  //Change color for the next field
                }
            }



            #endregion
        }