Esempio n. 1
0
        public ChessField OccupyField(int row, char column, Color color)
        {
            ChessField field = GetField(row, column);

            field.Status = color == Color.WHITE ? FieldStatus.OCCUPIED_WITH_WHITE : FieldStatus.OCCUPIED_WITH_BLACK;
            return(field);
        }
Esempio n. 2
0
 internal void ChessFieldColor(ChessField oneChecker, ChessBoard chessBoard)
 {
     foreach (var checker in chessBoard.Chess)
     {
         checker.BackColor = (checker.Enabled && checker != oneChecker) ? Color.Aquamarine : checker.BackColor;
     }
 }
Esempio n. 3
0
 internal void ChessFieldEnabled(ChessField oneChecker, ChessBoard chessBoard)
 {
     foreach (var checker in chessBoard.Chess)
     {
         checker.Enabled = checker == oneChecker;
     }
     //oneChecker.Enabled = true;
 }
Esempio n. 4
0
        public void HorseWays()
        {
            ChessField  chessField = new ChessField(8);
            ChessFigure figure     = new HorseFigure('a', 1);

            chessField.AddFigure(figure);
            Assert.IsTrue(2 == chessField.Go(figure, new HorseFigure('c', 5)));
        }
Esempio n. 5
0
        protected virtual bool CanOccupy(ChessField source, ChessField destination, bool isFinalField)
        {
            if (source.Figure is Knight)
            {
                isFinalField = true;
            }

            return(false);
        }
Esempio n. 6
0
        public override void CanOccupy(ChessField source, ChessBoard helpChessBoard)
        {
            //foreach (var list in lists) list.Clear();
            lists.ToList().ForEach(mm => mm.Clear());
            int x = source.X - 1;
            int y = source.Y - 1;

            for (int x1 = x - 1, y1 = y - 1; x1 >= 0 && y1 >= 0; x1--, y1--)
            {
                if (!FindField(x1, y1, source, helpChessBoard, lists[0]))
                {
                    break;
                }
            }
            for (int x1 = x + 1, y1 = y - 1; x1 <= 7 && y1 >= 0; x1++, y1--)
            {
                if (!FindField(x1, y1, source, helpChessBoard, lists[1]))
                {
                    break;
                }
                // lists[1].Add(helpChessBoard.Chess[x1, y1]);
            }
            for (int x1 = x - 1, y1 = y + 1; x1 >= 0 && y1 <= 7; x1--, y1++)
            {
                if (!FindField(x1, y1, source, helpChessBoard, lists[2]))
                {
                    break;
                }
                // lists[2].Add(helpChessBoard.Chess[x1, y1]);
            }
            for (int x1 = x + 1, y1 = y + 1; x1 <= 7 && y1 <= 7; x1++, y1++)
            {
                if (!FindField(x1, y1, source, helpChessBoard, lists[3]))
                {
                    break;
                }
                // lists[3].Add(helpChessBoard.Chess[x1, y1]);
            }

            foreach (var lista in lists)
            {
                if (lista.Any(lst => lst.Figure is SKing))
                {
                    source.Tag = lista.ToList();
                }
            }
            if (source.Figure is SQueen)
            {
                var c = new SRook();
                c.CanOccupy(source, helpChessBoard);
            }
            if (source.Tag != null)
            {
                var xx = 2;
            }
        }
Esempio n. 7
0
 public ChessMove(Color color, int moveNumber, ChessFigure figure, ChessFigure killed, ChessField initial,
                  ChessField result, bool isRoque = false)
 {
     Color        = color;
     MoveNumber   = moveNumber;
     MovingFigure = figure;
     KilledFigure = killed;
     InitialField = initial;
     ResultField  = result;
     IsRoque      = isRoque;
 }
Esempio n. 8
0
 public ChessBoard()
 {
     Fields = new ChessField[8, 8];
     for (int i = 0; i < 8; i++)     //Идем по линиям
     {
         for (int j = 0; j < 8; j++) //Идем по колонкам
         {
             ChessField field = new ChessField(i, j);
             field.Status = FieldStatus.EMPTY;
             Fields[i, j] = field;
         }
     }
 }
 public ChessBoard(int x, int y, ChessGameManager chessGameManager)
 {
     this.chessGameManager = chessGameManager;
     sizex       = x;
     sizey       = y;
     chessFields = new ChessField[sizex, sizey];
     for (int i = 0; i < sizex; i++)
     {
         for (int j = 0; j < sizey; j++)
         {
             chessFields[i, j] = new ChessField(i, j);
         }
     }
 }
Esempio n. 10
0
        public override void CanOccupy(ChessField source, ChessBoard helpChessBoard)
        {
            int x = source.X - 1;
            int y = source.Y - 1;

            lists.ToList().ForEach(mm => mm.Clear());
            var cc = 3;

            for (int i = y - 1; i >= 0; i--)
            {
                if (!FindField(x, i, source, helpChessBoard, lists[0]))
                {
                    break;
                }
                //lists[0].Add(helpChessBoard.Chess[x, i]);
            }
            for (int i = x - 1; i >= 0; i--)
            {
                if (!FindField(i, y, source, helpChessBoard, lists[1]))
                {
                    break;
                }
                //lists[1].Add(helpChessBoard.Chess[i, y]);
            }
            for (int i = y + 1; i <= 7; i++)
            {
                if (!FindField(x, i, source, helpChessBoard, lists[2]))
                {
                    break;
                }
                //lists[2].Add(helpChessBoard.Chess[x, i]);
            }
            for (int i = x + 1; i <= 7; i++)
            {
                if (!FindField(i, y, source, helpChessBoard, lists[3]))
                {
                    break;
                }
                // lists[3].Add(helpChessBoard.Chess[i, y]);
            }

            foreach (var lista in lists)
            {
                if (lista.Any(lst => lst.Figure is SKing))
                {
                    source.Tag = lista.ToList();
                }
            }
        }
Esempio n. 11
0
        private void KingMove(ChessField source, ChessBoard chessBoard, List <ChessField> list)
        {
            var xxx = chessBoard.Chess1.FindAll(field => (field.Figure != null && !(field.Figure is SKing) && field.Figure.Colour != source.Figure.Colour)).ToList();

            //xxx.ForEach(field => MessageBox.Show(field.Figure.GetType().Name));
            foreach (var cc in xxx)
            {
                cc.Figure.CanOccupy(cc, chessBoard);
            }
            var enemyList = chessBoard.Chess1.Where(field => field.Enabled).ToList();

            list.ForEach(field => field.Enabled      = true);
            enemyList.ForEach(field => field.Enabled = false);
            source.Enabled = true;
        }
Esempio n. 12
0
        public override void CanOccupy(ChessField source, ChessBoard helpChessBoard)
        {
            var x = source.X - 1;
            var y = source.Y - 1;

            Dictionary <int, KnightOccupyDelegates> delegates = new Dictionary <int, KnightOccupyDelegates>
            {
                { 1, new KnightOccupyDelegates {
                      BoolDelegate = (xInt, yInt) => xInt >= 0 && yInt >= 0, XDelegate = (minued) => minued - 1, YDelegate = (minued) => minued - 2
                  } },
                { 2, new KnightOccupyDelegates {
                      BoolDelegate = (xInt, yInt) => xInt >= 0 && yInt <= 7, XDelegate = (minued) => minued - 1, YDelegate = (addend) => addend + 2
                  } },
                { 3, new KnightOccupyDelegates {
                      BoolDelegate = (xInt, yInt) => xInt <= 7 && yInt >= 0, XDelegate = (addend) => addend + 1, YDelegate = (minued) => minued - 2
                  } },
                { 4, new KnightOccupyDelegates {
                      BoolDelegate = (xInt, yInt) => xInt <= 7 && yInt <= 7, XDelegate = (addend) => addend + 1, YDelegate = (addend) => addend + 2
                  } },
                { 5, new KnightOccupyDelegates {
                      BoolDelegate = (xInt, yInt) => xInt <= 7 && yInt >= 0, XDelegate = (addend) => addend + 2, YDelegate = (minued) => minued - 1
                  } },
                { 6, new KnightOccupyDelegates {
                      BoolDelegate = (xInt, yInt) => xInt <= 7 && yInt <= 7, XDelegate = (addend) => addend + 2, YDelegate = (addend) => addend + 1
                  } },
                { 7, new KnightOccupyDelegates {
                      BoolDelegate = (xInt, yInt) => xInt >= 0 && yInt >= 0, XDelegate = (minued) => minued - 2, YDelegate = (minued) => minued - 1
                  } },
                { 8, new KnightOccupyDelegates {
                      BoolDelegate = (xInt, yInt) => xInt >= 0 && yInt <= 7, XDelegate = (minued) => minued - 2, YDelegate = (addend) => addend + 1
                  } },
            };

            for (int i = 1; i < 9; i++)
            {
                if (!delegates[i].BoolDelegate(delegates[i].XDelegate(x), delegates[i].YDelegate(y)))
                {
                    continue;
                }
                var helpDest = helpChessBoard.Chess[delegates[i].XDelegate(x), delegates[i].YDelegate(y)];
                helpDest.Enabled = (helpDest.Figure == null || source.Figure.Colour != helpDest.Figure.Colour);
                if (helpDest.Figure is SKing && helpDest.Figure.Colour != source.Figure.Colour)
                {
                    source.Tag = helpDest;
                }
            }
        }
Esempio n. 13
0
        private void Clicker(object sender, EventArgs e)
        {
            var helpBtn = (ChessField)sender;

            if (_sField == helpBtn)//jezeli klikniemy jeszcze raz ta sama figurę
            {
                game.ChessFieldEnabled(Player, _chessBoard);
                game.ChessFieldColorBack(_chessBoard);
                _sField = null;
                return;
            }

            if (_sField != null) // obsługa przeniesienia figury
            {
                _dField = helpBtn;
                _sField.Figure.Move(_sField.X - 1, _sField.Y - 1, _dField.X - 1, _dField.Y - 1, _chessBoard);

                _chessBoard.Chess1.ForEach(field => field.Tag = null);


                label1.Text = _check.ToString();
                Player      = !Player;
                game.Check(_chessBoard, Player, out gamer.check);
                game.ChessFieldColorBack(_chessBoard);
                game.ChessFieldEnabled(Player, _chessBoard);
                _sField     = null;
                _dField     = null;
                label1.Text = gamer.check.ToString();
                return;
            }

            if (_sField == null)
            {
                _sField = helpBtn;                  // pierwsza klinieta figura i jej potenjalne ruchy
            }
            _sField.BackColor = Color.BlueViolet;
            game.ChessFieldEnabled(_chessBoard);
            game.ChessFieldEnabled(helpBtn, _chessBoard);
            MyFigure figure = (MyFigure)helpBtn.Figure;

            figure.CanOccupy(helpBtn, _chessBoard);

            game.ChessFieldColor(helpBtn, _chessBoard);
        }
Esempio n. 14
0
 private static void DisplayField(ChessField field)
 {
     for (int x = 0; x < field.Width; x++)
     {
         string line = "";
         for (int y = 0; y < field.Height; y++)
         {
             if (field[x, y].Color == CellColors.White)
             {
                 line += field.DisplaySymbols.WhiteSymb;
             }
             else
             {
                 line += field.DisplaySymbols.BlackSymb;
             }
         }
         Console.WriteLine(line);
     }
 }
Esempio n. 15
0
 public override void CanOccupy(ChessField source, ChessBoard helpChessBoard)
 {
     kingList.Clear();
     for (int i = source.X - 2; i < source.X + 1; i++)
     {
         for (int j = source.Y - 2; j < source.Y + 1; j++)
         {
             if (i >= 0 && i < 8 && j >= 0 && j < 8)
             {
                 var helpDest = helpChessBoard.Chess[i, j];
                 if (helpDest.Figure is SKing || helpDest.Figure == null || source.Figure.Colour != helpDest.Figure.Colour)
                 {
                     kingList.Add(helpDest);
                 }
             }
         }
     }
     KingMove(source, helpChessBoard, kingList);
 }
    public ChessBoard(ChessGameManager chessGameManager)
    {
        this.chessGameManager = chessGameManager;
        sizex = 8;
        sizey = 8;
        // creates the standardchessboardwith all pieces;
        chessFields = new ChessField[8, 8];
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                chessFields[i, j] = new ChessField(i, j);
            }
        }


        ChessAgent playerAgent   = RegisterAgent("Player", Color.white);
        ChessAgent computerAgent = RegisterAgent("Computergegner", Color.black);

        // register pawns:
        for (int i = 0; i < 8; i++)
        {
            playerAgent.RegisterPiece(ChessPiece.PieceType.PAWN, new Pos2D(i, 1));
            computerAgent.RegisterPiece(ChessPiece.PieceType.PAWN, new Pos2D(i, 6));
        }
        playerAgent.RegisterPiece(ChessPiece.PieceType.ROOK, new Pos2D(0, 0));
        playerAgent.RegisterPiece(ChessPiece.PieceType.ROOK, new Pos2D(7, 0));
        playerAgent.RegisterPiece(ChessPiece.PieceType.KNIGHT, new Pos2D(1, 0));
        playerAgent.RegisterPiece(ChessPiece.PieceType.KNIGHT, new Pos2D(6, 0));
        playerAgent.RegisterPiece(ChessPiece.PieceType.BISHOP, new Pos2D(2, 0));
        playerAgent.RegisterPiece(ChessPiece.PieceType.BISHOP, new Pos2D(5, 0));
        playerAgent.RegisterPiece(ChessPiece.PieceType.QUEEN, new Pos2D(3, 0));
        playerAgent.RegisterPiece(ChessPiece.PieceType.KING, new Pos2D(4, 0));

        computerAgent.RegisterPiece(ChessPiece.PieceType.ROOK, new Pos2D(0, 7));
        computerAgent.RegisterPiece(ChessPiece.PieceType.ROOK, new Pos2D(7, 7));
        computerAgent.RegisterPiece(ChessPiece.PieceType.KNIGHT, new Pos2D(1, 7));
        computerAgent.RegisterPiece(ChessPiece.PieceType.KNIGHT, new Pos2D(6, 7));
        computerAgent.RegisterPiece(ChessPiece.PieceType.BISHOP, new Pos2D(2, 7));
        computerAgent.RegisterPiece(ChessPiece.PieceType.BISHOP, new Pos2D(5, 7));
        computerAgent.RegisterPiece(ChessPiece.PieceType.QUEEN, new Pos2D(3, 7));
        computerAgent.RegisterPiece(ChessPiece.PieceType.KING, new Pos2D(4, 7));
    }
Esempio n. 17
0
        public Form1()
        {
            InitializeComponent();
            ratio = new double[4, Controls.Count];

            for (int i = 0; i < Controls.Count; i++)
            {
                ratio[0, i] = (double)Controls[i].Size.Width / Size.Width;
                ratio[1, i] = (double)Controls[i].Size.Height / Size.Height;
                ratio[2, i] = (double)Controls[i].Location.X / Size.Width;
                ratio[3, i] = (double)Controls[i].Location.Y / Size.Height;
            }

            chess_field            = new ChessField();
            chess_field.indicatior = L_indicator;
            chess_field.CreateField(GB_MainField);

            no_resize = false;
            this.Size = new Size(1280, 720);
        }
Esempio n. 18
0
        public override void CanOccupy(ChessField source, ChessBoard helpChessBoard)
        {
            int x = source.X - 1;
            int y = source.Y - 1;
            var b = source.Figure.Colour ? y + 1 : 7;
            var c = source.Figure.Colour ? 6 : 1;

            if (y == c)
            {
                c = source.Figure.Colour ? -1 : 1;
                if (helpChessBoard.Chess[x, y + c].Figure != null)
                {
                    helpChessBoard.Chess[x, y + c].Enabled = true;
                }
                if (helpChessBoard.Chess[x, y + c].Figure == null && helpChessBoard.Chess[x, y + 2 * c].Figure == null)
                {
                    helpChessBoard.Chess[x, y + 2 * c].Enabled = true;
                }
            }
            c = source.Figure.Colour ? 0 : y - 1;
            if (c < b)
            {
                c = source.Figure.Colour ? -1 : 1;
                var helpDest = helpChessBoard.Chess[x, y + c].Figure;
                helpChessBoard.Chess[x, y + c].Enabled = helpDest == null;

                helpDest = x > 0 ? helpChessBoard.Chess[x - 1, y + c].Figure : null;
                if (helpDest != null)
                {
                    helpChessBoard.Chess[x - 1, y + c].Enabled = x - 1 >= 0 && helpDest.Colour != source.Figure.Colour;
                }

                helpDest = x < 7 ? helpChessBoard.Chess[x + 1, y + c].Figure : null;
                if (helpDest != null)
                {
                    helpChessBoard.Chess[x + 1, y + c].Enabled = x + 1 < 8 && helpDest.Colour != source.Figure.Colour;
                }
            }
        }
Esempio n. 19
0
 protected override bool CanOccupy(ChessField source, ChessField destination, bool isFinalField)
 {
     return(false);
 }
Esempio n. 20
0
        private void PopulateFields(List <Piece> pieces)
        {
            BoardGrid.Children.RemoveRange(0, BoardGrid.Children.Count);             //Remove all children

            if (BoardGrid.ColumnDefinitions.Count > 0)
            {
                BoardGrid.ColumnDefinitions.RemoveRange(0, BoardGrid.ColumnDefinitions.Count);
            }
            if (BoardGrid.RowDefinitions.Count > 0)
            {
                BoardGrid.RowDefinitions.RemoveRange(0, BoardGrid.RowDefinitions.Count);
            }

            #region Bindings & Field population

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

            //Create grid
            for (int i = 0; i < Helper.BoardSize; i++)
            {
                var column = new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                };
                var row = new RowDefinition {
                    Height = new GridLength(1, GridUnitType.Star)
                };
                BoardGrid.ColumnDefinitions.Add(column);
                BoardGrid.RowDefinitions.Add(row);
            }

            //Create fields
            for (int y = 1; y <= Helper.BoardSize; y++)
            {
                darkerColor = !darkerColor;                 //Change color
                for (int x = 1; x <= Helper.BoardSize; x++)
                {
                    var pieceOnField = pieces.FirstOrDefault(piece => piece.Position.X == x && piece.Position.Y == y);                     //Get piece on field

                    #region Create Chess Field
                    SolidColorBrush color;
                    ImageSource     image;
                    if (darkerColor)
                    {
                        color = Helper.DarkerFieldsColor;
                    }
                    else
                    {
                        color = Helper.ClearerFieldsColor;
                    }

                    if (pieceOnField == null)
                    {
                        image = Helper.EmptyImage;
                    }
                    else
                    {
                        image = pieceOnField.GetImage();
                    }
                    ChessField chessField = new ChessField(x, y, color, image);                     //Create field

                    #endregion

                    //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       = chessField;
                    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.DataContext = chessField;
                    imageField.SetBinding(Image.SourceProperty, "ImageSource"); //Binding
                    imageField.MouseDown          += Field_Clicked;             //Click event
                    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(chessField);                   //Add to list --> needed for the initialization of the board
                    darkerColor = !darkerColor;                     //Change color for the next field
                }
            }

            _board = new Board(pieces, _chessFields);

            #endregion
        }
Esempio n. 21
0
 public void EmptyField(ChessField field)
 {
     field.Status = FieldStatus.EMPTY;
 }