A grid, deque, a step timer, an input queue and levels.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
משבצות לפיקסליםCells to pixels
כתבו to_pixels(cell, size) שמחזירה את הפינה השמאלית העליונה בפיקסלים, ו-to_cell(px, py, size) שעושה את ההפך (איזו משבצת מכילה את הפיקסל).
Write to_pixels(cell, size) returning the top-left corner in pixels, and to_cell(px, py, size) doing the reverse (which cell contains the pixel).
איך הפלט אמור להיראות?What should the output look like?
כתבו step(snake, direction, food, cols, rows) שמחזירה (new_snake, ate, alive): ראש חדש עם מעבר מקצה לקצה; אם הראש על האוכל, הנחש גדל (ate=True); אחרת הזנב נמחק. אם הראש פוגע בגוף (בלי הזנב, אלא אם גדלים), alive=False והנחש לא משתנה.
Write step(snake, direction, food, cols, rows) returning (new_snake, ate, alive): a new head with wrap-around; if the head is on the food, the snake grows (ate=True); otherwise the tail is removed. If the head hits the body (excluding the tail, unless growing), alive=False and the snake is unchanged.
איך הפלט אמור להיראות?What should the output look like?
כתבו place_food(snake, cols, rows, rng) שבוחרת משבצת פנויה באקראי בעזרת rng.choice (rng הוא random.Random), או מחזירה None אם הלוח מלא. השתמשו ב-set כדי שהבדיקה תהיה מהירה.
Write place_food(snake, cols, rows, rng) choosing a free cell at random with rng.choice (rng is a random.Random), or returning None if the board is full. Use a set so the check is fast.
איך הפלט אמור להיראות?What should the output look like?
כתבו מחלקה Controls(direction) עם press(d) שמוסיפה כיוון לתור אם הוא לא הפוך ולא זהה לכיוון האחרון בתור (או לנוכחי אם התור ריק), ושהתור לא מעל 2. next() מוציאה כיוון מהתור (אם יש), מעדכנת ומחזירה את הכיוון הנוכחי.
Write a Controls(direction) class with press(d) adding a direction to the queue if it isn't opposite or equal to the last queued direction (or the current one if the queue is empty), and the queue holds at most 2. next() takes a direction from the queue (if any), updates and returns the current direction.
איך הפלט אמור להיראות?What should the output look like?
[(0, -1), (-1, 0), (-1, 0)]
main.py
רמזHint 1:last = self.queue[-1] if self.queue else self.directionlast = self.queue[-1] if self.queue else self.direction
רמזHint 2:הפוך: d[0] == -last[0] and d[1] == -last[1].Opposite: d[0] == -last[0] and d[1] == -last[1].
Retro Snake: המשחק בלי חלוןRetro Snake: the game without a window
פרויקט מלווהCompanion project
כתבו מחלקה SnakeGame(cols, rows, seed=0): נחש באורך 3 במרכז שפונה ימינה, אוכל ראשון במשבצת פנויה (random.Random(seed)), score, alive. turn(d) מעבירה ל-Controls (מהתרגיל הקודם, כתבו מחדש בקצרה), ו-tick() מבצעת צעד (עם מעבר מקצה לקצה), גדילה וניקוד, ואוכל חדש. אחרי מוות tick לא עושה כלום.
Write a SnakeGame(cols, rows, seed=0) class: a length-3 snake in the centre facing right, the first food on a free cell (random.Random(seed)), score, alive. turn(d) passes to the controls (from the previous exercise, rewritten briefly), and tick() performs a step (with wrap-around), growing and scoring, and new food. After death, tick does nothing.
איך הפלט אמור להיראות?What should the output look like?
כתבו path_to_food(snake, food, cols, rows) שמוצאת את המסלול הקצר ביותר מהראש לאוכל בלי לעבור דרך הגוף (בלי מעבר מקצה לקצה), בעזרת חיפוש לרוחב (BFS) עם deque. מחזירה רשימת כיוונים, או None אם אין מסלול. מהזנב מותר לעבור (הוא זז).
Write path_to_food(snake, food, cols, rows) finding the shortest route from the head to the food without passing through the body (no wrap-around), using breadth-first search (BFS) with a deque. Return a list of directions, or None if there's no route. Passing through the tail is allowed (it moves).
איך הפלט אמור להיראות?What should the output look like?
[(0, 1), (0, 1)]
None []
main.py
רמזHint 1:תור של (cell, path) ו-set של seen.A queue of (cell, path) and a seen set.
כתבו save_state(game) שמחזירה מחרוזת JSON עם snake, direction, food, score (טאפלים כרשימות), ו-load_state(text, cols, rows) שמחזירה מילון עם אותם שדות כטאפלים, אחרי בדיקה: כל המשבצות בלוח, נחש בלי כפילויות, ואוכל לא על הנחש. מצב לא תקין: ValueError.
Write save_state(game) returning a JSON string with snake, direction, food, score (tuples as lists), and load_state(text, cols, rows) returning a dict with the same fields as tuples, after checking: every cell on the board, a snake without duplicates, and food not on the snake. An invalid state: ValueError.
איך הפלט אמור להיראות?What should the output look like?