Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package ca.utoronto.utm.othello.model;
  2.  
  3. public class PlayerStrategic extends Player {
  4.  
  5. public PlayerStrategic(Othello othello, char player) {
  6. super(othello, player);
  7. }
  8.  
  9. @Override
  10. public Move getMove() {
  11.  
  12. PlayerGreedy greedyPlayer = new PlayerGreedy(this.othello, this.player);
  13. Move cornerMove = this.getCornerMove();
  14. Move sideMove = this.getSideMove();
  15. Move greedyMove = greedyPlayer.getMove();
  16.  
  17. if (cornerMove != new Move(-1, -1)) {
  18. return cornerMove;
  19. }
  20.  
  21. else if (sideMove != new Move(-1, -1)) {
  22. return sideMove;
  23. }
  24.  
  25. else {
  26. return greedyMove;
  27. }
  28. }
  29.  
  30. public Move getCornerMove() {
  31.  
  32. Othello othelloCopy = othello.copy();
  33. //Used as an othello.board index
  34. int boardDim = Othello.DIMENSION - 1;
  35. int bestMoveCount = othelloCopy.getCount(this.player);
  36.  
  37. Move bestMove = new Move(-1, -1);
  38. Move topLeft = new Move(0, 0);
  39. Move topRight = new Move(0, boardDim);
  40. Move bottomLeft = new Move(boardDim, 0);
  41. Move bottomRight = new Move(boardDim, boardDim);
  42.  
  43.  
  44. if (othelloCopy.move(topLeft.getRow(), topLeft.getCol()) &&
  45. othelloCopy.getCount(this.player)>bestMoveCount) {
  46. bestMoveCount = othelloCopy.getCount(this.player);
  47. bestMove = new Move(topLeft.getRow(), topLeft.getCol());
  48. }
  49.  
  50. if (othelloCopy.move(topRight.getRow(), topRight.getCol()) &&
  51. othelloCopy.getCount(this.player)>bestMoveCount) {
  52. bestMoveCount = othelloCopy.getCount(this.player);
  53. bestMove = new Move(topRight.getRow(), topRight.getCol());
  54. }
  55.  
  56. if (othelloCopy.move(bottomLeft.getRow(), bottomLeft.getCol()) &&
  57. othelloCopy.getCount(this.player)>bestMoveCount) {
  58. bestMoveCount = othelloCopy.getCount(this.player);
  59. bestMove = new Move(bottomLeft.getRow(), bottomLeft.getCol());
  60. }
  61.  
  62. if (othelloCopy.move(topRight.getRow(), topRight.getCol()) &&
  63. othelloCopy.getCount(this.player)>bestMoveCount) {
  64. bestMoveCount = othelloCopy.getCount(this.player);
  65. bestMove = new Move(bottomRight.getRow(), bottomRight.getCol());
  66. }
  67.  
  68. return bestMove;
  69. }
  70.  
  71. //excludes corners intentionally, since corner move does that
  72. public Move getSideMove() {
  73.  
  74. int boardDim = Othello.DIMENSION -1;
  75. Othello othelloCopy = othello.copy();
  76. Move bestMove = new Move(-1, -1);
  77. int bestMoveCount = othelloCopy.getCount(this.player);
  78.  
  79. //check left wall
  80. for (int row = 1; row < boardDim - 1; row++) {
  81. othelloCopy = othello.copy();
  82. if(othelloCopy.move(row, 0) && othelloCopy.getCount(this.player)>bestMoveCount) {
  83. bestMoveCount = othelloCopy.getCount(this.player);
  84. bestMove = new Move(row, 0);
  85. }
  86. }
  87.  
  88. //check upper wall
  89. for (int col = 1; col < boardDim - 1; col++) {
  90. othelloCopy = othello.copy();
  91. if(othelloCopy.move(0, col) && othelloCopy.getCount(this.player)>bestMoveCount) {
  92. bestMoveCount = othelloCopy.getCount(this.player);
  93. bestMove = new Move(0, col);
  94. }
  95. }
  96.  
  97. //check right wall
  98. for (int row = 1; row < boardDim - 1; row++) {
  99. othelloCopy = othello.copy();
  100. if(othelloCopy.move(row, boardDim) && othelloCopy.getCount(this.player)>bestMoveCount) {
  101. bestMoveCount = othelloCopy.getCount(this.player);
  102. bestMove = new Move(row, boardDim);
  103. }
  104. }
  105.  
  106. //check bottom wall
  107. for (int col = 1; col < boardDim - 1; col++) {
  108. othelloCopy = othello.copy();
  109. if(othelloCopy.move(boardDim, col) && othelloCopy.getCount(this.player)>bestMoveCount) {
  110. bestMoveCount = othelloCopy.getCount(this.player);
  111. bestMove = new Move(boardDim, col);
  112. }
  113. }
  114.  
  115. return bestMove;
  116. }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement