repl.it
@panniu/

RandomWalk

Java

No description

fork
loading
Files
  • Main.java
  • jdt.ls-java-project
  • DrawingPanel.java
  • nohup.out
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.awt.*;
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static int totalWalk = 0;
    public static int totalSteps = 0;
    public static int bestWalk = 0;

    public static final int SIZE = 100;

    public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(SIZE, SIZE);
        Graphics g = panel.getGraphics();
        Random rand = new Random();
        Scanner kb = new Scanner(System.in);

        System.out.println("Welcome to the CSE 142 random walk simulator.");

        boolean yesNo = true;

        while (yesNo) {
            singleWalk(kb, panel, rand, g);
            yesNo = askAgain(kb, panel);
        }
        statReport();
    }

    public static void singleWalk(Scanner kb, DrawingPanel panel, Random rand, Graphics g) {
        System.out.println("Let's go for a walk!\n");

        System.out.print("Radius? ");
        int radius = kb.nextInt();

        System.out.print("Time between steps (ms)? ");
        int sleepTime = kb.nextInt();

        panel.zoom(5);

        g.drawOval(0, 0, radius * 2, radius * 2);

        System.out.printf("Walker starting at x=%d, y=%d\n", radius, radius);
        
        int x = radius;
        int y = radius;
        int steps = 0;
        
        while (Math.pow(x - radius, 2) + Math.pow(y - radius, 2) <= Math.pow(radius, 2)) {
            g.drawRect(x, y, 1 / radius, 1 / radius);
            int r = rand.nextInt(4);
        
            if (r == 0) {
                x++;
            } else if (r == 1) {
                x--;
            } else if (r == 2) {
                y++;
            } else if (r == 3) { // r == 3
                y--;
            }
            System.out.printf("x=%d, y=%d, steps=%d\n", x, y, steps + 1);
            panel.sleep(sleepTime);
            steps++;
        }

        totalSteps += steps;
        totalWalk++;
        if (Math.min(bestWalk, steps) == 0) {
            bestWalk = steps;
        }
        bestWalk = Math.min(bestWalk, steps);

        System.out.println("Walker escaped in " + steps + " step(s).");

    }

    public static boolean askAgain(Scanner kb, DrawingPanel panel) {
        System.out.print("Walk again (yes/no) ? ");
        String again = kb.next();
        System.out.println();
        if (again.startsWith("y") || again.startsWith("Y")) {
            panel.clear();
            return true;
        }
        return false;
    }

    public static void statReport() {
        System.out.println("Total walks = " + totalWalk);
        System.out.println("Total steps = " + totalSteps);
        System.out.println("Best Walk   = " + bestWalk);
    }
}
Fetching token
?