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

aima.core.environment.connectfour.ConnectFourGame Maven / Gradle / Ivy

Go to download

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

There is a newer version: 3.0.0
Show newest version
package aima.core.environment.connectfour;

import java.util.ArrayList;
import java.util.List;

import aima.core.search.adversarial.Game;

/**
 * Provides an implementation of the ConnectFour game which can be used for
 * experiments with the Minimax algorithm.
 * 
 * @author Ruediger Lunde
 * 
 */
public class ConnectFourGame implements Game {

	String[] players = new String[] { "red", "yellow" };
	ConnectFourState initialState = new ConnectFourState(6, 7);

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

	@Override
	public String[] getPlayers() {
		return players;
	}

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

	/**
	 * Returns the player corresponding to the specified player number. For
	 * efficiency reasons, ConnectFourStates use numbers
	 * instead of strings to identify players.
	 */
	public String getPlayer(int playerNum) {
		switch (playerNum) {
		case 1:
			return players[0];
		case 2:
			return players[1];
		}
		return null;
	}

	@Override
	public List getActions(ConnectFourState state) {
		List result = new ArrayList();
		for (int i = 0; i < state.getCols(); i++)
			if (state.getPlayerNum(0, i) == 0)
				result.add(i);
		return result;
	}

	@Override
	public ConnectFourState getResult(ConnectFourState state, Integer action) {
		ConnectFourState result = state.clone();
		result.dropDisk(action);
		return result;
	}

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy