<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="et">
	<id>http://courses.cs.taltech.ee/w/index.php?action=history&amp;feed=atom&amp;title=ITI0011RUS%3Aminimax</id>
	<title>ITI0011RUS:minimax - Redigeerimiste ajalugu</title>
	<link rel="self" type="application/atom+xml" href="http://courses.cs.taltech.ee/w/index.php?action=history&amp;feed=atom&amp;title=ITI0011RUS%3Aminimax"/>
	<link rel="alternate" type="text/html" href="http://courses.cs.taltech.ee/w/index.php?title=ITI0011RUS:minimax&amp;action=history"/>
	<updated>2026-05-14T05:07:33Z</updated>
	<subtitle>Selle lehekülje redigeerimiste ajalugu</subtitle>
	<generator>MediaWiki 1.35.9</generator>
	<entry>
		<id>http://courses.cs.taltech.ee/w/index.php?title=ITI0011RUS:minimax&amp;diff=2588&amp;oldid=prev</id>
		<title>Aleksandr: Uus lehekülg: &#039;Вернуться на страницу предмета  MinimaxGomoku.java: &lt;source lang=&quot;java&quot;&gt; import java.util.ArrayList;   public class MinimaxGomoku {  	pu...&#039;</title>
		<link rel="alternate" type="text/html" href="http://courses.cs.taltech.ee/w/index.php?title=ITI0011RUS:minimax&amp;diff=2588&amp;oldid=prev"/>
		<updated>2015-05-14T10:57:18Z</updated>

		<summary type="html">&lt;p&gt;Uus lehekülg: &amp;#039;Вернуться &lt;a href=&quot;/pages/ITI0011RUS&quot; class=&quot;mw-redirect&quot; title=&quot;ITI0011RUS&quot;&gt;на страницу предмета&lt;/a&gt;  MinimaxGomoku.java: &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt; import java.util.ArrayList;   public class MinimaxGomoku {  	pu...&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Uus lehekülg&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Вернуться [[ITI0011RUS|на страницу предмета]]&lt;br /&gt;
&lt;br /&gt;
MinimaxGomoku.java:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
 &lt;br /&gt;
public class MinimaxGomoku {&lt;br /&gt;
&lt;br /&gt;
	public static final int MAX_PAYOFF = 100;&lt;br /&gt;
	&lt;br /&gt;
	/**&lt;br /&gt;
	 * Empty cell on the board.&lt;br /&gt;
	 */&lt;br /&gt;
	public static final int E = 0;&lt;br /&gt;
 &lt;br /&gt;
	/**&lt;br /&gt;
	 * Player &amp;quot;X&amp;quot; piece in the board&lt;br /&gt;
	 * is marked by this value.&lt;br /&gt;
	 */&lt;br /&gt;
	public static final int X = 1;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Player &amp;quot;O&amp;quot; piece on the board.&lt;br /&gt;
	 */&lt;br /&gt;
	public static final int O = 2;&lt;br /&gt;
 &lt;br /&gt;
	/**&lt;br /&gt;
	 * Number of rows.&lt;br /&gt;
	 */&lt;br /&gt;
	public static final int ROWS = 10;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Number of columns.&lt;br /&gt;
	 */&lt;br /&gt;
	public static final int COLS = 10;&lt;br /&gt;
 &lt;br /&gt;
	/**&lt;br /&gt;
	 * Symbols to be printed out for each cell.&lt;br /&gt;
	 * 0, 1, 2 = {., X, O}&lt;br /&gt;
	 */&lt;br /&gt;
	public static final char[] SYM = {&amp;#039;.&amp;#039;, &amp;#039;X&amp;#039;, &amp;#039;O&amp;#039;};&lt;br /&gt;
 &lt;br /&gt;
	/**&lt;br /&gt;
	 * Number of pieces &amp;quot;in a row&amp;quot; to win.&lt;br /&gt;
	 */&lt;br /&gt;
	private static final int WINCOUNT = 5;&lt;br /&gt;
 &lt;br /&gt;
	public static class Location {&lt;br /&gt;
		public int row;&lt;br /&gt;
		public int col;&lt;br /&gt;
		public Location(int row, int col) {&lt;br /&gt;
			this.row = row;&lt;br /&gt;
			this.col = col;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		{&lt;br /&gt;
			// initial state,&lt;br /&gt;
			// X has (atleast) 5 in a row&lt;br /&gt;
			int[][] board = {&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, X, X, X, X, X, X, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
			};&lt;br /&gt;
			print(board);&lt;br /&gt;
			System.out.println(&amp;quot;score=&amp;quot; + getScore(board));&lt;br /&gt;
 &lt;br /&gt;
			// fill in the minimax method&lt;br /&gt;
			// after you have finished with minimax, you should get a score indication&lt;br /&gt;
			System.out.println(&amp;quot;minimax:&amp;quot; + minimax(board, X, 1));&lt;br /&gt;
		}&lt;br /&gt;
		{&lt;br /&gt;
			// X has 4 in a row, X can win with 1 move&lt;br /&gt;
			int[][] board = {&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, X, X, X, X, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
					{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},&lt;br /&gt;
			};&lt;br /&gt;
			print(board);&lt;br /&gt;
			System.out.println(&amp;quot;score=&amp;quot; + getScore(board));&lt;br /&gt;
 &lt;br /&gt;
			// this should output the winning score&lt;br /&gt;
			System.out.println(&amp;quot;minimax:&amp;quot; + minimax(board, X, 1));&lt;br /&gt;
		}&lt;br /&gt;
		// 12) we want to get location instead.&lt;br /&gt;
		// now modify minimax call to something like that:&lt;br /&gt;
		// moves = getPossibleMoves()&lt;br /&gt;
		// for (move : moves) {&lt;br /&gt;
		// &amp;quot;make a move&amp;quot;&lt;br /&gt;
		// call minimax&lt;br /&gt;
		// &amp;quot;undo the move&amp;quot;&lt;br /&gt;
		// if minimax returns a better score than previous best, &lt;br /&gt;
		// then store the value as the new best.&lt;br /&gt;
		// also, store the location as the best.&lt;br /&gt;
 &lt;br /&gt;
		// the stored best location is the place to make your move.&lt;br /&gt;
 &lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	public static boolean isTerminalState(int[][] board) {&lt;br /&gt;
		int score = getScore(board);&lt;br /&gt;
		return(  &lt;br /&gt;
			score == MAX_PAYOFF ||    // WIN&lt;br /&gt;
			score == -MAX_PAYOFF ||   // LOSS&lt;br /&gt;
			(  // DRAW&lt;br /&gt;
				Math.abs(score) != MAX_PAYOFF &amp;amp;&amp;amp;&lt;br /&gt;
				getPossibleMoves(board).size() == 0&lt;br /&gt;
			) &lt;br /&gt;
		);&lt;br /&gt;
	}&lt;br /&gt;
	public static int minimax(int[][] board, int player, int depth) {&lt;br /&gt;
		if(depth == 0 || isTerminalState(board)) {&lt;br /&gt;
			return getScore(board);&lt;br /&gt;
		} &lt;br /&gt;
 &lt;br /&gt;
		int best_score;&lt;br /&gt;
		Location best_move = null;&lt;br /&gt;
		&lt;br /&gt;
		ArrayList&amp;lt;Location&amp;gt; moves = getPossibleMoves(board);&lt;br /&gt;
		&lt;br /&gt;
		if(player == X) {&lt;br /&gt;
			best_score = -Integer.MAX_VALUE;&lt;br /&gt;
			for(Location move : moves) {&lt;br /&gt;
				board[move.row][move.col] = player;&lt;br /&gt;
				int score = minimax(board, X+O-player, depth-1);&lt;br /&gt;
				if(score &amp;gt; best_score) {&lt;br /&gt;
					best_score = score;&lt;br /&gt;
					best_move = move;&lt;br /&gt;
				}&lt;br /&gt;
				board[move.row][move.col] = E;&lt;br /&gt;
			}&lt;br /&gt;
		} else { // player == O&lt;br /&gt;
			best_score = Integer.MAX_VALUE;&lt;br /&gt;
			for(Location move : moves) {&lt;br /&gt;
				board[move.row][move.col] = player;&lt;br /&gt;
				int score = minimax(board, X+O-player, depth-1);&lt;br /&gt;
				if(score &amp;lt; best_score) {&lt;br /&gt;
					best_score = score;&lt;br /&gt;
					best_move = move;&lt;br /&gt;
				}&lt;br /&gt;
				board[move.row][move.col] = E;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
 &lt;br /&gt;
		return best_score;&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	/**&lt;br /&gt;
	 * Returns all possible moves.&lt;br /&gt;
	 * It is used by the minimax algorithm.&lt;br /&gt;
	 * It might be wise to only return&lt;br /&gt;
	 * certain empty cells (as cells in the corner&lt;br /&gt;
	 * and border are not so valuable as in the center etc).&lt;br /&gt;
	 * @param board The board.&lt;br /&gt;
	 * @return A list of location objects&lt;br /&gt;
	 */&lt;br /&gt;
	public static ArrayList&amp;lt;Location&amp;gt; getPossibleMoves(int[][] board) {&lt;br /&gt;
		ArrayList&amp;lt;Location&amp;gt; availableMoves = new ArrayList&amp;lt;Location&amp;gt;();&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
		// TODO:&lt;br /&gt;
		// loop over the board&lt;br /&gt;
		// and return only cells which are available&lt;br /&gt;
		for (int row = 0; row &amp;lt; board.length; row++) {&lt;br /&gt;
			for (int col = 0; col &amp;lt; board[row].length; col++) {&lt;br /&gt;
				if (board[row][col] == E) {&lt;br /&gt;
					availableMoves.add(new Location(row, col));&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		return availableMoves;&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
	/**&lt;br /&gt;
	 * Given a game state (board with pieces)&lt;br /&gt;
	 * returns a score for the state.&lt;br /&gt;
	 * &lt;br /&gt;
	 * @param board Game state (e.g. board)&lt;br /&gt;
	 * @return score A heuristic score for the given state.&lt;br /&gt;
	 */&lt;br /&gt;
	public static int getScore(int[][] board) {&lt;br /&gt;
		for (int row = 0; row &amp;lt; board.length; row++) {&lt;br /&gt;
			for (int col = 0; col &amp;lt; board[row].length; col++) {&lt;br /&gt;
				if (board[row][col] == X) {&lt;br /&gt;
					if (row &amp;lt;= WINCOUNT) {&lt;br /&gt;
						if (getCount(board, row, col, 1, 0, X) &amp;gt;= WINCOUNT) return MAX_PAYOFF; // |&lt;br /&gt;
						if (col &amp;lt;= WINCOUNT &amp;amp;&amp;amp; getCount(board, row, col, 1, 1, X) &amp;gt;= WINCOUNT) return MAX_PAYOFF; // \&lt;br /&gt;
						if (col &amp;gt;= WINCOUNT &amp;amp;&amp;amp; getCount(board, row, col, 1, -1, X) &amp;gt;= WINCOUNT) return MAX_PAYOFF; // /						&lt;br /&gt;
					}&lt;br /&gt;
					if (col &amp;lt;= WINCOUNT &amp;amp;&amp;amp; getCount(board, row, col, 0, 1, X) &amp;gt;= WINCOUNT) return MAX_PAYOFF; // -&lt;br /&gt;
				}&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	/**&lt;br /&gt;
	 * Counts pieces on the board starting from (row, col) and&lt;br /&gt;
	 * moving in direction (rowd, cold).&lt;br /&gt;
	 * @param board The board&lt;br /&gt;
	 * @param row Starting row&lt;br /&gt;
	 * @param col Starting col&lt;br /&gt;
	 * @param rowd Row step (-1, 0, 1)&lt;br /&gt;
	 * @param cold Col step (-1, 0, 1)&lt;br /&gt;
	 * @param player Player (whose piece is expected)&lt;br /&gt;
	 * @return The number of connected player pieces &amp;quot;in a row&amp;quot; &lt;br /&gt;
	 */&lt;br /&gt;
	public static int getCount(int[][] board, int row, int col, int rowd, int cold, int player) {&lt;br /&gt;
		int count = 0;&lt;br /&gt;
		for (int i = 0; i &amp;lt; WINCOUNT; i++) {&lt;br /&gt;
			if (board[row + i * rowd][col + i * cold] == player) count++;&lt;br /&gt;
			else break;&lt;br /&gt;
		}&lt;br /&gt;
		return count;&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	/**&lt;br /&gt;
	 * Prints out the board.&lt;br /&gt;
	 * @param board The board to be printed.&lt;br /&gt;
	 */&lt;br /&gt;
	public static void print(int[][] board) {&lt;br /&gt;
		for (int i = 0; i &amp;lt; board.length; i++) {&lt;br /&gt;
			for (int j = 0; j &amp;lt; board[0].length; j++) {&lt;br /&gt;
				System.out.print(SYM[board[i][j]]);&lt;br /&gt;
			}&lt;br /&gt;
			System.out.println();&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Aleksandr</name></author>
	</entry>
</feed>