Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. public boolean findValidMoves(playerColor turn)
  2. {
  3. for(int i = 0; i < 8; i++)
  4. {
  5. for(int j = 0; j < 8; j++)
  6. {
  7. int[] dir = {0, 0, 0, 0};
  8. int xDiffPos = 0;
  9. int xDiffNeg = 0;
  10. int yDiffPos = 0;
  11. int yDiffNeg = 0;
  12. if(i == 0)
  13. {
  14. //y diff pos
  15. dir[2] = 1;
  16. }
  17. else if(i == 7)
  18. {
  19. //y diff neg
  20. dir[3] = -1;
  21. }
  22. else
  23. {
  24. //y diff pos
  25. dir[2] = 1;
  26. //y diff neg
  27. dir[3] = -1;
  28. }
  29. if(j == 0)
  30. {
  31. //x diff pos
  32. dir[0] = 1;
  33. }
  34. else if(j == 7)
  35. {
  36. //x diff neg
  37. dir[1] = -1;
  38. }
  39. else
  40. {
  41. //x diff pos
  42. dir[0] = 1;
  43. //x diff neg
  44. dir[1] = -1;
  45. }
  46. //generatePossibilities(j, i, xDiffPos, xDiffNeg, yDiffPos, yDiffNeg, turn);
  47. if(turn == playerColor.BLACK)
  48. {
  49. blackMoves[j][i] = checkLoc(j, i, dir, turn, board);
  50. //System.out.println(blackMoves[j][i].takenPieces.size());
  51. }
  52. else
  53. {
  54. whiteMoves[j][i] = checkLoc(j, i, dir, turn, board);
  55. }
  56. }
  57. }
  58. //testPrintPotential(blackMoves);
  59. return false;
  60. }
  61. private potentialMove checkLoc(int xPos, int yPos, int[] dir, playerColor turnTemp, boardSpace[][] tempBoard)
  62. {
  63. //System.out.print("(" + xPos + ", " + yPos + ")");
  64. int iterations = 0;
  65. potentialMove p = new potentialMove();
  66. p.location.x = xPos;
  67. p.location.y = yPos;
  68. p.valid = false;
  69. p = initTakenPieces(p);
  70.  
  71. boardSpace opposing;
  72. boardSpace playing;
  73. if (turnTemp == playerColor.BLACK)
  74. {
  75. opposing = boardSpace.WHITE;
  76. playing = boardSpace.BLACK;
  77. }
  78. else
  79. {
  80. opposing = boardSpace.BLACK;
  81. playing = boardSpace.WHITE;
  82. }
  83.  
  84. for(int i = 0; i < dir.length; i++)
  85. {
  86. if (dir[i] != 0) iterations++;
  87. }
  88. if(iterations == 0)
  89. {
  90. p.valid = false;
  91. return p;
  92. }
  93.  
  94. int yDir = 0;
  95. int xDir = 0;
  96. boardSpace currSpace = boardSpace.EMPTY;
  97. int count = 0;
  98. for(int i = (yPos + dir[3]); i < (yPos + dir[2] + 1); i++) {
  99. if(i == yPos - 1)
  100. {
  101. yDir = -1;
  102. }
  103. else if(i == yPos + 1)
  104. {
  105. yDir = 1;
  106. }
  107. else
  108. {
  109. yDir = 0;
  110. }
  111. for (int j = (xPos + dir[1]); j < (xPos + dir[0] + 1); j++) {
  112. if ((j != xPos) || (i != yPos)) {
  113. //System.out.print( " " + j + ", " + i + " || ");
  114. if(j == xPos - 1)
  115. {
  116. xDir = -1;
  117. }
  118. else if(j == xPos + 1)
  119. {
  120. xDir = 1;
  121. }
  122. else
  123. {
  124. xDir = 0;
  125. }
  126. //System.out.print("Directions: " + xDir + " " + yDir + " | ");
  127. int tempXpos = j;
  128. int tempYpos = i;
  129. boolean valid = false;
  130. currSpace = tempBoard[tempXpos][tempYpos];
  131. //Point tempPoint = new Point();
  132. //tempPoint.x = -1;
  133. //tempPoint.y = -1;
  134. //p.takenPieces[count].add(tempPoint);
  135. while(currSpace == opposing)
  136. {
  137. if(currSpace == opposing && tempBoard[tempXpos + xDir][tempYpos + yDir] == playing)
  138. {
  139. valid = true;
  140. //System.out.print(" hi");
  141. }
  142. else
  143. {
  144. valid = false;
  145. }
  146. Point temp = new Point();
  147. temp.x = tempXpos;
  148. temp.y = tempYpos;
  149. p.takenPieces[count].add(temp);
  150. tempXpos += xDir;
  151. tempYpos += yDir;
  152. currSpace = tempBoard[tempXpos][tempYpos];
  153. }
  154. if(!valid)
  155. {
  156. if(p.takenPieces[count].size() > 0 && p.takenPieces[count].get(0).x >= 0 && p.takenPieces[count].get(0).y >=0)
  157. {
  158. p.takenPieces[count].clear();
  159. }
  160. }
  161. else
  162. {
  163. p.valid = true;
  164. //System.out.print("Valid Move");
  165. //System.out.print(" " + p.takenPieces.size() + " pieces taken");
  166. //for(int k = 0; k < p.takenPieces.size(); k++)
  167. //{
  168. // System.out.print(p.takenPieces.get(k));
  169. // }
  170. }
  171. count++;
  172. }
  173. //if(count >= 8) count--;
  174. }
  175. }
  176. //System.out.println();
  177. //System.out.println();
  178. return p;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement