Guest User

Untitled

a guest
Nov 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. public interface Player {
  2.  
  3.  
  4. void takeTurn(GameBoard board, GameLogic logic);
  5.  
  6. void lostGame(GameBoard board, GameLogic logic);
  7.  
  8. void wonGame(GameBoard board, GameLogic logic);
  9.  
  10. void drawGame(GameBoard board, GameLogic logic);
  11.  
  12. void startGame(GameBoard board, GameLogic logic);
  13.  
  14. void randomChoice(ComputerPlayer player);
  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. public void randomPlayer(){
  34. x = (int) (Math.random() * 300);
  35. y = (int) (Math.random() * 250);
  36. }
  37.  
  38. public void randomChoice(ComputerPlayer player){
  39. int smartNaiveChoice = (int)(1 + Math.random() * 2);
  40. if (smartNaiveChoice == 1){
  41. new SmartPlayer();
  42. }
  43. else if (smartNaiveChoice == 2){
  44. new NaivePlayer();
  45. }
  46. }
  47.  
  48. public void startGame(GameBoard board, GameLogic logic){
  49. printBoard(board, logic);
  50. }
  51.  
  52. public void lostGame(GameBoard board, GameLogic logic) {
  53. printBoard(board, logic);
  54. }
  55.  
  56. public void wonGame(GameBoard board, GameLogic logic) {
  57. printBoard(board, logic);
  58. }
  59.  
  60. public void drawGame(GameBoard board, GameLogic logic) {
  61. printBoard(board, logic);
  62. }
  63.  
  64. public void takeTurn(GameBoard board, GameLogic logic) {
  65. printBoard(board, logic);
  66.  
  67. }
  68.  
  69. }
  70.  
  71. public class NaivePlayer extends ComputerPlayer {
  72.  
  73. private int randomPosPart(int min, int max)
  74. {
  75. return min + (int)(Math.random() * ((max - min) + 1));
  76. }
  77.  
  78. private int[] randomPos()
  79. {
  80. int[] pos = {
  81. randomPosPart(0,2),
  82. randomPosPart(0,2)
  83. };
  84. return pos;
  85. }
  86.  
  87. private int[] generateMovePos(GameBoard board, GameLogic logic)
  88. {
  89. return randomPos();
  90. }
  91.  
  92. public void takeTurn(GameBoard board, GameLogic logic) {
  93. int[] pos = generateMovePos(board, logic);
  94. boolean valid = logic.move(this, pos);
  95.  
  96. }
  97.  
  98. }
  99.  
  100. public class SmartPlayer extends ComputerPlayer {
  101. private int randomPosPart(int min, int max)
  102. {
  103. return min + (int)(Math.random() * ((max - min) + 1));
  104. }
  105.  
  106. private int[] randomPos()
  107. {
  108. int[] pos = {
  109. randomPosPart(0,2),
  110. randomPosPart(0,2)
  111. };
  112. return pos;
  113. }
  114.  
  115. public int[] generateMovePos(GameBoard board, GameLogic logic){
  116.  
  117. if ((board.getPos(new int[]{0, 0}) == board.getPos(new int[]{1,
  118. 0}))){
  119. int[] pos = {2, 0};
  120. }
  121. else if ((board.getPos(new int[]{0, 0}) == board.getPos(new
  122. int[]{2, 0}))){
  123. int[] pos = {1, 0};
  124. }
  125. else if ((board.getPos(new int[]{1, 0}) == board.getPos(new
  126. int[]{2, 0}))){
  127. int[] pos = {0, 0};
  128. }
  129. else if ((board.getPos(new int[]{0, 1}) == board.getPos(new
  130. int[]{1, 1}))){
  131. int[] pos = {2, 1};
  132. }
  133. else if ((board.getPos(new int[]{0, 1}) == board.getPos(new
  134. int[]{2, 1}))){
  135. int[] pos = {1, 1};
  136. }
  137. else if ((board.getPos(new int[]{1, 1}) == board.getPos(new
  138. int[]{2, 1}))){
  139. int[] pos = {0, 1};
  140. }
  141. else if ((board.getPos(new int[]{0, 2}) == board.getPos(new
  142. int[]{1, 2}))){
  143. int[] pos = {2, 2};
  144. }
  145. else if ((board.getPos(new int[]{1, 2}) == board.getPos(new
  146. int[]{2, 2}))){
  147. int[] pos = {0, 2};
  148. }
  149. else if ((board.getPos(new int[]{0, 2}) == board.getPos(new
  150. int[]{2, 2}))){
  151. int[] pos = {1, 2};
  152. }
  153. else if ((board.getPos(new int[]{0, 0}) == board.getPos(new
  154. int[]{0, 1}))){
  155. int[] pos = {0, 2};
  156. }
  157. else if ((board.getPos(new int[]{0, 1}) == board.getPos(new
  158. int[]{0, 2}))){
  159. int[] pos = {0, 0};
  160. }
  161. else if ((board.getPos(new int[]{0, 0}) == board.getPos(new
  162. int[]{0, 2}))){
  163. int[] pos = {0, 1};
  164. }
  165. else if ((board.getPos(new int[]{1, 1}) == board.getPos(new
  166. int[]{1, 2}))){
  167. int[] pos = {1, 0};
  168. }
  169. else if ((board.getPos(new int[]{1, 0}) == board.getPos(new
  170. int[]{1, 2}))){
  171. int[] pos = {1, 1};
  172. }
  173. else if ((board.getPos(new int[]{1, 0}) == board.getPos(new
  174. int[]{1, 1}))){
  175. int[] pos = {1, 2};
  176. }
  177. else if ((board.getPos(new int[]{2, 0}) == board.getPos(new
  178. int[]{2, 1}))){
  179. int[] pos = {2, 2};
  180. }
  181. else if ((board.getPos(new int[]{2, 1}) == board.getPos(new
  182. int[]{2, 2}))){
  183. int[] pos = {2, 0};
  184. }
  185. else if ((board.getPos(new int[]{2, 0}) == board.getPos(new
  186. int[]{2, 2}))){
  187. int[] pos = {2, 1};
  188. }
  189. else if ((board.getPos(new int[]{0, 0}) == board.getPos(new
  190. int[]{1, 1}))){
  191. int[] pos = {2, 2};
  192. }
  193. else if ((board.getPos(new int[]{1, 1}) == board.getPos(new
  194. int[]{2, 2}))){
  195. int[] pos = {0, 0};
  196. }
  197. else if ((board.getPos(new int[]{0, 0}) == board.getPos(new
  198. int[]{2, 2}))){
  199. int[] pos = {1, 1};
  200. }
  201. else if ((board.getPos(new int[]{0, 2}) == board.getPos(new
  202. int[]{2, 0}))){
  203. int[] pos = {1, 1};
  204. }
  205. else if ((board.getPos(new int[]{1, 1}) == board.getPos(new
  206. int[]{2, 0}))){
  207. int[] pos = {0, 2};
  208. }
  209. else if((board.getPos(new int[]{1, 1}) == board.getPos(new
  210. int[]{0, 2}))){
  211. int[] pos = {2, 0};
  212. }
  213. else{
  214. return randomPos();
  215. }
  216. return null;
  217. }
  218.  
  219. public void takeTurn(GameBoard board, GameLogic logic) {
  220. int[] pos = generateMovePos(board, logic);
  221. boolean valid = logic.move(this, pos);
  222. }
Add Comment
Please, Sign In to add comment