Erinevus lehekülje "ITI0011-2014:Gomoku" redaktsioonide vahel
125. rida: | 125. rida: | ||
Teil tuleb "getMove" meetodis tagastada käigu asukoht Location objektina: | Teil tuleb "getMove" meetodis tagastada käigu asukoht Location objektina: | ||
− | <source> | + | <source lang="java"> |
package gomoku; | package gomoku; | ||
Redaktsioon: 10. november 2014, kell 14:55
Tuleb implementeerida interface'i: <source lang="java"> package gomoku;
/**
* @author Ago * * Interface for computer strategy. */
public interface ComputerStrategy { /** * Takes the game state and return the best move * @param board Board state * @return A location where to make computer's move. * * @see SimpleBoard * @see Location */ public Location getMove(SimpleBoard board);
/** * Name will be shown during the play. * This method should be overridden to * show student's name. * @return Name of the player */ public String getName(); } </source>
Objekt, mis antakse "getMove" meetodile kaasa:
<source lang="java"> package gomoku;
/**
* Simple 2-dimensional presentation * of the game board. * Every cell in the board is represented * by one integer, the Values are either * PLAYER_X, PLAYER_O or EMPTY. * This object also knows the size of the board * and the last move (Location object). * * @author Ago * @see Location */
public class SimpleBoard {
/** * Cell value for player X */ public static final int PLAYER_X = 1;
/** * Cell value for player O */ public static final int PLAYER_O = -1;
/** * Empty cell value */ public static final int EMPTY = 0;
/** * The height of the board. * Indicates the number of rows. */ private int height = -1; /** * The width of the board. * Indicates the number of columns. */ private int width = -1;
private int[][] board;
/** * Returns the height (number of rows) * of the board. * @return Number of rows */ public int getHeight() { return height; }
/** * Returns the width (number of columns) * of the board. * @return Number of columns */ public int getWidth() { return width; }
/** * Returns 2-dimensional * array of integers with values * PLAYER_O, PLAYER_X or EMPTY. * The values correspond to * "O" player's piece, * "X" player's piece, * or an empty cell accordingly. * @return */ public int[][] getBoard() { return board; }
/** * Constructor to instantiate the board. * @param simpleBoard 2-dimensional * array for the board. */ public SimpleBoard(int[][] simpleBoard) { height = simpleBoard.length; if (height > 0) width = simpleBoard[0].length; board = simpleBoard; } }
</source>
Teil tuleb "getMove" meetodis tagastada käigu asukoht Location objektina:
<source lang="java"> package gomoku;
/**
* @author Ago * Location on the board, 0-based. */
public class Location { /** * Index of the row. */ private final int row; /** * Index of the column. */ private final int column;
public Location(int row, int column) { this.row = row; this.column = column; }
/** * @return Row index */ public int getRow() { return row; }
/** * @return Columnt index */ public int getColumn() { return column; }
@Override public String toString() { return String.format("(%d, %d)", row, column); } } </source>
Näidis random strateegia: <source lang="java"> /**
* A random strategy implementation * for the gomoku game. * @author Ago * */
public class RandomStrategy implements ComputerStrategy {
@Override public Location getMove(SimpleBoard board) { int[][] b = board.getBoard(); while (true) { // let's loop until we find an empty spot int row = (int)(Math.random() * board.getHeight()); int col = (int)(Math.random() * board.getWidth()); if (b[row][col] == SimpleBoard.EMPTY) { // if empty, let's return this location return new Location(row, col); } } }
@Override public String getName() { return "Random computer strategy"; }
} </source>