Skip to content

Commit

Permalink
Merge pull request #4 from CogWorksBWSI/card-games
Browse files Browse the repository at this point in the history
Update instructions for clarity
  • Loading branch information
rsokl authored Jan 14, 2020
2 parents 727fc7a + d74310d commit e689fb0
Showing 1 changed file with 160 additions and 39 deletions.
199 changes: 160 additions & 39 deletions card_games/HW_card_games.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"# Card Games\n",
"\n",
Expand All @@ -11,22 +16,42 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"## Problem 1: Card\n",
"\n",
"To play card games, we'll first need some cards! Let's define a `Card` class that we'll be able to store in a `Deck` object later on. To get comfortable with writing classes, we'll start out with a skeleton. Later on you'll build your own `Deck` class from scratch.\n",
"To play card games, we'll first need some cards! Let's define a `Card` class that we'll be able to store in a `Deck`\n",
"object later on. To get comfortable with writing classes, we'll start out with a skeleton. Later on you'll build your\n",
"own `Deck` class from scratch.\n",
"\n",
"First, let's decide on the set of features we want out of our `Card` object:\n",
"\n",
"##### Rank\n",
"Each `Card` should keep track of its `rank`. These are the ranks 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. We can easily store this as an integer from 2 to 14.\n",
"##### Rank Each `Card` should keep track of its `rank`. These are the ranks 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen,\n",
"King, and Ace. We can easily store this as an integer from 2 to 14. We should be able to access this by calling `.rank`\n",
"on a `Card`:\n",
"\n",
"``` python\n",
">>> Card(3, \"C\").rank\n",
"3\n",
"```\n",
"\n",
"##### Suit\n",
"Each one of our `Card`s will be one of four suits: Clubs, Hearts, Spades, or Diamonds. Let's store this in a string.\n",
"We should be able to access this by calling `.suit` on a `Card`:\n",
"\n",
"``` python\n",
">>> Card(4, \"D\").suit\n",
"'D'\n",
"```\n",
"\n",
"##### repr\n",
"We should override the `__repr__` function of our `Card` class so that it will print nicely. We'll write this to print out \"[rank] of [suit]\" where [rank] is the rank of our card and [suit] is its suit. For example:\n",
"We should override the `__repr__` function of our `Card` class so that it will print nicely. We'll write this\n",
"to print out \"[rank] of [suit]\" where [rank] is the rank of our card and [suit] is its suit. For example:\n",
"\n",
"``` python\n",
">>> Card(7, 'H')\n",
Expand All @@ -50,15 +75,28 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"Fill out the remainder of the `Card` class below to function as described above."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"autoscroll": false,
"collapsed": false,
"ein.hycell": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"class Card:\n",
Expand Down Expand Up @@ -101,7 +139,7 @@
" Jack of Clubs\n",
" \"\"\"\n",
" # student code goes here\n",
" return ''\n",
" return \"\"\n",
"\n",
" def __lt__(self, other):\n",
" \"\"\" Determine whether the rank of this card is less than the rank of the other. \"\"\"\n",
Expand Down Expand Up @@ -132,7 +170,15 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"autoscroll": false,
"collapsed": false,
"ein.hycell": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"from bwsi_grader.python.card_games import grade_card\n",
Expand All @@ -141,24 +187,50 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"## Problem 2: Deck\n",
"\n",
"Now that we have a `Card` object that we can use, in order to play games we'll need to arrange them in a `Deck`. With one class definition under our belts, let's write this one from scratch! Let's define the functionality we need out of our `Deck`.\n",
"Now that we have a `Card` object that we can use, in order to play games we'll need to arrange them in a `Deck`. With\n",
"one class definition under our belts, let's write this one from scratch! Let's define the functionality we need out of\n",
"our `Deck`.\n",
"\n",
"##### init \n",
"A `Deck` should take one argument to its constructor: `shuffled`, which is a boolean variable indicating\n",
"whether the deck should be initialized in sorted order or shuffled. This should be an optional parameter, which is\n",
"`False` by default. It can simply be initialized as `my_deck = Deck()` or you can explicitly pass in whether to shuffle\n",
"the deck: `my_shuffled_deck = Deck(True)`. This initialization function should create a member variable `cards` that\n",
"holds a list of `Card`s. That member variable should be initialized with a whole set of 52 cards: 2, 3, 4, 5, 6, 7, 8,\n",
"9, 10, J, Q, K, A of each of the four suits.\n",
"\n",
"##### init\n",
"A `Deck` should take one argument to its constructor: `shuffled`, which is a boolean variable indicating whether the deck should be initialized in sorted order or shuffled. This should be an optional parameter, which is `False` by default. It can simply be initialized as `my_deck = Deck()` or you can explicitly pass in whether to shuffle the deck: `my_shuffled_deck = Deck(True)`. This initialization function should create a member variable `cards` that holds a list of `Card`s. That member variable should be initialized with a whole set of 52 cards: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A of each of the four suits.\n",
"Additionally, the initialization function should keep track of the number of cards that have been dealt. This will be\n",
"initialized to zero.\n",
"\n",
"Additionally, the initialization function should keep track of the number of cards that have been dealt. This will be initialized to zero.\n",
"Finally, a `Deck` should keep track of whether it has been shuffled. If the `Deck` is not shuffled, it should be in\n",
"sorted order. It may start with any suit, but it should follow the order 2, 3, 4, ..., 10, J, Q, K, A, 2, 3, 4, ..., K,\n",
"A for each suit. We should be able to access this through a `shuffled` parameter:\n",
"\n",
"Finally, a `Deck` should keep track of whether it has been shuffled. If the `Deck` is not shuffled, it should be in sorted order. It may start with any suit, but it should follow the order 2, 3, 4, ..., 10, J, Q, K, A, 2, 3, 4, ..., K, A for each suit.\n",
"``` python\n",
">>> Deck().shuffled\n",
"False\n",
"\n",
"##### shuffle\n",
"A `Deck` object won't do us any good for playing games if we can't shuffle it! We'll write a function called `shuffle` that will allow us to shuffle our deck. This will take no parameters. Instead, it will simply be called as `my_deck.shuffle()`. You may find the [random](https://docs.python.org/3/library/random.html) module helpful here.\n",
">>> Deck(shuffled=True).shuffled\n",
"True\n",
"```\n",
"\n",
"##### shuffle \n",
"A `Deck` object won't do us any good for playing games if we can't shuffle it! We'll write a function\n",
"called `shuffle` that will allow us to shuffle our deck. This will take no parameters. Instead, it will simply be called\n",
"as `my_deck.shuffle()`. You may find the [random](https://docs.python.org/3/library/random.html) module helpful here.\n",
"\n",
"##### deal_card\n",
"A `Deck` object should be able to deal `Card`s off the top. We'll write a function `deal_card` that returns the `Card` object at the top of the deck. That is, we might create a `Deck` and pull the top card off like so:\n",
"A `Deck` object should be able to deal `Card`s off the top. We'll write a function `deal_card` that\n",
"returns the `Card` object at the top of the deck. That is, we might create a `Deck` and pull the top card off like so:\n",
"\n",
"``` python\n",
">>> my_deck = Deck()\n",
Expand All @@ -171,7 +243,9 @@
"Queen of Spades\n",
"```\n",
"\n",
"This function should also increment our variable tracking the number of `Card`s we've dealt. Importantly, our `Deck` shouldn't deal cards once we've gotten to the end of the deck. If we reach the end, instead of returning the next `Card`, we'll return `None`:\n",
"This function should also increment our variable tracking the number of `Card`s we've dealt. Importantly, our `Deck`\n",
"shouldn't deal cards once we've gotten to the end of the deck. If we reach the end, instead of returning the next\n",
"`Card`, we'll return `None`:\n",
"\n",
"``` python\n",
">>> my_deck = Deck()\n",
Expand All @@ -184,7 +258,9 @@
"```\n",
"\n",
"##### repr\n",
"We'll write our own `__repr__` function for the `Deck` class just as we did with `Card`. The repr in this class will simply print out that it is a `Deck` object, the number of cards that have been dealt, and whether the deck has been shuffled:\n",
"We'll write our own `__repr__` function for the `Deck` class just as we did with `Card`. The repr in this\n",
"class will simply print out that it is a `Deck` object, the number of cards that have been dealt, and whether the deck\n",
"has been shuffled:\n",
"\n",
"``` python\n",
">>> my_deck = Deck()\n",
Expand All @@ -203,7 +279,10 @@
"```\n",
"\n",
"##### reset\n",
"Finally, let's write a `reset` function so that we don't have to construct a new `Deck` every time we want to use one. Imagine how ridiculous it would be if we had to go out and buy a new set of cards every time we wanted to play a game! The `reset` function should do exactly what our `__init__` function does: reset our counter and `shuffled` variable and set the `Card`s in our deck in order:\n",
"Finally, let's write a `reset` function so that we don't have to construct a new `Deck` every time we want\n",
"to use one. Imagine how ridiculous it would be if we had to go out and buy a new set of cards every time we wanted to\n",
"play a game! The `reset` function should do exactly what our `__init__` function does: reset our counter and `shuffled`\n",
"variable and set the `Card`s in our deck in order:\n",
"\n",
"``` python\n",
">>> my_deck = Deck()\n",
Expand All @@ -220,24 +299,28 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"Create the `Deck` class as decribed above."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# student code here"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"autoscroll": false,
"collapsed": false,
"ein.hycell": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"from bwsi_grader.python.card_games import grade_deck\n",
Expand All @@ -246,15 +329,29 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"With our `Deck` and `Card`s written out, let's write a very simple game of high-low. We'll just deal the top card to each of two players and determine whether Player 1 or Player 2 has the highest."
"With our `Deck` and `Card`s written out, let's write a very simple game of high-low. We'll just deal the top card to\n",
"each of two players and determine whether Player 1 or Player 2 has the highest."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"autoscroll": false,
"collapsed": false,
"ein.hycell": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"def play_high_low_game():\n",
Expand All @@ -268,24 +365,47 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"autoscroll": false,
"collapsed": false,
"ein.hycell": false,
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"play_high_low_game()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"ein.tags": "worksheet-0",
"slideshow": {
"slide_type": "-"
}
},
"source": [
"See what other games you can create with your new `Deck` of `Card`s!"
]
}
],
"metadata": {
"kernelspec": {
"argv": [
"/usr/bin/python3",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"env": null,
"interrupt_mode": "signal",
"language": "python",
"metadata": null,
"name": "python3"
},
"language_info": {
Expand All @@ -299,7 +419,8 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"name": "HW_card_games.ipynb"
},
"nbformat": 4,
"nbformat_minor": 2
Expand Down

0 comments on commit e689fb0

Please sign in to comment.