summaryrefslogtreecommitdiff
path: root/Beginning_C++_through_game_programming/ch2.ipynb
blob: ff66a26a1b8316d7db5124d000a865bbb1924b83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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": {}
  }
 ]
}