{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "144c89aa",
   "metadata": {},
   "source": [
    "# 19-23 · Restart / New Game\n",
    "\n",
    "Praktyka do sekcji [„Restart / New Game”](/pl/chapters/rozdzial-19/19-23-restart.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "576afe82",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Restart przenosi high_score do nowego stanu i zwiększa generation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dddfa181",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "061b78bb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:54:12.219360Z",
     "iopub.status.busy": "2026-09-03T00:54:12.219183Z",
     "iopub.status.idle": "2026-09-03T00:54:12.246790Z",
     "shell.execute_reply": "2026-09-03T00:54:12.246195Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'snake': [(0, 0)], 'score': 0, 'high_score': 40, 'status': 'ready'} 4\n"
     ]
    }
   ],
   "source": [
    "def new_game_state(high_score=0):\n",
    "    return {\"snake\": [(0, 0)], \"score\": 0, \"high_score\": high_score, \"status\": \"ready\"}\n",
    "\n",
    "def restart(state, generation):\n",
    "    return new_game_state(high_score=state[\"high_score\"]), generation + 1\n",
    "\n",
    "state = {\"snake\": [(0, 0), (-20, 0), (-40, 0)], \"score\": 40, \"high_score\": 40, \"status\": \"game_over\"}\n",
    "new_state, new_generation = restart(state, generation=3)\n",
    "print(new_state, new_generation)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "237a5fb0",
   "metadata": {},
   "source": [
    "## Podstawowa praktyka zadań ★"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a73e73d9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T00:54:12.248042Z",
     "iopub.status.busy": "2026-09-03T00:54:12.247847Z",
     "iopub.status.idle": "2026-09-03T00:54:12.250813Z",
     "shell.execute_reply": "2026-09-03T00:54:12.250271Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Restart сбрасывает игру, сохраняя рекорд и защищая от старых тиков через generation.\n"
     ]
    }
   ],
   "source": [
    "assert new_state[\"snake\"] == [(0, 0)]\n",
    "assert new_state[\"score\"] == 0\n",
    "assert new_state[\"high_score\"] == 40   # рекорд пережил рестарт\n",
    "assert new_generation == 4              # поколение увеличилось\n",
    "print(\"Restart сбрасывает игру, сохраняя рекорд и защищая от старых тиков через generation.\")"
   ]
  }
 ],
 "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
}
