{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b79cdfe",
   "metadata": {},
   "source": [
    "# 14-04 · Гонка Turtle с объектами\n",
    "\n",
    "Практика к разделу [«Гонка Turtle с объектами»](../../site/chapters/glava-14/14-04-gonka-turtle-obekty-itogi.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74b40026",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Обернуть гонку черепашек в собственный класс."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3803cdcf",
   "metadata": {},
   "source": [
    "## О случайности в этом ноутбуке\n",
    "\n",
    "Гонка использует `random.randint()` — для предсказуемого результата в автоматическом\n",
    "выполнении здесь закрепляем случайность через `random.seed()`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c1e07f6",
   "metadata": {},
   "source": [
    "## Настройка (выполнить один раз)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4813632e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:49.104715Z",
     "iopub.status.busy": "2026-08-14T20:58:49.104601Z",
     "iopub.status.idle": "2026-08-14T20:58:49.260649Z",
     "shell.execute_reply": "2026-08-14T20:58:49.260096Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Окно Turtle готово.\n"
     ]
    }
   ],
   "source": [
    "import turtle\n",
    "\n",
    "screen = turtle.Screen()\n",
    "print(\"Окно Turtle готово.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d68e210",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b9b6ba53",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:49.261691Z",
     "iopub.status.busy": "2026-08-14T20:58:49.261573Z",
     "iopub.status.idle": "2026-08-14T20:58:53.032637Z",
     "shell.execute_reply": "2026-08-14T20:58:53.032072Z"
    }
   },
   "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": "b64f702c",
   "metadata": {},
   "source": [
    "## Завершение (выполнить один раз, в самом конце)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "33393c66",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:53.033668Z",
     "iopub.status.busy": "2026-08-14T20:58:53.033555Z",
     "iopub.status.idle": "2026-08-14T20:58:53.036721Z",
     "shell.execute_reply": "2026-08-14T20:58:53.036245Z"
    }
   },
   "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
}
