Ejemplo n.º 1
0
 public ChessStep(int fr, int fc, int tr, int tc, Figure.StepType t)
 {
     fromRow     = fr;
     fromCol     = fc;
     toRow       = tr;
     toCol       = tc;
     type        = t;
 }
Ejemplo n.º 2
0
        // Megadja a legális lépéseket
        public static LinkedList<int[]> getLegalSteps(Board board, Figure figure, bool fromCheckTest)
        {
            if (figure == null || figure.getFigureType() == FigureType.Nothing)
                return null;

            LinkedList<int[]>  LegalSteps       = new LinkedList<int[]>();
            FigureType figureType               = figure.getFigureType();

            bool					white		= figure.isWhite();
            int                     j           = -1;
            LinkedList<int[]>       steps       = new LinkedList<int[]>();

            int fromRow                         = figure.getRow();
            int fromCol                         = figure.getCol();

            switch (figureType)
            {
            case FigureType.King:

                StepType ans;

                steps.AddLast(new int[] { fromRow - 1, fromCol - 1, 0 });
                steps.AddLast(new int[] { fromRow - 1, fromCol, 0 });
                steps.AddLast(new int[] { fromRow - 1, fromCol + 1, 0 });
                steps.AddLast(new int[] { fromRow, fromCol - 1, 0 });
                steps.AddLast(new int[] { fromRow, fromCol + 1, 0 });
                steps.AddLast(new int[] { fromRow + 1, fromCol - 1, 0 });
                steps.AddLast(new int[] { fromRow + 1, fromCol, 0 });
                steps.AddLast(new int[] { fromRow + 1, fromCol + 1, 0 });

                foreach (int[] step in steps)
                {
                    ans = Figure.isLegalStep(board, fromRow, fromCol, step[0], step[1]);
                    if (ans == StepType.Success)
                    {
                        step[2] = 1;
                        LegalSteps.AddLast(step);
                    } else if (ans == StepType.Capture)
                    {
                        step[2] = 2;
                        LegalSteps.AddLast(step);
                    } else if (ans == StepType.CaptureKing)
                    {
                        step[2] = 3;
                        LegalSteps.AddLast(step);
                    }
                }

                break;

            case FigureType.Queen:

                steps.AddLast( new int[] {1,	0,	0 } );
                steps.AddLast( new int[] {-1,	0,	0 } );
                steps.AddLast( new int[] {0,	1,	0 } );
                steps.AddLast( new int[] {0,	-1,	0 } );
                steps.AddLast( new int[] {1,	1,	0 } );
                steps.AddLast( new int[] {1,	-1,	0 } );
                steps.AddLast( new int[] {-1,	1,	0 } );
                steps.AddLast( new int[] {-1,	-1,	0 } );

                foreach (int[] step in steps)
                {
                    j   = 0;
                    while (j >= 0)
                    {
                        ++j;
                        ans = Figure.isLegalStep(board, fromRow, fromCol, fromRow + step[0] * j, fromCol + step[1] * j);
                        if (ans == StepType.Success)				LegalSteps.AddLast( new int[] {fromRow + step[0] * j, fromCol + step[1] * j, 1} );
                        else if (ans == StepType.Capture){		    LegalSteps.AddLast( new int[] {fromRow + step[0] * j, fromCol + step[1] * j, 2} ); j = -1; }
                        else if (ans == StepType.CaptureKing){	    LegalSteps.AddLast( new int[] {fromRow + step[0] * j, fromCol + step[1] * j, 3} ); j = -1; }
                        else if (ans == StepType.Failure)		    j = -1;
                    }
                }

                break;

            case FigureType.Rook:
                steps.AddLast( new int[] {1,	0,	0 } );
                steps.AddLast( new int[] {-1,	0,	0 } );
                steps.AddLast( new int[] {0,	1,	0 } );
                steps.AddLast( new int[] {0,	-1,	0 } );

                foreach (int[] step in steps)
                {
                    j   = 0;
                    while (j >= 0)
                    {
                        ++j;
                        ans = Figure.isLegalStep(board, fromRow, fromCol, fromRow + step[0] * j, fromCol + step[1] * j);
                        if (ans == StepType.Success)				LegalSteps.AddLast( new int[] {fromRow + step[0] * j, fromCol + step[1] * j, 1} );
                        else if (ans == StepType.Capture){		    LegalSteps.AddLast( new int[] {fromRow + step[0] * j, fromCol + step[1] * j, 2} ); j = -1; }
                        else if (ans == StepType.CaptureKing){	    LegalSteps.AddLast( new int[] {fromRow + step[0] * j, fromCol + step[1] * j, 3} ); j = -1; }
                        else if (ans == StepType.Failure)		    j = -1;
                    }
                }
                break;

            case FigureType.Bishop:
                steps.AddLast( new int[] {1,	1,	0 } );
                steps.AddLast( new int[] {1,	-1,	0 } );
                steps.AddLast( new int[] {-1,	1,	0 } );
                steps.AddLast( new int[] {-1,	-1,	0 } );

                foreach (int[] step in steps)
                {
                    j = 0;
                    while (j >= 0)
                    {
                        ++j;
                        ans = Figure.isLegalStep(board, fromRow, fromCol, fromRow + step[0] * j, fromCol + step[1] * j);
                        if (ans == StepType.Success)				LegalSteps.AddLast( new int[] {fromRow + step[0] * j, fromCol + step[1] * j, 1} );
                        else if (ans == StepType.Capture){		    LegalSteps.AddLast( new int[] {fromRow + step[0] * j, fromCol + step[1] * j, 2} ); j = -1; }
                        else if (ans == StepType.CaptureKing){	    LegalSteps.AddLast( new int[] {fromRow + step[0] * j, fromCol + step[1] * j, 3} ); j = -1; }
                        else if (ans == StepType.Failure)		    j = -1;
                    }
                }
                break;

            case FigureType.Knight:

                steps.AddLast( new int[] { fromRow - 2, fromCol - 1, 0 } );
                steps.AddLast( new int[] { fromRow - 2, fromCol + 1, 0 } );
                steps.AddLast( new int[] { fromRow + 2, fromCol - 1, 0 } );
                steps.AddLast( new int[] { fromRow + 2, fromCol + 1, 0 } );
                steps.AddLast( new int[] { fromRow - 1, fromCol - 2, 0 } );
                steps.AddLast( new int[] { fromRow + 1, fromCol - 2, 0 } );
                steps.AddLast( new int[] { fromRow - 1, fromCol + 2, 0 } );
                steps.AddLast( new int[] { fromRow + 1, fromCol + 2, 0 } );

                foreach (int[] step in steps)
                {
                    ans = Figure.isLegalStep(board, fromRow, fromCol, step[0], step[1]);
                    if (ans == StepType.Success)
                    {
                        step[2] = 1;
                        LegalSteps.AddLast(step);
                    } else if (ans == StepType.Capture)
                    {
                        step[2] = 2;
                        LegalSteps.AddLast(step);
                    } if (ans == StepType.CaptureKing)
                    {
                        step[2] = 3;
                        LegalSteps.AddLast(step);
                    }
                }
                break;

            case FigureType.Pawn:
                int modifier = white ? -1 : 1;
                if (Figure.isLegalStep(board, fromRow, fromCol, fromRow + modifier, fromCol) == StepType.Success)
                    LegalSteps.AddLast(new int[] { fromRow + modifier, fromCol, 1 });

                ans = Figure.isLegalStep(board, fromRow, fromCol, fromRow + modifier, fromCol - 1);
                if (ans == StepType.Capture)
                    LegalSteps.AddLast(new int[] { fromRow + modifier, fromCol - 1, 2 });
                if (ans == StepType.CaptureKing)
                    LegalSteps.AddLast(new int[] { fromRow + modifier, fromCol - 1, 3 });

                ans = Figure.isLegalStep(board, fromRow, fromCol, fromRow + modifier, fromCol + 1);
                if (ans == StepType.Capture)
                    LegalSteps.AddLast(new int[] { fromRow + modifier, fromCol + 1, 2 });
                if (ans == StepType.CaptureKing)
                    LegalSteps.AddLast(new int[] { fromRow + modifier, fromCol + 1, 3 });
                break;
            }

               // Console.WriteLine(figure.getFigureType() + " " + figure.getRow() + " " + fromCheckTest);
            if (!fromCheckTest)
            {
                Board tBoard = board.Clone();
                for (int i = 0; i < LegalSteps.Count(); ++i)
                {
                    Console.WriteLine(figure.getFigureType() + " " + figure.getRow() + " " + fromCheckTest);
                    Console.WriteLine(i + " " + fromRow + " " + fromCol);
                    Console.WriteLine(1 + " " + tBoard.getFigureAt(fromRow, fromCol).getFigureType());
                    tBoard.Step(fromRow, fromCol, LegalSteps.ElementAt(i)[0], LegalSteps.ElementAt(i)[1]);

                    if (tBoard.checkTest(white))
                    {
                        tBoard.Step(LegalSteps.ElementAt(i)[0], LegalSteps.ElementAt(i)[1], fromRow, fromCol);
                        LegalSteps.Remove(LegalSteps.ElementAt(i));
                        --i;
                    } else
                        tBoard.Step(LegalSteps.ElementAt(i)[0], LegalSteps.ElementAt(i)[1], fromRow, fromCol);

                }
            }

            return LegalSteps;
        }
Ejemplo n.º 3
0
 public Figure Clone()
 {
     Figure clone = new Figure(_figureType, _row, _col, _board);
     clone.setColor(_white);
     return clone;
 }
Ejemplo n.º 4
0
 public void addFigure(Figure fig)
 {
     data[fig.getRow(), fig.getCol()] = fig;
 }
Ejemplo n.º 5
0
        public void setContent(String fig, String col)
        {
            if (data == null)
                return;

            if (fig.Length != col.Length)
                return;

            for (int i = 0; i < fig.Length; ++i)
            {
                int row        = i / dimension[0];
                int column     = i % dimension[1];

                Figure.FigureType ftype = (Figure.FigureType)(int.Parse(fig[i] + ""));

                if (ftype == Figure.FigureType.Nothing)
                    continue;

                bool white      = col[i] == '1';

                Figure figure   = new Figure(ftype, row, column, this);
                figure.setColor(white);
                data[row, column]  = figure;
            }
        }