From 34887da4e2731004f7cf208ae59b72f2e27b33cf Mon Sep 17 00:00:00 2001 From: hardythe1 Date: Wed, 6 Aug 2014 16:41:00 +0530 Subject: adding books --- Beginning_C++_Through_Game_Programming/ch4.ipynb | 614 +++++++++++++++++++++++ 1 file changed, 614 insertions(+) create mode 100755 Beginning_C++_Through_Game_Programming/ch4.ipynb (limited to 'Beginning_C++_Through_Game_Programming/ch4.ipynb') diff --git a/Beginning_C++_Through_Game_Programming/ch4.ipynb b/Beginning_C++_Through_Game_Programming/ch4.ipynb new file mode 100755 index 00000000..df9c13b0 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch4.ipynb @@ -0,0 +1,614 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4 : The Standard Template Library: Hangman" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 4.1 page no : 118" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "inventory = []\n", + "inventory.append(\"sword\");\n", + "inventory.append(\"armor\");\n", + "inventory.append(\"shield\");\n", + "print \"You have \" , len(inventory) , \" items.\";\n", + "print \"\\nYour items:\";\n", + "for i in inventory:\n", + " print i\n", + "\n", + "print \"\\nYou trade your sword for a battle axe.\";\n", + "inventory[0] = \"battle axe\";\n", + "print \"\\nYour items:\\n\";\n", + "for i in inventory:\n", + " print i\n", + "\n", + "print \"\\nThe item name '\" , inventory[0] , \"' has \",\n", + "print len(inventory[0]) , \" letters in it.\";\n", + "print \"\\nYour shield is destroyed in a fierce battle.\";\n", + "inventory.pop();\n", + "print \"\\nYour items:\";\n", + "for i in inventory:\n", + " print i\n", + "\n", + "print \"\\nYou were robbed of all of your possessions by a thief.\";\n", + "inventory = []\n", + "if (not inventory):\n", + " print \"\\nYou have nothing.\";\n", + "else:\n", + " print \"\\nYou have at least one item.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have 3 items.\n", + "\n", + "Your items:\n", + "sword\n", + "armor\n", + "shield\n", + "\n", + "You trade your sword for a battle axe.\n", + "\n", + "Your items:\n", + "\n", + "battle axe\n", + "armor\n", + "shield\n", + "\n", + "The item name ' battle axe ' has 10 letters in it.\n", + "\n", + "Your shield is destroyed in a fierce battle.\n", + "\n", + "Your items:\n", + "battle axe\n", + "armor\n", + "\n", + "You were robbed of all of your possessions by a thief.\n", + "\n", + "You have nothing.\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 4.2 page no : 124" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "inventory = []\n", + "inventory.append(\"sword\");\n", + "inventory.append(\"armor\");\n", + "inventory.append(\"shield\");\n", + "print \"You have \" , len(inventory) , \" items.\";\n", + "print \"\\nYour items:\";\n", + "for i in inventory:\n", + " print i\n", + "\n", + "print \"\\nYou trade your sword for a battle axe.\";\n", + "inventory[0] = \"battle axe\";\n", + "print \"\\nYour items:\\n\";\n", + "for i in inventory:\n", + " print i\n", + "\n", + "print \"\\nThe item name '\" , inventory[0] , \"' has \",\n", + "print len(inventory[0]) , \" letters in it.\";\n", + "\n", + "print \"You recover a crossbow from a slain enemy.\";\n", + "inventory.insert(0,\"crossbow\");\n", + "print \"Your items:\";\n", + "for i in inventory:\n", + " print i\n", + " \n", + "print \"\\nYour shield is destroyed in a fierce battle.\";\n", + "inventory.pop();\n", + "print \"\\nYour items:\";\n", + "for i in inventory:\n", + " print i" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You have 3 items.\n", + "\n", + "Your items:\n", + "sword\n", + "armor\n", + "shield\n", + "\n", + "You trade your sword for a battle axe.\n", + "\n", + "Your items:\n", + "\n", + "battle axe\n", + "armor\n", + "shield\n", + "\n", + "The item name ' battle axe ' has 10 letters in it.\n", + "You recover a crossbow from a slain enemy.\n", + "Your items:\n", + "crossbow\n", + "battle axe\n", + "armor\n", + "shield\n", + "\n", + "Your shield is destroyed in a fierce battle.\n", + "\n", + "Your items:\n", + "crossbow\n", + "battle axe\n", + "armor\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 4.3 page no : 132" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "print \"Creating a list of scores.\";\n", + "scores = []\n", + "scores.append(1500);\n", + "scores.append(3500);\n", + "scores.append(7500);\n", + "print \"\\nHigh Scores:\";\n", + "for i in scores:\n", + " print i\n", + "\n", + "print \"\\nFinding a score.\";\n", + "\n", + "print \"\\nEnter a score to find: \";\n", + "score = 3500 #int(raw_input())\n", + "iter = score in scores\n", + "if (iter):\n", + " print \"Score found.\";\n", + "else:\n", + " print \"Score not found.\";\n", + "\n", + "print \"\\nRandomizing scores.\";\n", + "import random\n", + "for i in range(len(scores)):\n", + " scores[i] = int(random.random() * 10000) \n", + "\n", + "print \"\\nHigh Scores:\\n\";\n", + "for i in scores:\n", + " print i\n", + " \n", + "print \"\\nSorting scores.\";\n", + "scores.sort()\n", + "print \"\\nHigh Scores:\\n\";\n", + "for i in scores:\n", + " print i" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Creating a list of scores.\n", + "\n", + "High Scores:\n", + "1500\n", + "3500\n", + "7500\n", + "\n", + "Finding a score.\n", + "\n", + "Enter a score to find: \n", + "Score found.\n", + "\n", + "Randomizing scores.\n", + "\n", + "High Scores:\n", + "\n", + "6658\n", + "5396\n", + "8522\n", + "\n", + "Sorting scores.\n", + "\n", + "High Scores:\n", + "\n", + "5396\n", + "6658\n", + "8522\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 4.4 page no: 142" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "#setup\n", + "MAX_WRONG = 8;\n", + "#maximum number of incorrect guesses allowed\n", + "words = [] # collection of possible words to guess\n", + "words.append(\"GUESS\");\n", + "words.append(\"HANGMAN\");\n", + "words.append(\"DIFFICULT\");\n", + "THE_WORD = words[0];\n", + "wrong = 0;\n", + "soFar = []\n", + "for i in range(len(THE_WORD)):\n", + " soFar.append('-')\n", + "\n", + "used = \"\";\n", + "\n", + "print \"Welcome to Hangman. Good luck!\\n\";\n", + "# main loop\n", + "while ((wrong < MAX_WRONG) and (''.join(soFar) != THE_WORD)):\n", + " print \"\\n\\nYou have \" , (MAX_WRONG - wrong);\n", + " print \" incorrect guesses left.\";\n", + " print \"\\nYou've used the following letters:\\n\" , used \n", + " print \"\\nSo far, the word is:\" , soFar\n", + " print \"\\nEnter your guess: \";\n", + " guess = raw_input()\n", + " guess = guess.upper() #make uppercase since secret word in uppercase\n", + " while guess in used:\n", + " print \"\\nYou've already guessed \" , guess \n", + " print \"Enter your guess: \";\n", + " guess = 'GUESS' #raw_input()\n", + " guess = guess.upper()\n", + " used += guess;\n", + " \n", + " if (guess in THE_WORD):\n", + " print \"That's right! \" , guess , \" is in the word.\";\n", + " #update soFar to include newly guessed letter\n", + " for i in range(len(THE_WORD)):\n", + " if (THE_WORD[i] == guess):\n", + " soFar[i] = guess\n", + " else:\n", + " print \"Sorry, \" , guess , \" isn't in the word.\";\n", + " wrong += 1\n", + "#shut down\n", + "if (wrong == MAX_WRONG):\n", + " print \"\\nYou've been hanged!\";\n", + "else:\n", + " print \"\\nYou guessed it!\";\n", + "print \"\\nThe word was \" , THE_WORD " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Welcome to Hangman. Good luck!\n", + "\n", + "\n", + "\n", + "You have 8\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "\n", + "\n", + "So far, the word is: ['-', '-', '-', '-', '-']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "e\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "That's right! E is in the word.\n", + "\n", + "\n", + "You have 8\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "E\n", + "\n", + "So far, the word is: ['-', '-', 'E', '-', '-']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "ae\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, AE isn't in the word.\n", + "\n", + "\n", + "You have 7\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAE\n", + "\n", + "So far, the word is: ['-', '-', 'E', '-', '-']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "w\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, W isn't in the word.\n", + "\n", + "\n", + "You have 6\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEW\n", + "\n", + "So far, the word is: ['-', '-', 'E', '-', '-']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "wae\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, WAE isn't in the word.\n", + "\n", + "\n", + "You have 5\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAE\n", + "\n", + "So far, the word is: ['-', '-', 'E', '-', '-']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "s\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "That's right! S is in the word.\n", + "\n", + "\n", + "You have 5\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAES\n", + "\n", + "So far, the word is: ['-', '-', 'E', 'S', 'S']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "ews\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, EWS isn't in the word.\n", + "\n", + "\n", + "You have 4\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAESEWS\n", + "\n", + "So far, the word is: ['-', '-', 'E', 'S', 'S']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "h\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, H isn't in the word.\n", + "\n", + "\n", + "You have 3\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAESEWSH\n", + "\n", + "So far, the word is: ['-', '-', 'E', 'S', 'S']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "l\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Sorry, L isn't in the word.\n", + "\n", + "\n", + "You have 2\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAESEWSHL\n", + "\n", + "So far, the word is: ['-', '-', 'E', 'S', 'S']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "g\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "That's right! G is in the word.\n", + "\n", + "\n", + "You have 2\n", + " incorrect guesses left.\n", + "\n", + "You've used the following letters:\n", + "EAEWWAESEWSHLG\n", + "\n", + "So far, the word is: ['G', '-', 'E', 'S', 'S']\n", + "\n", + "Enter your guess: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "u\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "That's right! U is in the word.\n", + "\n", + "You guessed it!\n", + "\n", + "The word was GUESS\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file -- cgit