summaryrefslogtreecommitdiff
path: root/Beginning_C++_through_game_programming/ch3.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Beginning_C++_through_game_programming/ch3.ipynb')
-rwxr-xr-xBeginning_C++_through_game_programming/ch3.ipynb442
1 files changed, 442 insertions, 0 deletions
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<unsigned int>(time(0)));\n",
+ "choice = (int(random.random() * 10) % NUM_WORDS);\n",
+ "theWord = WORDS[choice][0]; # word to guess\n",
+ "theHint = WORDS[choice][1]; # hint for word\n",
+ "jumble = theWord; # jumbled version of word\n",
+ "length = len(jumble)\n",
+ "for i in range(length):\n",
+ " index1 = (int(random.random() * 10) % length);\n",
+ " index2 = (int(random.random() * 10) % length);\n",
+ " temp = list(jumble)\n",
+ " temp[index1],temp[index2] = temp[index2],temp[index1]\n",
+ " jumble = ''.join(temp)\n",
+ "\n",
+ "print \"\\t\\t\\tWelcome to Word Jumble!\\n\\n\";\n",
+ "print \"Unscramble the letters to make a word.\\n\";\n",
+ "print \"Enter 'hint' for a hint.\\n\";\n",
+ "print \"Enter 'quit' to quit the game.\\n\\n\";\n",
+ "print \"The jumble is: \" , jumble;\n",
+ "\n",
+ "print \"Your guess: \";\n",
+ "guess = raw_input()\n",
+ "\n",
+ "while ((guess != theWord) and (guess != \"quit\")):\n",
+ " if (guess == \"hint\"):\n",
+ " print theHint;\n",
+ " else:\n",
+ " print \"Sorry, that's not it.\";\n",
+ " print \"\\n\\nYour guess: \";\n",
+ " guess = raw_input()\n",
+ "\n",
+ "if (guess == theWord):\n",
+ " print \"\\nThat's it! You guessed it!\\n\";\n",
+ "\n",
+ "print \"\\nThanks for playing.\\n\";\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "There are 3 difficulty levels.\n",
+ "\t\t\tWelcome to Word Jumble!\n",
+ "\n",
+ "\n",
+ "Unscramble the letters to make a word.\n",
+ "\n",
+ "Enter 'hint' for a hint.\n",
+ "\n",
+ "Enter 'quit' to quit the game.\n",
+ "\n",
+ "\n",
+ "The jumble is: gasslse\n",
+ "Your guess: \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "glasses\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "That's it! You guessed it!\n",
+ "\n",
+ "\n",
+ "Thanks for playing.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file