Exemplo n.º 1
0
        private void BoardClick(object sender, EventArgs e)
        {
            MouseEventArgs mouseEvent = (MouseEventArgs)e;

            char[,] boardTab = board.GetBoardTab();

            int posi = (int)Math.Floor((double)mouseEvent.Y / boxSize);
            int posj = (int)Math.Floor((double)mouseEvent.X / boxSize);

            bool currentColor = board.GetColorTurn();

            foreach (Human human in player)
            {
                if (human.GetColor() == currentColor)
                {
                    if (movesToDisp.Count == 0)
                    {
                        if ((Char.IsLower(boardTab[posi, posj]) && currentColor == false) || (Char.IsUpper(boardTab[posi, posj]) && currentColor == true))
                        {
                            pieceCoord = board.IjToCoord(posi, posj);

                            movesToDisp = board.GetMove(pieceCoord);

                            Refresh();
                        }
                    }
                    else
                    {
                        moveCoord = board.IjToCoord(posi, posj);

                        if (movesToDisp.FindIndex(board.IjToCoord(posi, posj).Contains) >= 0)
                        {
                            board.SetPieceCoord(pieceCoord, moveCoord);
                            lastBoardArr = (char[, ])board.GetBoardTab().Clone();
                            human.IncrementMove();
                            movesToDisp.Clear();
                        }
                        else
                        {
                            movesToDisp.Clear();
                        }

                        Refresh();
                    }
                }
            }
        }
Exemplo n.º 2
0
        BoardForm(Board initBoard)
        {
            InitializeComponent();

            board        = initBoard;
            lastBoardArr = (char [, ])board.GetBoardTab().Clone();

            char[,] boardTab = board.GetBoardTab();

            player = board.GetHumanPlayer();

            movesToDisp = new List <string>();

            boardSize = boardDisp.Size;
            nbBoxEdge = (int)Math.Sqrt(boardTab.Length);
            boxSize   = boardSize.Width / nbBoxEdge;

            piecesImage = new Dictionary <PieceEnum, Bitmap>();

            lightColor = Color.FromArgb(238, 238, 210);
            darkColor  = Color.FromArgb(118, 150, 86);

            Bitmap sprite    = new Bitmap("../../pieces_sprite.png");
            int    pieceSize = sprite.Height / 2;
            int    cropx     = 0;
            int    cropy     = 0;


            foreach (PieceEnum piece in Enum.GetValues(typeof(PieceEnum)))
            {
                switch (piece)
                {
                case PieceEnum.WhiteKing:
                    cropx = 0 * pieceSize;
                    cropy = 0 * pieceSize;
                    break;

                case PieceEnum.WhiteQueen:
                    cropx = 1 * pieceSize;
                    cropy = 0 * pieceSize;
                    break;

                case PieceEnum.WhiteRook:
                    cropx = 4 * pieceSize;
                    cropy = 0 * pieceSize;
                    break;

                case PieceEnum.WhiteKnight:
                    cropx = 3 * pieceSize;
                    cropy = 0 * pieceSize;
                    break;

                case PieceEnum.WhiteBishop:
                    cropx = 2 * pieceSize;
                    cropy = 0 * pieceSize;
                    break;

                case PieceEnum.WhitePawn:
                    cropx = 5 * pieceSize;
                    cropy = 0 * pieceSize;
                    break;

                case PieceEnum.BlackKing:
                    cropx = 0 * pieceSize;
                    cropy = 1 * pieceSize;
                    break;

                case PieceEnum.BlackQueen:
                    cropx = 1 * pieceSize;
                    cropy = 1 * pieceSize;
                    break;

                case PieceEnum.BlackRook:
                    cropx = 4 * pieceSize;
                    cropy = 1 * pieceSize;
                    break;

                case PieceEnum.BlackKnight:
                    cropx = 3 * pieceSize;
                    cropy = 1 * pieceSize;
                    break;

                case PieceEnum.BlackBishop:
                    cropx = 2 * pieceSize;
                    cropy = 1 * pieceSize;
                    break;

                case PieceEnum.BlackPawn:
                    cropx = 5 * pieceSize;
                    cropy = 1 * pieceSize;
                    break;
                }

                piecesImage.Add(piece, ResizeImage(CropImage(sprite, cropx, cropy, pieceSize, pieceSize), boxSize, boxSize));
            }
        }