ITI0011:praktikum 13

Allikas: Kursused
Redaktsioon seisuga 24. november 2014, kell 14:38 kasutajalt Ago (arutelu | kaastöö) (→‎Üldine)
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

Praktikum: 25.11.2014 kell 8:00

Kood

TTT.java:

<source lang="java">

public class TTT {

public static final int X = 1; public static final int O = 2;

public static final int ROWS = 10; public static final int COLS = 10; /** * Symbols to be printed out */ 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)); }

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; }

public static int[][] putPieces(int[][] board, int[] pieces, int piece) { for (int p : pieces) { board[p / COLS][p % COLS] = piece; } return board; }

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. * @param board Game state (e.g. board) * @return score */ 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