Skip to content

Commit da9730f

Browse files
committed
update in-class exercises
1 parent 254ea78 commit da9730f

File tree

1 file changed

+158
-2
lines changed

1 file changed

+158
-2
lines changed

lectures/01-python/python-inclass-2.ipynb

Lines changed: 158 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,12 @@
198198
]
199199
},
200200
{
201-
"cell_type": "markdown",
202-
"metadata": {},
201+
"cell_type": "code",
202+
"execution_count": null,
203+
"metadata": {
204+
"collapsed": true
205+
},
206+
"outputs": [],
203207
"source": []
204208
},
205209
{
@@ -230,6 +234,158 @@
230234
"source": [
231235
" "
232236
]
237+
},
238+
{
239+
"cell_type": "markdown",
240+
"metadata": {},
241+
"source": [
242+
"## Q 4 (tic-tac-toe)\n",
243+
"\n",
244+
"Here we'll write a simple tic-tac-toe game that 2 players can play. First we'll create a string that represents our game board:"
245+
]
246+
},
247+
{
248+
"cell_type": "code",
249+
"execution_count": null,
250+
"metadata": {
251+
"collapsed": true
252+
},
253+
"outputs": [],
254+
"source": [
255+
"board = \"\"\"\n",
256+
" {s1:^3} | {s2:^3} | {s3:^3}\n",
257+
"-----+-----+-----\n",
258+
" {s4:^3} | {s5:^3} | {s6:^3}\n",
259+
"-----+-----+----- 123\n",
260+
" {s7:^3} | {s8:^3} | {s9:^3} 456\n",
261+
" 789 \n",
262+
"\"\"\""
263+
]
264+
},
265+
{
266+
"cell_type": "markdown",
267+
"metadata": {},
268+
"source": [
269+
"and well use a dictionary to denote the status of each square, \"x\", \"o\", or empty, \"\""
270+
]
271+
},
272+
{
273+
"cell_type": "code",
274+
"execution_count": null,
275+
"metadata": {
276+
"collapsed": false
277+
},
278+
"outputs": [],
279+
"source": [
280+
"play = {}\n",
281+
"\n",
282+
"def initialize_board(play):\n",
283+
" for n in range(9):\n",
284+
" play[\"s{}\".format(n+1)] = \"\"\n",
285+
"\n",
286+
"initialize_board(play)\n",
287+
"play"
288+
]
289+
},
290+
{
291+
"cell_type": "markdown",
292+
"metadata": {},
293+
"source": [
294+
"Here's an easy way to add the values of our dictionary to the appropriate squares in our game board. First note that each of the {} is labeled with a number that matches the keys in our dictionary. Python provides a way to unpack a dictionary into labeled arguments, using **\n",
295+
"\n",
296+
"For example:"
297+
]
298+
},
299+
{
300+
"cell_type": "code",
301+
"execution_count": null,
302+
"metadata": {
303+
"collapsed": false
304+
},
305+
"outputs": [],
306+
"source": [
307+
"def show_board(play):\n",
308+
" \"\"\" display the playing board. We take a dictionary with the current state of the board\n",
309+
" We rely on the board string to be a global variable\"\"\"\n",
310+
" print(board.format(**play))\n",
311+
" \n",
312+
"show_board(play)"
313+
]
314+
},
315+
{
316+
"cell_type": "markdown",
317+
"metadata": {},
318+
"source": [
319+
"Now, our task is to drive a function that prints the current state of the board, a second function that asks for input from a player (we'll call them player 1 and 2). Then we'll have a main loop that organizes the game play."
320+
]
321+
},
322+
{
323+
"cell_type": "code",
324+
"execution_count": null,
325+
"metadata": {
326+
"collapsed": true
327+
},
328+
"outputs": [],
329+
"source": [
330+
"def get_move(n, xo, play):\n",
331+
" \"\"\" ask the current player, n, to make a move -- make sure the square was not already played.\n",
332+
" xo is a string of the character (x or o) we will place in the desired square \"\"\"\n",
333+
" valid_move = False\n",
334+
" while not valid_move:\n",
335+
" idx = input(\"player {}, enter your move (1-9)\".format(n))\n",
336+
" if play[\"s{}\".format(idx)] == \"\":\n",
337+
" valid_move = True\n",
338+
" else:\n",
339+
" print(\"invalid: {}\".format(play[\"s{}\".format(idx)]))\n",
340+
" \n",
341+
" play[\"s{}\".format(idx)] = xo"
342+
]
343+
},
344+
{
345+
"cell_type": "markdown",
346+
"metadata": {},
347+
"source": [
348+
"### your task\n",
349+
"\n",
350+
"Using the functions defined above,\n",
351+
" * `initialize_board()`\n",
352+
" * `show_board()`\n",
353+
" * `play_game()`\n",
354+
"\n",
355+
"fill in the function `play_game()` below to complete the game, asking for the moves one at a time, alternating between player 1 and 2"
356+
]
357+
},
358+
{
359+
"cell_type": "code",
360+
"execution_count": null,
361+
"metadata": {
362+
"collapsed": true
363+
},
364+
"outputs": [],
365+
"source": [
366+
"def play_game():\n",
367+
" \"\"\" play a game of tic-tac-toe \"\"\"\n"
368+
]
369+
},
370+
{
371+
"cell_type": "code",
372+
"execution_count": null,
373+
"metadata": {
374+
"collapsed": false
375+
},
376+
"outputs": [],
377+
"source": [
378+
"play_game()"
379+
]
380+
},
381+
{
382+
"cell_type": "code",
383+
"execution_count": null,
384+
"metadata": {
385+
"collapsed": true
386+
},
387+
"outputs": [],
388+
"source": []
233389
}
234390
],
235391
"metadata": {

0 commit comments

Comments
 (0)