{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "48ae8e51",
   "metadata": {},
   "source": [
    "#19-18 · Jedzenie i darmowa klatka\n",
    "\n",
    "Praktyka do sekcji [„Jedzenie i wolna komórka”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f40f87a5",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "move_snake() i choose_food() – nowa głowa plus stare ciało, jedzenie tylko w wolnej celi."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af6dca77",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9c347b65",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:54:00.077112Z",
     "iopub.status.busy": "2026-09-03T00:54:00.076985Z",
     "iopub.status.idle": "2026-09-03T00:54:00.107277Z",
     "shell.execute_reply": "2026-09-03T00:54:00.106515Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(20, 0), (0, 0), (-20, 0)]\n",
      "(-40, 60)\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "STEP = 20\n",
    "\n",
    "def move_snake(snake, new_head, *, grow):\n",
    "    if grow:\n",
    "        return [new_head, *snake]\n",
    "    return [new_head, *snake[:-1]]\n",
    "\n",
    "def all_cells(half=100, step=STEP):\n",
    "    coords = range(-half, half + 1, step)\n",
    "    return tuple((x, y) for x in coords for y in coords)\n",
    "\n",
    "def choose_food(snake, rng, *, half=100, step=STEP):\n",
    "    occupied = set(snake)\n",
    "    free = tuple(c for c in all_cells(half, step) if c not in occupied)\n",
    "    if not free:\n",
    "        return None   # змейка заняла всё поле — честной клетки для еды нет\n",
    "    return rng.choice(free)\n",
    "\n",
    "rng = random.Random(7)\n",
    "print(move_snake([(0, 0), (-20, 0), (-40, 0)], (20, 0), grow=False))\n",
    "print(choose_food([(0, 0)], rng))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a446dbd3",
   "metadata": {},
   "source": [
    "## Podstawowa praktyka zadań ★"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "29e6319c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:54:00.108790Z",
     "iopub.status.busy": "2026-09-03T00:54:00.108609Z",
     "iopub.status.idle": "2026-09-03T00:54:00.115259Z",
     "shell.execute_reply": "2026-09-03T00:54:00.114701Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Еда никогда не попадает на змейку; move_snake растит или сдвигает верно.\n",
      "На полностью занятом поле choose_food() честно возвращает None.\n"
     ]
    }
   ],
   "source": [
    "rng = random.Random(1)\n",
    "snake = [(0, 0), (-20, 0), (-40, 0), (-60, 0)]\n",
    "for _ in range(50):\n",
    "    food = choose_food(snake, rng)\n",
    "    assert food not in snake\n",
    "\n",
    "grown = move_snake(snake, (20, 0), grow=True)\n",
    "assert len(grown) == len(snake) + 1\n",
    "shifted = move_snake(snake, (20, 0), grow=False)\n",
    "assert len(shifted) == len(snake)\n",
    "print(\"Еда никогда не попадает на змейку; move_snake растит или сдвигает верно.\")\n",
    "\n",
    "# Skrajny przypadek z rozdziału 19.18: nie pozostały żadne wolne pola.\n",
    "polnoe_pole = list(all_cells())\n",
    "assert choose_food(polnoe_pole, rng) is None\n",
    "print(\"На полностью занятом поле choose_food() честно возвращает None.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
