{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4ba249ca",
   "metadata": {},
   "source": [
    "# 17-27 · New Round vs New Match\n",
    "\n",
    "Praktyka do sekcji [„New Round vs New Match”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "412ac2bf",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Rozróżnij reset rundy (zapis punktu) od resetu meczu (wynik jest resetowany do zera) i zapisz wynik do JSON."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cf9a7d5",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fb786d51",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T10:13:22.906157Z",
     "iopub.status.busy": "2026-09-03T10:13:22.906005Z",
     "iopub.status.idle": "2026-09-03T10:13:22.931067Z",
     "shell.execute_reply": "2026-09-03T10:13:22.930424Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "После New Round — счёт сохранён: 3 1\n"
     ]
    }
   ],
   "source": [
    "def new_round(state):\n",
    "    state = dict(state)\n",
    "    state[\"board\"] = [\"\"] * 9\n",
    "    state[\"current_player\"] = \"X\"\n",
    "    state[\"game_over\"] = False\n",
    "    return state\n",
    "\n",
    "def new_match(state):\n",
    "    state = new_round(state)\n",
    "    state[\"score_x\"] = 0\n",
    "    state[\"score_o\"] = 0\n",
    "    state[\"draws\"] = 0\n",
    "    return state\n",
    "\n",
    "state = {\"board\": [\"X\"] * 9, \"current_player\": \"O\", \"game_over\": True, \"score_x\": 3, \"score_o\": 1, \"draws\": 0}\n",
    "after_round = new_round(state)\n",
    "print(\"После New Round — счёт сохранён:\", after_round[\"score_x\"], after_round[\"score_o\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee142899",
   "metadata": {},
   "source": [
    "## Podstawowa praktyka zadania ★ – New Round zapisuje konto, New Match resetuje"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "45c84eab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T10:13:22.932338Z",
     "iopub.status.busy": "2026-09-03T10:13:22.932180Z",
     "iopub.status.idle": "2026-09-03T10:13:22.935273Z",
     "shell.execute_reply": "2026-09-03T10:13:22.934760Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "New Round и New Match ведут себя по-разному, как и должны.\n"
     ]
    }
   ],
   "source": [
    "after_match = new_match(state)\n",
    "\n",
    "assert after_round[\"score_x\"] == 3 and after_round[\"score_o\"] == 1, \"New Round обязан сохранить счёт\"\n",
    "assert after_match[\"score_x\"] == 0 and after_match[\"score_o\"] == 0, \"New Match обязан обнулить счёт\"\n",
    "print(\"New Round и New Match ведут себя по-разному, как и должны.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a1572d3",
   "metadata": {},
   "source": [
    "## Opcjonalne rozszerzenie – zapisywanie konta do JSON (wirtualny system plików przeglądarki)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6c6f25e2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T10:13:22.936546Z",
     "iopub.status.busy": "2026-09-03T10:13:22.936384Z",
     "iopub.status.idle": "2026-09-03T10:13:22.940827Z",
     "shell.execute_reply": "2026-09-03T10:13:22.940329Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Счёт успешно сохранён и прочитан обратно: {'x': 3, 'o': 1, 'draws': 0}\n",
      "Временный файл удалён: True\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from pathlib import Path\n",
    "\n",
    "SCORES_PATH = Path(\"tic_tac_toe_scores_practice.json\")\n",
    "SCORES_PATH.write_text(json.dumps({\"x\": 3, \"o\": 1, \"draws\": 0}), encoding=\"utf-8\")\n",
    "\n",
    "loaded = json.loads(SCORES_PATH.read_text(encoding=\"utf-8\"))\n",
    "assert loaded == {\"x\": 3, \"o\": 1, \"draws\": 0}\n",
    "print(\"Счёт успешно сохранён и прочитан обратно:\", loaded)\n",
    "\n",
    "# Posprzątać po sobie: plik był potrzebny tylko do sprawdzenia.\n",
    "SCORES_PATH.unlink(missing_ok=True)\n",
    "print(\"Временный файл удалён:\", not SCORES_PATH.exists())"
   ]
  }
 ],
 "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
}
