Erinevus lehekülje "ITI0011:praktikum 13" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
(Uus lehekülg: 'Tagasi ITI0011 lehele. == Üldine == Praktikum: 11.11.2014 kell 8:00 == Kood == TTT.java: <source lang="java"> public class TTT { public static final int X = 1; publ...')
 
P (Ago teisaldas lehekülje ITI0011:praktikum 13 T8 pealkirja ITI0011:praktikum 13 alla)
 
(ei näidata sama kasutaja 2 vahepealset redaktsiooni)
3. rida: 3. rida:
 
== Üldine ==
 
== Üldine ==
  
Praktikum: 11.11.2014 kell 8:00
+
Praktikum: 25.11.2014 kell 8:00
  
 
== Kood ==
 
== Kood ==
  
TTT.java:
+
Gomoku.java:
  
 
<source lang="java">
 
<source lang="java">
  
public class TTT {
+
public class Gomoku {
  
 +
/**
 +
* Player "X" piece in the board
 +
* is marked by this value.
 +
*/
 
public static final int X = 1;
 
public static final int X = 1;
 +
/**
 +
* Player "O" piece on the board.
 +
*/
 
public static final int O = 2;
 
public static final int O = 2;
 
 
 +
/**
 +
* Number of rows.
 +
*/
 
public static final int ROWS = 10;
 
public static final int ROWS = 10;
 +
/**
 +
* Number of columns.
 +
*/
 
public static final int COLS = 10;
 
public static final int COLS = 10;
 +
 
/**
 
/**
* Symbols to be printed out
+
* Symbols to be printed out for each cell.
 
*/
 
*/
 
public static final char[] SYM = {'.', 'X', 'O'};
 
public static final char[] SYM = {'.', 'X', 'O'};
66. rida: 80. rida:
 
}
 
}
 
 
 +
/**
 +
* 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) {
 
public static int[][] generateBoard(int[] x, int[] o) {
 
int[][] board = new int[ROWS][COLS];
 
int[][] board = new int[ROWS][COLS];
73. rida: 105. rida:
 
}
 
}
 
 
 +
/**
 +
* 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) {
 
public static int[][] putPieces(int[][] board, int[] pieces, int piece) {
 
for (int p : pieces) {
 
for (int p : pieces) {
80. rida: 121. rida:
 
}
 
}
 
 
 +
/**
 +
* Prints out the board.
 +
* @param board The board to be printed.
 +
*/
 
public static void print(int[][] board) {
 
public static void print(int[][] board) {
 
for (int i = 0; i < board.length; i++) {
 
for (int i = 0; i < board.length; i++) {
93. rida: 138. rida:
 
* Given a game state (board with pieces)
 
* Given a game state (board with pieces)
 
* returns a score for the state.
 
* returns a score for the state.
 +
*
 +
* This method should be implemented by the student.
 
* @param board Game state (e.g. board)
 
* @param board Game state (e.g. board)
* @return score
+
* @return score A heuristic for the given state.
 
*/
 
*/
 
public static int getScore(int[][] board) {
 
public static int getScore(int[][] board) {
100. rida: 147. rida:
 
return 0;
 
return 0;
 
}
 
}
 +
}
 
}
 
}
  

Viimane redaktsioon: 27. november 2014, kell 00:30

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