Exemple #1
0
 // Simulates a game to the end (game over) based on the default policy
 // Returns the game over state
 private State SimulateGame(State state, int POLICY)
 {
     if (POLICY == RANDOM_POLICY)
     {
         while (state.GetMoves().Count != 0)
         {
             state = state.ApplyMove(state.GetRandomMove());
         }
         return state;
     }
     else if (POLICY == BEST_EVAL_POLICY)
     {
         List<Move> moves = state.GetMoves();
         while (moves.Count != 0)
         {
             // random move for computer
             if (state.Player == GameEngine.COMPUTER)
             {
                 state = state.ApplyMove(state.GetRandomMove());
             }
             else
             {
                 // find the move that results in the best child state, based on the evaluation function
                 State bestState = null;
                 double bestScore = Double.MinValue;
                 foreach (Move move in moves)
                 {
                     State result = state.ApplyMove(move);
                     double score = AI.Evaluate(result);
                     if (score > bestScore)
                     {
                         bestScore = score;
                         bestState = result;
                     }
                 }
                 state = bestState;
             }
             moves = state.GetMoves();
         }
         return state;
     }
     else if (POLICY == EXPECTIMAX_POLICY)
     {
         Expectimax expectimax = new Expectimax(gameEngine, 2);
         while (state.GetMoves().Count != 0) {
             if (state.Player == GameEngine.COMPUTER)
             {
                 state = state.ApplyMove(state.GetRandomMove());
             }
             else
             {
                 Move move = expectimax.ExpectimaxAlgorithm(state, 2, weights);
                 state = state.ApplyMove(move);
             }
         }
         return state;
     }
     else throw new Exception();
 }