コード例 #1
0
        private void ChessBoard_MouseClick(object sender, MouseEventArgs e)
        {
            int[] clickCoord = new int[2];
            clickCoord[0] = (int)(((e.X - BEGIN_X) - ((e.X - BEGIN_X) % STEP)) / STEP);
            clickCoord[1] = (int)(((e.Y - BEGIN_Y) - ((e.Y - BEGIN_Y) % STEP)) / STEP);
            Step qurStep = getStepFromCoord(clickCoord);

            if (qurStep != null)
            {
                qurStep.doStep(tile.Figures);
                if (drawCoordBuffer.Count > 0)
                {
                    drawCoordBuffer.Clear();
                }
                if (allowedSteps.Count > 0)
                {
                    allowedSteps.Clear();
                }
            }
            else
            {
                if (drawCoordBuffer.Count > 0)
                {
                    drawCoordBuffer.Clear();
                    Refresh();
                    return;
                }
                Figure qurF = tile.getFigureFromCoord(clickCoord);
                if (qurF == null)
                {
                    return;
                }
                // если мы щёлкнули по фигуре
                switch (qurF.GetType().ToString())
                {
                case "ChessDriver.Figures.Pawn":
                    allowedSteps = ((Pawn)qurF).getAllowedSteps(tile.Figures);
                    break;

                case "ChessDriver.Figures.Knight":
                    allowedSteps = ((Knight)qurF).getAllowedSteps(tile.Figures);
                    break;

                case "ChessDriver.Figures.King":
                    allowedSteps = ((King)qurF).getAllowedSteps(tile.Figures);
                    break;

                case "ChessDriver.Figures.Bishop":
                    allowedSteps = ((Bishop)qurF).getAllowedSteps(tile.Figures);
                    break;

                case "ChessDriver.Figures.Queen":
                    allowedSteps = ((Queen)qurF).getAllowedSteps(tile.Figures);
                    break;

                case "ChessDriver.Figures.Rook":
                    allowedSteps = ((Rook)qurF).getAllowedSteps(tile.Figures);
                    break;
                }
                float[] coordForFigureBorder = getCoordsForDrawing(qurF.Coord);
                drawCoordBuffer.Add("rectangle", new float[2] {
                    coordForFigureBorder[0] + 2, coordForFigureBorder[1] + 4
                });
                foreach (Step step in allowedSteps)
                {
                    float[] coordForDrawSteps = getCoordsForDrawing(step);
                    drawCoordBuffer.Add("point" + allowedSteps.IndexOf(step), new float[2] {
                        coordForDrawSteps[0] + STEP / 2, coordForDrawSteps[1] + STEP / 2
                    });
                }
            }
            Refresh();
        }