Advertisement
madegoff

TicTacToe(exception thrown wtf)

May 27th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 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.free_fields);
  25. }
  26. else if (board.free_fields == 0){ //es gibt keine freie felder
  27. return 0;
  28. }
  29.  
  30. for (Position pos_move : board.validMoves()) {
  31. board.doMove(pos_move, player);
  32. int score = - alphaBeta(board, -player, -beta, -alpha, 0);
  33. board.undoMove(pos_move);
  34. if (score > alpha) {
  35. alpha = score;
  36. if (alpha >= beta) break;
  37. }
  38. }
  39. return alpha;
  40. }
  41.  
  42.  
  43.  
  44. /**
  45. * Vividly prints a rating for each currently possible move out at System.out.
  46. * (from player's point of view)
  47. * Uses Alpha-Beta-Pruning to rate the possible moves.
  48. * formatting: See "Beispiel 1: Bewertung aller Zugmöglichkeiten" (Aufgabenblatt 4).
  49. *
  50. * @param board current Board object for game situation
  51. * @param player player who has a turn
  52. **/
  53.  
  54. public static void evaluatePossibleMoves(Board board, int player)
  55. {
  56. // TODO
  57. }
  58.  
  59. public static void main(String[] args)
  60. {
  61. Board first_board = new Board(3); //leeres Board erstellt
  62. //Position pos1 = new Position(1, 1);
  63. //first_board.doMove(pos1, 1); //x gesetzt
  64.  
  65. first_board.print();
  66. System.out.println("Score fuer Spieler 1 : " + TicTacToe.alphaBeta(first_board, 1));
  67. System.out.println("Score fuer Spieler -1 : " + TicTacToe.alphaBeta(first_board, -1));
  68.  
  69. }
  70. }
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement