Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1.  
  2. import java.util.Random;
  3. import java.util.HashMap;
  4.  
  5. public class Satunnaiskavely {
  6.  
  7. private HashMap<Integer, HashMap<Integer, Double>> taulukko;
  8. private int sijaintiX = 0;
  9. private int sijaintiY = 0;
  10. private int korkeus;
  11. private int leveys;
  12. private Random random;
  13.  
  14. public Satunnaiskavely(int leveys, int korkeus) {
  15. this.korkeus = korkeus;
  16. this.leveys = leveys;
  17. this.taulukko = new HashMap<>();
  18. this.random = new Random();
  19. int x = 0;
  20. while (x < leveys) {
  21. this.taulukko.putIfAbsent(x, new HashMap<>());
  22.  
  23. int y = 0;
  24. while (y < korkeus) {
  25. this.taulukko.get(x).put(y, 0.0);
  26. y++;
  27. }
  28.  
  29. x++;
  30. }
  31.  
  32. this.sijaintiX = leveys / 2;
  33. this.sijaintiY = korkeus / 2;
  34.  
  35. this.taulukko.get(this.sijaintiX).put(this.sijaintiY, 1.0);
  36. }
  37.  
  38. public void askel() {
  39. // pienennetään hajujalkeä
  40. int x = 0;
  41. while (x < this.taulukko.size()) {
  42. int y = 0;
  43.  
  44. while (y < this.taulukko.get(x).size()) {
  45.  
  46. double arvo = this.taulukko.get(x).get(y);
  47. if (arvo > 0) {
  48. arvo -= 0.01;
  49. }
  50.  
  51. this.taulukko.get(x).put(y, arvo);
  52. y++;
  53. }
  54.  
  55. x++;
  56. }
  57.  
  58. // kun hahmo on tietyssä pisteessä, asetetaan taulukkoon arvo 1
  59. int randomi = random.nextInt(100);
  60. if (randomi < 20) {
  61. sijaintiX += 0;
  62. sijaintiY += 0;
  63. } else if (randomi < 40) {
  64. if (sijaintiX + 1 <= leveys) {
  65. sijaintiX += 1;
  66. }
  67. } else if (randomi < 60) {
  68. if (sijaintiX - 1 >= 0) {
  69. sijaintiX -= 1;
  70. }
  71. } else if (randomi < 80) {
  72. if (sijaintiY + 1 <= korkeus) {
  73. sijaintiY += 1;
  74. }
  75. } else {
  76. if (sijaintiY - 1 >= 0) {
  77. sijaintiY -= 1;
  78. }
  79. }
  80. this.taulukko.get(this.sijaintiX).put(this.sijaintiY, 1.0);
  81. }
  82.  
  83. public HashMap<Integer, HashMap<Integer, Double>> getTaulukko() {
  84. return taulukko;
  85. }
  86.  
  87. public void setTaulukko(HashMap<Integer, HashMap<Integer, Double>> taulukko) {
  88. this.taulukko = taulukko;
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement