Advertisement
madegoff

TicTacToe_verbessert

May 27th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. /**
  2. * This class implements and evaluates game situations of a TicTacToe game.
  3. */
  4. public class TicTacToe {
  5.  
  6. /**
  7. * Returns an evaluation for player at the current board state.
  8. * Arbeitet nach dem Prinzip der Alphabeta-Suche. Works with the principle of Alpha-Beta-Pruning.
  9. *
  10. * @param board current Board object for game situation
  11. * @param player player who has a turn
  12. * @return rating of game situation from player's point of view
  13. **/
  14. public static int alphaBeta(Board board, int player)
  15. {
  16. // TODO
  17. return alphaBeta(board, player, -Integer.MAX_VALUE, Integer.MAX_VALUE, 0);
  18. }
  19.  
  20. //return alphabeta(1, -Integer.MAX_VALUE, Integer.MAX_VALUE);}
  21.  
  22. public static int alphaBeta(Board board, int player, int alpha, int beta, int depth) {
  23. if (board.isGameWon()) {
  24. return -player * (1 + board.nFreeFields());
  25. }
  26. if (board.nFreeFields() == 0) {
  27. return 0;
  28. }
  29. if (depth == board.getN()* board.getN() ) { // Hier MAX_DEPTH durch die maximale Tiefe ersetzen
  30. if (board.isGameWon()) {
  31. return -player * (1 + board.nFreeFields()); // Gewinner erhält hohen Wert, abhängig von verbleibenden freien Feldern
  32. } else {
  33. return 0; // Unentschieden oder Spiel läuft noch
  34. }
  35. }
  36.  
  37. if (player == 1) {
  38. for (Position pos_move : board.validMoves()) {
  39. board.doMove(pos_move, player);
  40. int score = alphaBeta(board, -player, alpha, beta, depth + 1); // Tiefe inkrementieren
  41. board.undoMove(pos_move);
  42. alpha = Math.max(alpha, score);
  43. if (alpha >= beta) {
  44. break;
  45. }
  46. }
  47. return alpha;
  48. } else {
  49. for (Position pos_move : board.validMoves()) {
  50. board.doMove(pos_move, player);
  51. int score = alphaBeta(board, -player, alpha, beta, depth + 1); // Tiefe inkrementieren
  52. board.undoMove(pos_move);
  53. beta = Math.min(beta, score);
  54. if (alpha >= beta) {
  55. break;
  56. }
  57. }
  58. return beta;
  59. }
  60. }
  61.  
  62.  
  63.  
  64. /**
  65. * Vividly prints a rating for each currently possible move out at System.out.
  66. * (from player's point of view)
  67. * Uses Alpha-Beta-Pruning to rate the possible moves.
  68. * formatting: See "Beispiel 1: Bewertung aller Zugmöglichkeiten" (Aufgabenblatt 4).
  69. *
  70. * @param board current Board object for game situation
  71. * @param player player who has a turn
  72. **/
  73.  
  74. public static void evaluatePossibleMoves(Board board, int player)
  75. {
  76. // TODO
  77. }
  78.  
  79. public static void main(String[] args)
  80. {
  81. Board first_board = new Board(3); //leeres Board erstellt
  82. //Position pos1 = new Position(1, 1);
  83. //first_board.doMove(pos1, 1); //x gesetzt
  84.  
  85. first_board.print();
  86. System.out.println("Score fuer Spieler 1 : " + TicTacToe.alphaBeta(first_board, 1));
  87. //System.out.println("Score fuer Spieler -1 : " + TicTacToe.alphaBeta(first_board, -1));
  88.  
  89. }
  90. }
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement