{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Card Shuffling Game" ] }, { "cell_type": "code", "execution_count": 771, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from random import shuffle" ] }, { "cell_type": "code", "execution_count": 772, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# define the cards\n", "suits = [\"Hearts\", \"Diamonds\", \"Clubs\", \"Spades\"]\n", "values = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']" ] }, { "cell_type": "code", "execution_count": 773, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Card:\n", " def __init__(self, suit, value):\n", " self.suit = suit\n", " self.value = value\n", " \n", " # define each card \n", " def __repr__(self):\n", " return f\"{self.value} of {self.suit}\"" ] }, { "cell_type": "code", "execution_count": 774, "metadata": {}, "outputs": [], "source": [ "class Deck:\n", " # all combinations of cards\n", " def __init__(self):\n", " self.cards = [Card(suit, value) for suit in suits for value in values]\n", " \n", " # count number of cards in a deck\n", " def count(self):\n", " return len(self.cards)\n", " \n", " # showing the count\n", " def __repr__(self):\n", " return f\"Deck of {self.count()} cards\"\n", " \n", " # the shuffle instance\n", " def shuffle(self):\n", " cards = self.cards\n", " if self.count() < len(self.cards):\n", " raise ValueError(\"Missing cards! Can't be shuffled\")\n", " else:\n", " shuffle(cards)\n", " return cards\n", " \n", " # deal x number cards each time\n", " def _deal(self, number):\n", " count = self.count()\n", " if number > count:\n", " raise ValueError(\"No more cards to deal!\")\n", " else:\n", " cards = self.cards[:number]\n", " self.cards = self.cards[number:]\n", " return cards\n", " \n", " # deal a single card each time\n", " def deal_card(self):\n", " cards = self._deal(1)\n", " return cards\n", " \n", " # deal a list of cards each time\n", " def deal_hand(self, number):\n", " cards = self._deal(number)\n", " return cards" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Checking the results" ] }, { "cell_type": "code", "execution_count": 775, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4 of Hearts" ] }, "execution_count": 775, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# define each card\n", "card1 = Card(\"Hearts\", \"4\")\n", "card1" ] }, { "cell_type": "code", "execution_count": 776, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Deck of 52 cards" ] }, "execution_count": 776, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# number of cards in a deck\n", "deck = Deck()\n", "deck" ] }, { "cell_type": "code", "execution_count": 777, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2 of Diamonds,\n", " 8 of Hearts,\n", " 10 of Hearts,\n", " K of Hearts,\n", " 3 of Clubs,\n", " J of Hearts,\n", " 5 of Spades,\n", " Q of Hearts,\n", " 8 of Diamonds,\n", " J of Diamonds,\n", " 7 of Diamonds,\n", " 4 of Hearts,\n", " Q of Spades,\n", " Q of Clubs,\n", " 6 of Spades,\n", " 4 of Spades,\n", " A of Hearts,\n", " 9 of Diamonds,\n", " J of Clubs,\n", " 4 of Diamonds,\n", " 7 of Clubs,\n", " 10 of Clubs,\n", " J of Spades,\n", " 5 of Diamonds,\n", " 3 of Diamonds,\n", " 6 of Clubs,\n", " 8 of Clubs,\n", " 7 of Hearts,\n", " A of Diamonds,\n", " 7 of Spades,\n", " 5 of Hearts,\n", " 2 of Hearts,\n", " 6 of Hearts,\n", " K of Diamonds,\n", " 2 of Clubs,\n", " 10 of Diamonds,\n", " K of Spades,\n", " 8 of Spades,\n", " 5 of Clubs,\n", " 3 of Hearts,\n", " 6 of Diamonds,\n", " 4 of Clubs,\n", " Q of Diamonds,\n", " K of Clubs,\n", " 9 of Clubs,\n", " A of Spades,\n", " A of Clubs,\n", " 9 of Spades,\n", " 3 of Spades,\n", " 9 of Hearts,\n", " 2 of Spades,\n", " 10 of Spades]" ] }, "execution_count": 777, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# shuffle the deck\n", "deck.shuffle()" ] }, { "cell_type": "code", "execution_count": 778, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2 of Diamonds]" ] }, "execution_count": 778, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# deal a single card\n", "deck.deal_card()" ] }, { "cell_type": "code", "execution_count": 779, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[8 of Hearts, 10 of Hearts, K of Hearts]" ] }, "execution_count": 779, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# deal several cards\n", "deck.deal_hand(3)" ] }, { "cell_type": "code", "execution_count": 780, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3 of Clubs,\n", " J of Hearts,\n", " 5 of Spades,\n", " Q of Hearts,\n", " 8 of Diamonds,\n", " J of Diamonds,\n", " 7 of Diamonds,\n", " 4 of Hearts,\n", " Q of Spades,\n", " Q of Clubs]" ] }, "execution_count": 780, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# deal more cards\n", "deck._deal(10)" ] }, { "cell_type": "code", "execution_count": 781, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Deck of 38 cards" ] }, "execution_count": 781, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# only 38 cards left\n", "deck" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }