diff options
Diffstat (limited to 'Beginning_C++_Through_Game_Programming/ch6.ipynb')
-rwxr-xr-x | Beginning_C++_Through_Game_Programming/ch6.ipynb | 616 |
1 files changed, 616 insertions, 0 deletions
diff --git a/Beginning_C++_Through_Game_Programming/ch6.ipynb b/Beginning_C++_Through_Game_Programming/ch6.ipynb new file mode 100755 index 00000000..65c2377e --- /dev/null +++ b/Beginning_C++_Through_Game_Programming/ch6.ipynb @@ -0,0 +1,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": {} + } + ] +}
\ No newline at end of file |