ThexXTURBOXx

Untitled

Jan 3rd, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. /**
  2. * Calculates and makes a move on the board.
  3. *
  4. * @param row is the Yth position of the board.
  5. * @param col is the Xth position of the board.
  6. * @param team is either the human or the computer. It determines which tile
  7. * will be set on the board.
  8. * @return The manipulated board is returned if a move has been made or null
  9. * if not.
  10. * @see Direction
  11. */
  12. public GameBoard moveHelp(int row, int col, Player team) {
  13. GameBoard changedBoard = this.clone();
  14. Player ownTileState;
  15. Player enemyTileState;
  16.  
  17. // determine which player is moving
  18. if (team == Player.HUMAN) {
  19. enemyTileState = Player.COM;
  20. ownTileState = Player.HUMAN;
  21. } else {
  22. enemyTileState = Player.HUMAN;
  23. ownTileState = Player.COM;
  24. }
  25.  
  26. if (board[row][col] != Player.EMPTY)
  27. return null;
  28.  
  29. // perform the move
  30. for (Direction dir : Direction.values()) {
  31. int i = 1;
  32. int rowDir = row + i * dir.getRow();
  33. int colDir = col + i * dir.getCol();
  34.  
  35. while (isWithinBoard(rowDir, colDir)) {
  36. Player boardState = board[rowDir][colDir];
  37. Player previousBoardState = board[rowDir - dir.getRow()][colDir
  38. - dir.getCol()];
  39.  
  40. if ((boardState == Player.EMPTY)) {
  41. break;
  42. } else if ((boardState == enemyTileState)
  43. && (previousBoardState == ownTileState)) {
  44. break;
  45. } else if ((boardState == ownTileState)
  46. && (previousBoardState == enemyTileState)) {
  47. while (i >= 0) {
  48. rowDir = row + i * dir.getRow();
  49. colDir = col + i * dir.getCol();
  50. changedBoard.board[rowDir][colDir] = ownTileState;
  51. i--;
  52. }
  53. break;
  54. }
  55. i++;
  56. rowDir = row + i * dir.getRow();
  57. colDir = col + i * dir.getCol();
  58. }
  59. }
  60. // Return null if no move operation has been made.
  61. if (this.equals(changedBoard)) {
  62. return null;
  63. }
  64. return changedBoard;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment