{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8c4ba0db",
   "metadata": {},
   "source": [
    "# 17-15 · Algorytm poprawnego ruchu\n",
    "\n",
    "Praktyka do sekcji [„Poprawny algorytm ruchu”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2c66305",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Sprawdź, czy nieprawidłowy ruch nie zmienia ani pola, ani aktualnego zawodnika."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcf55ab5",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "88a75ac4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T10:12:54.344982Z",
     "iopub.status.busy": "2026-09-03T10:12:54.344699Z",
     "iopub.status.idle": "2026-09-03T10:12:54.370641Z",
     "shell.execute_reply": "2026-09-03T10:12:54.370014Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['X', '', '', '', '', '', '', '', ''] O True\n"
     ]
    }
   ],
   "source": [
    "def attempt_move_pure(board, index, current_player, game_over):\n",
    "    \"\"\"Возвращает (new_board, new_player, moved: bool) — не трогает Tkinter вообще.\"\"\"\n",
    "    if game_over or board[index]:\n",
    "        return board, current_player, False\n",
    "    new_board = list(board)\n",
    "    new_board[index] = current_player\n",
    "    next_player = \"O\" if current_player == \"X\" else \"X\"\n",
    "    return new_board, next_player, True\n",
    "\n",
    "board = [\"\"] * 9\n",
    "board, player, moved = attempt_move_pure(board, 0, \"X\", False)\n",
    "print(board, player, moved)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b49ae7d2",
   "metadata": {},
   "source": [
    "## Zadanie Wyzwanie samopowtarzające – Zajęta komórka nie zmienia gracza ★★"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "062d860b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-03T10:12:54.371982Z",
     "iopub.status.busy": "2026-09-03T10:12:54.371834Z",
     "iopub.status.idle": "2026-09-03T10:12:54.374842Z",
     "shell.execute_reply": "2026-09-03T10:12:54.374227Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Невалидный ход корректно отклонён.\n"
     ]
    }
   ],
   "source": [
    "board_occupied = [\"X\"] + [\"\"] * 8\n",
    "board2, player2, moved2 = attempt_move_pure(board_occupied, 0, \"O\", False)\n",
    "\n",
    "assert moved2 is False\n",
    "assert player2 == \"O\", \"невалидный ход не должен переключать игрока\"\n",
    "assert board2 == board_occupied, \"невалидный ход не должен менять поле\"\n",
    "print(\"Невалидный ход корректно отклонён.\")"
   ]
  }
 ],
 "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
}
