예제 #1
0
파일: AI.cs 프로젝트: mapplet/Chess
        public void Move(Gameboard gameboard, State currentState)
        {
            Rulebook rulebook = new Rulebook();
            List<Tuple<Piece, Piece>> topValidMoves = new List<Tuple<Piece, Piece>>();
            foreach(Piece piece in gameboard.getTeam(thisTeam))
            {
                List<Tuple<int, int>> validDestinations = rulebook.getValidMoves(piece, gameboard);
                foreach (Tuple<int,int> coordinate in validDestinations)
                {
                    if (validDestinations.Count() == 0)
                        break;
                    else if (topValidMoves.Count() == 0 || rewards[topValidMoves[0].Item2.type] == rewards[gameboard.getPiece(coordinate.Item1, coordinate.Item2).type])
                        topValidMoves.Add(new Tuple<Piece, Piece>(piece, gameboard.getPiece(coordinate.Item1, coordinate.Item2)));
                    else if (rewards[topValidMoves[0].Item2.type] < rewards[gameboard.getPiece(coordinate.Item1, coordinate.Item2).type])
                    {
                        topValidMoves.Clear();
                        topValidMoves.Add(new Tuple<Piece, Piece>(piece, gameboard.getPiece(coordinate.Item1, coordinate.Item2)));
                    }
                }

            }
            if (topValidMoves.Count() != 0)
            {
                Random rand = new Random();
                int index = rand.Next(topValidMoves.Count());
                gameboard.Move(topValidMoves[index].Item1, topValidMoves[index].Item2);

                if (topValidMoves[index].Item1.type == (int)type.pawn && (topValidMoves[index].Item1.row == 0 || topValidMoves[index].Item1.row == 7))
                {
                    Piece bestPieceFromDead = new Piece();
                    foreach (Piece deadPiece in gameboard.getDead(thisTeam))
                    {
                        if (rewards[deadPiece.type] > rewards[bestPieceFromDead.type])
                            bestPieceFromDead = deadPiece;
                    }
                    gameboard.tradePawn(topValidMoves[index].Item1, bestPieceFromDead);
                }

                //gameboard.checkChessMate(currentState.getWhosTurn());
            }
            //currentState.swapTurn();
        }
예제 #2
0
파일: game.cs 프로젝트: mapplet/Chess
        private void initializeGame(int myTeam)
        {
            Console.WriteLine("# Init: Thread ID: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
            // Resume game?
            if (myTeam == (int)team.none)
            {
                Console.WriteLine("# Resuming Game.");
                Tuple<Gameboard, State> loadedGame = storage.LoadGame();
                gameboard = loadedGame.Item1;
                currentState = loadedGame.Item2;
                gameboard.updateBoard();
                currentState.updateState(currentState.getMyTeam());
            }

            // Else initialize default
            else
            {
                Console.WriteLine("# Initializes new Game.");
                gameboard = new Gameboard();
                gameboard.initialize();
                currentState = new State(myTeam);
                storage.manuallyInitialize(gameboard, currentState);
            }

            AIOpponent = new AI(currentState.getOpponentTeam());

            storage.getWatcher().SynchronizingObject = this;

            // Update GUI when database is changed
            storage.propertyChanged += new PropertyChangedEventHandler(OnDatabaseChanged);

            if (currentState.getOpponentTeam() == (int)team.white)
            {
                Console.WriteLine("# Computer will make first move.");
                AIOpponent.Move(gameboard, currentState);
            }

            updateBoard();
        }