summaryrefslogtreecommitdiff
path: root/Beginning_C++_through_Game_Programming/ch12.ipynb
blob: c78a7ba1f1c3e498c2d7419e2545973dc549792f (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
{
 "metadata": {
  "name": "ch12"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": "Chapter 12 : Vectors of Objects"
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 12.1 page no :124\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 12.1 page no :124\n'''\n\nclass Card:\n    def __init__(self,s=None,r=None):\n        if s==None:\n            self.suit = 0\n            self.rank = 0\n        else:\n            self.suit = s; \n            self.rank = r;\n\nc = Card (0,3)\n",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 12.2 page no : 126\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 12.2 page no : 126\n'''\nclass Card:\n    def __init__(self,s=None,r=None):\n        if s==None:\n            self.suit = 0\n            self.rank = 0\n        else:\n            self.suit = s; \n            self.rank = r;\n    def print_(self):\n        suits = [\"Clubs\",\"Diamonds\",\"Hearts\",\"Spades\"]\n        ranks = [\"Ace\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"Jack\",\"Queen\",\"King\"]\n        print  ranks[self.rank] , \" of \" , suits[self.suit] \n\nc = Card (1, 11)\nc.print_ ();",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Queen  of  Diamonds\n"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 12.3 page no :127\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 12.3 page no :127\n'''\n\nclass Card:\n    def __init__(self,s=None,r=None):\n        if s==None:\n            self.suit = 0\n            self.rank = 0\n        else:\n            self.suit = s; \n            self.rank = r;\n    def print_(self):\n        suits = [\"Clubs\",\"Diamonds\",\"Hearts\",\"Spades\"]\n        ranks = [\"Ace\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"Jack\",\"Queen\",\"King\"]\n        print  ranks[self.rank] , \" of \" , suits[self.suit] \n\n    def equals(self,c2):\n        return (self.rank == c2.rank and self.suit == c2.suit);\n\nc1 = Card(1, 11);\nc2 =Card(1, 11);\nif (c1.equals(c2)):\n    print \"Yup, that's the same card.\" ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Yup, that's the same card.\n"
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 12.4 page no :129\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 12.4 page no :129\n'''\n\n\nclass Card:\n    def __init__(self,s=None,r=None):\n        if s==None:\n            self.suit = 0\n            self.rank = 0\n        else:\n            self.suit = s; \n            self.rank = r;\n    def print_(self):\n        suits = [\"Clubs\",\"Diamonds\",\"Hearts\",\"Spades\"]\n        ranks = [\"Ace\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"Jack\",\"Queen\",\"King\"]\n        print  ranks[self.rank] , \" of \" , suits[self.suit] \n\n    def equals(self,c2):\n        return (self.rank == c2.rank and self.suit == c2.suit);\n\n    def  isGreater(self,c2):\n        if  (self.suit > c2.suit):\n            return True;\n        if (selfsuit < c2.suit):\n            return False;\n        if (self.rank > c2.rank):\n            return True;\n        if (self.rank < c2.rank):\n            return False;\n        return False;\n\nc1 = Card(2, 11)\nc2 = Card(1, 11)\nif (c1.isGreater (c2)):\n    c1.print_()\n    print \"is greater than\" \n    c2.print_();",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Queen  of  Hearts\nis greater than\nQueen  of  Diamonds\n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 12.5 page no :130\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 12.5 page no :130\n'''\nclass Card:\n    def __init__(self,s=None,r=None):\n        if s==None:\n            self.suit = 0\n            self.rank = 0\n        else:\n            self.suit = s; \n            self.rank = r;\n    def print_(self):\n        suits = [\"Clubs\",\"Diamonds\",\"Hearts\",\"Spades\"]\n        ranks = [\"Ace\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"Jack\",\"Queen\",\"King\"]\n        print  ranks[self.rank] , \" of \" , suits[self.suit] \n\n\ndef printDeck(deck):\n    for i in range(len(deck)):\n        deck[i].print_()\n\ndeck = []\n\nfor suit in range(3):\n    for rank in range(13):\n        a = Card(suit,rank)\n        deck.append(a)\n\nprintDeck(deck)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Ace  of  Clubs\n2  of  Clubs\n3  of  Clubs\n4  of  Clubs\n5  of  Clubs\n6  of  Clubs\n7  of  Clubs\n8  of  Clubs\n9  of  Clubs\n10  of  Clubs\nJack  of  Clubs\nQueen  of  Clubs\nKing  of  Clubs\nAce  of  Diamonds\n2  of  Diamonds\n3  of  Diamonds\n4  of  Diamonds\n5  of  Diamonds\n6  of  Diamonds\n7  of  Diamonds\n8  of  Diamonds\n9  of  Diamonds\n10  of  Diamonds\nJack  of  Diamonds\nQueen  of  Diamonds\nKing  of  Diamonds\nAce  of  Hearts\n2  of  Hearts\n3  of  Hearts\n4  of  Hearts\n5  of  Hearts\n6  of  Hearts\n7  of  Hearts\n8  of  Hearts\n9  of  Hearts\n10  of  Hearts\nJack  of  Hearts\nQueen  of  Hearts\nKing  of  Hearts\n"
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 12.6 page no : 131\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 12.6 page no : 131\n'''\nclass Card:\n    def __init__(self,s=None,r=None):\n        if s==None:\n            self.suit = 0\n            self.rank = 0\n        else:\n            self.suit = s; \n            self.rank = r;\n    def print_(self):\n        suits = [\"Clubs\",\"Diamonds\",\"Hearts\",\"Spades\"]\n        ranks = [\"Ace\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"Jack\",\"Queen\",\"King\"]\n        print  ranks[self.rank] , \" of \" , suits[self.suit] \n        \n    def equals(self,c2):\n        return (self.rank == c2.rank and self.suit == c2.suit);\n\n    def find(self,deck):\n        for i in range(len(deck)):\n            if (self.equals(deck[i])):\n                return i;\n        return -1;\n\ndeck = []\n\nfor suit in range(3):\n    for rank in range(13):\n        a = Card(suit,rank)\n        deck.append(a)\n\nindex = deck[17].find (deck)\nprint \"I found the card at index = \" , index ",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "I found the card at index =  17\n"
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": "example 12.7 page no : 133\n"
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "'''\nexample 12.7 page no : 133\n'''\n\nclass Card:\n    def __init__(self,s=None,r=None):\n        if s==None:\n            self.suit = 0\n            self.rank = 0\n        else:\n            self.suit = s; \n            self.rank = r;\n    def print_(self):\n        suits = [\"Clubs\",\"Diamonds\",\"Hearts\",\"Spades\"]\n        ranks = [\"Ace\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"Jack\",\"Queen\",\"King\"]\n        print  ranks[self.rank] , \" of \" , suits[self.suit] \n        \n    def equals(self,c2):\n        return (self.rank == c2.rank and self.suit == c2.suit);\n\n    def find(self,deck):\n        for i in range(len(deck)):\n            if (self.equals(deck[i])):\n                return i;\n        return -1;\n        \n    def  isGreater(self,c2):\n        if  (self.suit > c2.suit):\n            return True;\n        if (self.suit < c2.suit):\n            return False;\n        if (self.rank > c2.rank):\n            return True;\n        if (self.rank < c2.rank):\n            return False;\n        return False;\n        \ndef findBisect(deck,card,low,high):\n    print low , \", \" , high \n    if (high < low):\n        return -1;\n    mid = (high + low) / 2\n    if (card.equals(deck[mid])):\n        return mid;\n    if (deck[mid].isGreater (card)):\n        return findBisect (deck, card,low, mid-1);\n    else:\n        return findBisect (deck,card, mid+1, high)\n\n\ndeck = []\n\nfor suit in range(3):\n    for rank in range(13):\n        a = Card(suit,rank)\n        deck.append(a)\n\nprint findBisect (deck, deck[23], 0, 51)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "0 ,  51\n0 ,  24\n13 ,  24\n19 ,  24\n22 ,  24\n23\n"
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "",
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}