eugenp / tutorials

Just Announced - "Learn Spring Security OAuth":

Home Page:http://bit.ly/github-lsso

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[ISSUE] QUESTION: MCTS: Get All Possible States Question

EcoEditor opened this issue · comments

Article and Module Links
algorithms-modules/algorithms-searching/src/main/java/com/baeldung/algorithms/mcts/montecarlo/State.java

Describe the Issue
Hello, I'm doing the tutorial but in C#, and in this method on the State class:

public List<State> getAllPossibleStates() {
        List<State> possibleStates = new ArrayList<>();
        List<Position> availablePositions = this.board.getEmptyPositions();
        availablePositions.forEach(p -> {
            State newState = new State(this.board);
            newState.setPlayerNo(3 - this.playerNo);
            newState.getBoard().performMove(newState.getPlayerNo(), p);
            possibleStates.add(newState);
        });
        return possibleStates;
    }

I'm getting the board object modified such that, after 9 iterations of available positions my boardvalues look like this:
[0, 0] 2
[0, 1] 2
[0, 2] 2
[1, 0] 2
[1, 1] 2
[1, 2] 2
[2, 0] 2
[2, 1] 2
[2, 2] 2

which ultimately ends the game after one turn.
I would expect that at each iteration of the availablePositions I'll see all 0 except the current position for this possible state.
so at each iteration there would only be one "2" in the boardValues. right?

I also wonder, if I should use a deep copy, to get this behavior, since the board which is passed by reference is modified in the newState.

So my question is, what is the desired behavior here?

thanks so much.

the method in C#:

		public List<GameState> GetAllPossibleStates() {
			var possibleStates = new List<GameState>();
			var availablePositions = this.board.GetEmptyPositions();

			for (int i = 0; i < availablePositions.Count; i++) {
				var newState = new GameState(this.board);
				newState.PlayerNo = 3 - this.playerNo;
				newState.Board.PerformMove(newState.PlayerNo, availablePositions[i]);
				newState.Board.PrintBoard();
				possibleStates.Add(newState);
			}

			return possibleStates;
		}

printed board through iteration:
2 0 0
0 0 0
0 0 0

2 2 0
0 0 0
0 0 0

2 2 2
0 0 0
0 0 0

2 2 2
2 0 0
0 0 0

2 2 2
2 2 0
0 0 0

2 2 2
2 2 2
0 0 0

2 2 2
2 2 2
2 0 0

2 2 2
2 2 2
2 2 0

2 2 2
2 2 2
2 2 2

Hey, @EcoEditor.

That's right, each item on availablePositions will contain a zero except for the current possible position. This is the result if we print the board for each item from the possibleStates variable in the giveninitBoardState_whenGetAllPossibleStates_thenNonEmptyList() test:

3 0 0 
0 0 0 
0 0 0 

0 3 0 
0 0 0 
0 0 0 

0 0 3 
0 0 0 
0 0 0 

0 0 0 
3 0 0 
0 0 0 

0 0 0 
0 3 0 
0 0 0 

0 0 0 
0 0 3 
0 0 0 

0 0 0 
0 0 0 
3 0 0 

0 0 0 
0 0 0 
0 3 0 

0 0 0 
0 0 0 
0 0 3 

Unfortunately, we can only help with questions that are specifically and directly related to the article - not with your own, custom application.

Hope that makes sense.