summaryrefslogtreecommitdiff
path: root/Beginning_C++_Through_Game_Programming/ch6.ipynb
blob: 65c2377ea7545785c4885c6c4c02461fab2415be (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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example 6.1 page no : 188"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#Note : python does not have reference variable.\n",
      "\n",
      "myScore = 1000;\n",
      "mikesScore = myScore; #create a reference\n",
      "print \"myScore is: \" , myScore\n",
      "print \"mikesScore is: \" , mikesScore \n",
      "print \"Adding 500 to myScore\\n\";\n",
      "myScore += 500;\n",
      "print \"myScore is: \" , myScore\n",
      "print \"mikesScore is: \" , mikesScore\n",
      "print \"Adding 500 to mikesScore\";\n",
      "mikesScore += 500;\n",
      "print \"myScore is: \" , myScore \n",
      "print \"mikesScore is: \" , mikesScore"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "myScore is:  1000\n",
        "mikesScore is:  1000\n",
        "Adding 500 to myScore\n",
        "\n",
        "myScore is:  1500\n",
        "mikesScore is:  1000\n",
        "Adding 500 to mikesScore\n",
        "myScore is:  1500\n",
        "mikesScore is:  1500\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example 6.2 page no : 192"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def badSwap(x,y):\n",
      "    x,y = y,x\n",
      "\n",
      "def goodSwap(x, y):\n",
      "    x[0],y[0] = y[0],x[0]\n",
      "\n",
      "myScore = [1500];\n",
      "yourScore = [1000];\n",
      "print \"Original values\";\n",
      "print \"myScore: \" , myScore[0] \n",
      "print \"yourScore: \" , yourScore[0]\n",
      "print \"Calling badSwap()\\n\";\n",
      "badSwap(myScore[0], yourScore[0]);\n",
      "print \"myScore: \" , myScore[0] \n",
      "print \"yourScore: \" , yourScore[0]\n",
      "print \"Calling goodSwap()\\n\";\n",
      "goodSwap(myScore, yourScore);\n",
      "print \"myScore: \" , myScore[0] \n",
      "print \"yourScore: \" , yourScore[0] ,"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Original values\n",
        "myScore:  1500\n",
        "yourScore:  1000\n",
        "Calling badSwap()\n",
        "\n",
        "myScore:  1500\n",
        "yourScore:  1000\n",
        "Calling goodSwap()\n",
        "\n",
        "myScore:  1000\n",
        "yourScore:  1500\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example 6.3 page no : 196"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "\n",
      "def display(vec):\n",
      "    print \"Your items:\";\n",
      "    for i in vec:\n",
      "        print i\n",
      "\n",
      "inventory = []\n",
      "inventory.append(\"sword\");\n",
      "inventory.append(\"armor\");\n",
      "inventory.append(\"shield\");\n",
      "display(inventory);"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Your items:\n",
        "sword\n",
        "armor\n",
        "shield\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example : 6.4 page no : 199"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "#returns a reference to a string\n",
      "\n",
      "def refToElement(vec,i):\n",
      "    return vec[i]\n",
      "\n",
      "inventory = []\n",
      "inventory.append(\"sword\");\n",
      "inventory.append(\"armor\");\n",
      "inventory.append(\"shield\");\n",
      "\n",
      "#displays string that the returned reference refers to\n",
      "print \"Sending the returned reference to cout:\"\n",
      "print refToElement(inventory, 0) \n",
      "#assigns one reference to another -- inexpensive assignment\n",
      "print \"Assigning the returned reference to another reference.\";\n",
      "rStr = refToElement(inventory, 1);\n",
      "print \"Sending the new reference to cout:\";\n",
      "print rStr\n",
      "#copies a string object -- expensive assignment\n",
      "print \"Assigning the returned reference to a string object.\";\n",
      "s = refToElement(inventory, 2);\n",
      "print \"Sending the new string object to cout:\";\n",
      "print s\n",
      "#altering the string object through a returned reference\n",
      "print \"Altering an object through a returned reference.\";\n",
      "rStr = \"Healing Potion\";\n",
      "print \"Sending the altered object to cout:\";\n",
      "print inventory[1] "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Sending the returned reference to cout:\n",
        "sword\n",
        "Assigning the returned reference to another reference.\n",
        "Sending the new reference to cout:\n",
        "armor\n",
        "Assigning the returned reference to a string object.\n",
        "Sending the new string object to cout:\n",
        "shield\n",
        "Altering an object through a returned reference.\n",
        "Sending the altered object to cout:\n",
        "armor\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "example 6.5 page no : 205"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# global constants\n",
      "X = 'X'\n",
      "O = 'O'\n",
      "EMPTY = ' ';\n",
      "TIE = 'T';\n",
      "NO_ONE = 'N';\n",
      "\n",
      "def instructions():\n",
      "    print \"Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\";\n",
      "    print \"--where human brain is pit against silicon processor\";\n",
      "    print \"Make your move known by entering a number, 0 - 8. The number\";\n",
      "    print \"corresponds to the desired board position, as illustrated:\";\n",
      "    print \"0 | 1 | 2\";\n",
      "    print \"---------\";\n",
      "    print \"3 | 4 | 5\\n\";   \n",
      "    print \"---------\";\n",
      "    print \"6 | 7 | 8\\n\\n\";\n",
      "    print \"Prepare yourself, human. The battle is about to begin.\";\n",
      "\n",
      "def askYesNo(question):\n",
      "    response =''\n",
      "    while (response != 'y' and response != 'n'):\n",
      "        print question , \" (y/n): \",\n",
      "        response = raw_input()\n",
      "    return response;\n",
      "\n",
      "def askNumber( question, high, low):\n",
      "    while True:\n",
      "        print question , \" (\" , low , \" - \" , high , \"): \",\n",
      "        number = int(raw_input())\n",
      "        if (number > high or number < low):\n",
      "            pass\n",
      "        else:\n",
      "            break\n",
      "    return number;\n",
      "\n",
      "def humanPiece():\n",
      "    global X,O,EMPTY,TIE,NO_ONE\n",
      "    go_first = askYesNo(\"Do you require the first move?\");\n",
      "    if (go_first == 'y'):\n",
      "        print \"Then take the first move. You will need it.\"\n",
      "        return X;\n",
      "    else:\n",
      "        print \"Your bravery will be your undoing. . . I will go first.\";\n",
      "        return O;\n",
      "\n",
      "def opponent( piece):\n",
      "    global X,O,EMPTY,TIE,NO_ONE\n",
      "    if (piece == X):\n",
      "        return O;\n",
      "    else:\n",
      "        return X;\n",
      "\n",
      "def displayBoard( board):\n",
      "    print \"\\n\\t\" , board[0] , \" | \" , board[1] , \" | \" , board[2];\n",
      "    print \"\\t\" , \"---------\";\n",
      "    print \"\\t\" , board[3] , \" | \" , board[4] , \" | \" , board[5];\n",
      "    print \"\\t\" , \"---------\";\n",
      "    print \"\\t\" , board[6] , \" | \" , board[7] , \" | \" , board[8];\n",
      "    print \"\\n\";\n",
      "\n",
      "def winner(board):\n",
      "    global X,O,EMPTY,TIE,NO_ONE\n",
      "    # all possible winning rows\n",
      "    WINNING_ROWS = [ [0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]\n",
      "    TOTAL_ROWS = 8;\n",
      "    # if any winning row has three values that are the same (and not EMPTY),\n",
      "    # then we have a winner\n",
      "    for row in range (TOTAL_ROWS):\n",
      "        if ( (board[WINNING_ROWS[row][0]] != EMPTY) and\n",
      "            (board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) and\n",
      "            (board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) ):\n",
      "            return board[WINNING_ROWS[row][0]];\n",
      "    # since nobody has won, check for a tie (no empty squares left)\n",
      "    if EMPTY not in board:\n",
      "        return TIE;\n",
      "    return NO_ONE;\n",
      "\n",
      "def isLegal(move,board):\n",
      "    global X,O,EMPTY,TIE,NO_ONE\n",
      "    return (board[move] == EMPTY);\n",
      "\n",
      "def humanMove(board,human):\n",
      "    global X,O,EMPTY,TIE,NO_ONE\n",
      "    move = askNumber(\"Where will you move?\", len(board),0);\n",
      "    \n",
      "    while (not isLegal(move, board)):\n",
      "        print \"\\nThat square is already occupied, foolish human.\\n\";\n",
      "        move = askNumber(\"Where will you move?\", len(board),0);\n",
      "            \n",
      "    print \"Fine. . .\\n\";\n",
      "    return move;\n",
      "\n",
      "def computerMove(board, computer):\n",
      "    global X,O,EMPTY,TIE,NO_ONE\n",
      "    move = 0;\n",
      "    found = False;\n",
      "    #if computer can win on next move, that's the move to make\n",
      "    while (not found and move < len(board)):\n",
      "        if (isLegal(move, board)):\n",
      "            board[move] = computer;\n",
      "            found = winner(board) == computer;\n",
      "            board[move] = EMPTY;\n",
      "        if (not found):\n",
      "            move += 1\n",
      "    #otherwise, if human can win on next move, that's the move to make\n",
      "    if (not found):\n",
      "        move = 0;\n",
      "        human = opponent(computer);\n",
      "        while (not found and move < len(board)):\n",
      "            if (isLegal(move, board)):\n",
      "                board[move] = human;\n",
      "                found = winner(board) == human;\n",
      "                board[move] = EMPTY;\n",
      "            if (not found):\n",
      "                move += 1\n",
      "    #otherwise, moving to the best open square is the move to make\n",
      "    if (not found):\n",
      "        move = 0;\n",
      "        i = 0;\n",
      "        BEST_MOVES = [4, 0, 2, 6, 8, 1, 3, 5, 7] #pick best open square\n",
      "        while (not found and i < len(board)):\n",
      "            move = BEST_MOVES[i];\n",
      "            if (isLegal(move, board)):\n",
      "                found = True;\n",
      "            i += 1\n",
      "    print \"I shall take square number \" , move \n",
      "    return move;\n",
      "\n",
      "def announceWinner( winner, computer, human):\n",
      "    if (winner == computer):\n",
      "        print winner , \"'s won!\";\n",
      "        print \"As I predicted, human, I am triumphant once more -- proof\";\n",
      "        print \"that computers are superior to humans in all regards.\";\n",
      "    elif (winner == human):\n",
      "        print winner , \"'s won!\";\n",
      "        print \"No, no! It cannot be! Somehow you tricked me, human.\";\n",
      "        print \"But never again! I, the computer, so swear it!\";\n",
      "    else:\n",
      "        print \"It's a tie.\\n\";\n",
      "        print \"You were most lucky, human, and somehow managed to tie me.\\n\";\n",
      "        print \"Celebrate. . . for this is the best you will ever achieve.\\n\";\n",
      "\n",
      "\n",
      "NUM_SQUARES = 9;\n",
      "board = [EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY]\n",
      "instructions();\n",
      "human = humanPiece();\n",
      "computer = opponent(human);\n",
      "turn = X;\n",
      "displayBoard(board);\n",
      "while (winner(board) == NO_ONE):\n",
      "    if (turn == human):\n",
      "        move = humanMove(board, human);\n",
      "        board[move] = human;\n",
      "    else:\n",
      "        move = computerMove(board, computer);\n",
      "        board[move] = computer;\n",
      "    displayBoard(board);\n",
      "    turn = opponent(turn);\n",
      "\n",
      "announceWinner(winner(board), computer, human);\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\n",
        "--where human brain is pit against silicon processor\n",
        "Make your move known by entering a number, 0 - 8. The number\n",
        "corresponds to the desired board position, as illustrated:\n",
        "0 | 1 | 2\n",
        "---------\n",
        "3 | 4 | 5\n",
        "\n",
        "---------\n",
        "6 | 7 | 8\n",
        "\n",
        "\n",
        "Prepare yourself, human. The battle is about to begin.\n",
        "Do you require the first move?  (y/n): "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "y\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Then take the first move. You will need it.\n",
        "\n",
        "\t   |     |   \n",
        "\t---------\n",
        "\t   |     |   \n",
        "\t---------\n",
        "\t   |     |   \n",
        "\n",
        "\n",
        "Where will you move?  ( 0  -  9 ): "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "0\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Fine. . .\n",
        "\n",
        "\n",
        "\tX  |     |   \n",
        "\t---------\n",
        "\t   |     |   \n",
        "\t---------\n",
        "\t   |     |   \n",
        "\n",
        "\n",
        "I shall take square number  4\n",
        "\n",
        "\tX  |     |   \n",
        "\t---------\n",
        "\t   |  O  |   \n",
        "\t---------\n",
        "\t   |     |   \n",
        "\n",
        "\n",
        "Where will you move?  ( 0  -  9 ): "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "2\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Fine. . .\n",
        "\n",
        "\n",
        "\tX  |     |  X\n",
        "\t---------\n",
        "\t   |  O  |   \n",
        "\t---------\n",
        "\t   |     |   \n",
        "\n",
        "\n",
        "I shall take square number  1\n",
        "\n",
        "\tX  |  O  |  X\n",
        "\t---------\n",
        "\t   |  O  |   \n",
        "\t---------\n",
        "\t   |     |   \n",
        "\n",
        "\n",
        "Where will you move?  ( 0  -  9 ): "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "7\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Fine. . .\n",
        "\n",
        "\n",
        "\tX  |  O  |  X\n",
        "\t---------\n",
        "\t   |  O  |   \n",
        "\t---------\n",
        "\t   |  X  |   \n",
        "\n",
        "\n",
        "I shall take square number  6\n",
        "\n",
        "\tX  |  O  |  X\n",
        "\t---------\n",
        "\t   |  O  |   \n",
        "\t---------\n",
        "\tO  |  X  |   \n",
        "\n",
        "\n",
        "Where will you move?  ( 0  -  9 ): "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "5\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Fine. . .\n",
        "\n",
        "\n",
        "\tX  |  O  |  X\n",
        "\t---------\n",
        "\t   |  O  |  X\n",
        "\t---------\n",
        "\tO  |  X  |   \n",
        "\n",
        "\n",
        "I shall take square number  8\n",
        "\n",
        "\tX  |  O  |  X\n",
        "\t---------\n",
        "\t   |  O  |  X\n",
        "\t---------\n",
        "\tO  |  X  |  O\n",
        "\n",
        "\n",
        "Where will you move?  ( 0  -  9 ): "
       ]
      },
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "3\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        " Fine. . .\n",
        "\n",
        "\n",
        "\tX  |  O  |  X\n",
        "\t---------\n",
        "\tX  |  O  |  X\n",
        "\t---------\n",
        "\tO  |  X  |  O\n",
        "\n",
        "\n",
        "It's a tie.\n",
        "\n",
        "You were most lucky, human, and somehow managed to tie me.\n",
        "\n",
        "Celebrate. . . for this is the best you will ever achieve.\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 2
    }
   ],
   "metadata": {}
  }
 ]
}