{ "cells": [ { "cell_type": "markdown", "id": "468e25aa-24a4-4eaf-b4e0-c47792f38b1c", "metadata": { "tags": [] }, "source": [ "##### Python for High School (Summer 2022)\n", "\n", "* [Table of Contents](PY4HS.ipynb)\n", "* \"Open\n", "* [![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.org/github/4dsolutions/elite_school/blob/master/Py4HS_July_26_2022.ipynb)" ] }, { "cell_type": "markdown", "id": "394385ec-4610-483b-b928-bf5300b5964c", "metadata": {}, "source": [ "### Data Structures for Games\n", "\n", "![](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Chess_board_blank.svg/242px-Chess_board_blank.svg.png)\n", "\n", "\n", "We have looked at one possible data structure for a game so far. The adjacency matrix. Any structure that defines a graph is a good canditate for a game with some connected geography, such as a floor plan.\n", "\n", "How about a deck of cards or a chess board? What might be a data structure for those?" ] }, { "cell_type": "code", "execution_count": 5, "id": "832bbb03-d3b3-4e56-90cf-4cf2ec5b7a1e", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 6, "id": "8720c73c-b20f-425a-8367-26e92812a39e", "metadata": {}, "outputs": [], "source": [ "import unicodedata as ud" ] }, { "cell_type": "markdown", "id": "2606caed-45df-4f2a-bbdb-7681d747d620", "metadata": {}, "source": [ "### A Chess Board" ] }, { "cell_type": "code", "execution_count": 7, "id": "c238c32f-3533-452a-b9e9-3dd699c91a83", "metadata": {}, "outputs": [], "source": [ "def initialize():\n", " \"\"\"\n", " Return a chess board in the form of a 3D numpy array\n", " \"\"\"\n", " board = np.full((2,8,8),' ', dtype=str) # start with all spaces, shape of 8 by 8\n", " board[0, 0::2, 0::2] ='W' # every other row and column starting at (0,0), layer 0\n", " board[0, 1::2, 1::2] ='W' # every other row and column starting at (1,1), layer 0\n", " board[0, board[0]!='W'] = 'B' # black everywhere on layer 0 that isn't white\n", " # https://qwerty.dev/chess-symbols-to-copy-and-paste/\n", " white_pieces = '♖♘♗♕♔♗♘♖'\n", " white_pawns = 8 * '♙'\n", " black_pieces = '♜♞♝♛♚♝♞♜'\n", " black_pawns = 8 * '♟' \n", " board[1,7,0:] = list(white_pieces)\n", " board[1,6,0:] = list(white_pawns)\n", " board[1,0,0:] = list(black_pieces)\n", " board[1,1,0:] = list(black_pawns)\n", " return board" ] }, { "cell_type": "code", "execution_count": 2, "id": "ea7d3aa5-2f5d-4fd7-8447-5fc0fb77763f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9816" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wk = '♘'\n", "ord(wk)" ] }, { "cell_type": "code", "execution_count": 14, "id": "2afad2df-2523-499e-8512-fe68ed2873b0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],\n", " ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],\n", " ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],\n", " ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],\n", " ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],\n", " ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],\n", " ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],\n", " ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W']],\n", "\n", " [['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'],\n", " ['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'],\n", " [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\n", " [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\n", " [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\n", " [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\n", " ['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'],\n", " ['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']]], dtype='\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abcdefgh
0
1
2
3
4
5
6
7
\n", "" ], "text/plain": [ " a b c d e f g h\n", "0 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜\n", "1 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟\n", "2 \n", "3 \n", "4 \n", "5 \n", "6 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙\n", "7 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pdchess = pd.DataFrame(my_chess_board[1], columns=list(\"abcdefgh\"))\n", "pdchess" ] }, { "cell_type": "code", "execution_count": null, "id": "94d33dc4-9e4d-4312-8449-e315eae44b85", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'♞'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pdchess.loc[0,'b']" ] }, { "cell_type": "code", "execution_count": 15, "id": "bac02a55-e304-4f40-a8e9-de8acd44a695", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a ♜\n", "b ♞\n", "c ♝\n", "d ♛\n", "e ♚\n", "f ♝\n", "g ♞\n", "h ♜\n", "Name: 0, dtype: object" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pdchess.loc[0,'a':'h']" ] }, { "cell_type": "markdown", "id": "42b301ca-0bc0-475d-925e-195038e88a16", "metadata": {}, "source": [ "### A Deck of Playing Cards" ] }, { "cell_type": "code", "execution_count": 27, "id": "2b68a5cb-77ee-4d23-a8bc-bc45d549d55e", "metadata": {}, "outputs": [], "source": [ "spades = [chr(x) for x in range(0x1F0A1, 0x1F0A1+13)]\n", "hearts = [chr(x) for x in range(0x1F0B1, 0x1F0B1+13)]\n", "diamonds = [chr(x) for x in range(0x1F0C1, 0x1F0C1+13)]\n", "clubs = [chr(x) for x in range(0x1F0D1, 0x1F0D1+13)]" ] }, { "cell_type": "code", "execution_count": 28, "id": "d7268f78-6851-42f4-917e-5f66b432412d", "metadata": {}, "outputs": [], "source": [ "from random import shuffle\n", "\n", "class Deck:\n", " \"\"\"\n", " Deck of Cards: planning our methods\n", " \"\"\"\n", " \n", " def __init__(self):\n", " self._cards = spades + hearts + diamonds + clubs\n", " \n", " def shuffle(self):\n", " shuffle(self._cards)\n", " \n", " def deal(self, n):\n", " pass\n", " \n", " def add(self, cards):\n", " pass\n", " \n", " def show(self):\n", " return self._cards\n", " \n", " def __repr__(self):\n", " how_many = len(self._cards)\n", " return f\"Deck of {how_many} cards\"" ] }, { "cell_type": "code", "execution_count": 29, "id": "3cc6c04c-98f8-42bb-b49a-942710cabbf6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Deck of 52 cards" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "deck1 = Deck()\n", "deck1" ] }, { "cell_type": "code", "execution_count": 36, "id": "7100f4aa-6f31-43b4-833e-3e0a41422f3f", "metadata": {}, "outputs": [], "source": [ "Deck.shuffle(deck1)" ] }, { "cell_type": "code", "execution_count": 37, "id": "cbe8c9a4-5fe9-479e-9bce-8155879a0d5e", "metadata": {}, "outputs": [], "source": [ "Deck.shuffle(deck1)" ] }, { "cell_type": "code", "execution_count": 33, "id": "b6780ac4-a8cc-4679-b9c2-2ba142c68f75", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['🃆', '🂣', '🂬', '🂻', '🃜', '🂺', '🃅', '🃙', '🂲', '🃃', '🃈', '🂸', '🃛', '🂫', '🂦', '🂥', '🃊', '🂭', '🃑', '🂼', '🂳', '🃒', '🂩', '🃘', '🂡', '🃌', '🃁', '🂢', '🂹', '🂽', '🂵', '🃂', '🃄', '🃝', '🂧', '🂷', '🂤', '🃉', '🃚', '🂴', '🃕', '🂱', '🃗', '🃓', '🂨', '🃇', '🂶', '🃔', '🃖', '🃋', '🃍', '🂪']\n" ] } ], "source": [ "print(deck1.show())" ] }, { "cell_type": "code", "execution_count": 40, "id": "c0646740-1811-481d-931a-b10aef6656e2", "metadata": {}, "outputs": [], "source": [ "spades = [chr(x) for x in range(0x1F0A1, 0x1F0A1+13)]\n", "hearts = [chr(x) for x in range(0x1F0B1, 0x1F0B1+13)]\n", "diamonds = [chr(x) for x in range(0x1F0C1, 0x1F0C1+13)]\n", "clubs = [chr(x) for x in range(0x1F0D1, 0x1F0D1+13)]" ] }, { "cell_type": "code", "execution_count": 34, "id": "97f62abb-cb93-43f8-afd2-f831b1549e24", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Deck of 52 cards" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "deck1" ] }, { "cell_type": "code", "execution_count": 35, "id": "c11e9d18-5850-4f8d-bbde-19d8a72bff48", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'_cards': ['🃆',\n", " '🂣',\n", " '🂬',\n", " '🂻',\n", " '🃜',\n", " '🂺',\n", " '🃅',\n", " '🃙',\n", " '🂲',\n", " '🃃',\n", " '🃈',\n", " '🂸',\n", " '🃛',\n", " '🂫',\n", " '🂦',\n", " '🂥',\n", " '🃊',\n", " '🂭',\n", " '🃑',\n", " '🂼',\n", " '🂳',\n", " '🃒',\n", " '🂩',\n", " '🃘',\n", " '🂡',\n", " '🃌',\n", " '🃁',\n", " '🂢',\n", " '🂹',\n", " '🂽',\n", " '🂵',\n", " '🃂',\n", " '🃄',\n", " '🃝',\n", " '🂧',\n", " '🂷',\n", " '🂤',\n", " '🃉',\n", " '🃚',\n", " '🂴',\n", " '🃕',\n", " '🂱',\n", " '🃗',\n", " '🃓',\n", " '🂨',\n", " '🃇',\n", " '🂶',\n", " '🃔',\n", " '🃖',\n", " '🃋',\n", " '🃍',\n", " '🂪']}" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "deck1.__dict__" ] }, { "cell_type": "code", "execution_count": null, "id": "56c79670-9bb0-49ec-a9b0-2c1f10aa03e4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7ab6f02c-0f00-4595-9006-d3ef94426ac1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "f1de83d1-dc5f-4fff-ae96-1585905d614c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "e36d56e9-5aab-4125-9fe0-b07f2b1b31ba", "metadata": {}, "source": [ "![](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Chess_board_blank.svg/242px-Chess_board_blank.svg.png)" ] }, { "cell_type": "code", "execution_count": 18, "id": "7b762c12-f3f7-4c9f-968c-c90db285c4c7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],\n", " ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],\n", " ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],\n", " ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],\n", " ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],\n", " ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],\n", " ['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],\n", " ['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W']],\n", "\n", " [['♜', '♞', '♝', '♛', '♚', '♝', '♞', '♜'],\n", " ['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟'],\n", " [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\n", " [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\n", " [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\n", " [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\n", " ['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙'],\n", " ['♖', '♘', '♗', '♕', '♔', '♗', '♘', '♖']]], dtype='