Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4.  
  5. public class GameTactic {
  6. char[][] matrix;
  7. int size;
  8.  
  9. public GameTactic(char[][] matrix, int size) {
  10. this.matrix = matrix;
  11. this.size = size;
  12. }
  13.  
  14. public int GenerateCost(Node n){
  15. if(this.matrix[n.getRow()][n.getCol()]=='R'){
  16. return 1;
  17. }
  18. if(this.matrix[n.getRow()][n.getCol()]=='D'){
  19. return 3;
  20. }
  21. if(this.matrix[n.getRow()][n.getCol()]=='H'){
  22. return 10;
  23. }
  24. return -1;
  25. }
  26.  
  27. public List<Node> GetPossibleNeigh(Node n,int timeStamp){
  28. int row=n.getRow();
  29. int col=n.getCol();
  30. List<Node> possibleNeigh=new ArrayList<>();
  31. //R
  32. if(col<size-1 && matrix[row][col+1]!='W') {
  33. Node R = new Node(row,col+1);
  34. String path=n.getPath()+"-"+"R";
  35. R.setPath(path);
  36. R.setPriority("R");
  37. R.setCost(GenerateCost(R));
  38. R.setTimeStamp(timeStamp);
  39. possibleNeigh.add(R);
  40. }
  41. //RD
  42. if(col<size-1 && row<size-1 && matrix[row][col+1]!='W' && matrix[row+1][col]!='W' && matrix[row+1][col+1]!='W') {
  43. Node RD = new Node(row+1,col+1);
  44. String path=n.getPath()+"-"+"RD";
  45. RD.setPath(path);
  46. RD.setPriority("RD");
  47. RD.setCost(GenerateCost(RD));
  48. RD.setTimeStamp(timeStamp);
  49. possibleNeigh.add(RD);
  50. }
  51. //D
  52. if(row<size-1 && matrix[row+1][col]!='W') {
  53. Node D = new Node(row+1,col);
  54. String path=n.getPath()+"-"+"D";
  55. D.setPath(path);
  56. D.setPriority("D");
  57. D.setCost(GenerateCost(D));
  58. D.setTimeStamp(timeStamp);
  59. possibleNeigh.add(D);
  60. }
  61. //DL
  62. if(col>0 && row<size-1 && matrix[row+1][col-1]!='W' && matrix[row+1][col]!='W' && matrix[row][col-1]!='W') {
  63. Node DL = new Node(row+1,col-1);
  64. String path=n.getPath()+"-"+"DL";
  65. DL.setPath(path);
  66. DL.setPriority("DL");
  67. DL.setCost(GenerateCost(DL));
  68. DL.setTimeStamp(timeStamp);
  69. possibleNeigh.add(DL);
  70. }
  71. //L
  72. if(col>0 && matrix[row][col-1]!='W') {
  73. Node L = new Node(row,col-1);
  74. String path=n.getPath()+"-"+"L";
  75. L.setPath(path);
  76. L.setPriority("L");
  77. L.setCost(GenerateCost(L));
  78. L.setTimeStamp(timeStamp);
  79. possibleNeigh.add(L);
  80. }
  81. //LU
  82. if(col>0 && row>0 && matrix[row-1][col-1]!='W' && matrix[row-1][col]!='W' && matrix[row][col-1]!='W') {
  83. Node LU = new Node(row-1,col-1);
  84. String path=n.getPath()+"-"+"LU";
  85. LU.setPath(path);
  86. LU.setPriority("LU");
  87. LU.setCost(GenerateCost(LU));
  88. LU.setTimeStamp(timeStamp);
  89. possibleNeigh.add(LU);
  90. }
  91. //U
  92. if(row>0 && matrix[row-1][col]!='W') {
  93. Node U = new Node(row-1,col);
  94. String path=n.getPath()+"-"+"U";
  95. U.setPath(path);
  96. U.setPriority("U");
  97. U.setCost(GenerateCost(U));
  98. U.setTimeStamp(timeStamp);
  99. possibleNeigh.add(U);
  100. }
  101. //RU
  102. if(col<size-1 && row>0 && matrix[row-1][col+1]!='W' && matrix[row-1][col]!='W' && matrix[row][col+1]!='W') {
  103. Node RU = new Node(row-1,col+1);
  104. String path=n.getPath()+"-"+"RU";
  105. RU.setPath(path);
  106. RU.setPriority("RU");
  107. RU.setCost(GenerateCost(RU));
  108. RU.setTimeStamp(timeStamp);
  109. possibleNeigh.add(RU);
  110. }
  111. return possibleNeigh;
  112. }
  113. public boolean IsGoal(Node n){
  114. if(n.getRow()==size-1 && n.getCol()==size-1){
  115. return true;
  116. }
  117. return false;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement