Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.HashMap;
- public class Satunnaiskavely {
- private HashMap<Integer, HashMap<Integer, Double>> taulukko;
- private int sijaintiX = 0;
- private int sijaintiY = 0;
- private int korkeus;
- private int leveys;
- private Random random;
- public Satunnaiskavely(int leveys, int korkeus) {
- this.korkeus = korkeus;
- this.leveys = leveys;
- this.taulukko = new HashMap<>();
- this.random = new Random();
- int x = 0;
- while (x < leveys) {
- this.taulukko.putIfAbsent(x, new HashMap<>());
- int y = 0;
- while (y < korkeus) {
- this.taulukko.get(x).put(y, 0.0);
- y++;
- }
- x++;
- }
- this.sijaintiX = leveys / 2;
- this.sijaintiY = korkeus / 2;
- this.taulukko.get(this.sijaintiX).put(this.sijaintiY, 1.0);
- }
- public void askel() {
- // pienennetään hajujalkeä
- int x = 0;
- while (x < this.taulukko.size()) {
- int y = 0;
- while (y < this.taulukko.get(x).size()) {
- double arvo = this.taulukko.get(x).get(y);
- if (arvo > 0) {
- arvo -= 0.01;
- }
- this.taulukko.get(x).put(y, arvo);
- y++;
- }
- x++;
- }
- // kun hahmo on tietyssä pisteessä, asetetaan taulukkoon arvo 1
- int randomi = random.nextInt(100);
- if (randomi < 20) {
- sijaintiX += 0;
- sijaintiY += 0;
- } else if (randomi < 40) {
- if (sijaintiX + 1 <= leveys) {
- sijaintiX += 1;
- }
- } else if (randomi < 60) {
- if (sijaintiX - 1 >= 0) {
- sijaintiX -= 1;
- }
- } else if (randomi < 80) {
- if (sijaintiY + 1 <= korkeus) {
- sijaintiY += 1;
- }
- } else {
- if (sijaintiY - 1 >= 0) {
- sijaintiY -= 1;
- }
- }
- this.taulukko.get(this.sijaintiX).put(this.sijaintiY, 1.0);
- }
- public HashMap<Integer, HashMap<Integer, Double>> getTaulukko() {
- return taulukko;
- }
- public void setTaulukko(HashMap<Integer, HashMap<Integer, Double>> taulukko) {
- this.taulukko = taulukko;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement