{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| default_exp card" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Card\n", "> A playing card contains a rank and a suit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| hide\n", "from nbdev.showdoc import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastcore.test import *\n", "from fastcore.utils import *\n", "from fastai_learning import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will be using numbers to represent playing cards and ranks. These are the suits:" ] }, { "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": [ { "data": { "text/plain": "['♦️', '♣️', '♥️', '♣️']" }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "suits" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For instance, the suit at index `0`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": "'♦️'" }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "suits[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These are the ranks at index `1` (note that there isn't a playing card at position `0`, since we want the ranks to match the indices where possible):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": "'A'" }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ranks[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "class Card:\n", " def __init__(\n", " self,\n", " suit: int, # An index to the `suits`\n", " rank: int # An index to the `ranks`\n", " ):\n", " self.suit = suit\n", " self.rank = rank\n", "\n", " def __str__(self):\n", " return f\"{ranks[self.rank]}{suits[self.suit]}\"\n", "\n", " __repr__ = __str__\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c = Card(suit=1, rank=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": "3♣️" }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3♣️\n" ] } ], "source": [ "print(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": "---\n\n### Card\n\n> Card (suit:int, rank:int)\n\nInitialize self. See help(type(self)) for accurate signature.\n\n| | **Type** | **Details** |\n| -- | -------- | ----------- |\n| suit | int | An index to the `suits` |\n| rank | int | An index to the `ranks` |", "text/plain": "---\n\n### Card\n\n> Card (suit:int, rank:int)\n\nInitialize self. See help(type(self)) for accurate signature.\n\n| | **Type** | **Details** |\n| -- | -------- | ----------- |\n| suit | int | An index to the `suits` |\n| rank | int | An index to the `ranks` |" }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "show_doc(Card)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "@patch\n", "def __eq__(self: Card, other: Card):\n", " return (self.suit, self.rank) == (other.suit, other.rank)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "@patch\n", "def __lt__(self: Card, other: Card):\n", " return (self.suit, self.rank) < (other.suit, other.rank)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| export\n", "@patch\n", "def __gt__(self: Card, other: Card):\n", " return (self.suit, self.rank) > (other.suit, other.rank)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#| hide\n", "test_eq(Card(suit=1, rank=3), Card(suit=1, rank=3))\n", "test_ne(Card(suit=1, rank=2), Card(suit=1, rank=3))\n", "assert Card(suit=3, rank=2) > (Card(suit=1, rank=3))\n", "assert not Card(suit=1, rank=3) > (Card(suit=2, rank=3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "test_eq(Card(suit=1, rank=3), Card(suit=1, rank=3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Card(suit=1, rank=3) == Card(suit=1, rank=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": "3♣️" }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c == c" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c == Card(suit=1, rank=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "python3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }