예제 #1
0
        public void newGame(string loadedGamePath)
        {
            menu.Hide();

            gamePath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ChessMate";
            lastMove = null;
            computerMove = null;
            turn = "white";
            board = new Board();
            boardWindow = new BoardWindow();
            boardWindow.Show();
            boardWindow.Closing += ExitGame;

            boardWindow.repaint();

            if (loadedGamePath.Equals("INVALID_PATH"))
            {
                DataHandler.writeToJSON();
            }

            are = new AutoResetEvent(true);
            t = new Timer(checkforchange, are, 1000, 5000);

        }
예제 #2
0
        public static void loadGame()
        {
            DataHandler.loadFromJSON();
            lastMove = null;
            boardWindow.repaint();

            if (turn == "black")
            {
                turn = "white";
                nextPlayer();
            }
        }
예제 #3
0
        public static ChessMove nextDraw()
        {
            ChessMove finalMove = new ChessMove(new Position(0, 0), new Position(0, 0));
            Random    rnd       = new Random();
            double    best      = -10000;

            List <Piece> blackPieces = Chess.blackPieces();

            foreach (Piece piece in blackPieces)
            {
                Piece p = blackPieces.Find(x => x.Equals(piece));
                Dictionary <Position, bool?> moves = Chess.validMoves(p);
                foreach (KeyValuePair <Position, bool?> move in moves)
                {
                    double value = 0;
                    if (move.Value != false)
                    {
                        value = Chess.board.at(move.Key).getValue() + rnd.NextDouble() * 4;
                        if (isThreatened(p.getPosition()))
                        {
                            value += p.getValue();

                            if (isProtected(p.getPosition()))
                            {
                                int low = lowestThreathener(p.getPosition(), "white");
                                int val = p.getValue();
                                value -= low < val ? low : val;
                            }
                        }

                        int previousValue = p.getValue();
                        Chess.tempMove(p.getPosition(), move.Key);
                        value += p.getValue() - previousValue;

                        if (isThreatened(p.getPosition()))
                        {
                            value -= p.getValue();
                            if (isProtected(p.getPosition()))
                            {
                                int low = lowestThreathener(p.getPosition(), "white");
                                int val = p.getValue();
                                value += low < val ? low : val;
                            }
                        }

                        if (Chess.IsChecked("white"))
                        {
                            value += rnd.NextDouble() * 5;
                        }
                        Chess.undo();
                    }
                    else if (move.Value == false)
                    {
                        value = -10000;
                    }

                    if (value > best)
                    {
                        best      = value;
                        finalMove = new ChessMove(piece.getPosition(), move.Key);
                    }
                }
            }

            return(finalMove);
        }
예제 #4
0
        private static void move(Position from, Position to, bool realMove){
            lastMove = new ChessMove(from, to);
            Piece p1 = board.at(from);
            Piece p2 = board.at(to);
            pieceRemoved = p2;

            int xDiff = to.x - from.x;
            if (p1.getType() == "king" && Math.Abs(xDiff) > 1 && realMove)
            {
                int xDir = xDiff < 0 ? -2 : 1;
                Position rook = new Position(to.x + xDir, to.y);
                xDir = xDiff < 0 ? 1 : -1;
                board.at(rook).debug();
                move(rook, new Position(to.x + xDir, to.y));
            }

            board.set(from, new Piece("blank", "none", from));

            p1.numberOfMoves++;
            if (p1.getType() == "pawn" && Math.Abs(to.y - from.y) == 2)
            {
                p1.numberOfMoves++;
            }

            p1.setPosition(to);

            if (p1.getType() == "pawn" && (to.y == 0 || to.y == 7))
            {
                p1 = new Piece("queen", p1.getColor(), p1.getPosition(), p1.getNumberOfMoves());
            }

            board.set(to, p1);
        }
예제 #5
0
파일: AI.cs 프로젝트: johka885/chess-mate
        public static ChessMove nextDraw()
        {
            ChessMove finalMove = new ChessMove(new Position(0, 0), new Position(0, 0));
            Random rnd = new Random();
            double best = -10000;

            List<Piece> blackPieces = Chess.blackPieces();
            foreach (Piece piece in blackPieces)
            {
                Piece p = blackPieces.Find(x => x.Equals(piece));
                Dictionary<Position, bool?> moves = Chess.validMoves(p);
                foreach (KeyValuePair<Position, bool?> move in moves)
                {
                    double value = 0;
                    if (move.Value != false)
                    {
                        value = Chess.board.at(move.Key).getValue() + rnd.NextDouble()*4;
                        if(isThreatened(p.getPosition())){
                            value += p.getValue();

                            if (isProtected(p.getPosition()))
                            {
                                int low = lowestThreathener(p.getPosition(), "white");
                                int val = p.getValue();
                                value -= low < val ? low : val;
                            }
                        }

                        int previousValue = p.getValue();
                        Chess.tempMove(p.getPosition(), move.Key);
                        value += p.getValue() - previousValue;

                        if (isThreatened(p.getPosition()))
                        {
                            value -= p.getValue();
                            if (isProtected(p.getPosition()))
                            {
                                int low = lowestThreathener(p.getPosition(), "white");
                                int val = p.getValue();
                                value += low < val ? low : val;
                            }
                        }

                        if (Chess.IsChecked("white"))
                        {
                            value += rnd.NextDouble() * 5;
                        }
                        Chess.undo();
                    }
                    else if (move.Value == false)
                    {
                        value = -10000;
                    }

                    if (value > best)
                    {
                        best = value;
                        finalMove = new ChessMove(piece.getPosition(), move.Key);
                    }
                }                
            }

            return finalMove;
        }