summaryrefslogtreecommitdiff
path: root/Beginning_C++_through_game_programming/ch4.ipynb
blob: 29da32f0fd964d11dc6d082a9714b6ccd1521623 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
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<ipython-input-7-9b09a3413260>\u001b[0m in \u001b[0;36m<module>\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<lambda>\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": {}
  }
 ]
}