diff options
Diffstat (limited to 'Beginning_C++_Through_Game_Programming')
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/README.txt | 10 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch1.ipynb | 400 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch10.ipynb | 705 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch2.ipynb | 518 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch3.ipynb | 419 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch4.ipynb | 614 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch5.ipynb | 412 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch6.ipynb | 616 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch7.ipynb | 285 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch8.ipynb | 566 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch9.ipynb | 447 | ||||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/screenshots/chapter1.png | bin | 0 -> 112264 bytes | |||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/screenshots/chapter4.png | bin | 0 -> 136496 bytes | |||
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/screenshots/chapter8.png | bin | 0 -> 119773 bytes |
14 files changed, 4992 insertions, 0 deletions
diff --git a/Beginning_C++_Through_Game_Programming/README.txt b/Beginning_C++_Through_Game_Programming/README.txt new file mode 100755 index 00000000..5d5ad4c9 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/README.txt @@ -0,0 +1,10 @@ +Contributed By: Jatin Pavagadhi +Course: mca +College/Institute/Organization: C-DEC , Pune +Department/Designation: Developer +Book Title: Beginning C++ Through Game Programming +Author: Michael Dawson +Publisher: Course Tech., USA +Year of publication: 2011 +Isbn: 9781435457423 +Edition: 3
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/ch1.ipynb b/Beginning_C++_Through_Game_Programming/ch1.ipynb new file mode 100755 index 00000000..29a3af7f --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch1.ipynb @@ -0,0 +1,400 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1 : Types, Variables, and Standard I/O: Lost Fortune" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Exmaple 1.1 Page no : 6" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"Game Over!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Game Over!\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Exmaple 1.2 Page no : 11" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"Game Over!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Game Over!\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Exmaple 1.3 Page no : 12" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"Game Over!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Game Over!\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Exmaple 1.4 Page no : 13" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"7 + 3 = \" , 7 + 3\n", + "print \"7 - 3 = \" , 7 - 3\n", + "print \"7 * 3 = \" , 7 * 3 \n", + "print \"7 / 3 = \" , 7 / 3 \n", + "print \"7.0 / 3.0 = \" , 7.0 / 3.0 \n", + "print \"7 % 3 = \" , 7 % 3 \n", + "print \"7 + 3 * 5 = \" , 7 + 3 * 5\n", + "print \"(7 + 3) * 5 = \" , (7 + 3) * 5" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "7 + 3 = 10\n", + "7 - 3 = 4\n", + "7 * 3 = 21\n", + "7 / 3 = 2\n", + "7.0 / 3.0 = 2.33333333333\n", + "7 % 3 = 1\n", + "7 + 3 * 5 = 22\n", + "(7 + 3) * 5 = 50\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 1.5 Page no : 17" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "score = 0;\n", + "distance = 1200.76;\n", + "playAgain = 'y'\n", + "shieldsUp = True;\n", + "lives = 3;\n", + "aliensKilled = 10;\n", + "engineTemp = 6572.89;\n", + "print \"\\nscore: \" , score \n", + "print \"distance: \" , distance\n", + "print \"playAgain: \" , playAgain\n", + "#skipping shieldsUp since you dont generally print Boolean values\n", + "print \"lives: \" , lives\n", + "print \"aliensKilled: \" , aliensKilled\n", + "print \"engineTemp: \" , engineTemp\n", + "\n", + "print \"\\nHow much fuel? \",\n", + "fuel = 5 # raw_input()\n", + "\n", + "print \"fuel: \" , fuel\n", + "bonus = 10;\n", + "print \"\\nbonus: \" , bonus" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "score: 0\n", + "distance: 1200.76\n", + "playAgain: y\n", + "lives: 3\n", + "aliensKilled: 10\n", + "engineTemp: 6572.89\n", + "\n", + "How much fuel? fuel: 5\n", + "\n", + "bonus: 10\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 1.6 Page no : 35" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "score = 5000;\n", + "print \"score: \" , score\n", + "#altering the value of a variable\n", + "score = score + 100;\n", + "print \"score: \" , score\n", + "#combined assignment operator\n", + "score += 100;\n", + "print \"score: \" , score\n", + "# increment operators\n", + "lives = 3;\n", + "lives += 1\n", + "print \"lives: \", lives\n", + "\n", + "lives = 3;\n", + "lives += 1\n", + "print \"lives: \", lives\n", + "\n", + "lives = 3;\n", + "lives += 1\n", + "bonus = lives * 10;\n", + "print \"lives, bonus = \" , lives , \", \" , bonus \n", + "\n", + "lives = 3;\n", + "bonus = lives * 10;\n", + "lives += 1\n", + "print \"lives, bonus = \" , lives , \", \" , bonus \n", + "#integer wrap around\n", + "score = 4294967295;\n", + "print \"\\nscore: \" , score\n", + "score += 1\n", + "print \"score: \", score " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "score: 5000\n", + "score: 5100\n", + "score: 5200\n", + "lives: 4\n", + "lives: 4\n", + "lives, bonus = 4 , 40\n", + "lives, bonus = 4 , 30\n", + "\n", + "score: 4294967295\n", + "score: 4294967296\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 1.7 Page no : 30" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "ALIEN_POINTS = 150;\n", + "aliensKilled = 10;\n", + "score = aliensKilled * ALIEN_POINTS;\n", + "print \"score: \" , score\n", + "NOVICE = 0\n", + "EASY = 1\n", + "NORMAL =2\n", + "HARD=3\n", + "UNBEATABLE=4\n", + "myDifficulty = EASY;\n", + "FIGHTER_COST = 25\n", + "BOMBER_COST=26\n", + "CRUISER_COST = 50\n", + "myShipCost = BOMBER_COST;\n", + "print \"\\nTo upgrade my ship to a Cruiser will cost\" , (CRUISER_COST - myShipCost) , \" Resource Points.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "score: 1500\n", + "\n", + "To upgrade my ship to a Cruiser will cost 24 Resource Points.\n", + "\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 1.8 Page no : 33" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "GOLD_PIECES = 900;\n", + "\n", + "print \"Welcome to Lost Fortune\\n\\n\";\n", + "print \"Please enter the following for your personalized adventure\\n\";\n", + "print \"Enter a number: \";\n", + "adventurers = 10 # int(raw_input())\n", + "print \"Enter a number, smaller than the first: \";\n", + "killed = 5 #int(raw_input())\n", + "\n", + "survivors = adventurers - killed;\n", + "print \"Enter your last name: \";\n", + "leader = 'xyz' #raw_input()\n", + "#tell the story\n", + "print \"\\nA brave group of \" , adventurers , \" set out on a quest \";\n", + "print \"- in search of the lost treasure of the Ancient Dwarves. \";\n", + "print \"The group was led by that legendary rogue, \" , leader , \".\\n\";\n", + "print \"\\nAlong the way, a band of marauding ogres ambushed the party. \";\n", + "print \"All fought bravely under the command of \" , leader,\n", + "print \", and the ogres were defeated, but at a cost. \",\n", + "print \"Of the adventurers, \" , killed , \" were vanquished, \",\n", + "print \"leaving just \" , survivors , \" in the group.\\n\"\n", + "\n", + "print \"\\nThe party was about to give up all hope. \";\n", + "print \"But while laying the deceased to rest, \";\n", + "print \"they stumbled upon the buried fortune. \";\n", + "print \"So the adventurers split \" , GOLD_PIECES , \" gold pieces.\" ,\n", + "print leader , \" held on to the extra \" , (GOLD_PIECES % survivors),\n", + "print \" pieces to keep things fair of course.\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Welcome to Lost Fortune\n", + "\n", + "\n", + "Please enter the following for your personalized adventure\n", + "\n", + "Enter a number: \n", + "Enter a number, smaller than the first: \n", + "Enter your last name: \n", + "\n", + "A brave group of 10 set out on a quest \n", + "- in search of the lost treasure of the Ancient Dwarves. \n", + "The group was led by that legendary rogue, xyz .\n", + "\n", + "\n", + "Along the way, a band of marauding ogres ambushed the party. \n", + "All fought bravely under the command of xyz , and the ogres were defeated, but at a cost. Of the adventurers, 5 were vanquished, leaving just 5 in the group.\n", + "\n", + "\n", + "The party was about to give up all hope. \n", + "But while laying the deceased to rest, \n", + "they stumbled upon the buried fortune. \n", + "So the adventurers split 900 gold pieces. xyz held on to the extra 0 pieces to keep things fair of course.\n", + "\n" + ] + } + ], + "prompt_number": 8 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/ch10.ipynb b/Beginning_C++_Through_Game_Programming/ch10.ipynb new file mode 100755 index 00000000..e3ec6373 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch10.ipynb @@ -0,0 +1,705 @@ +{ + "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 \"<empty>\";\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" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter player name: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "John\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter player name: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mark\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter player name: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Peter\n" + ] + }, + { + "ename": "AttributeError", + "evalue": "Game instance has no attribute 'm_Deck'", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-1-edba0f01e2d5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 236\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m''\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 237\u001b[0m \u001b[1;31m#the game loop\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 238\u001b[1;33m \u001b[0maGame\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mGame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnames\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 239\u001b[0m \u001b[0magain\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'y'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 240\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0magain\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;34m'n'\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0magain\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;34m'N'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m<ipython-input-1-edba0f01e2d5>\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, names)\u001b[0m\n\u001b[0;32m 171\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mnames\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 172\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpName\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mPlayer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 173\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mm_Deck\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPopulate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 174\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mm_Deck\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mShuffle\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 175\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mAttributeError\u001b[0m: Game instance has no attribute 'm_Deck'" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/ch2.ipynb b/Beginning_C++_Through_Game_Programming/ch2.ipynb new file mode 100755 index 00000000..9b8f2db2 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch2.ipynb @@ -0,0 +1,518 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 2 : Truth, Branching, and the Game Loop: Guess My Number" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 2.1 page no : 41" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "if (True):\n", + " print \"This is always displayed.\\n\\n\";\n", + "\n", + "if (False):\n", + " print \"This is never displayed.\\n\\n\";\n", + "score = 1000;\n", + "if (score):\n", + " print \"At least you didn't score zero.\\n\\n\";\n", + "\n", + "if (score >= 250):\n", + " print \"You scored 250 or more. Decent.\\n\\n\";\n", + "\n", + "if (score >= 500):\n", + " print \"You scored 500 or more. Nice.\\n\\n\";\n", + "if (score >= 1000):\n", + " print \"You scored 1000 or more. Impressive!\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is always displayed.\n", + "\n", + "\n", + "At least you didn't score zero.\n", + "\n", + "\n", + "You scored 250 or more. Decent.\n", + "\n", + "\n", + "You scored 500 or more. Nice.\n", + "\n", + "\n", + "You scored 1000 or more. Impressive!\n", + "\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 2.2 page no :46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"Enter your score: \";\n", + "score = 1500 #int(raw_input())\n", + "if (score >= 1000):\n", + " print \"You scored 1000 or more. Impressive!\\n\";\n", + "else:\n", + " print \"You scored less than 1000.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your score: \n", + "You scored 1000 or more. Impressive!\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 2.3 page no : 49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "\n", + "print \"Enter your score: \";\n", + "score = 1500 #int(raw_input())\n", + "\n", + "if (score >= 1000):\n", + " print \"You scored 1000 or more. Impressive!\\n\";\n", + "elif (score >= 500):\n", + " print \"You scored 500 or more. Nice.\\n\";\n", + "elif (score >= 250):\n", + " print \"You scored 250 or more. Decent.\\n\";\n", + "else:\n", + " print \"You scored less than 250. Nothing to brag about.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your score: \n", + "You scored 1000 or more. Impressive!\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.4 page no : 53" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"Difficulty Levels\\n\\n\";\n", + "print \"1 - Easy\\n\";\n", + "print \"2 - Normal\\n\";\n", + "print \"3 - Hard\\n\\n\";\n", + "\n", + "print \"Choice: \";\n", + "choice = 2 #int(raw_input())\n", + "if choice==1:\n", + " print \"You picked Easy.\\n\";\n", + "elif choice==2: \n", + " print \"You picked Normal.\\n\";\n", + "elif choice==3:\n", + " \"You picked Hard.\\n\";\n", + "else: \n", + " \"You made an illegal choice.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Difficulty Levels\n", + "\n", + "\n", + "1 - Easy\n", + "\n", + "2 - Normal\n", + "\n", + "3 - Hard\n", + "\n", + "\n", + "Choice: \n", + "You picked Normal.\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.5 page no : 55" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "choices = ['y','y','n']\n", + "again = 'y'\n", + "i = 0\n", + "while (again == 'y'):\n", + " print \"\\n**Played an exciting game**\";\n", + " print \"\\nDo you want to play again? (y/n): \",\n", + " again = choices[i] #raw_input()\n", + " i += 1\n", + "\n", + "print \"\\nOkay, bye.\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "Okay, bye.\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.6 page no : 57" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "choices = ['y','y','n']\n", + "i = 0\n", + "again = 'y'\n", + "while (again == 'y'):\n", + " print \"\\n**Played an exciting game**\";\n", + " print \"\\nDo you want to play again? (y/n): \",\n", + " again = choices[i] #raw_input()\n", + " i += 1\n", + "\n", + "print \"\\nOkay, bye.\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "Okay, bye.\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.7 page no :59" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "count = 0;\n", + "while (True):\n", + " count += 1;\n", + " #end loop if count is greater than 10\n", + " if (count > 10):\n", + " break;\n", + " #skip the number 5\n", + " if (count == 5):\n", + " continue;\n", + " print count" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.8 page no : 63" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"\\tGame Designer's Network\\n\";\n", + "success = False\n", + "while not success:\n", + " print \"\\nUsername: \";\n", + " username = 'guest' #raw_input()\n", + " print \"Password: \";\n", + " password = 'guest' #raw_input()\n", + " if (username == \"S.Meier\" and password == \"civilization\"):\n", + " print \"\\nHey, Sid.\";\n", + " success = True;\n", + " elif (username == \"S.Miyamoto\" and password == \"mariobros\"):\n", + " print \"\\nWhat's up, Shigeru?\";\n", + " success = True;\n", + " elif (username == \"W.Wright\" and password == \"thesims\"):\n", + " print \"\\nHow goes it, Will?\";\n", + " success = True;\n", + " elif (username == \"guest\" or password == \"guest\"):\n", + " print \"\\nWelcome, guest.\";\n", + " success = True;\n", + " else:\n", + " print \"\\nYour login failed.\";\n", + " success = False;" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tGame Designer's Network\n", + "\n", + "\n", + "Username: \n", + "Password: \n", + "\n", + "Welcome, guest.\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.9 page no : 68" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import random\n", + "\n", + "randomNumber = random.random()*100;\n", + "#seed random number generator\n", + "#generate random number\n", + "die = int(randomNumber % 6) + 1; # get a number between 1 and 6\n", + "print \"You rolled a \" , die" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You rolled a 4\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.10 page no : 74" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import random\n", + "secretNumber = int(random.random()*100) % 10 + 1;\n", + "tries = 0;\n", + "#seed random number generator\n", + "# random number between 1 and 100\n", + "print \"\\tWelcome to Guess My Number\\n\\n\";\n", + "guesses = [0,1,2,3,4,5,6,7,8,9,10]\n", + "i = 0\n", + "while True:\n", + " print \"Enter a guess: \";\n", + " guess = guesses[i] #int(raw_input())\n", + " i += 1\n", + " tries += 1\n", + " if (guess > secretNumber):\n", + " print \"Too high!\\n\\n\";\n", + " elif (guess < secretNumber):\n", + " print \"Too low!\\n\\n\";\n", + " else:\n", + " print \"\\nThat's it! You got it in \" , tries , \" guesses!\\n\";\n", + " break" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tWelcome to Guess My Number\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "\n", + "That's it! You got it in 7 guesses!\n", + "\n" + ] + } + ], + "prompt_number": 11 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/ch3.ipynb b/Beginning_C++_Through_Game_Programming/ch3.ipynb new file mode 100755 index 00000000..63e27d97 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch3.ipynb @@ -0,0 +1,419 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 3 : For Loops, Strings, and Arrays: Word Jumble" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 3.1 page no :83" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "print \"Counting forward:\\n\";\n", + "for i in range(10):\n", + " print i ,\n", + "print \"\\n\\nCounting backward:\\n\";\n", + "for i in range(1,10,-1):\n", + " print i,\n", + "\n", + "print \"\\n\\nCounting by fives:\\n\";\n", + "for i in range(0,51,5):\n", + " print i,\n", + " \n", + "print \"\\n\\nCounting with null statements:\\n\";\n", + "count = 0;\n", + "while ( count < 10 ):\n", + " print count ,\n", + " count += 1\n", + "\n", + "print \"\\n\\nCounting with nested for loops:\\n\";\n", + "ROWS = 5;\n", + "COLUMNS = 3;\n", + "for i in range(ROWS):\n", + " for j in range(COLUMNS):\n", + " print i , \",\" , j ," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Counting forward:\n", + "\n", + "0 1 2 3 4 5 6 7 8 9 \n", + "\n", + "Counting backward:\n", + "\n", + "\n", + "\n", + "Counting by fives:\n", + "\n", + "0 5 10 15 20 25 30 35 40 45 50 \n", + "\n", + "Counting with null statements:\n", + "\n", + "0 1 2 3 4 5 6 7 8 9 \n", + "\n", + "Counting with nested for loops:\n", + "\n", + "0 , 0 0 , 1 0 , 2 1 , 0 1 , 1 1 , 2 2 , 0 2 , 1 2 , 2 3 , 0 3 , 1 3 , 2 4 , 0 4 , 1 4 , 2\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 3.2 page no : 90" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "word1 = \"Game\";\n", + "word2 =\"Over\"\n", + "word3 = '!'\n", + "phrase = word1 + \" \" + word2 + word3;\n", + "print \"The phrase is: \" , phrase , \"\\n\";\n", + "print \"The phrase has \" , len(phrase) , \" characters in it.\\n\";\n", + "print \"The character at position 0 is: \" , phrase[0] , \"\\n\\n\";\n", + "print \"Changing the character at position 0.\\n\";\n", + "#phrase[0] = 'L';\n", + "print \"The phrase is now: \" , phrase , \"\\n\"\n", + "for i in range(len(phrase)):\n", + " print \"Character at position \" , i , \" is: \" , phrase[i] \n", + "\n", + "print \"\\nThe sequence 'Over' begins at location \";\n", + "print phrase.find(\"Over\") \n", + "if (\"eggplant\" not in phrase):\n", + " print \"'eggplant' is not in the phrase.\\n\\n\";\n", + "\n", + "# Python does not support string erase function.\n", + "#phrase.erase(4, 5); \n", + "print \"The phrase is now: \" , phrase \n", + "#phrase.erase(4);\n", + "print \"The phrase is now: \" , phrase \n", + "#phrase.erase();\n", + "print \"The phrase is now: \" , phrase \n", + "if (phrase==''):\n", + " print \"\\nThe phrase is no more.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The phrase is: Game Over! \n", + "\n", + "The phrase has 10 characters in it.\n", + "\n", + "The character at position 0 is: G \n", + "\n", + "\n", + "Changing the character at position 0.\n", + "\n", + "The phrase is now: Game Over! \n", + "\n", + "Character at position 0 is: G\n", + "Character at position 1 is: a\n", + "Character at position 2 is: m\n", + "Character at position 3 is: e\n", + "Character at position 4 is: \n", + "Character at position 5 is: O\n", + "Character at position 6 is: v\n", + "Character at position 7 is: e\n", + "Character at position 8 is: r\n", + "Character at position 9 is: !\n", + "\n", + "The sequence 'Over' begins at location \n", + "5\n", + "'eggplant' is not in the phrase.\n", + "\n", + "\n", + "The phrase is now: Game Over!\n", + "The phrase is now: Game Over!\n", + "The phrase is now: Game Over!\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 3.3 page no : 97" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "MAX_ITEMS = 10;\n", + "inventory = [];\n", + "\n", + "inventory.append(\"sword\")\n", + "inventory.append(\"armor\")\n", + "inventory.append(\"shield\")\n", + "print \"Your items:\\n\";\n", + "for i in range(len(inventory)):\n", + " print inventory[i]\n", + "\n", + "print \"\\nYou trade your sword for a battle axe.\";\n", + "inventory[0] = \"battle axe\";\n", + "print \"\\nYour items:\\n\";\n", + "for i in range(len(inventory)):\n", + " print inventory[i]\n", + "\n", + "print \"\\nThe item name '\" , inventory[0] , \"' has \",\n", + "print len(inventory[0]) , \" letters in it.\\n\";\n", + "print \"\\nYou find a healing potion.\";\n", + "if (len(inventory) < MAX_ITEMS):\n", + " inventory.append(\"healing potion\")\n", + "else:\n", + " print \"You have too many items and can't carry another.\";\n", + "print \"\\nYour items:\\n\";\n", + "for i in range(len(inventory)):\n", + " print inventory[i]\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your items:\n", + "\n", + "sword\n", + "armor\n", + "shield\n", + "\n", + "You trade your sword for a battle axe.\n", + "\n", + "Your items:\n", + "\n", + "battle axe\n", + "armor\n", + "shield\n", + "\n", + "The item name ' battle axe ' has 10 letters in it.\n", + "\n", + "\n", + "You find a healing potion.\n", + "\n", + "Your items:\n", + "\n", + "battle axe\n", + "armor\n", + "shield\n", + "healing potion\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 3.4 page no : 104" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "ROWS = 3;\n", + "COLUMNS = 3;\n", + "board = [['O', 'X', 'O'],[' ', 'X', 'X'],['X', 'O', 'O']]\n", + "print \"Here's the tic-tac-toe board:\\n\";\n", + "for i in range(ROWS):\n", + " for j in range(COLUMNS):\n", + " print board[i][j],\n", + " print ''\n", + "\n", + "print \"\\n'X' moves to the empty location.\\n\\n\";\n", + "board[1][0] = 'X';\n", + "print \"Now the tic-tac-toe board is:\\n\";\n", + "for i in range(ROWS):\n", + " for j in range(COLUMNS):\n", + " print board[i][j],\n", + " print ''\n", + "print \"\\n'X' wins!\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Here's the tic-tac-toe board:\n", + "\n", + "O X O \n", + " X X \n", + "X O O \n", + "\n", + "'X' moves to the empty location.\n", + "\n", + "\n", + "Now the tic-tac-toe board is:\n", + "\n", + "O X O \n", + "X X X \n", + "X O O \n", + "\n", + "'X' wins!\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 3.5 page no : 107" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "fields = ['WORD', 'HINT', 'NUM_FIELDS']\n", + "NUM_WORDS = 5;\n", + "WORDS = [ [\"wall\", \"Do you feel you're banging your head against something?\"],\n", + " [\"glasses\", \"These might help you see the answer.\"],\n", + " [\"labored\", \"Going slowly, is it?\"],\n", + " [\"persistent\", \"Keep at it.\"],\n", + " [\"jumble\", \"It's what the game is all about.\"]]\n", + "\n", + "class difficulty:\n", + " EASY = 0\n", + " MEDIUM = 1\n", + " HARD = 2\n", + " NUM_DIFF_LEVELS = 3\n", + " \n", + "print \"There are \" , difficulty.NUM_DIFF_LEVELS ,\" difficulty levels.\"\n", + "\n", + "import random\n", + "#srand(static_cast<unsigned int>(time(0)));\n", + "choice = (int(random.random() * 10) % NUM_WORDS);\n", + "theWord = WORDS[choice][0]; # word to guess\n", + "theHint = WORDS[choice][1]; # hint for word\n", + "jumble = theWord; # jumbled version of word\n", + "length = len(jumble)\n", + "for i in range(length):\n", + " index1 = (int(random.random() * 10) % length);\n", + " index2 = (int(random.random() * 10) % length);\n", + " temp = list(jumble)\n", + " temp[index1],temp[index2] = temp[index2],temp[index1]\n", + " jumble = ''.join(temp)\n", + "\n", + "print \"\\t\\t\\tWelcome to Word Jumble!\\n\\n\";\n", + "print \"Unscramble the letters to make a word.\\n\";\n", + "print \"Enter 'hint' for a hint.\\n\";\n", + "print \"Enter 'quit' to quit the game.\\n\\n\";\n", + "print \"The jumble is: \" , jumble;\n", + "\n", + "print \"Your guess: \";\n", + "guess = raw_input()\n", + "\n", + "while ((guess != theWord) and (guess != \"quit\")):\n", + " if (guess == \"hint\"):\n", + " print theHint;\n", + " else:\n", + " print \"Sorry, that's not it.\";\n", + " print \"\\n\\nYour guess: \";\n", + " guess = raw_input()\n", + "\n", + "if (guess == theWord):\n", + " print \"\\nThat's it! You guessed it!\\n\";\n", + "\n", + "print \"\\nThanks for playing.\\n\";\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "There are 3 difficulty levels.\n", + "\t\t\tWelcome to Word Jumble!\n", + "\n", + "\n", + "Unscramble the letters to make a word.\n", + "\n", + "Enter 'hint' for a hint.\n", + "\n", + "Enter 'quit' to quit the game.\n", + "\n", + "\n", + "The jumble is: gasslse\n", + "Your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "glasses\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "That's it! You guessed it!\n", + "\n", + "\n", + "Thanks for playing.\n", + "\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/ch4.ipynb b/Beginning_C++_Through_Game_Programming/ch4.ipynb new file mode 100755 index 00000000..df9c13b0 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch4.ipynb @@ -0,0 +1,614 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4 : The Standard Template Library: Hangman" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 4.1 page no : 118" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "inventory = []\n", + "inventory.append(\"sword\");\n", + "inventory.append(\"armor\");\n", + "inventory.append(\"shield\");\n", + "print \"You have \" , len(inventory) , \" items.\";\n", + "print \"\\nYour items:\";\n", + "for i in inventory:\n", + " print i\n", + "\n", + "print \"\\nYou trade your sword for a battle axe.\";\n", + "inventory[0] = \"battle axe\";\n", + "print \"\\nYour items:\\n\";\n", + "for i in inventory:\n", + " print i\n", + "\n", + "print \"\\nThe item name '\" , inventory[0] , \"' has \",\n", + "print len(inventory[0]) , \" letters in it.\";\n", + "print \"\\nYour shield is destroyed in a fierce battle.\";\n", + "inventory.pop();\n", + "print \"\\nYour items:\";\n", + "for i in inventory:\n", + " print i\n", + "\n", + "print \"\\nYou were robbed of all of your possessions by a thief.\";\n", + "inventory = []\n", + "if (not inventory):\n", + " print \"\\nYou have nothing.\";\n", + "else:\n", + " print \"\\nYou have at least one item.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have 3 items.\n", + "\n", + "Your items:\n", + "sword\n", + "armor\n", + "shield\n", + "\n", + "You trade your sword for a battle axe.\n", + "\n", + "Your items:\n", + "\n", + "battle axe\n", + "armor\n", + "shield\n", + "\n", + "The item name ' battle axe ' has 10 letters in it.\n", + "\n", + "Your shield is destroyed in a fierce battle.\n", + "\n", + "Your items:\n", + "battle axe\n", + "armor\n", + "\n", + "You were robbed of all of your possessions by a thief.\n", + "\n", + "You have nothing.\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 4.2 page no : 124" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "inventory = []\n", + "inventory.append(\"sword\");\n", + "inventory.append(\"armor\");\n", + "inventory.append(\"shield\");\n", + "print \"You have \" , len(inventory) , \" items.\";\n", + "print \"\\nYour items:\";\n", + "for i in inventory:\n", + " print i\n", + "\n", + "print \"\\nYou trade your sword for a battle axe.\";\n", + "inventory[0] = \"battle axe\";\n", + "print \"\\nYour items:\\n\";\n", + "for i in inventory:\n", + " print i\n", + "\n", + "print \"\\nThe item name '\" , inventory[0] , \"' has \",\n", + "print len(inventory[0]) , \" letters in it.\";\n", + "\n", + "print \"You recover a crossbow from a slain enemy.\";\n", + "inventory.insert(0,\"crossbow\");\n", + "print \"Your items:\";\n", + "for i in inventory:\n", + " print i\n", + " \n", + "print \"\\nYour shield is destroyed in a fierce battle.\";\n", + "inventory.pop();\n", + "print \"\\nYour items:\";\n", + "for i in inventory:\n", + " print i" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have 3 items.\n", + "\n", + "Your items:\n", + "sword\n", + "armor\n", + "shield\n", + "\n", + "You trade your sword for a battle axe.\n", + "\n", + "Your items:\n", + "\n", + "battle axe\n", + "armor\n", + "shield\n", + "\n", + "The item name ' battle axe ' has 10 letters in it.\n", + "You recover a crossbow from a slain enemy.\n", + "Your items:\n", + "crossbow\n", + "battle axe\n", + "armor\n", + "shield\n", + "\n", + "Your shield is destroyed in a fierce battle.\n", + "\n", + "Your items:\n", + "crossbow\n", + "battle axe\n", + "armor\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 4.3 page no : 132" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "print \"Creating a list of scores.\";\n", + "scores = []\n", + "scores.append(1500);\n", + "scores.append(3500);\n", + "scores.append(7500);\n", + "print \"\\nHigh Scores:\";\n", + "for i in scores:\n", + " print i\n", + "\n", + "print \"\\nFinding a score.\";\n", + "\n", + "print \"\\nEnter a score to find: \";\n", + "score = 3500 #int(raw_input())\n", + "iter = score in scores\n", + "if (iter):\n", + " print \"Score found.\";\n", + "else:\n", + " print \"Score not found.\";\n", + "\n", + "print \"\\nRandomizing scores.\";\n", + "import random\n", + "for i in range(len(scores)):\n", + " scores[i] = int(random.random() * 10000) \n", + "\n", + "print \"\\nHigh Scores:\\n\";\n", + "for i in scores:\n", + " print i\n", + " \n", + "print \"\\nSorting scores.\";\n", + "scores.sort()\n", + "print \"\\nHigh Scores:\\n\";\n", + "for i in scores:\n", + " print i" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating a list of scores.\n", + "\n", + "High Scores:\n", + "1500\n", + "3500\n", + "7500\n", + "\n", + "Finding a score.\n", + "\n", + "Enter a score to find: \n", + "Score found.\n", + "\n", + "Randomizing scores.\n", + "\n", + "High Scores:\n", + "\n", + "6658\n", + "5396\n", + "8522\n", + "\n", + "Sorting scores.\n", + "\n", + "High Scores:\n", + "\n", + "5396\n", + "6658\n", + "8522\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 4.4 page no: 142" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#setup\n", + "MAX_WRONG = 8;\n", + "#maximum number of incorrect guesses allowed\n", + "words = [] # collection of possible words to guess\n", + "words.append(\"GUESS\");\n", + "words.append(\"HANGMAN\");\n", + "words.append(\"DIFFICULT\");\n", + "THE_WORD = words[0];\n", + "wrong = 0;\n", + "soFar = []\n", + "for i in range(len(THE_WORD)):\n", + " soFar.append('-')\n", + "\n", + "used = \"\";\n", + "\n", + "print \"Welcome to Hangman. Good luck!\\n\";\n", + "# main loop\n", + "while ((wrong < MAX_WRONG) and (''.join(soFar) != THE_WORD)):\n", + " print \"\\n\\nYou have \" , (MAX_WRONG - wrong);\n", + " print \" incorrect guesses left.\";\n", + " print \"\\nYou've used the following letters:\\n\" , used \n", + " print \"\\nSo far, the word is:\" , soFar\n", + " print \"\\nEnter your guess: \";\n", + " guess = raw_input()\n", + " guess = guess.upper() #make uppercase since secret word in uppercase\n", + " while guess in used:\n", + " print \"\\nYou've already guessed \" , guess \n", + " print \"Enter your guess: \";\n", + " guess = 'GUESS' #raw_input()\n", + " guess = guess.upper()\n", + " used += guess;\n", + " \n", + " if (guess in THE_WORD):\n", + " print \"That's right! \" , guess , \" is in the word.\";\n", + " #update soFar to include newly guessed letter\n", + " for i in range(len(THE_WORD)):\n", + " if (THE_WORD[i] == guess):\n", + " soFar[i] = guess\n", + " else:\n", + " print \"Sorry, \" , guess , \" isn't in the word.\";\n", + " wrong += 1\n", + "#shut down\n", + "if (wrong == MAX_WRONG):\n", + " print \"\\nYou've been hanged!\";\n", + "else:\n", + " print \"\\nYou guessed it!\";\n", + "print \"\\nThe word was \" , THE_WORD " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Welcome to Hangman. Good luck!\n", + "\n", + "\n", + "\n", + "You have 8\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "\n", + "\n", + "So far, the word is: ['-', '-', '-', '-', '-']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "e\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "That's right! E is in the word.\n", + "\n", + "\n", + "You have 8\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "E\n", + "\n", + "So far, the word is: ['-', '-', 'E', '-', '-']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "ae\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, AE isn't in the word.\n", + "\n", + "\n", + "You have 7\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAE\n", + "\n", + "So far, the word is: ['-', '-', 'E', '-', '-']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "w\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, W isn't in the word.\n", + "\n", + "\n", + "You have 6\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEW\n", + "\n", + "So far, the word is: ['-', '-', 'E', '-', '-']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "wae\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, WAE isn't in the word.\n", + "\n", + "\n", + "You have 5\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAE\n", + "\n", + "So far, the word is: ['-', '-', 'E', '-', '-']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "s\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "That's right! S is in the word.\n", + "\n", + "\n", + "You have 5\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAES\n", + "\n", + "So far, the word is: ['-', '-', 'E', 'S', 'S']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "ews\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, EWS isn't in the word.\n", + "\n", + "\n", + "You have 4\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAESEWS\n", + "\n", + "So far, the word is: ['-', '-', 'E', 'S', 'S']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "h\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, H isn't in the word.\n", + "\n", + "\n", + "You have 3\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAESEWSH\n", + "\n", + "So far, the word is: ['-', '-', 'E', 'S', 'S']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "l\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, L isn't in the word.\n", + "\n", + "\n", + "You have 2\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAESEWSHL\n", + "\n", + "So far, the word is: ['-', '-', 'E', 'S', 'S']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "g\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "That's right! G is in the word.\n", + "\n", + "\n", + "You have 2\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAESEWSHLG\n", + "\n", + "So far, the word is: ['G', '-', 'E', 'S', 'S']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "u\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "That's right! U is in the word.\n", + "\n", + "You guessed it!\n", + "\n", + "The word was GUESS\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/ch5.ipynb b/Beginning_C++_Through_Game_Programming/ch5.ipynb new file mode 100755 index 00000000..529765d2 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch5.ipynb @@ -0,0 +1,412 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "chapter 5 : Functions: Mad Lib" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 5.1 page no : 152" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "# function prototype (declaration)\n", + "def instructions():\n", + " print \"Welcome to the most fun you've ever had with text!\\n\";\n", + " print \"Here's how to play the game. . .\\n\";\n", + "\n", + "instructions();" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Welcome to the most fun you've ever had with text!\n", + "\n", + "Here's how to play the game. . .\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 5.2 page no : 156" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def askYesNo1():\n", + " while True:\n", + " print \"Please enter 'y' or 'n' : \",\n", + " response1 = 'y' #raw_input()\n", + " if (response1 == 'y' or response1 == 'n'):\n", + " return response1\n", + "\n", + "def askYesNo2(question):\n", + " while True:\n", + " print question ,\n", + " response1 = 'n' #raw_input()\n", + " if (response1 == 'y' or response1 == 'n'):\n", + " return response1\n", + "\n", + "\n", + "answer1 = askYesNo1();\n", + "print \"Thanks for answering: \" , answer1 \n", + "answer2 = askYesNo2(\"Do you wish to save your game?\");\n", + "print \"Thanks for answering: \" , answer2 " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter 'y' or 'n' : Thanks for answering: y\n", + "Do you wish to save your game? Thanks for answering: n\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 5.3 page no : 162" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def func():\n", + " var = -5; # local variable in func()\n", + " print \"In func() var is: \" , var \n", + "\n", + "var = 5 # local variable in main()\n", + "print \"In main() var is: \" , var\n", + "func();\n", + "print \"Back in main() var is: \" , var\n", + "print \"In main() in a new scope var is: \" , var\n", + "print \"Creating new var in new scope.\";\n", + "var = 10; # variable in new scope, hides other variable named var\n", + "print \"In main() in a new scope var is: \" , var \n", + "\n", + "print \"At end of main() var created in new scope no longer exists.\";\n", + "print \"At end of main() var is: \" , var " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In main() var is: 5\n", + "In func() var is: -5\n", + "Back in main() var is: 5\n", + "In main() in a new scope var is: 5\n", + "Creating new var in new scope.\n", + "In main() in a new scope var is: 10\n", + "At end of main() var created in new scope no longer exists.\n", + "At end of main() var is: 10\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 5.4 page no : 167" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "glob = 10; # global variable\n", + "\n", + "def access_global():\n", + " global glob\n", + " print \"In access_global() glob is: \" , glob\n", + "\n", + "def hide_global():\n", + " glob = 0 # hide global variable glob\n", + " print \"In hide_global() glob is: \" , glob\n", + "\n", + "def change_global():\n", + " glob = -10; # change global variable glob\n", + " print \"In change_global() glob is: \" , glob\n", + "\n", + "\n", + "print \"In main() glob is: \" , glob\n", + "access_global();\n", + "hide_global();\n", + "print \"In main() glob is: \" , glob\n", + "change_global();\n", + "print \"In main() glob is: \" , glob" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "In main() glob is: 10\n", + "In access_global() glob is: 10\n", + "In hide_global() glob is: 0\n", + "In main() glob is: 10\n", + "In change_global() glob is: -10\n", + "In main() glob is: 10\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 5.5 page no : 172" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def askNumber(high,low=1):\n", + " a = high + 2\n", + " while True:\n", + " print \"Please enter a number\" , \" (\" , low , \" - \" , high , \"): \"\n", + " num = a #int(raw_input())\n", + " if (num > high or num < low):\n", + " return num\n", + "\n", + "\n", + "number = askNumber(5);\n", + "print \"Thanks for entering: \" , number\n", + "number = askNumber(10, 5);\n", + "print \"Thanks for entering: \" , number" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please enter a number ( 1 - 5 ): \n", + "Thanks for entering: 7\n", + "Please enter a number ( 5 - 10 ): \n", + "Thanks for entering: 12\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 5.6 page no : 175" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def triple(number):\n", + " return (number * 3);\n", + "\n", + "def triple1(text):\n", + " return (text + text + text);\n", + "\n", + "print \"Tripling 5: \" , triple(5) \n", + "print \"Tripling 'gamer': \" , triple1(\"gamer\");" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Tripling 5: 15\n", + "Tripling 'gamer': gamergamergamer\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 5.7 page no : 178" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def radiation( health):\n", + " return (health / 2);\n", + "\n", + "health = 80;\n", + "print \"Your health is \" , health \n", + "health = radiation(health);\n", + "print \"After radiation exposure your health is \" , health \n", + "health = radiation(health);\n", + "print \"After radiation exposure your health is \" , health \n", + "health = radiation(health);\n", + "print \"After radiation exposure your health is \" , health " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your health is 80\n", + "After radiation exposure your health is 40\n", + "After radiation exposure your health is 20\n", + "After radiation exposure your health is 10\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 5.8 page no : 181" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def askText(prompt):\n", + " print prompt ,\n", + " a = raw_input()\n", + " return a\n", + "\n", + "def askNumber(prompt):\n", + " print prompt ,\n", + " return int(raw_input())\n", + "\n", + "def tellStory(name, noun, number, bodyPart, verb):\n", + " print \"\\nHere's your story:\";\n", + " print \"The famous explorer \" ,;\n", + " print name ,\n", + " print \" had nearly given up a life-long quest to find\",\n", + " print \"The Lost City of \",\n", + " print noun,\n", + " print \" when one day, the \",\n", + " print noun,\n", + " print \" found the explorer.\"\n", + " print \"Surrounded by \",\n", + " print number,\n", + " print \" \" , noun,\n", + " print \", a tear came to \",\n", + " print name , \"'s \",\n", + " print bodyPart , \".\"\n", + " print \"After all this time, the quest was finally over. \",\n", + " print \"And then, the \",\n", + " print noun \n", + " print \"promptly devoured \",\n", + " print name , \". \"\n", + " print \"The moral of the story? Be careful what you \",\n", + " print verb,\n", + " print \" for.\",\n", + "\n", + "\n", + "print \"Welcome to Mad Lib.\\n\";\n", + "print \"Answer the following questions to help create a new story.\";\n", + "name = 'jay' #askText(\"Please enter a name: \");\n", + "noun = 'go' #askText(\"Please enter a plural noun: \");\n", + "number = 10 #askNumber(\"Please enter a number: \");\n", + "bodyPart = 'nothing' #askText(\"Please enter a body part: \");\n", + "verb = 'verb' #askText(\"Please enter a verb: \");\n", + "tellStory(name, noun, number, bodyPart, verb);\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Welcome to Mad Lib.\n", + "\n", + "Answer the following questions to help create a new story.\n", + "\n", + "Here's your story:\n", + "The famous explorer jay had nearly given up a life-long quest to find The Lost City of go when one day, the go found the explorer.\n", + "Surrounded by 10 go , a tear came to jay 's nothing .\n", + "After all this time, the quest was finally over. And then, the go\n", + "promptly devoured jay . \n", + "The moral of the story? Be careful what you verb for.\n" + ] + } + ], + "prompt_number": 9 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/ch6.ipynb b/Beginning_C++_Through_Game_Programming/ch6.ipynb new file mode 100755 index 00000000..65c2377e --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch6.ipynb @@ -0,0 +1,616 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 6.1 page no : 188" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#Note : python does not have reference variable.\n", + "\n", + "myScore = 1000;\n", + "mikesScore = myScore; #create a reference\n", + "print \"myScore is: \" , myScore\n", + "print \"mikesScore is: \" , mikesScore \n", + "print \"Adding 500 to myScore\\n\";\n", + "myScore += 500;\n", + "print \"myScore is: \" , myScore\n", + "print \"mikesScore is: \" , mikesScore\n", + "print \"Adding 500 to mikesScore\";\n", + "mikesScore += 500;\n", + "print \"myScore is: \" , myScore \n", + "print \"mikesScore is: \" , mikesScore" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "myScore is: 1000\n", + "mikesScore is: 1000\n", + "Adding 500 to myScore\n", + "\n", + "myScore is: 1500\n", + "mikesScore is: 1000\n", + "Adding 500 to mikesScore\n", + "myScore is: 1500\n", + "mikesScore is: 1500\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 6.2 page no : 192" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def badSwap(x,y):\n", + " x,y = y,x\n", + "\n", + "def goodSwap(x, y):\n", + " x[0],y[0] = y[0],x[0]\n", + "\n", + "myScore = [1500];\n", + "yourScore = [1000];\n", + "print \"Original values\";\n", + "print \"myScore: \" , myScore[0] \n", + "print \"yourScore: \" , yourScore[0]\n", + "print \"Calling badSwap()\\n\";\n", + "badSwap(myScore[0], yourScore[0]);\n", + "print \"myScore: \" , myScore[0] \n", + "print \"yourScore: \" , yourScore[0]\n", + "print \"Calling goodSwap()\\n\";\n", + "goodSwap(myScore, yourScore);\n", + "print \"myScore: \" , myScore[0] \n", + "print \"yourScore: \" , yourScore[0] ," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original values\n", + "myScore: 1500\n", + "yourScore: 1000\n", + "Calling badSwap()\n", + "\n", + "myScore: 1500\n", + "yourScore: 1000\n", + "Calling goodSwap()\n", + "\n", + "myScore: 1000\n", + "yourScore: 1500\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 6.3 page no : 196" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def display(vec):\n", + " print \"Your items:\";\n", + " for i in vec:\n", + " print i\n", + "\n", + "inventory = []\n", + "inventory.append(\"sword\");\n", + "inventory.append(\"armor\");\n", + "inventory.append(\"shield\");\n", + "display(inventory);" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your items:\n", + "sword\n", + "armor\n", + "shield\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example : 6.4 page no : 199" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#returns a reference to a string\n", + "\n", + "def refToElement(vec,i):\n", + " return vec[i]\n", + "\n", + "inventory = []\n", + "inventory.append(\"sword\");\n", + "inventory.append(\"armor\");\n", + "inventory.append(\"shield\");\n", + "\n", + "#displays string that the returned reference refers to\n", + "print \"Sending the returned reference to cout:\"\n", + "print refToElement(inventory, 0) \n", + "#assigns one reference to another -- inexpensive assignment\n", + "print \"Assigning the returned reference to another reference.\";\n", + "rStr = refToElement(inventory, 1);\n", + "print \"Sending the new reference to cout:\";\n", + "print rStr\n", + "#copies a string object -- expensive assignment\n", + "print \"Assigning the returned reference to a string object.\";\n", + "s = refToElement(inventory, 2);\n", + "print \"Sending the new string object to cout:\";\n", + "print s\n", + "#altering the string object through a returned reference\n", + "print \"Altering an object through a returned reference.\";\n", + "rStr = \"Healing Potion\";\n", + "print \"Sending the altered object to cout:\";\n", + "print inventory[1] " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sending the returned reference to cout:\n", + "sword\n", + "Assigning the returned reference to another reference.\n", + "Sending the new reference to cout:\n", + "armor\n", + "Assigning the returned reference to a string object.\n", + "Sending the new string object to cout:\n", + "shield\n", + "Altering an object through a returned reference.\n", + "Sending the altered object to cout:\n", + "armor\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 6.5 page no : 205" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# global constants\n", + "X = 'X'\n", + "O = 'O'\n", + "EMPTY = ' ';\n", + "TIE = 'T';\n", + "NO_ONE = 'N';\n", + "\n", + "def instructions():\n", + " print \"Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\";\n", + " print \"--where human brain is pit against silicon processor\";\n", + " print \"Make your move known by entering a number, 0 - 8. The number\";\n", + " print \"corresponds to the desired board position, as illustrated:\";\n", + " print \"0 | 1 | 2\";\n", + " print \"---------\";\n", + " print \"3 | 4 | 5\\n\"; \n", + " print \"---------\";\n", + " print \"6 | 7 | 8\\n\\n\";\n", + " print \"Prepare yourself, human. The battle is about to begin.\";\n", + "\n", + "def askYesNo(question):\n", + " response =''\n", + " while (response != 'y' and response != 'n'):\n", + " print question , \" (y/n): \",\n", + " response = raw_input()\n", + " return response;\n", + "\n", + "def askNumber( question, high, low):\n", + " while True:\n", + " print question , \" (\" , low , \" - \" , high , \"): \",\n", + " number = int(raw_input())\n", + " if (number > high or number < low):\n", + " pass\n", + " else:\n", + " break\n", + " return number;\n", + "\n", + "def humanPiece():\n", + " global X,O,EMPTY,TIE,NO_ONE\n", + " go_first = askYesNo(\"Do you require the first move?\");\n", + " if (go_first == 'y'):\n", + " print \"Then take the first move. You will need it.\"\n", + " return X;\n", + " else:\n", + " print \"Your bravery will be your undoing. . . I will go first.\";\n", + " return O;\n", + "\n", + "def opponent( piece):\n", + " global X,O,EMPTY,TIE,NO_ONE\n", + " if (piece == X):\n", + " return O;\n", + " else:\n", + " return X;\n", + "\n", + "def displayBoard( board):\n", + " print \"\\n\\t\" , board[0] , \" | \" , board[1] , \" | \" , board[2];\n", + " print \"\\t\" , \"---------\";\n", + " print \"\\t\" , board[3] , \" | \" , board[4] , \" | \" , board[5];\n", + " print \"\\t\" , \"---------\";\n", + " print \"\\t\" , board[6] , \" | \" , board[7] , \" | \" , board[8];\n", + " print \"\\n\";\n", + "\n", + "def winner(board):\n", + " global X,O,EMPTY,TIE,NO_ONE\n", + " # all possible winning rows\n", + " WINNING_ROWS = [ [0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]\n", + " TOTAL_ROWS = 8;\n", + " # if any winning row has three values that are the same (and not EMPTY),\n", + " # then we have a winner\n", + " for row in range (TOTAL_ROWS):\n", + " if ( (board[WINNING_ROWS[row][0]] != EMPTY) and\n", + " (board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) and\n", + " (board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) ):\n", + " return board[WINNING_ROWS[row][0]];\n", + " # since nobody has won, check for a tie (no empty squares left)\n", + " if EMPTY not in board:\n", + " return TIE;\n", + " return NO_ONE;\n", + "\n", + "def isLegal(move,board):\n", + " global X,O,EMPTY,TIE,NO_ONE\n", + " return (board[move] == EMPTY);\n", + "\n", + "def humanMove(board,human):\n", + " global X,O,EMPTY,TIE,NO_ONE\n", + " move = askNumber(\"Where will you move?\", len(board),0);\n", + " \n", + " while (not isLegal(move, board)):\n", + " print \"\\nThat square is already occupied, foolish human.\\n\";\n", + " move = askNumber(\"Where will you move?\", len(board),0);\n", + " \n", + " print \"Fine. . .\\n\";\n", + " return move;\n", + "\n", + "def computerMove(board, computer):\n", + " global X,O,EMPTY,TIE,NO_ONE\n", + " move = 0;\n", + " found = False;\n", + " #if computer can win on next move, that's the move to make\n", + " while (not found and move < len(board)):\n", + " if (isLegal(move, board)):\n", + " board[move] = computer;\n", + " found = winner(board) == computer;\n", + " board[move] = EMPTY;\n", + " if (not found):\n", + " move += 1\n", + " #otherwise, if human can win on next move, that's the move to make\n", + " if (not found):\n", + " move = 0;\n", + " human = opponent(computer);\n", + " while (not found and move < len(board)):\n", + " if (isLegal(move, board)):\n", + " board[move] = human;\n", + " found = winner(board) == human;\n", + " board[move] = EMPTY;\n", + " if (not found):\n", + " move += 1\n", + " #otherwise, moving to the best open square is the move to make\n", + " if (not found):\n", + " move = 0;\n", + " i = 0;\n", + " BEST_MOVES = [4, 0, 2, 6, 8, 1, 3, 5, 7] #pick best open square\n", + " while (not found and i < len(board)):\n", + " move = BEST_MOVES[i];\n", + " if (isLegal(move, board)):\n", + " found = True;\n", + " i += 1\n", + " print \"I shall take square number \" , move \n", + " return move;\n", + "\n", + "def announceWinner( winner, computer, human):\n", + " if (winner == computer):\n", + " print winner , \"'s won!\";\n", + " print \"As I predicted, human, I am triumphant once more -- proof\";\n", + " print \"that computers are superior to humans in all regards.\";\n", + " elif (winner == human):\n", + " print winner , \"'s won!\";\n", + " print \"No, no! It cannot be! Somehow you tricked me, human.\";\n", + " print \"But never again! I, the computer, so swear it!\";\n", + " else:\n", + " print \"It's a tie.\\n\";\n", + " print \"You were most lucky, human, and somehow managed to tie me.\\n\";\n", + " print \"Celebrate. . . for this is the best you will ever achieve.\\n\";\n", + "\n", + "\n", + "NUM_SQUARES = 9;\n", + "board = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]\n", + "instructions();\n", + "human = humanPiece();\n", + "computer = opponent(human);\n", + "turn = X;\n", + "displayBoard(board);\n", + "while (winner(board) == NO_ONE):\n", + " if (turn == human):\n", + " move = humanMove(board, human);\n", + " board[move] = human;\n", + " else:\n", + " move = computerMove(board, computer);\n", + " board[move] = computer;\n", + " displayBoard(board);\n", + " turn = opponent(turn);\n", + "\n", + "announceWinner(winner(board), computer, human);\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\n", + "--where human brain is pit against silicon processor\n", + "Make your move known by entering a number, 0 - 8. The number\n", + "corresponds to the desired board position, as illustrated:\n", + "0 | 1 | 2\n", + "---------\n", + "3 | 4 | 5\n", + "\n", + "---------\n", + "6 | 7 | 8\n", + "\n", + "\n", + "Prepare yourself, human. The battle is about to begin.\n", + "Do you require the first move? (y/n): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Then take the first move. You will need it.\n", + "\n", + "\t | | \n", + "\t---------\n", + "\t | | \n", + "\t---------\n", + "\t | | \n", + "\n", + "\n", + "Where will you move? ( 0 - 9 ): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Fine. . .\n", + "\n", + "\n", + "\tX | | \n", + "\t---------\n", + "\t | | \n", + "\t---------\n", + "\t | | \n", + "\n", + "\n", + "I shall take square number 4\n", + "\n", + "\tX | | \n", + "\t---------\n", + "\t | O | \n", + "\t---------\n", + "\t | | \n", + "\n", + "\n", + "Where will you move? ( 0 - 9 ): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Fine. . .\n", + "\n", + "\n", + "\tX | | X\n", + "\t---------\n", + "\t | O | \n", + "\t---------\n", + "\t | | \n", + "\n", + "\n", + "I shall take square number 1\n", + "\n", + "\tX | O | X\n", + "\t---------\n", + "\t | O | \n", + "\t---------\n", + "\t | | \n", + "\n", + "\n", + "Where will you move? ( 0 - 9 ): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Fine. . .\n", + "\n", + "\n", + "\tX | O | X\n", + "\t---------\n", + "\t | O | \n", + "\t---------\n", + "\t | X | \n", + "\n", + "\n", + "I shall take square number 6\n", + "\n", + "\tX | O | X\n", + "\t---------\n", + "\t | O | \n", + "\t---------\n", + "\tO | X | \n", + "\n", + "\n", + "Where will you move? ( 0 - 9 ): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Fine. . .\n", + "\n", + "\n", + "\tX | O | X\n", + "\t---------\n", + "\t | O | X\n", + "\t---------\n", + "\tO | X | \n", + "\n", + "\n", + "I shall take square number 8\n", + "\n", + "\tX | O | X\n", + "\t---------\n", + "\t | O | X\n", + "\t---------\n", + "\tO | X | O\n", + "\n", + "\n", + "Where will you move? ( 0 - 9 ): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Fine. . .\n", + "\n", + "\n", + "\tX | O | X\n", + "\t---------\n", + "\tX | O | X\n", + "\t---------\n", + "\tO | X | O\n", + "\n", + "\n", + "It's a tie.\n", + "\n", + "You were most lucky, human, and somehow managed to tie me.\n", + "\n", + "Celebrate. . . for this is the best you will ever achieve.\n", + "\n" + ] + } + ], + "prompt_number": 2 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/ch7.ipynb b/Beginning_C++_Through_Game_Programming/ch7.ipynb new file mode 100755 index 00000000..b13fd7d8 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch7.ipynb @@ -0,0 +1,285 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 7 : Pointers: Tic-Tac-Toe 2.0" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 7.1 page no :225" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "pScore = 0 #declare and initialize a pointer\n", + "score = 1000 #assign pointer pScore address of variable score\n", + "pScore = score; \n", + "print \"Assigning &score to pScore\\n\";\n", + "print \"&score is: \" , id(score) # address of score variable\n", + "print \"pScore is: \" , pScore # address stored in pointer\n", + "print \"score is: \" , score\n", + "print \"pScore is: \" , pScore # value pointed to by pointer\n", + "print \"Adding 500 to score\";\n", + "score += 500;\n", + "print \"score is: \" , score \n", + "print \"*pScore is: \" , pScore \n", + "print \"Adding 500 to *pScore\\n\";\n", + "pScore += 500;\n", + "print \"score is: \" , score \n", + "print \"*pScore is: \" , pScore\n", + "print \"Assigning &newScore to pScore\\n\";\n", + "newScore = 5000;\n", + "pScore = newScore;\n", + "print \"&newScore is: \" , id(newScore)\n", + "print \"pScore is: \" , pScore\n", + "print \"newScore is: \" , newScore\n", + "print \"*pScore is: \" , pScore\n", + "\n", + "print \"Assigning &str to pStr\\n\";\n", + "str = \"score\";\n", + "pStr = str; \n", + "print \"str is: \" , str\n", + "print \"*pStr is: \" , pStr\n", + "print \"(*pStr).size() is: \" , len(pStr)\n", + "print \"pStr->size() is: \" , len(pStr)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Assigning &score to pScore\n", + "\n", + "&score is: 24878128\n", + "pScore is: 1000\n", + "score is: 1000\n", + "pScore is: 1000\n", + "Adding 500 to score\n", + "score is: 1500\n", + "*pScore is: 1000\n", + "Adding 500 to *pScore\n", + "\n", + "score is: 1500\n", + "*pScore is: 1500\n", + "Assigning &newScore to pScore\n", + "\n", + "&newScore is: 24880648\n", + "pScore is: 5000\n", + "newScore is: 5000\n", + "*pScore is: 5000\n", + "Assigning &str to pStr\n", + "\n", + "str is: score\n", + "*pStr is: score\n", + "(*pStr).size() is: 5\n", + "pStr->size() is: 5\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 7.2 page no : 235" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "def badSwap(x,y):\n", + " x,y = y,x\n", + " \n", + "def goodSwap(x,y):\n", + " x[0],y[0] = y[0],x[0]\n", + "\n", + "\n", + "myScore = [150]\n", + "yourScore = [1000]\n", + "print \"Original values\";\n", + "print \"myScore: \" , myScore[0]\n", + "print \"yourScore: \" , yourScore[0]\n", + "print \"Calling badSwap()\";\n", + "badSwap(myScore[0], yourScore[0]);\n", + "print \"myScore: \" , myScore[0] \n", + "print \"yourScore: \" , yourScore[0]\n", + "print \"Calling goodSwap()\\n\";\n", + "goodSwap(myScore,yourScore);\n", + "print \"myScore: \" , myScore[0] \n", + "print \"yourScore: \" , yourScore[0]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Original values\n", + "myScore: 150\n", + "yourScore: 1000\n", + "Calling badSwap()\n", + "myScore: 150\n", + "yourScore: 1000\n", + "Calling goodSwap()\n", + "\n", + "myScore: 1000\n", + "yourScore: 150\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example : 7.3 page no : 239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def ptrToElement(pVec, i):\n", + " return pVec[i]\n", + "\n", + "inventory = []\n", + "inventory.append(\"sword\");\n", + "inventory.append(\"armor\");\n", + "inventory.append(\"shield\");\n", + "#displays string object that the returned pointer points to\n", + "print \"Sending the objected pointed to by returned pointer:\";\n", + "print ptrToElement(inventory, 0)\n", + "# assigns one pointer to another - - inexpensive assignment\n", + "print \"Assigning the returned pointer to another pointer.\";\n", + "pStr = ptrToElement(inventory, 1);\n", + "print \"Sending the object pointed to by new pointer to cout:\";\n", + "print pStr \n", + "#copies a string object - - expensive assignment\n", + "print \"Assigning object pointed by pointer to a string object.\\n\";\n", + "s = (ptrToElement(inventory, 2));\n", + "print \"Sending the new string object to cout:\";\n", + "print s \n", + "#altering the string object through a returned pointer\n", + "print \"Altering an object through a returned pointer.\";\n", + "pStr = \"Healing Potion\";\n", + "inventory[1] = pStr\n", + "print \"Sending the altered object to cout:\\n\";\n", + "print inventory[1] " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sending the objected pointed to by returned pointer:\n", + "sword\n", + "Assigning the returned pointer to another pointer.\n", + "Sending the object pointed to by new pointer to cout:\n", + "armor\n", + "Assigning object pointed by pointer to a string object.\n", + "\n", + "Sending the new string object to cout:\n", + "shield\n", + "Altering an object through a returned pointer.\n", + "Sending the altered object to cout:\n", + "\n", + "Healing Potion\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 7.4 page no : 244" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def increase(array,NUM_ELEMENTS):\n", + " for i in range(NUM_ELEMENTS):\n", + " array[i] += 500;\n", + "\n", + "def display( array, NUM_ELEMENTS):\n", + " for i in range(NUM_ELEMENTS):\n", + " print array[i]\n", + "\n", + "\n", + "print \"Creating an array of high scores.\\n\\n\";\n", + "NUM_SCORES = 3;\n", + "highScores = [5000, 3500, 2700]\n", + "print \"Displaying scores using array name as a constant pointer.\";\n", + "print highScores[0]\n", + "print highScores[1]\n", + "print highScores[2]\n", + "print \"Increasing scores by passing array as a constant pointer.\";\n", + "increase(highScores, NUM_SCORES)\n", + "print \"Displaying scores by passing array as a constant pointer to a constant.\";\n", + "display(highScores, NUM_SCORES);" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating an array of high scores.\n", + "\n", + "\n", + "Displaying scores using array name as a constant pointer.\n", + "5000\n", + "3500\n", + "2700\n", + "Increasing scores by passing array as a constant pointer.\n", + "Displaying scores by passing array as a constant pointer to a constant.\n", + "5500\n", + "4000\n", + "3200\n" + ] + } + ], + "prompt_number": 4 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/ch8.ipynb b/Beginning_C++_Through_Game_Programming/ch8.ipynb new file mode 100755 index 00000000..a69c4913 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch8.ipynb @@ -0,0 +1,566 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8 : Classes: Critter Caretaker" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 8.1 page no : 256" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "class Critter:\n", + " def __init__(self):\n", + " self.m_Hunger = 0\n", + "\n", + " def Greet(self):\n", + " print \"Hi. I'm a critter. My hunger level is \" , self.m_Hunger\n", + "\n", + "crit1 = Critter()\n", + "crit2 = Critter()\n", + "crit1.m_Hunger = 9;\n", + "print \"crit1's hunger level is \" , crit1.m_Hunger\n", + "crit2.m_Hunger = 3;\n", + "print \"crit2's hunger level is \" , crit2.m_Hunger\n", + "crit1.Greet()\n", + "crit2.Greet()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "crit1's hunger level is 9\n", + "crit2's hunger level is 3\n", + "Hi. I'm a critter. My hunger level is 9\n", + "Hi. I'm a critter. My hunger level is 3\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 8.2 page no: 261" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "class Critter:\n", + " def __init__(self,h=0):\n", + " print \"A new critter has been born!\" \n", + " self.m_Hunger = h\n", + "\n", + " def Greet(self):\n", + " print \"Hi. I'm a critter. My hunger level is \" , self.m_Hunger\n", + "\n", + "crit = Critter(7);\n", + "crit.Greet();" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "A new critter has been born!\n", + "Hi. I'm a critter. My hunger level is 7\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 8.3 page no : 265" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "class Critter:\n", + " def __init__(self,h=0):\n", + " print \"A new critter has been born!\" \n", + " self.m_Hunger = h\n", + "\n", + " def Greet(self):\n", + " print \"Hi. I'm a critter. My hunger level is \" , self.m_Hunger\n", + "\n", + " def GetHunger(self):\n", + " return self.m_Hunger;\n", + "\n", + " def SetHunger(self,hunger):\n", + " if (hunger < 0):\n", + " print \"You can't set a critter's hunger to a negative number.\";\n", + " else:\n", + " self.m_Hunger = hunger;\n", + "\n", + "\n", + "crit = Critter(5)\n", + "#print crit.m_Hunger; //illegal, m_Hunger is private!\n", + "print \"Calling GetHunger(): \" , crit.GetHunger()\n", + "print \"Calling SetHunger() with -1.\";\n", + "crit.SetHunger(-1);\n", + "print \"Calling SetHunger() with 9.\";\n", + "crit.SetHunger(9);\n", + "print \"Calling GetHunger(): \" , crit.GetHunger()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "A new critter has been born!\n", + "Calling GetHunger(): 5\n", + "Calling SetHunger() with -1.\n", + "You can't set a critter's hunger to a negative number.\n", + "Calling SetHunger() with 9.\n", + "Calling GetHunger(): 9\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 8.4 page no: 270" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "class Critter:\n", + " s_Total = 0\n", + " def __init__(self,h=0):\n", + " print \"A new critter has been born!\" \n", + " self.m_Hunger = h\n", + " Critter.s_Total += 1\n", + "\n", + " def GetTotal(self):\n", + " return Critter.s_Total;\n", + "\n", + "print \"The total number of critters is: \";\n", + "print Critter.s_Total\n", + "crit1 = Critter() \n", + "crit2 = Critter()\n", + "crit3 = Critter()\n", + "print \"\\nThe total number of critters is: \",\n", + "print Critter.GetTotal(crit1) " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The total number of critters is: \n", + "0\n", + "A new critter has been born!\n", + "A new critter has been born!\n", + "A new critter has been born!\n", + "\n", + "The total number of critters is: 3\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 8.5 page no : 277" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "\n", + "class Critter:\n", + " def __init__(self,hunger = 0, boredom = 0):\n", + " self.m_Hunger= hunger\n", + " self.m_Boredom = boredom\n", + "\n", + " def GetMood(self):\n", + " return (self.m_Hunger + self.m_Boredom);\n", + "\n", + " def PassTime(self,time):\n", + " self.m_Hunger += time;\n", + " self.m_Boredom += time;\n", + "\n", + " def Talk(self):\n", + " print \"I'm a critter and I feel \";\n", + " mood = self.GetMood();\n", + " if (mood > 15):\n", + " print \"mad.\\n\";\n", + " elif (mood > 10):\n", + " print \"frustrated.\\n\";\n", + " elif (mood > 5):\n", + " print \"okay.\\n\";\n", + " else:\n", + " print \"happy.\\n\";\n", + " self.PassTime(0);\n", + "\n", + " def Eat(self,food):\n", + " print \"Brruppp.\\n\";\n", + " self.m_Hunger -= food;\n", + " if (self.m_Hunger < 0):\n", + " self.m_Hunger = 0;\n", + " self.PassTime(0);\n", + "\n", + " def Play(self,fun):\n", + " print \"Wheee!\\n\";\n", + " self.m_Boredom -= fun;\n", + " if (self.m_Boredom < 0):\n", + " self.m_Boredom = 0;\n", + " self.PassTime(0);\n", + "\n", + "crit = Critter()\n", + "crit.Talk();\n", + "while True:\n", + " print \"\\nCritter Caretaker\\n\\n\";\n", + " print \"0 - Quit\\n\";\n", + " print \"1 - Listen to your critter\\n\";\n", + " print \"2 - Feed your critter\\n\";\n", + " print \"3 - Play with your critter\\n\\n\";\n", + " print \"Choice: \";\n", + " choice = int(raw_input())\n", + " if choice == 0:\n", + " print \"Good-bye.\\n\";\n", + " break\n", + " elif choice == 1:\n", + " crit.Talk();\n", + " elif choice == 2:\n", + " crit.Eat(0);\n", + " elif choice == 3:\n", + " crit.Play(0);\n", + " else:\n", + " print \"\\nSorry, but \" , choice , \" isn't a valid choice.\\n\";\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I'm a critter and I feel \n", + "happy.\n", + "\n", + "\n", + "Critter Caretaker\n", + "\n", + "\n", + "0 - Quit\n", + "\n", + "1 - Listen to your critter\n", + "\n", + "2 - Feed your critter\n", + "\n", + "3 - Play with your critter\n", + "\n", + "\n", + "Choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I'm a critter and I feel \n", + "happy.\n", + "\n", + "\n", + "Critter Caretaker\n", + "\n", + "\n", + "0 - Quit\n", + "\n", + "1 - Listen to your critter\n", + "\n", + "2 - Feed your critter\n", + "\n", + "3 - Play with your critter\n", + "\n", + "\n", + "Choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Brruppp.\n", + "\n", + "\n", + "Critter Caretaker\n", + "\n", + "\n", + "0 - Quit\n", + "\n", + "1 - Listen to your critter\n", + "\n", + "2 - Feed your critter\n", + "\n", + "3 - Play with your critter\n", + "\n", + "\n", + "Choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Wheee!\n", + "\n", + "\n", + "Critter Caretaker\n", + "\n", + "\n", + "0 - Quit\n", + "\n", + "1 - Listen to your critter\n", + "\n", + "2 - Feed your critter\n", + "\n", + "3 - Play with your critter\n", + "\n", + "\n", + "Choice: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Good-bye.\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 8.6 page no : 280" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "\n", + "class Critter:\n", + " def __init__(self,hunger = 0, boredom = 0):\n", + " self.m_Hunger= hunger\n", + " self.m_Boredom = boredom\n", + "\n", + " def Talk(self):\n", + " print \"I'm a critter and I feel \";\n", + " mood = self.GetMood();\n", + " if (mood > 15):\n", + " print \"mad.\\n\";\n", + " elif (mood > 10):\n", + " print \"frustrated.\\n\";\n", + " elif (mood > 5):\n", + " print \"okay.\\n\";\n", + " else:\n", + " print \"happy.\\n\";\n", + " self.PassTime();\n", + " \n", + " def Eat(self,food = 4):\n", + " print \"Brruppp.\";\n", + " self.m_Hunger -= food;\n", + " if (self.m_Hunger < 0):\n", + " self.m_Hunger = 0;\n", + " self.PassTime();\n", + " \n", + " def Play(self,fun = 4):\n", + " print \"Wheee!\";\n", + " self.m_Boredom -= fun;\n", + " if (self.m_Boredom < 0):\n", + " self.m_Boredom = 0;\n", + " self.PassTime();\n", + " \n", + " def PassTime(self, time = 1):\n", + " self.m_Hunger += time;\n", + " self.m_Boredom += time;\n", + "\n", + "\n", + " def GetMood(self):\n", + " return (self.m_Hunger + self.m_Boredom);\n", + "\n", + "crit = Critter()\n", + "crit.Talk();\n", + "choice = 1;\n", + "choices = [1,2,3,0]\n", + "i = 0\n", + "while choice != 0 :\n", + " print \"\\nCritter Caretaker\\n\";\n", + " print \"0 - Quit\";\n", + " print \"1 - Listen to your critter\";\n", + " print \"2 - Feed your critter\";\n", + " print \"3 - Play with your critter\\n\";\n", + "\n", + " print \"Choice: \",\n", + " choice = choices[i] #int(raw_input())\n", + " i += 1\n", + "\n", + " if choice ==0:\n", + " print \"Good-bye.\\n\";\n", + " elif choice==1:\n", + " crit.Talk(); \n", + " elif choice == 2:\n", + " crit.Eat();\n", + " elif choice == 3:\n", + " crit.Play();\n", + " else:\n", + " print \"\\nSorry, but \" , choice , \" isn't a valid choice.\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " I'm a critter and I feel \n", + "happy.\n", + "\n", + "\n", + "Critter Caretaker\n", + "\n", + "0 - Quit\n", + "1 - Listen to your critter\n", + "2 - Feed your critter\n", + "3 - Play with your critter\n", + "\n", + "Choice: I'm a critter and I feel \n", + "happy.\n", + "\n", + "\n", + "Critter Caretaker\n", + "\n", + "0 - Quit\n", + "1 - Listen to your critter\n", + "2 - Feed your critter\n", + "3 - Play with your critter\n", + "\n", + "Choice: Brruppp.\n", + "\n", + "Critter Caretaker\n", + "\n", + "0 - Quit\n", + "1 - Listen to your critter\n", + "2 - Feed your critter\n", + "3 - Play with your critter\n", + "\n", + "Choice: Wheee!\n", + "\n", + "Critter Caretaker\n", + "\n", + "0 - Quit\n", + "1 - Listen to your critter\n", + "2 - Feed your critter\n", + "3 - Play with your critter\n", + "\n", + "Choice: Good-bye.\n", + "\n" + ] + } + ], + "prompt_number": 7 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/ch9.ipynb b/Beginning_C++_Through_Game_Programming/ch9.ipynb new file mode 100755 index 00000000..dde7fee6 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch9.ipynb @@ -0,0 +1,447 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 9 : Advanced Classes and Dynamic Memory: Game Lobby" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 9.1 page no : 288" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "class Critter:\n", + " def __init__(self,name=\"\"):\n", + " self.m_Name = name\n", + "\n", + " def GetName(self):\n", + " return self.m_Name;\n", + "\n", + "class Farm:\n", + " def __init__(self,spaces = 1):\n", + " self.m_Critters = []\n", + "\n", + " def Add(self,aCritter):\n", + " self.m_Critters.append(aCritter);\n", + "\n", + " def RollCall(self):\n", + " for i in self.m_Critters:\n", + " print i.GetName() , \" here.\";\n", + "\n", + "crit = Critter(\"Poochie\");\n", + "print \"My critter's name is \" , crit.GetName()\n", + "print \"\\nCreating critter farm.\";\n", + "myFarm = Farm(3)\n", + "print \"Adding three critters to the farm.\";\n", + "myFarm.Add(Critter(\"Moe\"));\n", + "myFarm.Add(Critter(\"Larry\"));\n", + "myFarm.Add(Critter(\"Curly\"));\n", + "print \"Calling Roll. . .\";\n", + "myFarm.RollCall();" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "My critter's name is Poochie\n", + "\n", + "Creating critter farm.\n", + "Adding three critters to the farm.\n", + "Calling Roll. . .\n", + "Moe here.\n", + "Larry here.\n", + "Curly here.\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 9.2 page no : 293" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "class Critter:\n", + " def __init__(self,name=\"\"):\n", + " self.m_Name = name\n", + "\n", + " def GetName(self):\n", + " return self.m_Name;\n", + "\n", + "\n", + "def Peek(aCritter):\n", + " print aCritter.m_Name\n", + "\n", + "def print_(aCritter):\n", + " print \"Critter Object - \",\n", + " print \"m_Name: \" , aCritter.m_Name,\n", + "\n", + "\n", + "crit = Critter(\"Poochie\");\n", + "print \"Calling Peek() to access crit's private data member, m_Name: \";\n", + "Peek(crit);\n", + "print \"Sending crit object to cout with the , operator:\";\n", + "print_(crit)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Calling Peek() to access crit's private data member, m_Name: \n", + "Poochie\n", + "Sending crit object to cout with the , operator:\n", + "Critter Object - m_Name: Poochie\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 9.3 page no : 297" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "def intOnHeap():\n", + " pTemp = []\n", + " return pTemp;\n", + "\n", + "def leak1():\n", + " drip1 = []\n", + "\n", + "def leak2():\n", + " drip2 = []\n", + "\n", + "\n", + "pHeap = 10;\n", + "print \"*pHeap: \" , pHeap\n", + "pHeap2 = intOnHeap();\n", + "print \"pHeap2: \" , pHeap2 \n", + "print \"Freeing memory pointed to by pHeap.\"; # python automatically deletes the memory.\n", + "print \"Freeing memory pointed to by pHeap2.\";\n", + "pHeap = 0;\n", + "pHeap2 = 0;" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "*pHeap: 10\n", + "pHeap2: []\n", + "Freeing memory pointed to by pHeap.\n", + "Freeing memory pointed to by pHeap2.\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 9.4 page no : 304" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "\n", + "class Critter:\n", + " def __init__(self,name=None,age=None):\n", + " if (age!=None and name != None) or (age==None and name==None):\n", + " print \"Constructor called\";\n", + " self.m_pName = name\n", + " self.m_Age = age\n", + " else:\n", + " print \"Copy Constructor called\";\n", + " self.m_pName = name.m_pName\n", + " self.m_Age = name.m_Age;\n", + "\n", + " def GetName(self):\n", + " return self.m_Name;\n", + "\n", + " def __del__(self):\n", + " print \"Destructor called\\n\";\n", + "\n", + " def operator(self,c):\n", + " print \"Overloaded Assignment Operator called\";\n", + " return self\n", + " \n", + " def Greet(self):\n", + " print \"I'm \" , self.m_pName , \" and I'm \" , self.m_Age , \" years old.\";\n", + " print \"&m_pName: \" , \n", + " print id(self.m_pName)\n", + "\n", + "\n", + "def testDestructor():\n", + " toDestroy = Critter(\"Rover\", 3);\n", + " toDestroy.Greet(); \n", + "\n", + "def testCopyConstructor(aCopy):\n", + " aCopy.Greet();\n", + "\n", + "def testAssignmentOp():\n", + " crit1 = Critter(\"crit1\", 7);\n", + " crit2 = Critter(\"crit2\", 9);\n", + " crit1 = crit2;\n", + " crit1.Greet();\n", + " crit2.Greet();\n", + " crit3 = Critter(\"crit\", 11);\n", + " crit3 = crit3;\n", + " crit3.Greet();\n", + "\n", + "testDestructor();\n", + "print ''\n", + "crit = Critter(\"Poochie\", 5);\n", + "crit.Greet();\n", + "testCopyConstructor(crit);\n", + "crit.Greet();\n", + "print ''\n", + "testAssignmentOp();" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Constructor called\n", + "I'm Rover and I'm 3 years old.\n", + "&m_pName: 30745776\n", + "Destructor called\n", + "\n", + "\n", + "Constructor called\n", + "I'm Poochie and I'm 5 years old.\n", + "&m_pName: 30744672\n", + "I'm Poochie and I'm 5 years old.\n", + "&m_pName: 30744672\n", + "I'm Poochie and I'm 5 years old.\n", + "&m_pName: 30744672\n", + "\n", + "Constructor called\n", + "Constructor called\n", + "Destructor called\n", + "\n", + "I'm crit2 and I'm 9 years old.\n", + "&m_pName: 30725152\n", + "I'm crit2 and I'm 9 years old.\n", + "&m_pName: 30725152\n", + "Constructor called\n", + "I'm crit and I'm 11 years old.\n", + "&m_pName: 30744624\n", + "Destructor called\n", + "\n", + "Destructor called\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 9.5 and 9.6 page no : 318" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "class Player:\n", + " def __init__(self,name):\n", + " self.m_Name =name\n", + " self.m_pNext = 0\n", + "\n", + " def GetName(self):\n", + " return self.m_Name;\n", + "\n", + " def GetNext(self):\n", + " return self.m_pNext;\n", + "\n", + " def SetNext(self,next):\n", + " self.m_pNext = next;\n", + " \n", + "class Lobby:\n", + " def __init__(self):\n", + " self.m_pHead =0\n", + "\n", + " def __del__(self):\n", + " self.Clear();\n", + "\n", + " def AddPlayer(self):\n", + " #create a new player node\n", + " print \"Please enter the name of the new player: \",\n", + " name = 'abc' #raw_input()\n", + " pNewPlayer = Player(name);\n", + " #if list is empty, make head of list this new player\n", + " if (self.m_pHead == 0):\n", + " self.m_pHead = pNewPlayer;\n", + " #otherwise find the end of the list and add the player there\n", + " else:\n", + " pIter = self.m_pHead;\n", + " while (pIter.GetNext() != 0):\n", + " pIter = pIter.GetNext();\n", + " pIter.SetNext(pNewPlayer);\n", + " \n", + " def RemovePlayer(self):\n", + " if (self.m_pHead == 0):\n", + " print \"The game lobby is empty.\"\n", + " else:\n", + " pTemp = self.m_pHead;\n", + " self.m_pHead = self.m_pHead.GetNext();\n", + "\n", + " def Clear(self):\n", + " while (self.m_pHead != 0):\n", + " RemovePlayer();\n", + "\n", + " def print_(self):\n", + " pIter = self.m_pHead;\n", + "\n", + " print \"Here's who's in the game lobby:\";\n", + " if (pIter == 0):\n", + " print \"The lobby is empty.\\n\";\n", + " else:\n", + " while (pIter != 0):\n", + " print pIter.GetName()\n", + " pIter = pIter.GetNext();\n", + "\n", + "myLobby = Lobby()\n", + "choice = 1 \n", + "choices = [1,2,3,0]\n", + "i = 0\n", + "while (choice != 0):\n", + " print myLobby.print_();\n", + " print \"\\nGAME LOBBY\";\n", + " print \"0 - Exit the program.\";\n", + " print \"1 - Add a player to the lobby.\";\n", + " print \"2 - Remove a player from the lobby.\";\n", + " print \"3 - Clear the lobby.\";\n", + " print \"\\nEnter choice: \",\n", + " choice = choices[i] #int(raw_input())\n", + " i += 1\n", + " if choice ==0:\n", + " print \"Good-bye.\"\n", + " elif choice ==1: \n", + " myLobby.AddPlayer()\n", + " elif choice==2: \n", + " myLobby.RemovePlayer(); \n", + " elif choice==3: \n", + " myLobby.Clear()\n", + " else:\n", + " print \"That was not a valid choice.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Here's who's in the game lobby:\n", + "The lobby is empty.\n", + "\n", + "None\n", + "\n", + "GAME LOBBY\n", + "0 - Exit the program.\n", + "1 - Add a player to the lobby.\n", + "2 - Remove a player from the lobby.\n", + "3 - Clear the lobby.\n", + "\n", + "Enter choice: Please enter the name of the new player: Here's who's in the game lobby:\n", + "abc\n", + "None\n", + "\n", + "GAME LOBBY\n", + "0 - Exit the program.\n", + "1 - Add a player to the lobby.\n", + "2 - Remove a player from the lobby.\n", + "3 - Clear the lobby.\n", + "\n", + "Enter choice: Here's who's in the game lobby:\n", + "The lobby is empty.\n", + "\n", + "None\n", + "\n", + "GAME LOBBY\n", + "0 - Exit the program.\n", + "1 - Add a player to the lobby.\n", + "2 - Remove a player from the lobby.\n", + "3 - Clear the lobby.\n", + "\n", + "Enter choice: Here's who's in the game lobby:\n", + "The lobby is empty.\n", + "\n", + "None\n", + "\n", + "GAME LOBBY\n", + "0 - Exit the program.\n", + "1 - Add a player to the lobby.\n", + "2 - Remove a player from the lobby.\n", + "3 - Clear the lobby.\n", + "\n", + "Enter choice: Good-bye.\n" + ] + } + ], + "prompt_number": 7 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/Beginning_C++_Through_Game_Programming/screenshots/chapter1.png b/Beginning_C++_Through_Game_Programming/screenshots/chapter1.png Binary files differnew file mode 100755 index 00000000..44527cdd --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/screenshots/chapter1.png diff --git a/Beginning_C++_Through_Game_Programming/screenshots/chapter4.png b/Beginning_C++_Through_Game_Programming/screenshots/chapter4.png Binary files differnew file mode 100755 index 00000000..eb1b6ea5 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/screenshots/chapter4.png diff --git a/Beginning_C++_Through_Game_Programming/screenshots/chapter8.png b/Beginning_C++_Through_Game_Programming/screenshots/chapter8.png Binary files differnew file mode 100755 index 00000000..1032e338 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/screenshots/chapter8.png |