All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aima.core.environment.tictactoe.TicTacToeGame Maven / Gradle / Ivy

Go to download

AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.

The newest version!
package aima.core.environment.tictactoe;

import java.util.List;

import aima.core.search.adversarial.Game;
import aima.core.util.datastructure.XYLocation;

/**
 * Provides an implementation of the Tic-tac-toe game which can be used for
 * experiments with the Minimax algorithm.
 * 
 * @author Ruediger Lunde
 * 
 */
public class TicTacToeGame implements Game {

	TicTacToeState initialState = new TicTacToeState();

	@Override
	public TicTacToeState getInitialState() {
		return initialState;
	}

	@Override
	public String[] getPlayers() {
		return new String[] { TicTacToeState.X, TicTacToeState.O };
	}

	@Override
	public String getPlayer(TicTacToeState state) {
		return state.getPlayerToMove();
	}

	@Override
	public List getActions(TicTacToeState state) {
		return state.getUnMarkedPositions();
	}

	@Override
	public TicTacToeState getResult(TicTacToeState state, XYLocation action) {
		TicTacToeState result = state.clone();
		result.mark(action);
		return result;
	}

	@Override
	public boolean isTerminal(TicTacToeState state) {
		return state.getUtility() != -1;
	}

	@Override
	public double getUtility(TicTacToeState state, String player) {
		double result = state.getUtility();
		if (result != -1) {
			if (player == TicTacToeState.O)
				result = 1 - result;
		} else {
			throw new IllegalArgumentException("State is not terminal.");
		}
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy