{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 10 : Inheritance and Polymorphism: Blackjack" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 10.1 page no: 334" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class Enemy:\n", " def __init__(self):\n", " self.m_Damage = 10\n", "\n", " def Attack(self):\n", " print \"An enemy attacks and inflicts \" , self.m_Damage , \" damage points!\";\n", " \n", " def __del__(self):\n", " print \"In Enemy destructor, deleting m_pDamage.\\n\";\n", "\n", "class Boss(Enemy):\n", " def __init__(self):\n", " Enemy.__init__(self)\n", " self.m_DamageMultiplier = 3\n", "\n", " def SpecialAttack(self):\n", " print \"A boss attacks and inflicts \" , (self.m_DamageMultiplier * self.m_Damage),\n", " print \" damage points!\";\n", "\n", "print \"Creating an enemy.\\n\";\n", "enemy1 = Enemy()\n", "enemy1.Attack();\n", "print \"\\nCreating a boss.\\n\";\n", "boss1 = Boss()\n", "boss1.Attack();\n", "boss1.SpecialAttack();" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Creating an enemy.\n", "\n", "An enemy attacks and inflicts 10 damage points!\n", "\n", "Creating a boss.\n", "\n", "An enemy attacks and inflicts 10 damage points!\n", "A boss attacks and inflicts 30 damage points!\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 10.2 page no : 338" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "\n", "class Enemy:\n", " def __init__(self):\n", " self.m_Damage = 10\n", "\n", " def Attack(self):\n", " print \"Attack inflicts \" , self.m_Damage , \" damage points!\";\n", "\n", "class Boss(Enemy):\n", " def __init__(self):\n", " Enemy.__init__(self)\n", " self.m_DamageMultiplier = 3\n", "\n", " def SpecialAttack(self):\n", " print \"Special Attack inflicts \" , (self.m_DamageMultiplier * self.m_Damage),\n", " print \" damage points!\";\n", "\n", "print \"Creating an enemy.\\n\";\n", "enemy1 = Enemy()\n", "enemy1.Attack();\n", "print \"\\nCreating a boss.\\n\";\n", "boss1 = Boss()\n", "boss1.Attack();\n", "boss1.SpecialAttack();" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Creating an enemy.\n", "\n", "Attack inflicts 10 damage points!\n", "\n", "Creating a boss.\n", "\n", "Attack inflicts 10 damage points!\n", "Special Attack inflicts 30 damage points!\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 10.3 page no : 343" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class Enemy:\n", " def __init__(self,d=10):\n", " self.m_Damage = d\n", "\n", " def Attack(self):\n", " print \"Attack inflicts \" , self.m_Damage , \" damage points!\";\n", "\n", " def Taunt(self):\n", " print \"The enemy says he will fight you.\";\n", " __Attack = Attack \n", "\n", "\n", "class Boss(Enemy):\n", " def __init__(self,d=30):\n", " Enemy.__init__(self)\n", " self.m_DamageMultiplier = d\n", "\n", " def SpecialAttack(self):\n", " print \"Special Attack inflicts \" , (self.m_DamageMultiplier * self.m_Damage),\n", " print \" damage points!\";\n", "\n", " def Taunt(self): #override base class member function\n", " print \"The boss says he will end your pitiful existence.\"\n", "\n", " def Attack(self): #override base class member function\n", " #call base class member function\n", " Enemy.Attack(self)\n", " print \" And laughs heartily at you.\\n\";\n", "\n", "\n", "print \"Enemy object :\";\n", "enemy1 = Enemy()\n", "enemy1.Taunt()\n", "enemy1.Attack();\n", "print \"\\n boss object.\\n\";\n", "boss1 = Boss()\n", "boss1.Taunt()\n", "boss1.Attack();\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enemy object :\n", "The enemy says he will fight you.\n", "Attack inflicts 10 damage points!\n", "\n", " boss object.\n", "\n", "The boss says he will end your pitiful existence.\n", "Attack inflicts 10 damage points!\n", " And laughs heartily at you.\n", "\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 10.4 page no : 341,348" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "class Enemy:\n", " def __init__(self,d=10):\n", " self.m_Damage = d\n", "\n", " def Attack(self):\n", " print \"Attack inflicts \" , self.m_Damage , \" damage points!\";\n", "\n", " def __del__(self):\n", " print \"In Enemy destructor, deleting m_pDamage.\";\n", " \n", "class Boss(Enemy):\n", " def __init__(self,d=30):\n", " Enemy.__init__(self)\n", " self.m_DamageMultiplier = d\n", "\n", " def SpecialAttack(self):\n", " print \"Special Attack inflicts \" , (self.m_DamageMultiplier * self.m_Damage),\n", " print \" damage points!\";\n", "\n", " def Taunt(self): #override base class member function\n", " print \"The boss says he will end your pitiful existence.\"\n", "\n", " def Attack(self): #override base class member function\n", " #call base class member function\n", " Enemy.Attack(self)\n", " print \" And laughs heartily at you.\\n\";\n", "\n", " def __del__(self):\n", " Enemy.__del__(self)\n", " print \"In Boss destructor, deleting m_pMultiplier.\";\n", " self.m_pMultiplier = 0;\n", "\n", "print \"Calling Attack() on Boss object through pointer to Enemy:\"\n", "pBadGuy = Boss();\n", "pBadGuy.Attack();\n", "print \"\\nDeleting pointer to Enemy:\\n\";\n", "pBadGuy = 0;" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Calling Attack() on Boss object through pointer to Enemy:\n", "Attack inflicts 10 damage points!\n", " And laughs heartily at you.\n", "\n", "\n", "Deleting pointer to Enemy:\n", "\n", "In Enemy destructor, deleting m_pDamage.\n", "In Boss destructor, deleting m_pMultiplier.\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 10.5 page no : 353" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "class Creature :\n", " def __init__(self,h=100):\n", " self.m_Health = h\n", " \n", " def Greet(self): # pure virtual member function\n", " pass\n", "\n", " def DisplayHealth(self):\n", " print \"Health: \" , self.m_Health\n", "\n", "\n", "class Orc(Creature):\n", " def __init__(self,h=120):\n", " Creature.__init__(self,h)\n", "\n", " def Greet(self):\n", " print \"The orc grunts hello.\\n\";\n", "\n", "pCreature = Orc();\n", "pCreature.Greet();\n", "pCreature.DisplayHealth();" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The orc grunts hello.\n", "\n", "Health: 120\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 10.6, page no : 361-379" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ACE = 1\n", "TWO = 2\n", "THREE = 3\n", "FOUR = 4\n", "FIVE = 5\n", "SIX = 6\n", "SEVEN = 7\n", "EIGHT = 8\n", "NINE = 9\n", "TEN = 10\n", "JACK = 11\n", "QUEEN = 12\n", "KING = 13\n", "\n", "CLUBS = 0\n", "DIAMONDS = 1\n", "HEARTS = 2\n", "SPADES = 3\n", "\n", " \n", "class Card:\n", " def __init__(self,r = ACE,s = SPADES,ifu = True): #returns the value of a card, 1 - 11\n", " self.m_Rank = r\n", " self.m_Suit = s\n", " self.m_IsFaceUp =ifu\n", "\n", " def GetValue(self):\n", " #if a cards is face down, its value is 0\n", " value = 0;\n", " if (m_IsFaceUp):\n", " #value is number showing on card\n", " value = m_Rank;\n", " #value is 10 for face cards\n", " if (value > 10):\n", " value = 10;\n", " return value;\n", "\n", " def Flip(self):\n", " self.m_IsFaceUp = not (self.m_IsFaceUp);\n", "\n", "class Hand:\n", " def __init__(self):\n", " self.m_Cards = []\n", "\n", " def __del__(self):\n", " self.Clear() \n", "\n", " def Add(self,pCard):\n", " self.m_Cards.append(pCard);\n", "\n", " def Clear(self):\n", " #iterate through vector, freeing all memory on the heap\n", " self.m_Cards = []\n", "\n", " def GetTotal(self):\n", " #if no cards in hand, return 0\n", " if (self.m_Cards.empty()):\n", " return 0;\n", " #if a first card has value of 0, then card is face down; return 0\n", " if (self.m_Cards[0].GetValue() == 0):\n", " return 0;\n", " #add up card values, treat each ace as 1\n", " total = 0;\n", " for i in self.m_Cards:\n", " total += i.GetValue()\n", " #determine if hand contains an ace\n", " containsAce = False;\n", " for i in self.m_Cards:\n", " if i.GetValue() == Card.ACE:\n", " containsAce = True;\n", "\n", " #if hand contains ace and total is low enough, treat ace as 11\n", " if (containsAce and total <= 11):\n", " #add only 10 since we've already added 1 for the ace\n", " total += 10;\n", " return total;\n", "\n", "\n", "class GenericPlayer(Hand):\n", " def __init__(self,name):\n", " self.m_Name = name\n", "\n", " def __del__(self):\n", " pass\n", "\n", " def IsBusted(self):\n", " return (self.GetTotal() > 21);\n", "\n", " def Bust(self):\n", " print self.m_Name , \" busts.\";\n", "\n", "class Player(GenericPlayer):\n", " def _init_(self,name):\n", " GenericPlayer.__init__(self,name)\n", " \n", " def _del_(self):\n", " pass\n", "\n", " def IsHitting(self):\n", " print self.m_Name , \", do you want a hit? (Y/N): \",\n", " response = raw_input()\n", " return (response == 'y' or response == 'Y');\n", "\n", " def Win(self):\n", " print self.m_Name , \" wins.\";\n", "\n", " def Lose(self):\n", " print self.m_Name , \" loses.\";\n", "\n", " def Push(self):\n", " print self.m_Name , \" pushes.\";\n", "\n", "class House(GenericPlayer):\n", " def __init__(self,name=\"House\"):\n", " GenericPlayer.__init__(self,name)\n", "\n", " def __del__(self):\n", " pass\n", "\n", " def IsHitting(self):\n", " return (self.GetTotal() <= 16)\n", "\n", " def FlipFirstCard(self):\n", " if (not self.m_Cards):\n", " self.m_Cards[0].Flip();\n", " else:\n", " print \"No card to flip!\";\n", " \n", "class Deck(Hand):\n", " def __init__(self):\n", " self.m_Cards = []\n", " self.Populate();\n", " \n", " def __del__(self):\n", " pass\n", " \n", " def Populate(self):\n", " self.Clear();\n", " #create standard deck\n", " for s in range(CLUBS,SPADES+1):\n", " for r in range(ACE,KING+1):\n", " self.Add(Card(r,s))\n", "\n", " def Shuffle(self):\n", " pass\n", "\n", " def Deal(self, aHand):\n", " if (not self.m_Cards):\n", " aHand.Add(self.m_Cards[-1]);\n", " self.m_Cards.pop();\n", " else:\n", " print \"Out of cards. Unable to deal.\";\n", "\n", " def AdditionalCards(self,aGenericPlayer):\n", " print ''\n", " #continue to deal a card as long as generic player isn't busted and\n", " #wants another hit\n", " while ( not (aGenericPlayer.IsBusted()) and aGenericPlayer.IsHitting() ):\n", " self.Deal(aGenericPlayer);\n", " print aGenericPlayer\n", " if (aGenericPlayer.IsBusted()):\n", " aGenericPlayer.Bust();\n", " \n", "\n", "class Game:\n", " def __init__(self,names):\n", " #create a vector of players from a vector of names\n", " self.m_Players = []\n", " for i in names:\n", " self.m_Players.append(Player(i))\n", " self.m_Deck = Deck()\n", " self.m_House = House()\n", " self.m_Deck.Populate();\n", " self.m_Deck.Shuffle();\n", "\n", " def __del__(self):\n", " pass\n", " \n", " def Play(self):\n", " # deal initial 2 cards to everyone\n", " for i in range(2):\n", " for pPlayer in self.m_Players:\n", " self.m_Deck.Deal(pPlayer);\n", " self.m_Deck.Deal(self.m_House);\n", " #hide house's first card\n", " self.m_House.FlipFirstCard();\n", " for pPlayer in self.m_Players:\n", " print pPlayer \n", "\n", " print self.m_House\n", " #deal additional cards to players\n", " \n", " for pPlayer in self.m_Players:\n", " self.m_Deck.AdditionalCards(pPlayer);\n", "\n", " #reveal house's first card\n", " self.m_House.FlipFirstCard();\n", " print self.m_House;\n", " #deal additional cards to house\n", " self.m_Deck.AdditionalCards(m_House);\n", " if (self.m_House.IsBusted()):\n", " #everyone still playing wins\n", " for pPlayer in self.m_Players:\n", " if ( not (pPlayer.IsBusted()) ):\n", " pPlayer.Win();\n", " else:\n", " #compare each player still playing to house\n", " for pPlayer in self.m_Players:\n", " if ( not (pPlayer.IsBusted()) ):\n", " if (pPlayer.GetTotal() > self.m_House.GetTotal()):\n", " pPlayer.Win();\n", " elif (pPlayer.GetTotal() < self.m_House.GetTotal()):\n", " pPlayer.Lose();\n", " else:\n", " pPlayer.Push();\n", "\n", " #remove everyones cards\n", " for pPlayer in self.m_Players:\n", " pPlayer.Clear();\n", "\n", " self.m_House.Clear();\n", "\n", "print \"\\t\\tWelcome to Blackjack!\\n\";\n", "numPlayers = 0;\n", "while (numPlayers < 1 or numPlayers > 7):\n", " print \"How many players? (1 - 7): \";\n", " numPlayers = int(raw_input())\n", "\n", "names = []\n", "name = ''\n", "for i in range(numPlayers):\n", " print \"Enter player name: \";\n", " name = raw_input()\n", " names.append(name); \n", "\n", "print ''\n", "#the game loop\n", "aGame = Game(names);\n", "again = 'y'\n", "while (again != 'n' and again != 'N'):\n", " aGame.Play();\n", " print \"\\nDo you want to play again? (Y/N): \",\n", " again = raw_input()\n", "\n", "#overloads << operator so Card object can be sent to cout\n", "def print_(aCard):\n", " RANKS = [\"0\", \"A\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\",\"10\", \"J\", \"Q\", \"K\"]\n", " SUITS = [\"c\", \"d\", \"h\", \"s\"]\n", " if (aCard.m_IsFaceUp):\n", " print RANKS[aCard.m_Rank] , SUITS[aCard.m_Suit];\n", " else:\n", " print \"XX\";\n", " \n", "\n", "def print__(aGenericPlayer):\n", " print aGenericPlayer.m_Name\n", " pCard = []\n", " if (not aGenericPlayer.m_Cards):\n", " for pCard in aGenericPlayer.m_Cards:\n", " print pCard\n", " \n", " if (aGenericPlayer.GetTotal() != 0):\n", " print \"(\" , aGenericPlayer.GetTotal() , \")\";\n", " else:\n", " print \"\";\n", " \n", "# Note : this example is just for concept purpose. i.e. Inheritance example. it has errors so do not run it \n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\t\tWelcome to Blackjack!\n", "\n", "How many players? (1 - 7): \n" ] } ], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }