Erinevus lehekülje "ITI0011-2014:Gomoku" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
15. rida: 15. rida:
 
* Takes the game state and return the best move
 
* Takes the game state and return the best move
 
* @param board Board state
 
* @param board Board state
 +
* @param player Player indicator. Which player's
 +
* strategy it is. Possible values: SimpleBoard.PLAYER_*.
 
* @return A location where to make computer's move.
 
* @return A location where to make computer's move.
 
*  
 
*  
20. rida: 22. rida:
 
* @see Location
 
* @see Location
 
*/
 
*/
public Location getMove(SimpleBoard board);
+
public Location getMove(SimpleBoard board, int player);
 
 
 
/**
 
/**
42. rida: 44. rida:
 
  * Every cell in the board is represented
 
  * Every cell in the board is represented
 
  * by one integer, the Values are either
 
  * by one integer, the Values are either
  * PLAYER_X, PLAYER_O or EMPTY.
+
  * PLAYER_BLACK, PLAYER_WHITE or EMPTY.
 
  * This object also knows the size of the board
 
  * This object also knows the size of the board
 
  * and the last move (Location object).
 
  * and the last move (Location object).
52. rida: 54. rida:
 
 
 
/**
 
/**
* Cell value for player X
+
* Cell value for black player's piece
 
*/
 
*/
public static final int PLAYER_X = 1;
+
public static final int PLAYER_BLACK = 1;
 
 
 
/**
 
/**
* Cell value for player O
+
* Cell value for white player's piece
 
*/
 
*/
public static final int PLAYER_O = -1;
+
public static final int PLAYER_WHITE = -1;
 
 
 
/**
 
/**
100. rida: 102. rida:
 
* Returns 2-dimensional
 
* Returns 2-dimensional
 
* array of integers with values
 
* array of integers with values
* PLAYER_O, PLAYER_X or EMPTY.
+
* PLAYER_WHITE, PLAYER_BLACK or EMPTY.
 
* The values correspond to
 
* The values correspond to
* "O" player's piece,
+
* White player's piece,
* "X" player's piece,
+
* Black player's piece,
 
* or an empty cell accordingly.
 
* or an empty cell accordingly.
 
* @return
 
* @return
181. rida: 183. rida:
  
 
@Override
 
@Override
public Location getMove(SimpleBoard board) {
+
public Location getMove(SimpleBoard board, int player) {
 
int[][] b = board.getBoard();
 
int[][] b = board.getBoard();
 
while (true) {
 
while (true) {
209. rida: 211. rida:
  
 
@Override
 
@Override
public Location getMove(SimpleBoard board) {
+
public Location getMove(SimpleBoard board, int player) {
 
// let's operate on 2-d array
 
// let's operate on 2-d array
 
int[][] b = board.getBoard();
 
int[][] b = board.getBoard();

Redaktsioon: 12. november 2014, kell 13:04

Work in progress, vaadake jooksvalt uut seisu!

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 * @param player Player indicator. Which player's * strategy it is. Possible values: SimpleBoard.PLAYER_*. * @return A location where to make computer's move. * * @see SimpleBoard * @see Location */ public Location getMove(SimpleBoard board, int player);

/** * 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_BLACK, PLAYER_WHITE 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 black player's piece */ public static final int PLAYER_BLACK = 1;

/** * Cell value for white player's piece */ public static final int PLAYER_WHITE = -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_WHITE, PLAYER_BLACK or EMPTY. * The values correspond to * White player's piece, * Black 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 player) { 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>

Tudeng peab täiendama järgmist klassi: StudengStrategy.java: <source lang="java"> package gomoku;

public class StudentStrategy implements ComputerStrategy {

@Override public Location getMove(SimpleBoard board, int player) { // let's operate on 2-d array int[][] b = board.getBoard(); for (int row = b.length - 1; row >= 0; row--) { for (int col = b[0].length - 1; col >= 0; col--) { if (b[row][col] == SimpleBoard.EMPTY) { // first empty location return new Location(row, col); } } } return null; }

@Override public String getName() { return "Tudengi nimi"; }

}

</source>

Praegune tudengi kood hakkab alt paremalt järjest nuppe mööda rida käima (kui on vaba koht).