{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1a3f6d49",
   "metadata": {},
   "source": [
    "# 19-20 · Zderzenie z samym sobą\n",
    "\n",
    "Praktyka do sekcji [„Zderzenie z sobą”](/pl/chapters/rozdzial-19/19-20-stolknovenie-s-soboj.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4b01590",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "is_self_collision() jest sprawdzane względem korpusu PO zakręcie – wjeżdżanie w klatkę uwolnionego ogona jest dozwolone."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c96bf8c",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "33dea4f3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:54:04.794529Z",
     "iopub.status.busy": "2026-09-03T00:54:04.794312Z",
     "iopub.status.idle": "2026-09-03T00:54:04.819543Z",
     "shell.execute_reply": "2026-09-03T00:54:04.818993Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "def move_snake(snake, new_head, *, grow):\n",
    "    if grow:\n",
    "        return [new_head, *snake]\n",
    "    return [new_head, *snake[:-1]]\n",
    "\n",
    "def is_self_collision(new_head, body_after_move):\n",
    "    return new_head in body_after_move\n",
    "\n",
    "snake = [(0, 0), (20, 0), (40, 0), (40, 20)]\n",
    "new_head = (40, 20)   # клетка старого хвоста\n",
    "new_snake = move_snake(snake, new_head, grow=False)\n",
    "print(is_self_collision(new_head, new_snake[1:]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ef2c340",
   "metadata": {},
   "source": [
    "## Podstawowa praktyka zadań ★"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f46b8685",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:54:04.820761Z",
     "iopub.status.busy": "2026-09-03T00:54:04.820625Z",
     "iopub.status.idle": "2026-09-03T00:54:04.824662Z",
     "shell.execute_reply": "2026-09-03T00:54:04.823220Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Заезд в клетку освободившегося хвоста легален только без роста.\n"
     ]
    }
   ],
   "source": [
    "assert is_self_collision(new_head, new_snake[1:]) is False  # хвост уже освободил клетку\n",
    "\n",
    "middle_hit = move_snake(snake, (20, 0), grow=False)\n",
    "assert is_self_collision((20, 0), middle_hit[1:]) is True  # (20,0) — не хвост, настоящий удар\n",
    "\n",
    "grown = move_snake(snake, new_head, grow=True)\n",
    "assert is_self_collision(new_head, grown[1:]) is True  # при росте хвост НЕ освобождается\n",
    "print(\"Заезд в клетку освободившегося хвоста легален только без роста.\")"
   ]
  }
 ],
 "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
}
