ITI0011:praktikum 13

Allikas: Kursused
Redaktsioon seisuga 25. november 2014, kell 06:01 kasutajalt Ago (arutelu | kaastöö)
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

Praktikum: 25.11.2014 kell 8:00

Kood

Gomoku.java:

<source lang="java">

public class Gomoku {

/** * Player "X" piece in the board * is marked by this value. */ public static final int X = 1; /** * Player "O" piece on the board. */ public static final int O = 2;

/** * Number of rows. */ public static final int ROWS = 10; /** * Number of columns. */ public static final int COLS = 10;

/** * Symbols to be printed out for each cell. */ public static final char[] SYM = {'.', 'X', 'O'};

public static void main(String[] args) { // initial state, // X has 5 in a row int[][] board = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, X, X, X, X, X, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, }; System.out.println("score=" + getScore(board));

// 5 in a row board = generateBoard(new int[]{33, 34, 35, 36, 37}, new int[]{}); print(board); System.out.println("score=" + getScore(board));

// 5 in a col board = generateBoard(new int[]{21, 31, 41, 51, 61}, new int[]{}); print(board); System.out.println("score=" + getScore(board));

// 5 in a diag / board = generateBoard(new int[]{45, 36, 27, 18, 9}, new int[]{}); print(board); System.out.println("score=" + getScore(board));

// 5 in a diag \ board = generateBoard(new int[]{55, 66, 77, 88, 99}, new int[]{}); print(board); System.out.println("score=" + getScore(board));

// some other board = generateBoard(new int[]{21, 31, 41, 51}, new int[]{61, 62, 63, 64}); print(board); System.out.println("score=" + getScore(board)); }

/** * Generates a new board where * player stones are marked accordingly * to the arguments. * * An array of stones indicates "encoded" cell values. * One number represents the index of the cell * starting from upper left corner, moving to the right * on one row. After the first row, the index continues * on the second row from the left to right etc. * A general formula: * cell value = row index * number of columns + column index. * @param x An array of player "X" stones. * @param o An array of player "O" stones. * @return A new board with size ROWS x COLUMNS * where players' stones are marked according to the * passed arguments. */ public static int[][] generateBoard(int[] x, int[] o) { int[][] board = new int[ROWS][COLS]; board = putPieces(board, x, X); board = putPieces(board, o, O); return board; }

/** * Puts a piece (or a stone) to the board. * A helper method which works for both players * the same way (the player is passed as an argument). * @param board A board matrix. * @param pieces An array of player's stone "coordinates". * @param piece Player (or the stone) value. * @return A modified board with given stones on it. */ public static int[][] putPieces(int[][] board, int[] pieces, int piece) { for (int p : pieces) { board[p / COLS][p % COLS] = piece; } return board; }

/** * Prints out the board. * @param board The board to be printed. */ public static void print(int[][] board) { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { System.out.print(SYM[board[i][j]]); } System.out.println(); } }


/** * Given a game state (board with pieces) * returns a score for the state. * * This method should be implemented by the student. * @param board Game state (e.g. board) * @return score A heuristic for the given state. */ public static int getScore(int[][] board) { // TODO return 0; } } }

</source>

Exercise

Write a method getScore(int[][] board) which will calculate a score for the given board state. Minimal requirements:

  • calculate score for player X (see the constants)
  • the getScore should detect winning state (5 in a row/column/diagonal) by returning corresponding value (the score for winning condition is up to you, it might be 10, it might be 100000 or even 90234098)


Optional stuff:

  • calculate score for smaller combinations (like open 3, open 4 etc). Combine the scores for every combination into one score.
  • winning check should work for both X and O player