+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#Putting both halves together\n",
+ "We now have systems that can do both halves of the Hangman game. Now it's time to put them together so that we can get a machine to play itself. At the moment, let's concentrate on just the plumbing: let's get the two halves working together. In the next step, we'll think about different strategies and compare their effectiveness.\n",
+ "\n",
+ "The word of this section is \"encapsulation.\" \n",
+ "\n",
+ "First, a change in terminology. We'll call a `player` the thing that makes the guesses (the guesser player from before). We'll call a `game` the thing that handles receiving guesses, updating `discovered` words, updating lives and all of that. That's what was the setting player from before, but without the bit about picking a random word. We'll have the `game` accept the `target` as a parameter, so that we can test everything more easily (if we know what word the `player` is trying to guess, we can predict what it should do).\n",
+ "\n",
+ "Back to encapsulation.\n",
+ "\n",
+ "We want to bundle all the state information for a `game` into one 'thing' so that we don't need to worry about global variables. We will also want to bundle up everything to do with a `player` into one 'thing' so we can tell the game about it. For the `player`, the thing will include both any state it needs and the definition of that `player`'s guessing strategy. We're also going to have a bunch of related players, all doing much the same thing but with different strategies.\n",
+ "\n",
+ "In this case, objects seem like a natural fit.\n",
+ "\n",
+ "(Insert Neil's aikido instructor story here.)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Object-orientation in Python\n",
+ "There are a few things to remember about object orientation in Python.\n",
+ "\n",
+ "1. It's an ugly kludge that was bolted on long after the language was defined.\n",
+ "\n",
+ "2. `self` needs to be passed in to all object methods as the first parameter, though it doesn't appear when you call the method.\n",
+ "\n",
+ "3. Use `self.whatever` to refer to the instance variable `whatever`\n",
+ "\n",
+ "4. Use `self.however(arg1, arg2)` when one method calls another in the same object.\n",
+ "\n",
+ "Apart from that, it's pretty straightforward."
+ ]
+ },