diff options
Diffstat (limited to 'Beginning_C++_Through_Game_Programming/ch2.ipynb')
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch2.ipynb | 518 |
1 files changed, 518 insertions, 0 deletions
diff --git a/Beginning_C++_Through_Game_Programming/ch2.ipynb b/Beginning_C++_Through_Game_Programming/ch2.ipynb new file mode 100755 index 00000000..9b8f2db2 --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch2.ipynb @@ -0,0 +1,518 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 2 : Truth, Branching, and the Game Loop: Guess My Number" + ] + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 2.1 page no : 41" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "if (True):\n", + " print \"This is always displayed.\\n\\n\";\n", + "\n", + "if (False):\n", + " print \"This is never displayed.\\n\\n\";\n", + "score = 1000;\n", + "if (score):\n", + " print \"At least you didn't score zero.\\n\\n\";\n", + "\n", + "if (score >= 250):\n", + " print \"You scored 250 or more. Decent.\\n\\n\";\n", + "\n", + "if (score >= 500):\n", + " print \"You scored 500 or more. Nice.\\n\\n\";\n", + "if (score >= 1000):\n", + " print \"You scored 1000 or more. Impressive!\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is always displayed.\n", + "\n", + "\n", + "At least you didn't score zero.\n", + "\n", + "\n", + "You scored 250 or more. Decent.\n", + "\n", + "\n", + "You scored 500 or more. Nice.\n", + "\n", + "\n", + "You scored 1000 or more. Impressive!\n", + "\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 2.2 page no :46" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"Enter your score: \";\n", + "score = 1500 #int(raw_input())\n", + "if (score >= 1000):\n", + " print \"You scored 1000 or more. Impressive!\\n\";\n", + "else:\n", + " print \"You scored less than 1000.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your score: \n", + "You scored 1000 or more. Impressive!\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "Example 2.3 page no : 49" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "\n", + "print \"Enter your score: \";\n", + "score = 1500 #int(raw_input())\n", + "\n", + "if (score >= 1000):\n", + " print \"You scored 1000 or more. Impressive!\\n\";\n", + "elif (score >= 500):\n", + " print \"You scored 500 or more. Nice.\\n\";\n", + "elif (score >= 250):\n", + " print \"You scored 250 or more. Decent.\\n\";\n", + "else:\n", + " print \"You scored less than 250. Nothing to brag about.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your score: \n", + "You scored 1000 or more. Impressive!\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.4 page no : 53" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"Difficulty Levels\\n\\n\";\n", + "print \"1 - Easy\\n\";\n", + "print \"2 - Normal\\n\";\n", + "print \"3 - Hard\\n\\n\";\n", + "\n", + "print \"Choice: \";\n", + "choice = 2 #int(raw_input())\n", + "if choice==1:\n", + " print \"You picked Easy.\\n\";\n", + "elif choice==2: \n", + " print \"You picked Normal.\\n\";\n", + "elif choice==3:\n", + " \"You picked Hard.\\n\";\n", + "else: \n", + " \"You made an illegal choice.\\n\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Difficulty Levels\n", + "\n", + "\n", + "1 - Easy\n", + "\n", + "2 - Normal\n", + "\n", + "3 - Hard\n", + "\n", + "\n", + "Choice: \n", + "You picked Normal.\n", + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.5 page no : 55" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "choices = ['y','y','n']\n", + "again = 'y'\n", + "i = 0\n", + "while (again == 'y'):\n", + " print \"\\n**Played an exciting game**\";\n", + " print \"\\nDo you want to play again? (y/n): \",\n", + " again = choices[i] #raw_input()\n", + " i += 1\n", + "\n", + "print \"\\nOkay, bye.\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "Okay, bye.\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.6 page no : 57" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "choices = ['y','y','n']\n", + "i = 0\n", + "again = 'y'\n", + "while (again == 'y'):\n", + " print \"\\n**Played an exciting game**\";\n", + " print \"\\nDo you want to play again? (y/n): \",\n", + " again = choices[i] #raw_input()\n", + " i += 1\n", + "\n", + "print \"\\nOkay, bye.\";" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "**Played an exciting game**\n", + "\n", + "Do you want to play again? (y/n): \n", + "Okay, bye.\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.7 page no :59" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "count = 0;\n", + "while (True):\n", + " count += 1;\n", + " #end loop if count is greater than 10\n", + " if (count > 10):\n", + " break;\n", + " #skip the number 5\n", + " if (count == 5):\n", + " continue;\n", + " print count" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.8 page no : 63" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "\n", + "print \"\\tGame Designer's Network\\n\";\n", + "success = False\n", + "while not success:\n", + " print \"\\nUsername: \";\n", + " username = 'guest' #raw_input()\n", + " print \"Password: \";\n", + " password = 'guest' #raw_input()\n", + " if (username == \"S.Meier\" and password == \"civilization\"):\n", + " print \"\\nHey, Sid.\";\n", + " success = True;\n", + " elif (username == \"S.Miyamoto\" and password == \"mariobros\"):\n", + " print \"\\nWhat's up, Shigeru?\";\n", + " success = True;\n", + " elif (username == \"W.Wright\" and password == \"thesims\"):\n", + " print \"\\nHow goes it, Will?\";\n", + " success = True;\n", + " elif (username == \"guest\" or password == \"guest\"):\n", + " print \"\\nWelcome, guest.\";\n", + " success = True;\n", + " else:\n", + " print \"\\nYour login failed.\";\n", + " success = False;" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tGame Designer's Network\n", + "\n", + "\n", + "Username: \n", + "Password: \n", + "\n", + "Welcome, guest.\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.9 page no : 68" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import random\n", + "\n", + "randomNumber = random.random()*100;\n", + "#seed random number generator\n", + "#generate random number\n", + "die = int(randomNumber % 6) + 1; # get a number between 1 and 6\n", + "print \"You rolled a \" , die" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You rolled a 4\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 2.10 page no : 74" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\n", + "import random\n", + "secretNumber = int(random.random()*100) % 10 + 1;\n", + "tries = 0;\n", + "#seed random number generator\n", + "# random number between 1 and 100\n", + "print \"\\tWelcome to Guess My Number\\n\\n\";\n", + "guesses = [0,1,2,3,4,5,6,7,8,9,10]\n", + "i = 0\n", + "while True:\n", + " print \"Enter a guess: \";\n", + " guess = guesses[i] #int(raw_input())\n", + " i += 1\n", + " tries += 1\n", + " if (guess > secretNumber):\n", + " print \"Too high!\\n\\n\";\n", + " elif (guess < secretNumber):\n", + " print \"Too low!\\n\\n\";\n", + " else:\n", + " print \"\\nThat's it! You got it in \" , tries , \" guesses!\\n\";\n", + " break" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\tWelcome to Guess My Number\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "Too low!\n", + "\n", + "\n", + "Enter a guess: \n", + "\n", + "That's it! You got it in 7 guesses!\n", + "\n" + ] + } + ], + "prompt_number": 11 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |