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

JavaJava
46
חלקPart 8: משחקים ב-JavaGames in Java כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

מצבי משחק: תפריט, משחק ו-Game OverGame states: menu, play & game over

מכונת מצבים עם enum, מסכים, שמירת שיא, וקול.

A state machine with an enum, screens, a saved high score, and sound.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

1. השחקן לחץ HIT בזמן שהוא בתפריט. מה קורה?The player pressed HIT while in the menu. What happens?

2. למה enum ולא מחרוזות כמו "menu"?Why an enum and not strings like "menu"?

3. מתי שומרים שיא לקובץ?When do you save a high score to the file?

תרגיליםExercises

תרגילExercise 1

מעבריםTransitions

עם Game מהתזכורת הריצו את האירועים HIT, ENTER, HIT, ENTER, HIT, HIT והדפיסו את המצב והחיים אחרי כל אחד. HIT בתפריט ו-ENTER באמצע משחק לא עושים כלום.

With Game from the refresher run the events HIT, ENTER, HIT, ENTER, HIT, HIT and print the state and lives after each. HIT in the menu and ENTER mid-game do nothing.

איך הפלט אמור להיראות?What should the output look like?
HIT -> MENU lives=3
ENTER -> PLAYING lives=3
HIT -> PLAYING lives=2
ENTER -> PLAYING lives=2
HIT -> PLAYING lives=1
HIT -> GAME_OVER lives=0
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: עקבו בסימולציה אחרי state.Follow state in the simulation.
לפתרוןSolution
תרגילExercise 2

מסכיםScreens

screen(state, score) עם switch בחצים מחזיר את הטקסט של כל מסך. הדפיסו לכל המצבים עם ניקוד 120.

screen(state, score) with an arrow switch returns each screen's text. Print for every state with a score of 120.

איך הפלט אמור להיראות?What should the output look like?
PRESS ENTER TO START
score 120
GAME OVER - final score 120
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: State.values() עובר על כל המצבים.State.values() goes over every state.
לפתרוןSolution
תרגילExercise 3

שיא בקובץA high score in a file

loadBest() קוראת את best.txt (או 0), ו-saveBest(score) כותבת רק אם יותר גבוה. אחרי משחקים של 40, 120 ו-90 הדפיסו played 40, best 40, played 120, best 120, played 90, best 120.

loadBest() reads best.txt (or 0), and saveBest(score) writes only if it's higher. After games of 40, 120 and 90 print played 40, best 40, played 120, best 120, played 90, best 120.

איך הפלט אמור להיראות?What should the output look like?
played 40, best 40
played 120, best 120
played 90, best 120
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: מתחילים מ-deleteIfExists כדי שכל הרצה תתחיל נקי.Start with deleteIfExists so every run starts clean.
לפתרוןSolution
תרגילExercise 4

צייד באגים: מצב שנשכחBug hunter: a forgotten state

music(state) לא מכסה את GAME_OVER, ו-javac עוצר. הוסיפו sad.wav למצב החסר.

music(state) doesn't cover GAME_OVER, and javac stops. Add sad.wav for the missing state.

איך הפלט אמור להיראות?What should the output look like?
sad.wav
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: case GAME_OVER -> "sad.wav";case GAME_OVER -> "sad.wav";
לפתרוןSolution
תרגילExercise 5

משחק 4: משחק עם תפריט ושיאGame 4: a game with a menu and a high score

פרויקט מלווהCompanion project

הוסיפו ניקוד: STAR נותן 10, ושלוש פגיעות מסיימות את המשחק ומעדכנות את best. אחרי התסריט (שני משחקים: 30 נקודות ואז 10) הדפיסו GAME_OVER score=10 best=30.

Add scoring: STAR gives 10, and three hits end the game and update best. After the script (two games: 30 points then 10) print GAME_OVER score=10 best=30.

איך הפלט אמור להיראות?What should the output look like?
GAME_OVER score=10 best=30
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: בכניסה ל-PLAYING מאפסים חיים וניקוד, אבל לא את best.On entering PLAYING reset lives and score, but not best.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

מחסנית מסכיםA stack of screens

משחק → השהיה → הגדרות: כל מסך חדש נדחף על המחסנית, ו-"חזרה" מוציא. הדפיסו top: SETTINGS ואחרי שתי חזרות back to: PLAYING, depth 1.

Game → pause → settings: each new screen is pushed onto the stack, and "back" pops. Print top: SETTINGS and after two backs back to: PLAYING, depth 1.

איך הפלט אמור להיראות?What should the output look like?
top: SETTINGS
back to: PLAYING, depth 1
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: ArrayDeque עם push, pop, peek.An ArrayDeque with push, pop, peek.
לפתרוןSolution
אתגרChallenge 2

צלילים לפי אירועיםSounds by events

SoundQueue.on(event) מוסיפה צליל לכל אירוע (ding, bump, game-over), ואחרי Game Over מתעלמת מהכול. הדפיסו [ding, ding, bump, game-over]. (במחשב שלכם: javax.sound.sampled.Clip מנגן את הקבצים.)

SoundQueue.on(event) adds a sound per event (ding, bump, game-over), and after game over ignores everything. Print [ding, ding, bump, game-over]. (On your computer: javax.sound.sampled.Clip plays the files.)

איך הפלט אמור להיראות?What should the output look like?
[ding, ding, bump, game-over]
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: דגל over.An over flag.
לפתרוןSolution

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