Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This class implements and evaluates game situations of a TicTacToe game.
- */
- public class TicTacToe {
- /**
- * Returns an evaluation for player at the current board state.
- * Arbeitet nach dem Prinzip der Alphabeta-Suche. Works with the principle of Alpha-Beta-Pruning.
- *
- * @param board current Board object for game situation
- * @param player player who has a turn
- * @return rating of game situation from player's point of view
- **/
- public static int alphaBeta(Board board, int player)
- {
- // TODO
- return alphaBeta(board, player, -Integer.MAX_VALUE, Integer.MAX_VALUE, 0);
- }
- //return alphabeta(1, -Integer.MAX_VALUE, Integer.MAX_VALUE);}
- public static int alphaBeta(Board board, int player, int alpha, int beta, int depth) {
- if (board.isGameWon()) {
- return -player * (1 + board.nFreeFields());
- }
- if (board.nFreeFields() == 0) {
- return 0;
- }
- if (depth == board.getN()* board.getN() ) { // Hier MAX_DEPTH durch die maximale Tiefe ersetzen
- if (board.isGameWon()) {
- return -player * (1 + board.nFreeFields()); // Gewinner erhält hohen Wert, abhängig von verbleibenden freien Feldern
- } else {
- return 0; // Unentschieden oder Spiel läuft noch
- }
- }
- if (player == 1) {
- for (Position pos_move : board.validMoves()) {
- board.doMove(pos_move, player);
- int score = alphaBeta(board, -player, alpha, beta, depth + 1); // Tiefe inkrementieren
- board.undoMove(pos_move);
- alpha = Math.max(alpha, score);
- if (alpha >= beta) {
- break;
- }
- }
- return alpha;
- } else {
- for (Position pos_move : board.validMoves()) {
- board.doMove(pos_move, player);
- int score = alphaBeta(board, -player, alpha, beta, depth + 1); // Tiefe inkrementieren
- board.undoMove(pos_move);
- beta = Math.min(beta, score);
- if (alpha >= beta) {
- break;
- }
- }
- return beta;
- }
- }
- /**
- * Vividly prints a rating for each currently possible move out at System.out.
- * (from player's point of view)
- * Uses Alpha-Beta-Pruning to rate the possible moves.
- * formatting: See "Beispiel 1: Bewertung aller Zugmöglichkeiten" (Aufgabenblatt 4).
- *
- * @param board current Board object for game situation
- * @param player player who has a turn
- **/
- public static void evaluatePossibleMoves(Board board, int player)
- {
- // TODO
- }
- public static void main(String[] args)
- {
- Board first_board = new Board(3); //leeres Board erstellt
- //Position pos1 = new Position(1, 1);
- //first_board.doMove(pos1, 1); //x gesetzt
- first_board.print();
- System.out.println("Score fuer Spieler 1 : " + TicTacToe.alphaBeta(first_board, 1));
- //System.out.println("Score fuer Spieler -1 : " + TicTacToe.alphaBeta(first_board, -1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement