{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| default_exp card" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Card\n", "\n", "> Define a card in a deck of cards" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastcore.all import *\n", "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "suits = [\"♣️\", \"♦️\", \"❤️\", \"♠️\"]\n", "ranks = [None, \"A\"] + [str(x) for x in range(2,11)] + [\"J\", \"Q\", \"K\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "class Card:\n", " \"Represents a standard playing card.\"\n", " def __init__(self, suit=0, rank=2):\n", " self.suit,self.rank = suit, rank\n", " self.suit_nm,self.rank_nm = suits[self.suit],ranks[self.rank]\n", "\n", " def __eq__(self, other): return (self.suit, self.rank) == (other.suit, other.rank)\n", " def __lt__(self, other): return (self.suit, self.rank) < (other.suit, other.rank)\n", " def __str__(self): return f'{self.rank_nm}{self.suit_nm}'\n", " __repr__ = __str__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating Cards" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Card` is a class that represents a single card in a deck of cards. We can create cards like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3♦️" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = Card(suit=1, rank=3)\n", "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In these docs we'll generally show the expected output from our code like so:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_eq(str(c), '3♦️')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c2 = Card(suit=2, rank=11)\n", "test_eq(str(c2), 'J❤️')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ":::{.callout-tip}\n", "\n", "**These `test_eq` statements are not just documentation, they are also unit tests!** These cells will get tested automatically with continous integration. You can also run tests locally with the command `nbdev_test`. If you do not want to show a test in your docs, you can choose to hide cells with the `#|hide` directive.\n", "\n", ":::" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comparing Cards" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also compare cards like so:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_eq(c2 > c, True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|hide\n", "assert Card(suit=1, rank=3) == Card(suit=1, rank=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|hide\n", "from nbdev.doclinks import nbdev_export\n", "nbdev_export()" ] }, { "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 }