From e9c8c9201198909af53ace24755d70e30bc24950 Mon Sep 17 00:00:00 2001 From: Jovina Dsouza Date: Wed, 9 Jul 2014 14:16:11 +0530 Subject: adding book --- Beginning_C++_through_game_programming/README.txt | 10 + Beginning_C++_through_game_programming/ch1.ipynb | 427 +++++++++++++ Beginning_C++_through_game_programming/ch10.ipynb | 670 +++++++++++++++++++++ Beginning_C++_through_game_programming/ch2.ipynb | 238 ++++++++ Beginning_C++_through_game_programming/ch3.ipynb | 442 ++++++++++++++ Beginning_C++_through_game_programming/ch4.ipynb | 393 ++++++++++++ Beginning_C++_through_game_programming/ch5.ipynb | 196 ++++++ Beginning_C++_through_game_programming/ch6.ipynb | 141 +++++ Beginning_C++_through_game_programming/ch7.ipynb | 112 ++++ Beginning_C++_through_game_programming/ch8.ipynb | 484 +++++++++++++++ Beginning_C++_through_game_programming/ch9.ipynb | 469 +++++++++++++++ .../screenshots/chapter1.png | Bin 0 -> 112264 bytes .../screenshots/chapter4.png | Bin 0 -> 136496 bytes .../screenshots/chapter8.png | Bin 0 -> 119773 bytes 14 files changed, 3582 insertions(+) create mode 100755 Beginning_C++_through_game_programming/README.txt create mode 100755 Beginning_C++_through_game_programming/ch1.ipynb create mode 100755 Beginning_C++_through_game_programming/ch10.ipynb create mode 100755 Beginning_C++_through_game_programming/ch2.ipynb create mode 100755 Beginning_C++_through_game_programming/ch3.ipynb create mode 100755 Beginning_C++_through_game_programming/ch4.ipynb create mode 100755 Beginning_C++_through_game_programming/ch5.ipynb create mode 100755 Beginning_C++_through_game_programming/ch6.ipynb create mode 100755 Beginning_C++_through_game_programming/ch7.ipynb create mode 100755 Beginning_C++_through_game_programming/ch8.ipynb create mode 100755 Beginning_C++_through_game_programming/ch9.ipynb create mode 100755 Beginning_C++_through_game_programming/screenshots/chapter1.png create mode 100755 Beginning_C++_through_game_programming/screenshots/chapter4.png create mode 100755 Beginning_C++_through_game_programming/screenshots/chapter8.png (limited to 'Beginning_C++_through_game_programming') diff --git a/Beginning_C++_through_game_programming/README.txt b/Beginning_C++_through_game_programming/README.txt new file mode 100755 index 00000000..1d5d0c17 --- /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..2b946f8b --- /dev/null +++ b/Beginning_C++_through_game_programming/ch1.ipynb @@ -0,0 +1,427 @@ +{ + "metadata": { + "name": "ch1" + }, + "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", + "prints Game Over\n", + "'''\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", + "prints \"Game Over!\"\n", + "'''\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", + "print \"Game Over!\"\n", + "'''\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", + "Expensive calculator\n", + "'''\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", + "Game Stats\n", + "Demonstrates declaring and initializing variables\n", + "'''\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", + "small calculation\n", + "'''\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", + "Game Stats 3.0\n", + "Demonstrates constants\n", + "'''\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", + "// Lost Fortune\n", + "// A personalized adventure\n", + "'''\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 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "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..393c4d1c --- /dev/null +++ b/Beginning_C++_through_game_programming/ch10.ipynb @@ -0,0 +1,670 @@ +{ + "metadata": { + "name": "ch10" + }, + "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", + "//Simple Boss\n", + "//Demonstrates inheritance\n", + "'''\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", + "//Simple Boss 2.0\n", + "//Demonstrates access control under inheritance\n", + "'''\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", + "//Overriding Boss\n", + "//Demonstrates calling and overriding base member functions\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 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", + "//Polymorphic Bad Guy\n", + "//Demonstrates calling member functions dynamically\n", + "'''\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", + "//Abstract Creature\n", + "//Demonstrates abstract classes\n", + "'''\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": [ + "'''\n", + "defining game class.\n", + "Note : this would give error as we have not initialized player class here.\n", + "'''\n", + "\n", + "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 (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 (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):\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", + " 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(Card.CLUBS,Card.SPADES+1):\n", + " for r in range(Card.ACE,Card.KING+1):\n", + " self.Add(Card(r,s))\n", + "\n", + " def Shuffle(self):\n", + " pass\n", + "\n", + " def Deal(self, aHand):\n", + " if (not 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.pName = []\n", + " for i in names:\n", + " self.pName.append(Player(i))\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(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 \"\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "StdinNotImplementedError", + "evalue": "raw_input was called, but this frontend does not support stdin.", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 63\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mnumPlayers\u001b[0m \u001b[1;33m<\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mnumPlayers\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m7\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 64\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"How many players? (1 - 7): \"\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 65\u001b[1;33m \u001b[0mnumPlayers\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mraw_input\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 66\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 67\u001b[0m \u001b[0mnames\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[1;32m/home/jay/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m\u001b[1;34m(prompt)\u001b[0m\n\u001b[0;32m 343\u001b[0m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_raw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mident\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 344\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 345\u001b[1;33m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m \u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_no_raw_input\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 346\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 347\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mpy3compat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPY3\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m/home/jay/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m_no_raw_input\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 686\u001b[0m \"\"\"Raise StdinNotImplentedError if active frontend doesn't support\n\u001b[0;32m 687\u001b[0m stdin.\"\"\"\n\u001b[1;32m--> 688\u001b[1;33m raise StdinNotImplementedError(\"raw_input was called, but this \"\n\u001b[0m\u001b[0;32m 689\u001b[0m \"frontend does not support stdin.\") \n\u001b[0;32m 690\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support stdin." + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\t\tWelcome to Blackjack!\n", + "\n", + "How many players? (1 - 7): \n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "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..ff66a26a --- /dev/null +++ b/Beginning_C++_through_game_programming/ch2.ipynb @@ -0,0 +1,238 @@ +{ + "metadata": { + "name": "ch2" + }, + "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": "'''\nScore Rater\nDemonstrates the if statement\n'''\n\nif (True):\n print \"This is always displayed.\\n\\n\";\n\nif (False):\n print \"This is never displayed.\\n\\n\";\nscore = 1000;\nif (score):\n print \"At least you didn't score zero.\\n\\n\";\n\nif (score >= 250):\n print \"You scored 250 or more. Decent.\\n\\n\";\n\nif (score >= 500):\n print \"You scored 500 or more. Nice.\\n\\n\";\nif (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\nAt least you didn't score zero.\n\n\nYou scored 250 or more. Decent.\n\n\nYou scored 500 or more. Nice.\n\n\nYou 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": "'''\nScore Rater 2.0\nDemonstrates an else clause\n'''\n\nprint \"Enter your score: \";\nscore = 1500 #int(raw_input())\nif (score >= 1000):\n print \"You scored 1000 or more. Impressive!\\n\";\nelse:\n print \"You scored less than 1000.\\n\";", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Enter your score: \nYou 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// Score Rater 3.0\n// Demonstrates if else-if else suite\n'''\n\n\nprint \"Enter your score: \";\nscore = 1500 #int(raw_input())\n\nif (score >= 1000):\n print \"You scored 1000 or more. Impressive!\\n\";\nelif (score >= 500):\n print \"You scored 500 or more. Nice.\\n\";\nelif (score >= 250):\n print \"You scored 250 or more. Decent.\\n\";\nelse:\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: \nYou 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// Menu Chooser\n// Demonstrates the switch statement\n'''\n\nprint \"Difficulty Levels\\n\\n\";\nprint \"1 - Easy\\n\";\nprint \"2 - Normal\\n\";\nprint \"3 - Hard\\n\\n\";\n\nprint \"Choice: \";\nchoice = 2 #int(raw_input())\nif choice==1:\n print \"You picked Easy.\\n\";\nelif choice==2: \n print \"You picked Normal.\\n\";\nelif choice==3:\n \"You picked Hard.\\n\";\nelse: \n \"You made an illegal choice.\\n\";", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Difficulty Levels\n\n\n1 - Easy\n\n2 - Normal\n\n3 - Hard\n\n\nChoice: \nYou 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": "'''\nPlay Again\nDemonstrates while loops\n'''\nchoices = ['y','y','n']\nagain = 'y'\ni = 0\nwhile (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\nprint \"\\nOkay, bye.\";", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "\n**Played an exciting game**\n\nDo you want to play again? (y/n): \n**Played an exciting game**\n\nDo you want to play again? (y/n): \n**Played an exciting game**\n\nDo you want to play again? (y/n): \nOkay, bye.\n" + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "example 2.6 page no : 57" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nPlay Again\nDemonstrates while loops\n'''\nchoices = ['y','y','n']\ni = 0\nagain = 'y'\nwhile (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\nprint \"\\nOkay, bye.\";", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "\n**Played an exciting game**\n\nDo you want to play again? (y/n): \n**Played an exciting game**\n\nDo you want to play again? (y/n): \n**Played an exciting game**\n\nDo you want to play again? (y/n): \nOkay, 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// Finicky Counter\n// Demonstrates break and continue statements\n'''\ncount = 0;\nwhile (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\n2\n3\n4\n6\n7\n8\n9\n10\n" + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "example 2.8 page no : 63" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\n// Designers Network\n// Demonstrates logical operators\n'''\n\nprint \"\\tGame Designer's Network\\n\";\nsuccess = False\nwhile 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\nUsername: \nPassword: \n\nWelcome, 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// Die Roller\n// Demonstrates generating random numbers\n'''\nimport random\n\nrandomNumber = random.random()*100;\n#seed random number generator\n#generate random number\ndie = int(randomNumber % 6) + 1; # get a number between 1 and 6\nprint \"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": "'''\nGuess My Number\nThe classic number guessing game\n'''\nimport random\nsecretNumber = int(random.random()*100) % 10 + 1;\ntries = 0;\n#seed random number generator\n# random number between 1 and 100\nprint \"\\tWelcome to Guess My Number\\n\\n\";\nguesses = [0,1,2,3,4,5,6,7,8,9,10]\ni = 0\nwhile 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\nEnter a guess: \nToo low!\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \nToo low!\n\n\nEnter a guess: \n\nThat's it! You got it in 7 guesses!\n\n" + } + ], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "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..ee604f70 --- /dev/null +++ b/Beginning_C++_through_game_programming/ch3.ipynb @@ -0,0 +1,442 @@ +{ + "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", + "// Counter\n", + "// Demonstrates for loops\n", + "'''\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", + "// String Tester\n", + "// Demonstrates string objects\n", + "'''\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", + "// Heros Inventory\n", + "// Demonstrates arrays\n", + "'''\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", + "// Tic-Tac-Toe Board\n", + "// Demonstrates multidimensional arrays\n", + "'''\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", + "# Word Jumble\n", + "# The classic word jumble game where the player can ask for a hint\n", + "'''\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(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 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "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..29da32f0 --- /dev/null +++ b/Beginning_C++_through_game_programming/ch4.ipynb @@ -0,0 +1,393 @@ +{ + "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", + "// Hero's Inventory 2.0\n", + "// Demonstrates vectors\n", + "'''\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", + "// Hero's Inventory 3.0\n", + "// Demonstrates iterators\n", + "'''\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", + "// High Scores\n", + "// Demonstrates algorithms\n", + "'''\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", + "// Hangman\n", + "// The classic game of hangman\n", + "'''\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 += '-'\n", + "\n", + "used = \"\";\n", + "\n", + "print \"Welcome to Hangman. Good luck!\\n\";\n", + "# main loop\n", + "while ((wrong < MAX_WRONG) and (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", + " soFar = ''\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 += 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": [ + { + "ename": "StdinNotImplementedError", + "evalue": "raw_input was called, but this frontend does not support stdin.", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 27\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"\\nSo far, the word is:\"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0msoFar\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 28\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"\\nEnter your guess: \"\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 29\u001b[1;33m \u001b[0mguess\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mraw_input\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 30\u001b[0m \u001b[0mguess\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mguess\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#make uppercase since secret word in uppercase\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 31\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0mguess\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mused\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m/home/jay/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m\u001b[1;34m(prompt)\u001b[0m\n\u001b[0;32m 343\u001b[0m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_raw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mident\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 344\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 345\u001b[1;33m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m \u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_no_raw_input\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 346\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 347\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mpy3compat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPY3\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m/home/jay/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m_no_raw_input\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 686\u001b[0m \"\"\"Raise StdinNotImplentedError if active frontend doesn't support\n\u001b[0;32m 687\u001b[0m stdin.\"\"\"\n\u001b[1;32m--> 688\u001b[1;33m raise StdinNotImplementedError(\"raw_input was called, but this \"\n\u001b[0m\u001b[0;32m 689\u001b[0m \"frontend does not support stdin.\") \n\u001b[0;32m 690\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support stdin." + ] + }, + { + "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" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "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..923fa8d5 --- /dev/null +++ b/Beginning_C++_through_game_programming/ch5.ipynb @@ -0,0 +1,196 @@ +{ + "metadata": { + "name": "ch5" + }, + "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// Instructions\n// Demonstrates writing new functions\n'''\n\n# function prototype (declaration)\ndef 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\ninstructions();", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Welcome to the most fun you've ever had with text!\n\nHere'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// Yes or No\n// Demonstrates return values and parameters\n'''\n\ndef 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\ndef 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\nanswer1 = askYesNo1();\nprint \"Thanks for answering: \" , answer1 \nanswer2 = askYesNo2(\"Do you wish to save your game?\");\nprint \"Thanks for answering: \" , answer2 ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Please enter 'y' or 'n' : Thanks for answering: y\nDo 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# Scoping\n# Demonstrates scopes\n'''\ndef func():\n var = -5; # local variable in func()\n print \"In func() var is: \" , var \n\nvar = 5 # local variable in main()\nprint \"In main() var is: \" , var\nfunc();\nprint \"Back in main() var is: \" , var\nprint \"In main() in a new scope var is: \" , var\nprint \"Creating new var in new scope.\";\nvar = 10; # variable in new scope, hides other variable named var\nprint \"In main() in a new scope var is: \" , var \n\nprint \"At end of main() var created in new scope no longer exists.\";\nprint \"At end of main() var is: \" , var ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "In main() var is: 5\nIn func() var is: -5\nBack in main() var is: 5\nIn main() in a new scope var is: 5\nCreating new var in new scope.\nIn main() in a new scope var is: 10\nAt end of main() var created in new scope no longer exists.\nAt 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": "'''\nglobal access of variable\n'''\n\nglob = 10; # global variable\n\ndef access_global():\n global glob\n print \"In access_global() glob is: \" , glob\n\ndef hide_global():\n glob = 0 # hide global variable glob\n print \"In hide_global() glob is: \" , glob\n\ndef change_global():\n glob = -10; # change global variable glob\n print \"In change_global() glob is: \" , glob\n\n\nprint \"In main() glob is: \" , glob\naccess_global();\nhide_global();\nprint \"In main() glob is: \" , glob\nchange_global();\nprint \"In main() glob is: \" , glob", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "In main() glob is: 10\nIn access_global() glob is: 10\nIn hide_global() glob is: 0\nIn main() glob is: 10\nIn change_global() glob is: -10\nIn 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// Give Me a Number\n// Demonstrates default function arguments\n'''\ndef 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\nnumber = askNumber(5);\nprint \"Thanks for entering: \" , number\nnumber = askNumber(10, 5);\nprint \"Thanks for entering: \" , number", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Please enter a number ( 1 - 5 ): \nThanks for entering: 7\nPlease enter a number ( 5 - 10 ): \nThanks 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// Triple\n'''\n\ndef triple(number):\n return (number * 3);\n\ndef triple1(text):\n return (text + text + text);\n\nprint \"Tripling 5: \" , triple(5) \nprint \"Tripling 'gamer': \" , triple1(\"gamer\");", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Tripling 5: 15\nTripling '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// Taking Damage\n// Demonstrates function inlining\n'''\n\ndef radiation( health):\n return (health / 2);\n\nhealth = 80;\nprint \"Your health is \" , health \nhealth = radiation(health);\nprint \"After radiation exposure your health is \" , health \nhealth = radiation(health);\nprint \"After radiation exposure your health is \" , health \nhealth = radiation(health);\nprint \"After radiation exposure your health is \" , health ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Your health is 80\nAfter radiation exposure your health is 40\nAfter radiation exposure your health is 20\nAfter 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// Mad-Lib\n// Creates a story based on user input\n'''\n\ndef askText(prompt):\n print prompt ,\n a = raw_input()\n return a\n\ndef askNumber(prompt):\n print prompt ,\n return int(raw_input())\n\ndef 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\nprint \"Welcome to Mad Lib.\\n\";\nprint \"Answer the following questions to help create a new story.\";\nname = 'jay' #askText(\"Please enter a name: \");\nnoun = 'go' #askText(\"Please enter a plural noun: \");\nnumber = 10 #askNumber(\"Please enter a number: \");\nbodyPart = 'nothing' #askText(\"Please enter a body part: \");\nverb = 'verb' #askText(\"Please enter a verb: \");\ntellStory(name, noun, number, bodyPart, verb);\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Welcome to Mad Lib.\n\nAnswer the following questions to help create a new story.\n\nHere's your story:\nThe 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.\nSurrounded by 10 go , a tear came to jay 's nothing .\nAfter all this time, the quest was finally over. And then, the go\npromptly devoured jay . \nThe moral of the story? Be careful what you verb for.\n" + } + ], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "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..d5486321 --- /dev/null +++ b/Beginning_C++_through_game_programming/ch6.ipynb @@ -0,0 +1,141 @@ +{ + "metadata": { + "name": "ch6" + }, + "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// Referencing\n// Demonstrates using references\nNote : python does not have reference variable.\n'''\nmyScore = 1000;\nmikesScore = myScore; #create a reference\nprint \"myScore is: \" , myScore\nprint \"mikesScore is: \" , mikesScore \nprint \"Adding 500 to myScore\\n\";\nmyScore += 500;\nprint \"myScore is: \" , myScore\nprint \"mikesScore is: \" , mikesScore\nprint \"Adding 500 to mikesScore\";\nmikesScore += 500;\nprint \"myScore is: \" , myScore \nprint \"mikesScore is: \" , mikesScore", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "myScore is: 1000\nmikesScore is: 1000\nAdding 500 to myScore\n\nmyScore is: 1500\nmikesScore is: 1000\nAdding 500 to mikesScore\nmyScore is: 1500\nmikesScore 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// Swap\n// Demonstrates passing references to alter argument variables\n'''\ndef badSwap(x,y):\n x,y = y,x\n\ndef goodSwap(x, y):\n x[0],y[0] = y[0],x[0]\n\nmyScore = [1500];\nyourScore = [1000];\nprint \"Original values\";\nprint \"myScore: \" , myScore[0] \nprint \"yourScore: \" , yourScore[0]\nprint \"Calling badSwap()\\n\";\nbadSwap(myScore[0], yourScore[0]);\nprint \"myScore: \" , myScore[0] \nprint \"yourScore: \" , yourScore[0]\nprint \"Calling goodSwap()\\n\";\ngoodSwap(myScore, yourScore);\nprint \"myScore: \" , myScore[0] \nprint \"yourScore: \" , yourScore[0] ,", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Original values\nmyScore: 1500\nyourScore: 1000\nCalling badSwap()\n\nmyScore: 1500\nyourScore: 1000\nCalling goodSwap()\n\nmyScore: 1000\nyourScore: 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// Inventory Displayer\n// Demonstrates constant references\n'''\n\ndef display(vec):\n print \"Your items:\";\n for i in vec:\n print i\n\ninventory = []\ninventory.append(\"sword\");\ninventory.append(\"armor\");\ninventory.append(\"shield\");\ndisplay(inventory);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Your items:\nsword\narmor\nshield\n" + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "example : 6.4 page no : 199" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\n// Inventory Referencer\n// Demonstrates returning a reference\n'''\n#returns a reference to a string\n\ndef refToElement(vec,i):\n return vec[i]\n\ninventory = []\ninventory.append(\"sword\");\ninventory.append(\"armor\");\ninventory.append(\"shield\");\n\n#displays string that the returned reference refers to\nprint \"Sending the returned reference to cout:\"\nprint refToElement(inventory, 0) \n#assigns one reference to another -- inexpensive assignment\nprint \"Assigning the returned reference to another reference.\";\nrStr = refToElement(inventory, 1);\nprint \"Sending the new reference to cout:\";\nprint rStr\n#copies a string object -- expensive assignment\nprint \"Assigning the returned reference to a string object.\";\ns = refToElement(inventory, 2);\nprint \"Sending the new string object to cout:\";\nprint s\n#altering the string object through a returned reference\nprint \"Altering an object through a returned reference.\";\nrStr = \"Healing Potion\";\nprint \"Sending the altered object to cout:\";\nprint inventory[1] ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Sending the returned reference to cout:\nsword\nAssigning the returned reference to another reference.\nSending the new reference to cout:\narmor\nAssigning the returned reference to a string object.\nSending the new string object to cout:\nshield\nAltering an object through a returned reference.\nSending the altered object to cout:\narmor\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "example 6.5 page no : 205" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nTic-Tac-Toe\nPlays the game of tic-tac-toe against a human opponent\n'''\n\n\n# global constants\nX = 'X'\nO = 'O'\nEMPTY = ' ';\nTIE = 'T';\nNO_ONE = 'N';\n\ndef 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\ndef askYesNo(question):\n response =''\n while (response != 'y' and response != 'n'):\n print question , \" (y/n): \",\n response = raw_input()\n return response;\n\ndef 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\ndef 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\ndef opponent( piece):\n global X,O,EMPTY,TIE,NO_ONE\n if (piece == X):\n return O;\n else:\n return X;\n\ndef 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\ndef 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],[0,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\ndef isLegal(move,board):\n global X,O,EMPTY,TIE,NO_ONE\n return (board[move] == EMPTY);\n\ndef humanMove(board,human):\n global X,O,EMPTY,TIE,NO_ONE\n move = askNumber(\"Where will you move?\", len(board),1);\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),1);\n print move\n ''' \n print \"Fine. . .\\n\";\n return move;\n\ndef 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\ndef 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\nNUM_SQUARES = 9;\nboard = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]\ninstructions();\nhuman = humanPiece();\ncomputer = opponent(human);\nturn = X;\ndisplayBoard(board);\nwhile (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\nannounceWinner(winner(board), computer, human);\n\n", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "StdinNotImplementedError", + "evalue": "raw_input was called, but this frontend does not support stdin.", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 154\u001b[0m \u001b[0mboard\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mEMPTY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mEMPTY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mEMPTY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mEMPTY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mEMPTY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mEMPTY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mEMPTY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mEMPTY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mEMPTY\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 155\u001b[0m \u001b[0minstructions\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[1;32m--> 156\u001b[1;33m \u001b[0mhuman\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mhumanPiece\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 157\u001b[0m \u001b[0mcomputer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopponent\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhuman\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 158\u001b[0m \u001b[0mturn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m\u001b[0m in \u001b[0;36mhumanPiece\u001b[1;34m()\u001b[0m\n\u001b[0;32m 44\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mhumanPiece\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 45\u001b[0m \u001b[1;32mglobal\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mO\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mEMPTY\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mTIE\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mNO_ONE\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 46\u001b[1;33m \u001b[0mgo_first\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0maskYesNo\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Do you require the first move?\"\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 47\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mgo_first\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'y'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 48\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"Then take the first move. You will need it.\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m\u001b[0m in \u001b[0;36maskYesNo\u001b[1;34m(question)\u001b[0m\n\u001b[0;32m 29\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;34m'y'\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mresponse\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[0;32m 30\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[0mquestion\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;34m\" (y/n): \"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 31\u001b[1;33m \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mraw_input\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 32\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 33\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m/home/jay/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m\u001b[1;34m(prompt)\u001b[0m\n\u001b[0;32m 343\u001b[0m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_raw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mident\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 344\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 345\u001b[1;33m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m \u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_no_raw_input\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 346\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 347\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mpy3compat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPY3\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m/home/jay/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m_no_raw_input\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 686\u001b[0m \"\"\"Raise StdinNotImplentedError if active frontend doesn't support\n\u001b[0;32m 687\u001b[0m stdin.\"\"\"\n\u001b[1;32m--> 688\u001b[1;33m raise StdinNotImplementedError(\"raw_input was called, but this \"\n\u001b[0m\u001b[0;32m 689\u001b[0m \"frontend does not support stdin.\") \n\u001b[0;32m 690\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support stdin." + ] + }, + { + "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\nMake your move known by entering a number, 0 - 8. The number\ncorresponds to the desired board position, as illustrated:\n0 | 1 | 2\n---------\n3 | 4 | 5\n\n---------\n6 | 7 | 8\n\n\nPrepare yourself, human. The battle is about to begin.\nDo you require the first move? (y/n): " + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "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..ce1452b8 --- /dev/null +++ b/Beginning_C++_through_game_programming/ch7.ipynb @@ -0,0 +1,112 @@ +{ + "metadata": { + "name": "ch7" + }, + "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": "'''\nPointing\nDemonstrates using pointers\nNOte : pointer concept is not in python, so output would be differ.\n'''\n\npScore = 0 #declare and initialize a pointer\nscore = 1000 #assign pointer pScore address of variable score\npScore = score; \nprint \"Assigning &score to pScore\\n\";\nprint \"&score is: \" , id(score) # address of score variable\nprint \"pScore is: \" , pScore # address stored in pointer\nprint \"score is: \" , score\nprint \"pScore is: \" , pScore # value pointed to by pointer\nprint \"Adding 500 to score\";\nscore += 500;\nprint \"score is: \" , score \nprint \"*pScore is: \" , pScore \nprint \"Adding 500 to *pScore\\n\";\npScore += 500;\nprint \"score is: \" , score \nprint \"*pScore is: \" , pScore\nprint \"Assigning &newScore to pScore\\n\";\nnewScore = 5000;\npScore = newScore;\nprint \"&newScore is: \" , id(newScore)\nprint \"pScore is: \" , pScore\nprint \"newScore is: \" , newScore\nprint \"*pScore is: \" , pScore\n\nprint \"Assigning &str to pStr\\n\";\nstr = \"score\";\npStr = str; \nprint \"str is: \" , str\nprint \"*pStr is: \" , pStr\nprint \"(*pStr).size() is: \" , len(pStr)\nprint \"pStr->size() is: \" , len(pStr)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Assigning &score to pScore\n\n&score is: 24878128\npScore is: 1000\nscore is: 1000\npScore is: 1000\nAdding 500 to score\nscore is: 1500\n*pScore is: 1000\nAdding 500 to *pScore\n\nscore is: 1500\n*pScore is: 1500\nAssigning &newScore to pScore\n\n&newScore is: 24880648\npScore is: 5000\nnewScore is: 5000\n*pScore is: 5000\nAssigning &str to pStr\n\nstr is: score\n*pStr is: score\n(*pStr).size() is: 5\npStr->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// Swap Pointer\n// Demonstrates passing constant pointers to alter argument variables\n'''\n\ndef badSwap(x,y):\n x,y = y,x\n \ndef goodSwap(x,y):\n x[0],y[0] = y[0],x[0]\n\n\nmyScore = [150]\nyourScore = [1000]\nprint \"Original values\";\nprint \"myScore: \" , myScore[0]\nprint \"yourScore: \" , yourScore[0]\nprint \"Calling badSwap()\";\nbadSwap(myScore[0], yourScore[0]);\nprint \"myScore: \" , myScore[0] \nprint \"yourScore: \" , yourScore[0]\nprint \"Calling goodSwap()\\n\";\ngoodSwap(myScore,yourScore);\nprint \"myScore: \" , myScore[0] \nprint \"yourScore: \" , yourScore[0]", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Original values\nmyScore: 150\nyourScore: 1000\nCalling badSwap()\nmyScore: 150\nyourScore: 1000\nCalling goodSwap()\n\nmyScore: 1000\nyourScore: 150\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "example : 7.3 page no : 239" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nInventory Pointer\nDemonstrates returning a pointer\n'''\ndef ptrToElement(pVec, i):\n return pVec[i]\n\ninventory = []\ninventory.append(\"sword\");\ninventory.append(\"armor\");\ninventory.append(\"shield\");\n#displays string object that the returned pointer points to\nprint \"Sending the objected pointed to by returned pointer:\";\nprint ptrToElement(inventory, 0)\n# assigns one pointer to another - - inexpensive assignment\nprint \"Assigning the returned pointer to another pointer.\";\npStr = ptrToElement(inventory, 1);\nprint \"Sending the object pointed to by new pointer to cout:\";\nprint pStr \n#copies a string object - - expensive assignment\nprint \"Assigning object pointed by pointer to a string object.\\n\";\ns = (ptrToElement(inventory, 2));\nprint \"Sending the new string object to cout:\";\nprint s \n#altering the string object through a returned pointer\nprint \"Altering an object through a returned pointer.\";\npStr = \"Healing Potion\";\ninventory[1] = pStr\nprint \"Sending the altered object to cout:\\n\";\nprint inventory[1] ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Sending the objected pointed to by returned pointer:\nsword\nAssigning the returned pointer to another pointer.\nSending the object pointed to by new pointer to cout:\narmor\nAssigning object pointed by pointer to a string object.\n\nSending the new string object to cout:\nshield\nAltering an object through a returned pointer.\nSending the altered object to cout:\n\nHealing 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//Array Passer\n//Demonstrates relationship between pointers and arrays\n'''\ndef increase(array,NUM_ELEMENTS):\n for i in range(NUM_ELEMENTS):\n array[i] += 500;\n\ndef display( array, NUM_ELEMENTS):\n for i in range(NUM_ELEMENTS):\n print array[i]\n\n\nprint \"Creating an array of high scores.\\n\\n\";\nNUM_SCORES = 3;\nhighScores = [5000, 3500, 2700]\nprint \"Displaying scores using array name as a constant pointer.\";\nprint highScores[0]\nprint highScores[1]\nprint highScores[2]\nprint \"Increasing scores by passing array as a constant pointer.\";\nincrease(highScores, NUM_SCORES)\nprint \"Displaying scores by passing array as a constant pointer to a constant.\";\ndisplay(highScores, NUM_SCORES);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Creating an array of high scores.\n\n\nDisplaying scores using array name as a constant pointer.\n5000\n3500\n2700\nIncreasing scores by passing array as a constant pointer.\nDisplaying scores by passing array as a constant pointer to a constant.\n5500\n4000\n3200\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "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..1b140b06 --- /dev/null +++ b/Beginning_C++_through_game_programming/ch8.ipynb @@ -0,0 +1,484 @@ +{ + "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", + "Simple Critter\n", + "Demonstrates creating a new type\n", + "'''\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", + "Constructor Critter\n", + "Demonstrates constructors\n", + "'''\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", + "Private Critter\n", + "Demonstrates setting member access levels\n", + "'''\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", + "//Static Critter\n", + "//Demonstrates static member variables and functions\n", + "'''\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", + "//Critter Caretaker\n", + "//Simulates caring for a virtual pet\n", + "Note : Book program has error, PassTime() is taking time integer as argument, but while calling it , no-function is \n", + "passing that time so this would give error.\n", + "'''\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();\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();\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();\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();\n", + " elif choice == 3:\n", + " crit.Play();\n", + " else:\n", + " print \"\\nSorry, but \" , choice , \" isn't a valid choice.\\n\";\n", + "\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "PassTime() takes exactly 2 arguments (1 given)", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 47\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 48\u001b[0m \u001b[0mcrit\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mCritter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 49\u001b[1;33m \u001b[0mcrit\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTalk\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 50\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 51\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"\\nCritter Caretaker\\n\\n\"\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m\u001b[0m in \u001b[0;36mTalk\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 30\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 31\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"happy.\\n\"\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 32\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPassTime\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 33\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 34\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mEat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mfood\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: PassTime() takes exactly 2 arguments (1 given)" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "I'm a critter and I feel \n", + "happy.\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 8.6 page no : 280" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "'''\n", + "Critter Caretaker\n", + "Simulates caring for a virtual pet\n", + "'''\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 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "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..4c67034d --- /dev/null +++ b/Beginning_C++_through_game_programming/ch9.ipynb @@ -0,0 +1,469 @@ +{ + "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", + "//Critter Farm\n", + "//Demonstrates object containment\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", + "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", + "//Friend Critter\n", + "//Demonstrates friend functions and operator overloading\n", + "'''\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", + "// Heap\n", + "// Demonstrates dynamically allocating memory\n", + "'''\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", + "//Heap Data Member\n", + "//Demonstrates an object with a dynamically allocated data member\n", + "'''\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", + "defining player and lobby classes\n", + "'''\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 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "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 new file mode 100755 index 00000000..44527cdd Binary files /dev/null and b/Beginning_C++_through_game_programming/screenshots/chapter1.png differ diff --git a/Beginning_C++_through_game_programming/screenshots/chapter4.png b/Beginning_C++_through_game_programming/screenshots/chapter4.png new file mode 100755 index 00000000..eb1b6ea5 Binary files /dev/null and b/Beginning_C++_through_game_programming/screenshots/chapter4.png differ diff --git a/Beginning_C++_through_game_programming/screenshots/chapter8.png b/Beginning_C++_through_game_programming/screenshots/chapter8.png new file mode 100755 index 00000000..1032e338 Binary files /dev/null and b/Beginning_C++_through_game_programming/screenshots/chapter8.png differ -- cgit