Exemplo n.º 1
0
        //Kallas på efter att en player försöker göra ett drag för att se till att det är giltigt
        public void makeMove(int fromX, int fromY, int toX, int toY)
        {
            Move move = new Move(fromX, fromY, toX, toY);

            if (rules.isValid(move, gamestatus))
            {
                //gui.updateBoard();
                board.updateTable(move);
                switchTurn();
                System.Console.WriteLine(gamestatus + " turn!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.");
            }

            if (rules.isCheck(board.getWhiteKing()))
            {
                if (rules.isCheckMate(board.getWhiteKing()))
                {
                    gamestatus = "Black wins!";
                }
            }
            else if (rules.isCheck(board.getBlackKing()))
            {
                if (rules.isCheckMate(board.getBlackKing()))
                {
                    gamestatus = "White wins!";
                }
            }
        }
Exemplo n.º 2
0
 public override Boolean isValidMove(Move move)
 {
     if (move.gettoY() - move.getfromY() == 1 && (move.gettoX() - move.getfromX() == 1 ||
           move.gettoX() - move.getfromX() == -1 || move.gettoX() == move.getfromX()))
     {
         System.Console.WriteLine("1kung Draget är tillåtet!");
         return true;
     }
     else if (move.gettoY() - move.getfromY() == -1 && (move.gettoX() - move.getfromX() == 1 ||
          move.gettoX() - move.getfromX() == -1 || move.gettoX() == move.getfromX()))
     {
         System.Console.WriteLine("2kung Draget är tillåtet!");
         return true;
     }
     else if ((move.gettoX() - move.getfromX() == 1 || move.gettoX() - move.getfromX() == -1) &&
         (move.gettoY() - move.getfromY() == 1 || move.gettoY() - move.getfromY() == -1))
     {
         System.Console.WriteLine("3kung Draget är tillåtet!");
         return true;
     }
     else
     {
         System.Console.WriteLine("4kung Draget är inte tillåtet!");
         return false;
     }
 }
Exemplo n.º 3
0
 public override Boolean isValidMove(Move move)
 {
     if(move.gettoY() == move.getfromY() || move.gettoX() == move.getfromX())
     {
         System.Console.WriteLine("tower draget är tillåtet");
         return true;
     }
     else
     {
         System.Console.WriteLine("tower draget är inte tillåtet");
         return false;
     }
 }
Exemplo n.º 4
0
        public override Boolean isValidMove(Move move)
        {
            //Check if the move is valid for white pieces
            if (player is HumanPlayer)
            {
                if (move.gettoY() - move.getfromY() == 2 && move.gettoX() == move.getfromX() && move.getfromY() == 1)

                if (move.gettoY() - move.getfromY() == 2 && move.gettoX() == move.getfromX()
                    && move.getfromY() == 1)
                {
                    System.Console.WriteLine(" bondeHUMAN Draget är tillåtet!");
                    return true;
                }

                if (move.gettoY() - move.getfromY() == 1 && (move.gettoX() - move.getfromX() == 1 ||
                    (move.gettoX() - move.getfromX() == 1 && move.gettoY() - move.getfromY() == 1) ||
                    (move.gettoX() == move.getfromX() && move.gettoY() - move.getfromY() == 1)))
                {
                    System.Console.WriteLine(" bondeHUMAN Draget är tillåtet!");
                    return true;
                }
            }

            //Check if the move is valid for black pieces
            if (player is CPUPlayer)
            {
                if (move.gettoY() - move.getfromY() == -2 && move.gettoX() == move.getfromX()
                    && move.getfromY() == 6)
                {
                    System.Console.WriteLine("1bondeCPU Draget är tillåtet!");
                    return true;
                }

                if ((move.gettoY() - move.getfromY() == -1 && move.gettoX() - move.getfromX() == 1) ||
                    (move.gettoX() - move.getfromX() == -1 && (move.gettoY() - move.getfromY() == -1) ||
                    (move.gettoX() == move.getfromX() && move.gettoY() - move.getfromY() == -1)))
                {
                    System.Console.WriteLine("2bondeCPU Draget är tillåtet!");
                    return true;
                }
            }

            System.Console.WriteLine("3bondeCPU Draget är inte tillåtet!");
            return false;
        }
Exemplo n.º 5
0
        public override Boolean isValidMove(Move move)
        {
            if (move.gettoX() - move.getfromX() == move.gettoY() - move.getfromY())
            {
                return true;
            }
            else if (move.gettoX() + move.getfromX() == move.gettoY() + move.getfromY())
            {
                return true;
            }
            else if (move.gettoY() == move.getfromY() || move.gettoX() == move.getfromX())
            {
                System.Console.WriteLine("queen draget är tillåtet");
                return true;
            }

            return false;
        }
Exemplo n.º 6
0
        public override Boolean isValidMove(Move move)
        {
            if(move.gettoX() - move.getfromX() == move.gettoY() - move.getfromY())
            {

                System.Console.WriteLine("Springare draget är tillåtet");
                return true;
            }
            else if(move.gettoX() + move.getfromX() == move.gettoY() + move.getfromY())
            {

                System.Console.WriteLine("Springare draget är tillåtet");
                return true;
            }

            System.Console.WriteLine("springare draget är inte tillåtet");
            return false;
        }
Exemplo n.º 7
0
 public override Boolean isValidMove(Move move)
 {
     if(move.getfromY() - move.gettoY() == 2 || move.getfromY() - move.gettoY() == -2)
     {
         if(move.getfromX() - move.gettoX() == 1 || move.getfromX() - move.gettoX() == -1)
         {
             System.Console.WriteLine("häst Draget är tillåtet!");
             return true;
         }
     }
     if (move.getfromX() - move.gettoX() == 2 || move.getfromX() - move.gettoX() == -2)
     {
         if (move.getfromY() - move.gettoY() == 1 || move.getfromY() - move.gettoY() == -1)
         {
             System.Console.WriteLine("häst Draget är tillåtet!");
             return true;
         }
     }
     System.Console.WriteLine("häst Draget är inte tillåtet!");
     return false;
 }
Exemplo n.º 8
0
 public AiNode(Move move, int Value)
 {
     Move       = move;
     this.Value = Value;
 }
Exemplo n.º 9
0
 static Move()
 {
     None = new Move(0x00);
     Null = new Move(0x41);
 }
Exemplo n.º 10
0
        public void MovePieceTo(Piece piece,Position to)
        {
            var toMove = piecesinplay.Where(p => p.Piece.Equals(piece))
                .FirstOrDefault();

            if (toMove == null) throw new InvalidOperationException("No such piece in play " +  piece);

            var move = new Move(toMove.Position,to,piece,isfirst);

            if(move.Valid())
            {
                move.Apply(piecesinplay);
                Announce(piece, to);
            }
            else
                AnnounceIllegalMove();

            isfirst = false;
        }
Exemplo n.º 11
0
        private int AlphaBetaQuite(Game gameCopy, Move node, int alpha, int beta, bool maximizingPlayer, int depth, int recursion)
        {
            if ((SearchFor > TimeSpan.Zero && Stopwatch.Elapsed > SearchFor) || Stopped)
            {
                Stopped = true;
                return(0);
            }
            QuiteSearchNodes++;
            int bestVal;

            if (node.Capture == null || gameCopy.Ended || depth == 0)
            {
                bestVal = node.ScoreAfterMove.Value + (maximizingPlayer ? 1 : -1);
                QuiteLeafVisits++;
            }
            else if (maximizingPlayer)
            {
                bestVal = alpha;
                var childern = gameCopy.GetLegalCaptureMoves(Color.Black);
                if (!childern.Any())
                {
                    bestVal = node.ScoreAfterMove.Value;
                }
                else
                {
                    for (int i = 0; i < childern.Length; i++)
                    {
                        var move = childern[i];
                        gameCopy.PerformLegalMove(move);
                        var childValue = AlphaBetaQuite(gameCopy, move, bestVal, beta, false, depth - 1, recursion + 1);
                        gameCopy.Undo(move);
                        if (childValue > bestVal || node.OpponentsBestAiMove == null)
                        {
                            node.OpponentsBestAiMove = move;
                        }
                        bestVal = Math.Max(bestVal, childValue);
                        if (beta <= bestVal)
                        {
                            BetaCutOffCount++;
                            break;
                        }
                    }
                }
            }
            else
            {
                bestVal = beta;
                var childern = gameCopy.GetLegalCaptureMoves(Color.White);
                if (!childern.Any())
                {
                    bestVal = node.ScoreAfterMove.Value;
                }
                else
                {
                    for (int i = 0; i < childern.Length; i++)
                    {
                        var move = childern[i];
                        gameCopy.PerformLegalMove(move);
                        var childValue = AlphaBetaQuite(gameCopy, move, alpha, bestVal, true, depth - 1, recursion + 1);
                        gameCopy.Undo(move);
                        if (childValue < bestVal || node.OpponentsBestAiMove == null)
                        {
                            node.OpponentsBestAiMove = move;
                        }
                        bestVal = Math.Min(bestVal, childValue);
                        if (bestVal <= alpha)
                        {
                            AlphaCutOffCount++;
                            break;
                        }
                    }
                }
            }
            return(bestVal);
        }
Exemplo n.º 12
0
 //Updaterar den brädet (arrayen) efter att ett drag genomförts
 public void updateTable(Move move)
 {
     board[move.gettoX(), move.gettoY()] = board[move.getfromX(), move.getfromY()];
     board[move.getfromX(), move.getfromY()] = null;
     board[move.gettoX(), move.gettoY()].posX = move.gettoX();
     board[move.gettoX(), move.gettoY()].posY = move.gettoY();
 }
Exemplo n.º 13
0
        //1. Egen pjäs
        //2. Motståndares pjäs
        //3. Tom ruta
        public int squareStatus(Move move)
        {
            if (board[move.gettoX(), move.gettoY()] != null)
            {
                ChessPiece p1 = board[move.getfromX(), move.getfromY()];
                ChessPiece p2 = board[move.gettoX(), move.gettoY()];

                if (p1.getPlayer == p2.getPlayer)
                {
                    return 1;
                }
                else
                {
                    return 2;
                }
            }

            return 3;
        }
Exemplo n.º 14
0
 public string colourOfPiece(Move move)
 {
     return board[move.getfromX(), move.getfromY()].getPlayer.getColour;
 }
Exemplo n.º 15
0
 public abstract bool CanMoveTo(Move move, Board board);
Exemplo n.º 16
0
        static Move MoveFromAlgebraic(Board board, string algebraicMove)
        {
            MoveGenerator moveGenerator = new MoveGenerator();

            // Remove unrequired info from move string
            algebraicMove = algebraicMove.Replace("+", "").Replace("#", "").Replace("x", "").Replace("-", "");
            var allMoves = moveGenerator.GenerateMoves(board);

            Move move = new Move();

            foreach (Move moveToTest in allMoves)
            {
                move = moveToTest;

                int   moveFromIndex = move.StartSquare;
                int   moveToIndex   = move.TargetSquare;
                int   movePieceType = Piece.PieceType(board.Square[moveFromIndex]);
                Coord fromCoord     = BoardRepresentation.CoordFromIndex(moveFromIndex);
                Coord toCoord       = BoardRepresentation.CoordFromIndex(moveToIndex);
                if (algebraicMove == "OO")                   // castle kingside
                {
                    if (movePieceType == Piece.King && moveToIndex - moveFromIndex == 2)
                    {
                        return(move);
                    }
                }
                else if (algebraicMove == "OOO")                     // castle queenside
                {
                    if (movePieceType == Piece.King && moveToIndex - moveFromIndex == -2)
                    {
                        return(move);
                    }
                }
                // Is pawn move if starts with any file indicator (e.g. 'e'4. Note that uppercase B is used for bishops)
                else if (fileNames.Contains(algebraicMove[0].ToString()))
                {
                    if (movePieceType != Piece.Pawn)
                    {
                        continue;
                    }
                    if (fileNames.IndexOf(algebraicMove[0]) == fromCoord.fileIndex) // correct starting file
                    {
                        if (algebraicMove.Contains("="))                            // is promotion
                        {
                            if (toCoord.rankIndex == 0 || toCoord.rankIndex == 7)
                            {
                                if (algebraicMove.Length == 5)                                 // pawn is capturing to promote
                                {
                                    char targetFile = algebraicMove[1];
                                    if (BoardRepresentation.fileNames.IndexOf(targetFile) != toCoord.fileIndex)
                                    {
                                        // Skip if not moving to correct file
                                        continue;
                                    }
                                }
                                char promotionChar = algebraicMove[algebraicMove.Length - 1];

                                if (move.PromotionPieceType != GetPieceTypeFromSymbol(promotionChar))
                                {
                                    continue;                                     // skip this move, incorrect promotion type
                                }

                                return(move);
                            }
                        }
                        else
                        {
                            char targetFile = algebraicMove[algebraicMove.Length - 2];
                            char targetRank = algebraicMove[algebraicMove.Length - 1];

                            if (BoardRepresentation.fileNames.IndexOf(targetFile) == toCoord.fileIndex)                              // correct ending file
                            {
                                if (targetRank.ToString() == (toCoord.rankIndex + 1).ToString())                                     // correct ending rank
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                else                     // regular piece move

                {
                    char movePieceChar = algebraicMove[0];
                    if (GetPieceTypeFromSymbol(movePieceChar) != movePieceType)
                    {
                        continue;                         // skip this move, incorrect move piece type
                    }

                    char targetFile = algebraicMove[algebraicMove.Length - 2];
                    char targetRank = algebraicMove[algebraicMove.Length - 1];
                    if (BoardRepresentation.fileNames.IndexOf(targetFile) == toCoord.fileIndex)                      // correct ending file
                    {
                        if (targetRank.ToString() == (toCoord.rankIndex + 1).ToString())                             // correct ending rank

                        {
                            if (algebraicMove.Length == 4)                               // addition char present for disambiguation (e.g. Nbd7 or R7e2)
                            {
                                char disambiguationChar = algebraicMove[1];

                                if (BoardRepresentation.fileNames.Contains(disambiguationChar.ToString()))                                     // is file disambiguation
                                {
                                    if (BoardRepresentation.fileNames.IndexOf(disambiguationChar) != fromCoord.fileIndex)                      // incorrect starting file
                                    {
                                        continue;
                                    }
                                }
                                else                                                                           // is rank disambiguation
                                {
                                    if (disambiguationChar.ToString() != (fromCoord.rankIndex + 1).ToString()) // incorrect starting rank
                                    {
                                        continue;
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
            }
            return(move);
        }
Exemplo n.º 17
0
 public abstract bool CanAttackPosition(Move move, Board board);