Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong move by computer #7

Open
ariforu opened this issue Dec 23, 2020 · 2 comments
Open

Wrong move by computer #7

ariforu opened this issue Dec 23, 2020 · 2 comments

Comments

@ariforu
Copy link

ariforu commented Dec 23, 2020

Start with this board position.
board = [
[1, 1, -1],
[ -1, 1, 0],
[ -1, 0, 0],
]

Choose X or O
Chosen: O
First to start?[y/n]: n
Computer turn [X]

---------------
| X || X || O |
---------------
| O || X ||   |
---------------
| O ||   ||   |
---------------

Human turn [O]

---------------
| X || X || O |
---------------
| O || X || X | <--------- Wrong move by computer
---------------
| O ||   ||   |
---------------
Use numpad (1..9): 
@ariforu
Copy link
Author

ariforu commented Dec 23, 2020

X starts with a winning board state. Any move by X will result in win. The algorithm just marks the first available position (6 in this case) instead of the positions that will bring the quickest win.

@Cybertron3
Copy link

Cybertron3 commented Dec 27, 2020

To solve this problem we can say that if computer is winning in the current move then computer will not check further and it should consider this move as the best move . If there are many move in which computer can win in current move then computer can select any one of them.
To do this , In Minimax function when we are iterating over all empty cells we should check if this move will lead to immediate victory, if yes then this move is optimal otherwise check further.
In C++ :

	if (wins(state , COMP)) {
		state[x][y] = 0;
		best[0] = x;
		best[1] = y;
		best[2] = inf;
		return best;

	}

I have a opened a pull request for C++ implementation which also deals with this situation.

If there is anything mistake in this logic please correct me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants