נבנה על ידיBuilt by Ariel Yohimovich

JavaJava

מבחן 8: משחקיםExam 8: Games

לולאת משחק, delta time, התנגשויות, מצבים ו-EDT

The game loop, delta time, collisions, states and the EDT

75 דקותminutes4 שאלות בחירהmultiple choice3 שאלות קודcoding questions100 נקודותpoints
75:00השעון רק עוזר להתאמן. אפשר גם בלי.The timer is just for practice. You can skip it.

חלק א: שאלות בחירה (5 נקודות לשאלה)Part A: multiple choice (5 points each)

התשובות מתגלות רק כשמסיימים את המבחן.Answers are revealed only when you finish the exam.

1. מה יודפס?What is printed? 5
java
Rectangle a = new Rectangle(0, 0, 10, 10);
Rectangle b = new Rectangle(10, 0, 5, 5);
System.out.println(a.intersects(b));
2. מה יודפס?What is printed? 5
java
double x = 0;
double speed = 200;
double dt = 0.05;
x += speed * dt;
System.out.println(x);
3. מה יודפס?What is printed? 5
java
enum State {
    MENU, PLAYING, GAME_OVER
}

System.out.println(State.PLAYING.ordinal());
4. משחק Swing עם thread לוגיקה נפרד מצייר ישירות על JPanel מה-thread שלו. מה הבעיה?A Swing game with a separate logic thread paints directly onto a JPanel from its own thread. What's the problem? 5

חלק ב: שאלות קודPart B: coding questions

שאלהQuestion 5

פגיעהA hit

כתבו static boolean hit(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh) שבודקת חפיפה בין שני מלבנים (AABB) בלי java.awt. נגיעה בקצה בלבד אינה חפיפה. main מדפיס true false.

Write static boolean hit(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh) checking overlap between two rectangles (AABB) without java.awt. Only touching an edge isn't an overlap. main prints true false.

איך הפלט אמור להיראות?What should the output look like?
true false
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: חפיפה כשכל אחד מתחיל לפני שהשני נגמר, בשני הצירים.They overlap when each starts before the other ends, on both axes.
הפתרון שלנוOur solution
Main.java
public class Main {
    static boolean hit(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh) {
        return ax < bx + bw && bx < ax + aw && ay < by + bh && by < ay + ah;
    }

    public static void main(String[] args) {
        System.out.println(hit(0, 0, 10, 10, 5, 5, 10, 10) + " " + hit(0, 0, 10, 10, 10, 0, 5, 5));
    }
}
פלטOutput
true false

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.

מחווןRubric (25 נקודותpoints)

כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.

שאלהQuestion 6

זמן טעינהA cooldown

מחלקה Gun עם זמן טעינה של 0.5 שנייה. boolean tryFire(double now) מחזירה true ויורה אם עברו לפחות 0.5 שנייה מהירייה הקודמת (הירייה הראשונה תמיד מצליחה), אחרת false. shots() מחזירה כמה יריות היו. main מדפיס true false true 2.

A Gun class with a 0.5-second cooldown. boolean tryFire(double now) returns true and fires if at least 0.5 seconds passed since the previous shot (the first shot always succeeds), otherwise false. shots() returns how many shots there were. main prints true false true 2.

איך הפלט אמור להיראות?What should the output look like?
true false true 2
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: private double last = Double.NEGATIVE_INFINITY;private double last = Double.NEGATIVE_INFINITY;
הפתרון שלנוOur solution
Main.java
public class Main {
    public static void main(String[] args) {
        Gun g = new Gun();
        System.out.println(g.tryFire(0.0) + " " + g.tryFire(0.3) + " " + g.tryFire(0.5) + " " + g.shots());
    }
}

class Gun {
    private static final double COOLDOWN = 0.5;
    private double last = Double.NEGATIVE_INFINITY;
    private int shots;

    boolean tryFire(double now) {
        if (now - last < COOLDOWN) {
            return false;
        }
        last = now;
        shots++;
        return true;
    }

    int shots() {
        return shots;
    }
}
פלטOutput
true false true 2

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.

מחווןRubric (25 נקודותpoints)

כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.

שאלהQuestion 7

מכונת מצביםA state machine

enum State { MENU, PLAYING, PAUSED, GAME_OVER }. מעברים: MENU + start → PLAYING; PLAYING + pause → PAUSED; PAUSED + resume → PLAYING; PLAYING + die → GAME_OVER; GAME_OVER + start → PLAYING. כל אירוע אחר במצב הנוכחי מתעלמים ממנו. כתבו static String run(List<String> events) שמחזירה STATE (ignored N). main מדפיס GAME_OVER (ignored 2).

enum State { MENU, PLAYING, PAUSED, GAME_OVER }. Transitions: MENU + start → PLAYING; PLAYING + pause → PAUSED; PAUSED + resume → PLAYING; PLAYING + die → GAME_OVER; GAME_OVER + start → PLAYING. Any other event in the current state is ignored. Write static String run(List<String> events) returning STATE (ignored N). main prints GAME_OVER (ignored 2).

איך הפלט אמור להיראות?What should the output look like?
GAME_OVER (ignored 2)
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: מתודה next(State s, String e) שמחזירה את המצב הבא, או null אם אין מעבר.A next(State s, String e) method returning the next state, or null if there is no transition.
הפתרון שלנוOur solution
Main.java
import java.util.List;

public class Main {
    enum State {
        MENU, PLAYING, PAUSED, GAME_OVER
    }

    static State next(State s, String event) {
        return switch (s) {
            case MENU -> event.equals("start") ? State.PLAYING : null;
            case PLAYING -> switch (event) {
                case "pause" -> State.PAUSED;
                case "die" -> State.GAME_OVER;
                default -> null;
            };
            case PAUSED -> event.equals("resume") ? State.PLAYING : null;
            case GAME_OVER -> event.equals("start") ? State.PLAYING : null;
        };
    }

    static String run(List<String> events) {
        State s = State.MENU;
        int ignored = 0;
        for (String e : events) {
            State n = next(s, e);
            if (n == null) {
                ignored++;
            } else {
                s = n;
            }
        }
        return s + " (ignored " + ignored + ")";
    }

    public static void main(String[] args) {
        System.out.println(run(List.of("pause", "start", "pause", "die", "resume", "die")));
    }
}
פלטOutput
GAME_OVER (ignored 2)

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.

מחווןRubric (30 נקודותpoints)

כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.

נבנה על ידיBuilt by Ariel Yohimovich