Guest User

Untitled

a guest
Nov 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. public interface Player {
  2.  
  3. void takeTurn(GameBoard board, GameLogic logic);
  4.  
  5. void lostGame(GameBoard board, GameLogic logic);
  6.  
  7. void wonGame(GameBoard board, GameLogic logic);
  8.  
  9. void drawGame(GameBoard board, GameLogic logic);
  10.  
  11. void startGame(GameBoard board, GameLogic logic);
  12.  
  13. void randomChoice(ComputerPlayer player);
  14.  
  15. }
  16.  
  17. public abstract class ComputerPlayer implements Player {
  18. public int x;
  19. public int y;
  20.  
  21. public void printBoard(GameBoard board, GameLogic logic)
  22. {
  23. for (int y=0; y<3; y++)
  24. {
  25. for (int x=0; x<3; x++)
  26. {
  27. int[] pos = {x,y};
  28. ComputerPlayer curr = board.getPos(pos);
  29. }
  30. }
  31. }
  32.  
  33. // Generates Random position
  34. public void randomPlayer(){
  35. x = (int) (Math.random() * 300);
  36. y = (int) (Math.random() * 250);
  37. }
  38.  
  39. public void randomChoice(ComputerPlayer player){
  40. int smartNaiveChoice = (int)(1 + Math.random() * 2);
  41. if (smartNaiveChoice == 1){
  42. new SmartPlayer();
  43. }
  44. else if (smartNaiveChoice == 2){
  45. new NaivePlayer();
  46. }
  47. }
  48.  
  49. public void startGame(GameBoard board, GameLogic logic){
  50. printBoard(board, logic);
  51. }
  52.  
  53. public void lostGame(GameBoard board, GameLogic logic) {
  54. printBoard(board, logic);
  55. }
  56.  
  57. public void wonGame(GameBoard board, GameLogic logic) {
  58. printBoard(board, logic);
  59. }
  60.  
  61. public void drawGame(GameBoard board, GameLogic logic) {
  62. printBoard(board, logic);
  63. }
  64.  
  65. public void takeTurn(GameBoard board, GameLogic logic) {
  66. printBoard(board, logic);
  67. }
  68.  
  69. }
  70.  
  71. public class SmartPlayer extends ComputerPlayer {
  72. private int randomPosPart(int min, int max)
  73. {
  74. return min + (int)(Math.random() * ((max - min) + 1));
  75. }
  76.  
  77. private int[] randomPos()
  78. {
  79. int[] pos = {
  80. randomPosPart(0,2),
  81. randomPosPart(0,2)
  82. };
  83. return pos;
  84. }
  85. private int[] computerTurn(GameBoard board, GameLogic logic){
  86.  
  87. if ((board.getPos(new int[]{0, 0}) == board.getPos(new int[]{1, 0}))){
  88. int[] pos = {2, 0};
  89. }
  90. else if ((board.getPos(new int[]{0, 0}) == board.getPos(new int[]{2, 0}))){
  91. int[] pos = {1, 0};
  92. }
  93. else if ((board.getPos(new int[]{1, 0}) == board.getPos(new int[]{2, 0}))){
  94. int[] pos = {0, 0};
  95. }
  96. else if ((board.getPos(new int[]{0, 1}) == board.getPos(new int[]{1, 1}))){
  97. int[] pos = {2, 1};
  98. }
  99. else if ((board.getPos(new int[]{0, 1}) == board.getPos(new int[]{2, 1}))){
  100. int[] pos = {1, 1};
  101. }
  102. else if ((board.getPos(new int[]{1, 1}) == board.getPos(new int[]{2, 1}))){
  103. int[] pos = {0, 1};
  104. }
  105. else if ((board.getPos(new int[]{0, 2}) == board.getPos(new int[]{1, 2}))){
  106. int[] pos = {2, 2};
  107. }
  108. else if ((board.getPos(new int[]{1, 2}) == board.getPos(new int[]{2, 2}))){
  109. int[] pos = {0, 2};
  110. }
  111. else if ((board.getPos(new int[]{0, 2}) == board.getPos(new int[]{2, 2}))){
  112. int[] pos = {1, 2};
  113. }
  114. else if ((board.getPos(new int[]{0, 0}) == board.getPos(new int[]{0, 1}))){
  115. int[] pos = {0, 2};
  116. }
  117. else if ((board.getPos(new int[]{0, 1}) == board.getPos(new int[]{0, 2}))){
  118. int[] pos = {0, 0};
  119. }
  120. else if ((board.getPos(new int[]{0, 0}) == board.getPos(new int[]{0, 2}))){
  121. int[] pos = {0, 1};
  122. }
  123. else if ((board.getPos(new int[]{1, 1}) == board.getPos(new int[]{1, 2}))){
  124. int[] pos = {1, 0};
  125. }
  126. else if ((board.getPos(new int[]{1, 0}) == board.getPos(new int[]{1, 2}))){
  127. int[] pos = {1, 1};
  128. }
  129. else if ((board.getPos(new int[]{1, 0}) == board.getPos(new int[]{1, 1}))){
  130. int[] pos = {1, 2};
  131. }
  132. else if ((board.getPos(new int[]{2, 0}) == board.getPos(new int[]{2, 1}))){
  133. int[] pos = {2, 2};
  134. }
  135. else if ((board.getPos(new int[]{2, 1}) == board.getPos(new int[]{2, 2}))){
  136. int[] pos = {2, 0};
  137. }
  138. else if ((board.getPos(new int[]{2, 0}) == board.getPos(new int[]{2, 2}))){
  139. int[] pos = {2, 1};
  140. }
  141. else if ((board.getPos(new int[]{0, 0}) == board.getPos(new int[]{1, 1}))){
  142. int[] pos = {2, 2};
  143. }
  144. else if ((board.getPos(new int[]{1, 1}) == board.getPos(new int[]{2, 2}))){
  145. int[] pos = {0, 0};
  146. }
  147. else if ((board.getPos(new int[]{0, 0}) == board.getPos(new int[]{2, 2}))){
  148. int[] pos = {1, 1};
  149. }
  150. else if ((board.getPos(new int[]{0, 2}) == board.getPos(new int[]{2, 0}))){
  151. int[] pos = {1, 1};
  152. }
  153. else if ((board.getPos(new int[]{1, 1}) == board.getPos(new int[]{2, 0}))){
  154. int[] pos = {0, 2};
  155. }
  156. else if((board.getPos(new int[]{1, 1}) == board.getPos(new int[]{0, 2}))){
  157. int[] pos = {2, 0};
  158. }
  159. else{
  160. return randomPos();
  161. }
  162.  
  163. return null;
  164. }
  165.  
  166. private int[] generateMovePos(GameBoard board, GameLogic logic){
  167. return computerTurn(board, logic);
  168. }
  169.  
  170. public void takeTurn(GameBoard board, GameLogic logic) {
  171. int[] pos = generateMovePos(board, logic);
  172. boolean valid = board.move(this, pos);
  173. }
  174. }
Add Comment
Please, Sign In to add comment