Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Dungeon {
  4. private Monster[][] monsters;
  5. public int gridWidth;
  6. public int gridHeight;
  7.  
  8. public Dungeon(int w, int h) {
  9.  
  10. monsters = new Monster[h][w];
  11. for(int i = 0;i < h; i++) {
  12. for(int j = 0;j < w; j++) {
  13. monsters[i][j] = new Monster();
  14. }
  15. }
  16. this.gridWidth = w;
  17. this.gridHeight = h;
  18. }
  19.  
  20. public void updateMonster(int x, int y, Monster m)
  21. {
  22. boolean done = false;
  23. for (int row = 0; row< monsters.length; row++)
  24. {
  25. for (int col = 0; col < monsters[row].length; col++)
  26. {
  27. if (row == x && col == y && !done)
  28. {
  29. Monster temp = monsters[col][row];
  30. if (temp.getLevel() == -1 && temp.getName().equals(""))
  31. {
  32. monsters[col][row] = m;
  33. done = true;
  34. }
  35. }
  36. }
  37. }
  38. }
  39.  
  40. public void shift(char command) {
  41. Monster temp;
  42. switch (command) {
  43. case 'w' :
  44. for(int i = 0; i < gridWidth; i++) {
  45. temp = monsters[0][i];
  46. for(int j = 1; j < gridHeight; j++) {
  47. monsters[j-1][i] = monsters[j][i];
  48. }
  49. monsters[gridHeight -1][i] = temp;
  50. }
  51. break;
  52. case 'd' :
  53. for(int i = 0; i < gridHeight; i++) {
  54. temp = monsters[i][gridWidth-1];
  55. for(int j = gridWidth-1; j > 0; j--) {
  56. monsters[i][j] = monsters[i][j-1];
  57. }
  58. monsters[i][0] = temp;
  59. }
  60. break;
  61.  
  62. case 's' :
  63. for(int i = 0; i < gridWidth; i++) {
  64. temp = monsters[gridHeight-1][i];
  65. for(int j = gridHeight-1; j>0; j--) {
  66. monsters[j][i] = monsters[j-1][i];
  67. }
  68. monsters[0][i] = temp;
  69. }
  70. break;
  71.  
  72.  
  73.  
  74. case 'a' :
  75. for(int i = 0; i < gridHeight; i++) {
  76. temp = monsters[i][0];
  77. for(int j=1;j<gridWidth;j++) {
  78. monsters[i][j-1] = monsters[i][j];
  79. }
  80. monsters[i][gridWidth-1] = temp;
  81. }
  82. break;
  83. }
  84.  
  85.  
  86. }
  87.  
  88. public void shuffle() {
  89. Random rand = new Random();
  90. for(int i = monsters.length-1; i>0; i--){
  91. for(int j = monsters.length-1; j > 0; j--) {
  92. int y1 = rand.nextInt(j + 1);
  93. int y2 = rand.nextInt(j + 1);
  94. int x1 = rand.nextInt(i+1);
  95. int x2 = rand.nextInt(i+1);
  96. this.swap(x1, y1, x2, y2);
  97. }
  98. }
  99. }
  100. private void swap(int x1 , int y1 , int x2 , int y2) {
  101. Monster temp = monsters[y1][x1];
  102. monsters[y1][x1] = monsters[y2][x2];
  103. monsters[y2][x2] = temp;
  104. }
  105. public String printInfo(){
  106. String result = "";
  107. for(int i=0;i<gridHeight;i++) {
  108. for(int j=0;j<gridWidth;j++) {
  109. result += monsters[i][j].getInfo();
  110. result += " ";
  111. }
  112. result += "\n";
  113. }
  114. return result;
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement