Esempio n. 1
0
File: AI.cs Progetto: etheone/tddd49
        //Calculate reduction (points) for move, if AIs own piece can be taken
        private int reducePoints(Board board, Coord fromPos, Coord toPos)
        {
            int pointsToDeduct = 0;

            Board tempBoard = new Board(board);

            Piece temp = tempBoard.board [fromPos.xpos, fromPos.ypos];

            temp.coord = toPos;
            if (tempBoard.board [toPos.xpos, toPos.ypos].type != Piece.PieceType.NONE)
            {
                tempBoard.board [toPos.xpos, toPos.ypos] = new Piece(Piece.PieceType.NONE, toPos.xpos, toPos.ypos, Piece.PieceColor.NONE, 99);
            }
            tempBoard.board [fromPos.xpos, fromPos.ypos] = tempBoard.board [toPos.xpos, toPos.ypos];
            tempBoard.board [toPos.xpos, toPos.ypos]     = temp;

            Piece.PieceColor colorToCheck;

            if (color == Piece.PieceColor.WHITE)
            {
                colorToCheck = Piece.PieceColor.BLACK;
            }
            else
            {
                colorToCheck = Piece.PieceColor.WHITE;
            }

            List <Coord> test = new List <Coord>();

            foreach (Piece p in tempBoard.board)
            {
                if (p.color == colorToCheck)
                {
                    List <Coord> tempList = new List <Coord>();
                    tempList = Rules.getLegalMoves(tempBoard.board, p.coord);
                    foreach (Coord c in tempList)
                    {
                        test.Add(c);
                    }
                }
            }

            Piece.PieceType pieceAtPos;

            foreach (Piece p in tempBoard.board)
            {
                if (p.color == color)
                {
                    if (test.Contains(p.coord))
                    {
                        pieceAtPos = p.type;

                        switch (pieceAtPos)
                        {
                        case Piece.PieceType.NONE:
                            if (pointsToDeduct > 0)
                            {
                                break;
                            }
                            else
                            {
                                pointsToDeduct = 0;
                            }
                            break;

                        case Piece.PieceType.PAWN:
                            if (pointsToDeduct >= 7)
                            {
                                break;
                            }
                            else
                            {
                                pointsToDeduct = 7;
                            }
                            break;

                        case Piece.PieceType.ROOK:
                            if (pointsToDeduct >= 11)
                            {
                                break;
                            }
                            else
                            {
                                pointsToDeduct = 11;
                            }
                            break;

                        case Piece.PieceType.KNIGHT:
                            if (pointsToDeduct >= 11)
                            {
                                break;
                            }
                            else
                            {
                                pointsToDeduct = 11;
                            }
                            break;

                        case Piece.PieceType.BISHOP:
                            if (pointsToDeduct >= 11)
                            {
                                break;
                            }
                            else
                            {
                                pointsToDeduct = 11;
                            }
                            break;

                        case Piece.PieceType.QUEEN:
                            if (pointsToDeduct >= 16)
                            {
                                break;
                            }
                            else
                            {
                                pointsToDeduct = 16;
                            }
                            break;

                        case Piece.PieceType.KING:
                            if (pointsToDeduct >= 15)
                            {
                                break;
                            }
                            else
                            {
                                pointsToDeduct = 15;
                            }
                            break;
                        }
                    }
                }
            }

            return(pointsToDeduct);
        }
Esempio n. 2
0
File: AI.cs Progetto: etheone/tddd49
        //Calculate next AI move
        public void calculateMove(Board board)
        {
            moves      = new List <Coord[]> ();
            bestPoints = -1000;


            List <Coord> possibleMoves = new List <Coord> ();

            //Check which moves generate the most points
            foreach (Piece p in board.board)
            {
                if (p.color == color)
                {
                    possibleMoves = Rules.getLegalMoves(board.board, p.coord);
                    Coord currentPieceCoord = p.coord;
                    foreach (Coord c in possibleMoves)
                    {
                        if (causeCheck(board, currentPieceCoord, c))
                        {
                            continue;
                        }
                        else
                        {
                            Piece.PieceType pieceAtToPos = board.board [c.xpos, c.ypos].type;
                            //Check if move puts us in a vulnerable position
                            int deduct = reducePoints(board, p.coord, c);
                            switch (pieceAtToPos)
                            {
                            case Piece.PieceType.NONE:

                                isBetterMove(2 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.PAWN:
                                isBetterMove(6 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.ROOK:
                                isBetterMove(10 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.KNIGHT:
                                isBetterMove(10 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.BISHOP:
                                isBetterMove(10 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.QUEEN:
                                isBetterMove(15 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.KING:
                                isBetterMove(14 - deduct, p.coord, c);
                                break;
                            }
                        }
                    }
                }
            }

            //If we have severals moves with the same points, pick one at random
            Random rnd   = new Random();
            int    index = rnd.Next(0, moves.Count);

            if (moves.Count == 0)
            {
                Console.WriteLine("Out of moves!!");
            }
            else
            {
                board.tryMove(moves [index] [0], moves [index] [1]);
            }
        }
Esempio n. 3
0
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            MouseState ms    = Mouse.GetState();
            Boolean    moved = false;

            if (ms.LeftButton == ButtonState.Pressed)
            {
                if (startGameClick == false)
                {
                    startGameClick = true;
                    board.startGame();
                    return;
                }
                if ((int)(ms.X) > 480)
                {
                    board.startNewGame();
                    legalMoves = null;
                    return;
                }
                if (!firstClick)
                {
                    int newxPos = (int)(ms.X / 60);
                    int newyPos = (int)(ms.Y / 60);

                    toPos = new Coord(newxPos, newyPos);

                    // Here we try to make a move
                    board.tryMove(fromPos, toPos);

                    legalMoves = null;

                    currentIndex [0] = 99;
                    currentIndex [1] = 99;
                    firstClick       = true;
                    moved            = true;
                }
                else
                {
                    int xPos = (int)(ms.X / 60);
                    int yPos = (int)(ms.Y / 60);
                    if (board.board [xPos, yPos].color == board.turn)
                    {
                        fromPos = new Coord(xPos, yPos);
                        if (moved == false)
                        {
                            if (currentIndex [0] == xPos && currentIndex [1] == yPos)
                            {
                                currentIndex [0] = 99;
                                currentIndex [1] = 99;
                            }
                            else
                            {
                                currentIndex [0] = (int)(ms.X / 60);
                                currentIndex [1] = (int)(ms.Y / 60);
                            }
                        }
                        firstClick = false;
                        legalMoves = Rules.getLegalMoves(board.board, fromPos);
                    }
                    else
                    {
                        Console.WriteLine("NOT CORRECT TURN");
                    }
                }
            }
        }