{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c725940d",
   "metadata": {},
   "source": [
    "# 14-04 · Гонка Turtle с объектами\n",
    "\n",
    "Практика к разделу [«Гонка Turtle с объектами»](../../site/chapters/glava-14/14-04-gonka-turtle-obekty-itogi.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9f91a91",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Обернуть гонку черепашек в собственный класс."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da108a95",
   "metadata": {},
   "source": [
    "## О случайности в этом ноутбуке\n",
    "\n",
    "Гонка использует `random.randint()` — для предсказуемого результата в автоматическом\n",
    "выполнении здесь закрепляем случайность через `random.seed()`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbef36f0",
   "metadata": {},
   "source": [
    "## Настройка (выполнить один раз)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "274578ec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T19:47:52.886392Z",
     "iopub.status.busy": "2026-08-17T19:47:52.886217Z",
     "iopub.status.idle": "2026-08-17T19:47:53.029909Z",
     "shell.execute_reply": "2026-08-17T19:47:53.029274Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Окно Turtle готово.\n"
     ]
    }
   ],
   "source": [
    "import turtle\n",
    "\n",
    "screen = turtle.Screen()\n",
    "print(\"Окно Turtle готово.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb6bc830",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f33474d6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T19:47:53.031627Z",
     "iopub.status.busy": "2026-08-17T19:47:53.031474Z",
     "iopub.status.idle": "2026-08-17T19:47:56.816582Z",
     "shell.execute_reply": "2026-08-17T19:47:56.816001Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Победил участник цвета orange!\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "random.seed(5)\n",
    "screen.setup(500, 400)\n",
    "\n",
    "class Uchastnik:\n",
    "    def __init__(self, cvet, startovyj_y):\n",
    "        self.t = turtle.Turtle()\n",
    "        self.t.shape(\"turtle\")\n",
    "        self.t.color(cvet)\n",
    "        self.t.penup()\n",
    "        self.t.goto(-200, startovyj_y)\n",
    "        self.cvet = cvet\n",
    "\n",
    "    def sdelat_shag(self):\n",
    "        self.t.forward(random.randint(1, 10))\n",
    "\n",
    "    def finishiroval(self, finish_line):\n",
    "        return self.t.xcor() >= finish_line\n",
    "\n",
    "cveta = [\"red\", \"blue\", \"green\", \"orange\"]\n",
    "uchastniki = [Uchastnik(cvet, i * 40 - 60) for i, cvet in enumerate(cveta)]\n",
    "\n",
    "pobeditel = None\n",
    "while pobeditel is None:\n",
    "    for u in uchastniki:\n",
    "        u.sdelat_shag()\n",
    "        if u.finishiroval(200):\n",
    "            pobeditel = u.cvet\n",
    "            break\n",
    "\n",
    "print(f\"Победил участник цвета {pobeditel}!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38ad807f",
   "metadata": {},
   "source": [
    "## Завершение (выполнить один раз, в самом конце)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "867e33cd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T19:47:56.817590Z",
     "iopub.status.busy": "2026-08-17T19:47:56.817481Z",
     "iopub.status.idle": "2026-08-17T19:47:56.820386Z",
     "shell.execute_reply": "2026-08-17T19:47:56.819952Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Окно Turtle закрыто.\n"
     ]
    }
   ],
   "source": [
    "screen.bye()\n",
    "print(\"Окно Turtle закрыто.\")"
   ]
  }
 ],
 "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
}
