summaryrefslogtreecommitdiff
path: root/Beginning_C++_through_game_programming/ch8.ipynb
blob: 1b140b06b208157f2e8cf59bf270aef767e0247a (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 8 : Classes: Critter Caretaker"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example 8.1 page no : 256"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Simple Critter\n",
      "Demonstrates creating a new type\n",
      "'''\n",
      "\n",
      "class Critter:\n",
      "    def __init__(self):\n",
      "        self.m_Hunger = 0\n",
      "\n",
      "    def Greet(self):\n",
      "        print \"Hi. I'm a critter. My hunger level is \" , self.m_Hunger\n",
      "\n",
      "crit1 = Critter()\n",
      "crit2 = Critter()\n",
      "crit1.m_Hunger = 9;\n",
      "print \"crit1's hunger level is \" , crit1.m_Hunger\n",
      "crit2.m_Hunger = 3;\n",
      "print \"crit2's hunger level is \" , crit2.m_Hunger\n",
      "crit1.Greet()\n",
      "crit2.Greet()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "crit1's hunger level is  9\n",
        "crit2's hunger level is  3\n",
        "Hi. I'm a critter. My hunger level is  9\n",
        "Hi. I'm a critter. My hunger level is  3\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example 8.2  page no: 261"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Constructor Critter\n",
      "Demonstrates constructors\n",
      "'''\n",
      "\n",
      "class Critter:\n",
      "    def __init__(self,h=0):\n",
      "        print  \"A new critter has been born!\" \n",
      "        self.m_Hunger = h\n",
      "\n",
      "    def Greet(self):\n",
      "        print \"Hi. I'm a critter. My hunger level is \" , self.m_Hunger\n",
      "\n",
      "crit = Critter(7);\n",
      "crit.Greet();"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "A new critter has been born!\n",
        "Hi. I'm a critter. My hunger level is  7\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example 8.3 page no : 265"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Private Critter\n",
      "Demonstrates setting member access levels\n",
      "'''\n",
      "\n",
      "class Critter:\n",
      "    def __init__(self,h=0):\n",
      "        print  \"A new critter has been born!\" \n",
      "        self.m_Hunger = h\n",
      "\n",
      "    def Greet(self):\n",
      "        print \"Hi. I'm a critter. My hunger level is \" , self.m_Hunger\n",
      "\n",
      "    def GetHunger(self):\n",
      "        return self.m_Hunger;\n",
      "\n",
      "    def SetHunger(self,hunger):\n",
      "        if (hunger < 0):\n",
      "            print \"You can't set a critter's hunger to a negative number.\";\n",
      "        else:\n",
      "            self.m_Hunger = hunger;\n",
      "\n",
      "\n",
      "crit = Critter(5)\n",
      "#print crit.m_Hunger; //illegal, m_Hunger is private!\n",
      "print \"Calling GetHunger(): \" , crit.GetHunger()\n",
      "print \"Calling SetHunger() with -1.\";\n",
      "crit.SetHunger(-1);\n",
      "print \"Calling SetHunger() with 9.\";\n",
      "crit.SetHunger(9);\n",
      "print \"Calling GetHunger(): \" , crit.GetHunger()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "A new critter has been born!\n",
        "Calling GetHunger():  5\n",
        "Calling SetHunger() with -1.\n",
        "You can't set a critter's hunger to a negative number.\n",
        "Calling SetHunger() with 9.\n",
        "Calling GetHunger():  9\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example 8.4  page no: 270"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "//Static Critter\n",
      "//Demonstrates static member variables and functions\n",
      "'''\n",
      "\n",
      "class Critter:\n",
      "    s_Total = 0\n",
      "    def __init__(self,h=0):\n",
      "        print  \"A new critter has been born!\" \n",
      "        self.m_Hunger = h\n",
      "        Critter.s_Total += 1\n",
      "\n",
      "    def GetTotal(self):\n",
      "        return Critter.s_Total;\n",
      "\n",
      "print \"The total number of critters is: \";\n",
      "print Critter.s_Total\n",
      "crit1 = Critter() \n",
      "crit2 = Critter()\n",
      "crit3 = Critter()\n",
      "print \"\\nThe total number of critters is: \",\n",
      "print Critter.GetTotal(crit1) "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "The total number of critters is: \n",
        "0\n",
        "A new critter has been born!\n",
        "A new critter has been born!\n",
        "A new critter has been born!\n",
        "\n",
        "The total number of critters is:  3\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example 8.5 page no : 277"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "//Critter Caretaker\n",
      "//Simulates caring for a virtual pet\n",
      "Note : Book program has error, PassTime() is taking time integer as argument, but while calling it , no-function is \n",
      "passing that time so this would give error.\n",
      "'''\n",
      "\n",
      "\n",
      "class Critter:\n",
      "    def __init__(self,hunger = 0, boredom = 0):\n",
      "        self.m_Hunger= hunger\n",
      "        self.m_Boredom = boredom\n",
      "\n",
      "    def  GetMood(self):\n",
      "        return (self.m_Hunger + self.m_Boredom);\n",
      "\n",
      "    def PassTime(self,time):\n",
      "        self.m_Hunger += time;\n",
      "        self.m_Boredom += time;\n",
      "\n",
      "    def Talk(self):\n",
      "        print \"I'm a critter and I feel \";\n",
      "        mood = self.GetMood();\n",
      "        if (mood > 15):\n",
      "            print \"mad.\\n\";\n",
      "        elif (mood > 10):\n",
      "            print \"frustrated.\\n\";\n",
      "        elif (mood > 5):\n",
      "            print \"okay.\\n\";\n",
      "        else:\n",
      "            print  \"happy.\\n\";\n",
      "        self.PassTime();\n",
      "\n",
      "    def Eat(self,food):\n",
      "        print  \"Brruppp.\\n\";\n",
      "        self.m_Hunger -= food;\n",
      "        if (self.m_Hunger < 0):\n",
      "            self.m_Hunger = 0;\n",
      "        self.PassTime();\n",
      "\n",
      "    def Play(self,fun):\n",
      "        print \"Wheee!\\n\";\n",
      "        self.m_Boredom -= fun;\n",
      "        if (self.m_Boredom < 0):\n",
      "            self.m_Boredom = 0;\n",
      "        self.PassTime();\n",
      "\n",
      "crit = Critter()\n",
      "crit.Talk();\n",
      "while True:\n",
      "    print \"\\nCritter Caretaker\\n\\n\";\n",
      "    print \"0 - Quit\\n\";\n",
      "    print \"1 -  Listen to your critter\\n\";\n",
      "    print \"2 - Feed your critter\\n\";\n",
      "    print \"3 - Play with your critter\\n\\n\";\n",
      "    print  \"Choice: \";\n",
      "    choice = int(raw_input())\n",
      "    if choice == 0:\n",
      "        print  \"Good-bye.\\n\";\n",
      "        break\n",
      "    elif choice == 1:\n",
      "        crit.Talk();\n",
      "    elif choice == 2:\n",
      "        crit.Eat();\n",
      "    elif choice == 3:\n",
      "        crit.Play();\n",
      "    else:\n",
      "        print  \"\\nSorry, but \" , choice , \" isn't a valid choice.\\n\";\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "TypeError",
       "evalue": "PassTime() takes exactly 2 arguments (1 given)",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-1-6005a1e33d51>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     47\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     48\u001b[0m \u001b[0mcrit\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mCritter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 49\u001b[1;33m \u001b[0mcrit\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTalk\u001b[0m\u001b[1;33m(\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     50\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     51\u001b[0m     \u001b[1;32mprint\u001b[0m \u001b[1;34m\"\\nCritter Caretaker\\n\\n\"\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;32m<ipython-input-1-6005a1e33d51>\u001b[0m in \u001b[0;36mTalk\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     30\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     31\u001b[0m             \u001b[1;32mprint\u001b[0m  \u001b[1;34m\"happy.\\n\"\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 32\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPassTime\u001b[0m\u001b[1;33m(\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     33\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     34\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mEat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mfood\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;31mTypeError\u001b[0m: PassTime() takes exactly 2 arguments (1 given)"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "I'm a critter and I feel \n",
        "happy.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example 8.6 page no : 280"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'''\n",
      "Critter Caretaker\n",
      "Simulates caring for a virtual pet\n",
      "'''\n",
      "\n",
      "\n",
      "class Critter:\n",
      "    def __init__(self,hunger = 0, boredom = 0):\n",
      "        self.m_Hunger= hunger\n",
      "        self.m_Boredom = boredom\n",
      "\n",
      "    def Talk(self):\n",
      "        print \"I'm a critter and I feel \";\n",
      "        mood = self.GetMood();\n",
      "        if (mood > 15):\n",
      "            print \"mad.\\n\";\n",
      "        elif (mood > 10):\n",
      "            print \"frustrated.\\n\";\n",
      "        elif (mood > 5):\n",
      "            print \"okay.\\n\";\n",
      "        else:\n",
      "            print \"happy.\\n\";\n",
      "        self.PassTime();\n",
      "        \n",
      "    def Eat(self,food = 4):\n",
      "        print \"Brruppp.\";\n",
      "        self.m_Hunger -= food;\n",
      "        if (self.m_Hunger < 0):\n",
      "            self.m_Hunger = 0;\n",
      "        self.PassTime();\n",
      "        \n",
      "    def Play(self,fun = 4):\n",
      "        print \"Wheee!\";\n",
      "        self.m_Boredom -= fun;\n",
      "        if (self.m_Boredom < 0):\n",
      "            self.m_Boredom = 0;\n",
      "        self.PassTime();\n",
      "    \n",
      "    def PassTime(self, time = 1):\n",
      "        self.m_Hunger += time;\n",
      "        self.m_Boredom += time;\n",
      "\n",
      "\n",
      "    def GetMood(self):\n",
      "        return (self.m_Hunger + self.m_Boredom);\n",
      "\n",
      "crit = Critter()\n",
      "crit.Talk();\n",
      "choice = 1;\n",
      "choices = [1,2,3,0]\n",
      "i = 0\n",
      "while choice != 0 :\n",
      "    print \"\\nCritter Caretaker\\n\";\n",
      "    print \"0 - Quit\";\n",
      "    print \"1 - Listen to your critter\";\n",
      "    print \"2 - Feed your critter\";\n",
      "    print \"3 - Play with your critter\\n\";\n",
      "\n",
      "    print \"Choice: \",\n",
      "    choice = choices[i] #int(raw_input())\n",
      "    i += 1\n",
      "\n",
      "    if choice ==0:\n",
      "        print \"Good-bye.\\n\";\n",
      "    elif choice==1:\n",
      "        crit.Talk();    \n",
      "    elif choice == 2:\n",
      "        crit.Eat();\n",
      "    elif choice == 3:\n",
      "        crit.Play();\n",
      "    else:\n",
      "        print \"\\nSorry, but \" , choice , \" isn't a valid choice.\";"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " I'm a critter and I feel \n",
        "happy.\n",
        "\n",
        "\n",
        "Critter Caretaker\n",
        "\n",
        "0 - Quit\n",
        "1 - Listen to your critter\n",
        "2 - Feed your critter\n",
        "3 - Play with your critter\n",
        "\n",
        "Choice:  I'm a critter and I feel \n",
        "happy.\n",
        "\n",
        "\n",
        "Critter Caretaker\n",
        "\n",
        "0 - Quit\n",
        "1 - Listen to your critter\n",
        "2 - Feed your critter\n",
        "3 - Play with your critter\n",
        "\n",
        "Choice:  Brruppp.\n",
        "\n",
        "Critter Caretaker\n",
        "\n",
        "0 - Quit\n",
        "1 - Listen to your critter\n",
        "2 - Feed your critter\n",
        "3 - Play with your critter\n",
        "\n",
        "Choice:  Wheee!\n",
        "\n",
        "Critter Caretaker\n",
        "\n",
        "0 - Quit\n",
        "1 - Listen to your critter\n",
        "2 - Feed your critter\n",
        "3 - Play with your critter\n",
        "\n",
        "Choice:  Good-bye.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}