Programkod fr�n f�rel�sning 11

Jag hinner tyv�rr inte kommentera det jag gjorde, men h�r �r sj�lva programkoden (med n�gra korta kommentarer).

import se.lth.cs.pt.graphics.*;
import se.lth.cs.pt.graphics.events.*;
import se.lth.cs.pt.turtle.visible.*;
import se.lth.cs.pt.debug.*;
import se.lth.cs.pt.io.*;
import se.lth.cs.pt.die.*;
import java.util.*;

class FunGame {

    public static void main(String[] args) {
        new FunGame().run();
    }

    GraphicsWindow w;
    Turtle t;

    void run() {
        setup();
        eventLoop();
    }

    void setup() {
        Debug.off();
        w = new GraphicsWindow(500, 500);
        t = new Turtle(w, 250, 250);
        t.setSpeed(30);
        w.checkKeys(true, false, false);
        w.checkMouse(true, false, false, false, false);
        w.timeStep(100);
    }

    void eventLoop() {
        while (true) {
            Debug.println("V�ntar p� event");
            GameEvent e = w.getNextEvent();
            Debug.println("N�got h�nde...");
            switch (e.getKind()) {
            case GameEvent.KEY_PRESSED:
                keyPressed(e.getKey());
                break;
            case GameEvent.MOUSE_CLICKED:
                mouseClicked(e.getX(), e.getY());
                break;
            case GameEvent.TICK:
                tick();
                break;
            }
        }
    }

    void tick() {
        t.forward(5);
    }

    void keyPressed(char key) {
        int angle = 30;
        Debug.println("Du tryckte p� " + key);
        switch (key) {
        case 'a':
            t.left(angle);
            break;
        case 'd':
            t.right(angle);
            break;
        }
    }

    void mouseClicked(double x, double y) {
        t.jumpTo(x, y);
    }
}


class ArrayExample {

    public static void main(String[] args) {
        new ArrayExample().run();
    }

    void run() {
        int n = 5;
        double[] values = new double[n];
        read(values, n);
        for (int k = values.length - 1; k >= 0; k--) {
            System.out.println(values[k]);
        }
    }

    void read(double[] values, int n) {
	// vi kan h�r inne �ndra inneh�llet i vektorn values, och
	// eftersom vi har en referens till samma vektor som i det
	// anropande programmet s� kommer �ndringarna att vara
	// tillg�ngliga �ven utanf�r denna metod
        for (int k = 0; k < values.length; k++) {
            values[k] = Keyboard.nextDouble("Ange v�rde: ");
        }
        n++;                       // p�verkar inte n i anropande metod
        values = new double[1];    // p�verkar inte vektorn i anropande metod
    }        
}


class DieGame {

    public static void main(String[] args) {
        new DieGame().run();
    }

    void run() {
        int nbrOfDice = 5;
        Die[] dice = new Die[nbrOfDice];
        for (int k = 0; k < dice.length; k++) {
            dice[k] = new Die();
        }
	// ...anv�nd t�rningarna i dice till n�gonting...
    }
}


class ArraysAgain {

    public static void main(String[] args) {
        new ArraysAgain().run();
    }

    void run() {
        double[] values = new double[1000];
        int size = read(values);
        System.out.println(sum(values, size));
    }

    int read(double[] values) {
	// denna metod l�gger in v�rden i vektorn values (som �r samma
	// vektor-objekt som i den anropande metoden), och returnerar
	// antalet element som vi l�st in.
        int size = 0;
        while (true) {
            double value = Keyboard.nextDouble("Ange tal (< 0 f�r avbryt): ");
            if (value < 0) {
                return size;
            }
            values[size] = value;
            size++;
        }
    }

    double sum(double[] values, int size) {
        double sum = 0;
        for (int k = 0; k < size; k++) {
            sum += values[k];
        }
        return sum;
    }
}


class DieInvestigation {

    public static void main(String[] args) {
        new DieInvestigation().run();
    }

    void run() {
        Die die = new Die();
        int count = 0;
        for (int k = 1; k <= 6000; k++) {
            die.roll();
            if (die.getNbrOfSpots() == 1) {
                count++;
            }
        }
        System.out.println(count);
    }
}