{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "917f051e",
   "metadata": {},
   "source": [
    "# 19-11 · Направление как вектор\n",
    "\n",
    "Практика к разделу [«Направление как вектор»](../../site/chapters/glava-19/19-11-napravlenie-kak-vektor.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6de916f2",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Вычислить новую позицию головы через словарь векторов вместо if/elif."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a2e8041",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a6814524",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:53:41.933141Z",
     "iopub.status.busy": "2026-09-03T00:53:41.932959Z",
     "iopub.status.idle": "2026-09-03T00:53:41.959776Z",
     "shell.execute_reply": "2026-09-03T00:53:41.958652Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(20, 0)\n"
     ]
    }
   ],
   "source": [
    "STEP = 20\n",
    "DIRECTION_VECTORS = {\n",
    "    \"up\": (0, STEP),\n",
    "    \"down\": (0, -STEP),\n",
    "    \"left\": (-STEP, 0),\n",
    "    \"right\": (STEP, 0),\n",
    "}\n",
    "\n",
    "def next_head(head, direction):\n",
    "    dx, dy = DIRECTION_VECTORS[direction]\n",
    "    return head[0] + dx, head[1] + dy\n",
    "\n",
    "print(next_head((0, 0), \"right\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89370eca",
   "metadata": {},
   "source": [
    "## Задание ★ Базовая практика"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e8c8fb06",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:53:41.961453Z",
     "iopub.status.busy": "2026-09-03T00:53:41.961281Z",
     "iopub.status.idle": "2026-09-03T00:53:41.965716Z",
     "shell.execute_reply": "2026-09-03T00:53:41.965084Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "next_head() верна для всех четырёх направлений.\n"
     ]
    }
   ],
   "source": [
    "assert next_head((0, 0), \"up\") == (0, 20)\n",
    "assert next_head((0, 0), \"down\") == (0, -20)\n",
    "assert next_head((0, 0), \"left\") == (-20, 0)\n",
    "assert next_head((100, 100), \"right\") == (120, 100)\n",
    "print(\"next_head() верна для всех четырёх направлений.\")"
   ]
  }
 ],
 "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
}
