{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| default_exp deck" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# deck\n", "\n", "> A deck of playing cards" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "from nbdev_cards.card import *\n", "from fastcore.utils import *\n", "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| hide\n", "from nbdev.showdoc import *\n", "from fastcore.test import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "class Deck:\n", " \"A deck of 52 cards, not including jokers\"\n", " def __init__(self): self.cards = [Card(s, r) for s in range(4) for r in range(1, 14)]\n", " def __len__(self): return len(self.cards)\n", " def __str__(self): return '; '.join(map(str, self.cards))\n", " def __contains__(self, card): return card in self.cards\n", " __repr__=__str__\n", "\n", " def shuffle(self):\n", " \"Shuffles the cards in this deck\"\n", " random.shuffle(self.cards)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we initially create a deck, all of the cards will be present:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "A♣️; 2♣️; 3♣️; 4♣️; 5♣️; 6♣️; 7♣️; 8♣️; 9♣️; 10♣️; J♣️; Q♣️; K♣️; A♦️; 2♦️; 3♦️; 4♦️; 5♦️; 6♦️; 7♦️; 8♦️; 9♦️; 10♦️; J♦️; Q♦️; K♦️; A❤️; 2❤️; 3❤️; 4❤️; 5❤️; 6❤️; 7❤️; 8❤️; 9❤️; 10❤️; J❤️; Q❤️; K❤️; A♠️; 2♠️; 3♠️; 4♠️; 5♠️; 6♠️; 7♠️; 8♠️; 9♠️; 10♠️; J♠️; Q♠️; K♠️" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "deck = Deck()\n", "deck" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That should be 52 cards." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_eq(len(deck), 52)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As a reminder, these are the suits we defined for a `Card`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['♣️', '♦️', '❤️', '♠️']" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "suits" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can check if, say, the Ace of Clubs is in the deck:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Card(1,1) in deck" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "@patch\n", "def pop(self:Deck,\n", " idx:int=-1): # The index of the card to remove, defaulting to the last one\n", " \"Remove one card from the deck\"\n", " return self.cards.pop(idx)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "deck = Deck()\n", "test_eq(deck.pop(), Card(3,13)) # K♠️" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are 51 cards left in the deck now." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_eq(len(deck), 51)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "@patch\n", "def remove(self:Deck,\n", " card:Card): # Card to remove\n", " \"Removes `card` from the deck or raises exception if it is not there\"\n", " self.cards.remove(card)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "card23 = Card(2, 3)\n", "deck.remove(card23)\n", "\n", "assert card23 not in deck" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "---\n", "\n", "#### Deck.shuffle\n", "\n", "> Deck.shuffle ()\n", "\n", "Shuffles the cards in this deck" ], "text/plain": [ "" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "show_doc(Deck.shuffle)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "def draw_n(n:int, # number of cards to draw\n", " replace:bool=True): # whether or not draw with replacement\n", " \"Draw `n` cards, with replacement iif `replace`\"\n", " d = Deck()\n", " d.shuffle()\n", " if replace: return [d.cards[random.choice(range(len(d.cards)))] for _ in range(n)]\n", " else: return d.cards[:n]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Q♠️, 8❤️, 3♦️, 7♣️, 2♣️, 9♦️, 9♠️, 7♠️, 4♠️, K♣️, A♣️, 4♣️, 5♦️]" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "draw_n(13, replace=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }