예제 #1
0
        public object Clone()
        {
            Board board = new Board(m_Board.Length);

            board.m_Board = (Square[, ])m_Board.Clone();

            return(board);
        }
예제 #2
0
파일: Board.cs 프로젝트: jaraki/Chess
 public Square[,] CopyBoard()
 {
     return(board.Clone() as Square[, ]);
 }
예제 #3
0
        private static Move[] GetOpenSquares(Square[,] board, Point checker, Move lastMove, List <Point> priorPositions)
        {
            if (priorPositions == null)
            {
                priorPositions = new List <Point> {
                    checker
                };
            }

            List <Move> openSquares = new List <Move>();

            //Top Left
            if (board[priorPositions[0].Y, priorPositions[0].X].Colour != CheckerColour.Red || board[priorPositions[0].Y, priorPositions[0].X].King)        //Stop regular red pieces from moving backwards
            {
                if (IsValidPoint(checker.X - 1, checker.Y - 1))
                {
                    if (board[checker.Y - 1, checker.X - 1].Colour == CheckerColour.Empty && lastMove.Destination.X == -1)  //Allow immediate empty spaces if it's the first jump
                    {
                        openSquares.Add(new Move(priorPositions[0], checker.X - 1, checker.Y - 1));
                    }
                    //Check for a capturable piece
                    else if (IsValidPoint(checker.X - 2, checker.Y - 2) &&
                             ((checker.X - 2) != lastMove.Destination.X || (checker.Y - 2) != lastMove.Destination.Y) &&
                             ((checker.X - 2) != priorPositions[0].X || (checker.Y - 2) != priorPositions[0].Y) &&
                             board[checker.Y - 1, checker.X - 1].Colour != board[checker.Y, checker.X].Colour &&
                             board[checker.Y - 2, checker.X - 2].Colour == CheckerColour.Empty)
                    {
                        Point newDest = new Point(checker.X - 2, checker.Y - 2);
                        if (!priorPositions.Contains(newDest))
                        {
                            Move move = new Move(priorPositions[0], newDest);
                            move.Captures.Add(new Point(checker.X - 1, checker.Y - 1));
                            move.Captures.AddRange(lastMove.Captures);
                            openSquares.Add(move);

                            priorPositions.Add(newDest);

                            //Use recursion to find multiple captures
                            openSquares.AddRange(GetOpenSquares(board, new Point(checker.X - 2, checker.Y - 2), move, priorPositions));
                        }
                    }
                }
            }

            //Top Right
            if (board[priorPositions[0].Y, priorPositions[0].X].Colour != CheckerColour.Red || board[priorPositions[0].Y, priorPositions[0].X].King)
            {
                if (IsValidPoint(checker.X + 1, checker.Y - 1))
                {
                    if (board[checker.Y - 1, checker.X + 1].Colour == CheckerColour.Empty && lastMove.Destination.X == -1)
                    {
                        openSquares.Add(new Move(priorPositions[0], checker.X + 1, checker.Y - 1));
                    }
                    //Check for a capturable piece
                    else if (IsValidPoint(checker.X + 2, checker.Y - 2) &&
                             ((checker.X + 2) != lastMove.Destination.X || (checker.Y - 2) != lastMove.Destination.Y) &&
                             ((checker.X + 2) != priorPositions[0].X || (checker.Y - 2) != priorPositions[0].Y) &&
                             board[checker.Y - 1, checker.X + 1].Colour != board[checker.Y, checker.X].Colour &&
                             board[checker.Y - 2, checker.X + 2].Colour == CheckerColour.Empty)
                    {
                        Point newDest = new Point(checker.X + 2, checker.Y - 2);
                        if (!priorPositions.Contains(new Point(checker.X + 2, checker.Y - 2)))
                        {
                            Move move = new Move(priorPositions[0], newDest);
                            move.Captures.Add(new Point(checker.X + 1, checker.Y - 1));
                            move.Captures.AddRange(lastMove.Captures);
                            openSquares.Add(move);

                            priorPositions.Add(newDest);

                            //Use recursion to find multiple captures
                            openSquares.AddRange(GetOpenSquares(board, new Point(checker.X + 2, checker.Y - 2), move, priorPositions));
                        }
                    }
                }
            }

            //Bottom Left
            if (board[priorPositions[0].Y, priorPositions[0].X].Colour != CheckerColour.Black || board[priorPositions[0].Y, priorPositions[0].X].King)
            {
                if (IsValidPoint(checker.X - 1, checker.Y + 1))
                {
                    if (board[checker.Y + 1, checker.X - 1].Colour == CheckerColour.Empty && lastMove.Destination.X == -1)
                    {
                        openSquares.Add(new Move(priorPositions[0], checker.X - 1, checker.Y + 1));
                    }
                    //Check for a capturable piece
                    else if (IsValidPoint(checker.X - 2, checker.Y + 2) &&
                             ((checker.X - 2) != lastMove.Destination.X || (checker.Y + 2) != lastMove.Destination.Y) &&
                             ((checker.X - 2) != priorPositions[0].X || (checker.Y + 2) != priorPositions[0].Y) &&
                             board[checker.Y + 1, checker.X - 1].Colour != board[checker.Y, checker.X].Colour &&
                             board[checker.Y + 2, checker.X - 2].Colour == CheckerColour.Empty)
                    {
                        Point newDest = new Point(checker.X - 2, checker.Y + 2);
                        if (!priorPositions.Contains(newDest))
                        {
                            Move move = new Move(priorPositions[0], newDest);
                            move.Captures.Add(new Point(checker.X - 1, checker.Y + 1));
                            move.Captures.AddRange(lastMove.Captures);
                            openSquares.Add(move);

                            priorPositions.Add(newDest);

                            //Use recursion to find multiple captures
                            openSquares.AddRange(GetOpenSquares(board, new Point(checker.X - 2, checker.Y + 2), move, priorPositions));
                        }
                    }
                }
            }

            //Bottom Right
            if (board[priorPositions[0].Y, priorPositions[0].X].Colour != CheckerColour.Black || board[priorPositions[0].Y, priorPositions[0].X].King)
            {
                if (IsValidPoint(checker.X + 1, checker.Y + 1))
                {
                    if (board[checker.Y + 1, checker.X + 1].Colour == CheckerColour.Empty && lastMove.Destination.X == -1)
                    {
                        openSquares.Add(new Move(priorPositions[0], checker.X + 1, checker.Y + 1));
                    }
                    //Check for a capturable piece
                    else if (IsValidPoint(checker.X + 2, checker.Y + 2) &&
                             ((checker.X + 2) != lastMove.Destination.X || (checker.Y + 2) != lastMove.Destination.Y) &&
                             ((checker.X + 2) != priorPositions[0].X || (checker.Y + 2) != priorPositions[0].Y) &&
                             board[checker.Y + 1, checker.X + 1].Colour != board[checker.Y, checker.X].Colour &&
                             board[checker.Y + 2, checker.X + 2].Colour == CheckerColour.Empty)
                    {
                        Point newDest = new Point(checker.X + 2, checker.Y + 2);
                        if (!priorPositions.Contains(newDest))
                        {
                            Move move = new Move(priorPositions[0], newDest);
                            move.Captures.Add(new Point(checker.X + 1, checker.Y + 1));
                            move.Captures.AddRange(lastMove.Captures);
                            openSquares.Add(move);

                            priorPositions.Add(newDest);

                            //Use recursion to find multiple captures
                            openSquares.AddRange(GetOpenSquares(board, new Point(checker.X + 2, checker.Y + 2), move, priorPositions));
                        }
                    }
                }
            }

            List <Move> capturesMoves = new List <Move>();

            // The color of the player
            CheckerColour playerColor = board[checker.Y, checker.X].Colour;

            // The possibility for the king to move
            int[,] possibility = { { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } };
            ////King move
            if ((board[checker.Y, checker.X].King))
            {
                for (int i = 0; i < 4; i++)
                {
                    int testX = checker.X + possibility[i, 0];
                    int testY = checker.Y + possibility[i, 1];

                    List <Move> emptyMove    = new List <Move>();
                    List <Move> capturedMove = new List <Move>();

                    while (IsValidPoint(testY, testX))
                    {
                        if (board[testY, testX].Colour == CheckerColour.Empty)
                        {
                            Move move = new Move(new Point(checker.X, checker.Y), new Point(testX, testY));
                            emptyMove.Add(move);
                        }
                        else
                        {
                            // Position of the king
                            int furtherX = testX + possibility[i, 0];
                            int furtherY = testY + possibility[i, 1];

                            // Check if the point to catch is a ennemy
                            // And if it is
                            if (IsValidPoint(furtherX, furtherY) && board[testY, testX].Colour != playerColor && board[furtherY, furtherX].Colour == CheckerColour.Empty)
                            {
                                emptyMove.Clear();
                                Move move = new Move(new Point(checker.X, checker.Y), new Point(furtherX, furtherY));
                                move.Captures.Add(new Point(testX, testY));
                                move.Captures.AddRange(lastMove.Captures);

                                // Add the position
                                openSquares.Add(move);

                                Square[,] copy = board.Clone() as Square[, ];

                                Square tmp = new Square {
                                    Colour = CheckerColour.Empty
                                };
                                if (copy != null)
                                {
                                    copy[checker.Y, checker.X] = tmp;
                                    copy[testY, testX]         = tmp;

                                    copy[furtherY, furtherX] = board[checker.Y, checker.X];
                                }


                                //Use recursion to find multiple captures
                                capturesMoves.AddRange(GetOpenSquares(board, new Point(furtherX, furtherY), move, priorPositions));

                                break;
                            }
                        }

                        // Iterate through all the possibility
                        testX += possibility[i, 0];
                        testY += possibility[i, 1];
                    }


                    openSquares.AddRange(emptyMove.Any() ? emptyMove : capturedMove);
                }
            }


            // Check Captures
            foreach (Move move in openSquares)
            {
                if (move.Captures.Any())
                {
                    capturesMoves.Add(move);
                }
            }

            if (capturesMoves.Any())
            {
                return(capturesMoves.ToArray());
            }
            return(openSquares.ToArray());
        }