Created
August 5, 2019 09:04
-
-
Save story645/81044c8b068de374e9bc9ff3876cbbf3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Before your interview, write a program that lets two humans play a game of Tic Tac Toe in a terminal. \n", | |
| "The program should let the players take turns to input their moves. \n", | |
| "The program should report the outcome of the game" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%matplotlib widget\n", | |
| "from ipywidgets import interact, interactive, fixed, interact_manual\n", | |
| "import ipywidgets as widgets\n", | |
| "import itertools\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "import matplotlib.patches as mpatches\n", | |
| "import matplotlib.path as mpath\n", | |
| "import matplotlib.cm as mcm\n", | |
| "import matplotlib.transforms as mtransforms\n", | |
| "from matplotlib.collections import PatchCollection\n", | |
| "import matplotlib.colors as m\n", | |
| "# https://matplotlib.org/3.1.1/gallery/shapes_and_collections/artist_reference.html\n", | |
| "import numpy as np" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "class TicTacToe(object):\n", | |
| " loc = [0, .33, .66]\n", | |
| " inds = dict(zip(loc, [0,1,2]))\n", | |
| " facecolor = \"0.99\"\n", | |
| " gridcolor = \"#11557c\"\n", | |
| " edgecolor = '0.5'\n", | |
| " radii = 10 * np.array([0.2, 0.6, 0.8, 0.7, 0.4, 0.5, 0.8])\n", | |
| " colors = [mcm.jet(r/10.) for r in radii]\n", | |
| " players = itertools.cycle(['X','O'])\n", | |
| "\n", | |
| " \n", | |
| " def __init__(self):\n", | |
| " self.player = next(self.players)\n", | |
| " self.draw_symbol = {'X':self.draw_x, 'O':self.draw_o}\n", | |
| " self.board = np.empty(shape=(3,3), dtype='object')\n", | |
| " self.plays = 0\n", | |
| " self.game_over = False\n", | |
| " \n", | |
| " self.fig, self.ax = plt.subplots()\n", | |
| " \n", | |
| " #MPL dev Tom Caswell suggested I use patches\n", | |
| " for (x,y) in itertools.product(self.loc, repeat=2):\n", | |
| " patch = mpatches.Rectangle((x,y), height=.3, width=.3, \n", | |
| " ec=self.edgecolor, fc=self.facecolor,\n", | |
| " picker=True)\n", | |
| " self.ax.add_patch(patch)\n", | |
| "\n", | |
| " self.ax.set_aspect('equal')\n", | |
| " self.ax.set_xlim(0,.96) # set clean borders\n", | |
| " self.ax.set_ylim(0,.96)\n", | |
| " self.ax.set_facecolor(self.gridcolor)\n", | |
| " for spine in self.ax.spines.values():\n", | |
| " spine.set_edgecolor(self.edgecolor)\n", | |
| " self.ax.set_xticks([])\n", | |
| " self.ax.set_yticks([])\n", | |
| "\n", | |
| " # Connect the click function to the button press event\n", | |
| " # bind pick events to our on_pick function\n", | |
| " self.fig.canvas.mpl_connect('pick_event', self.on_pick)\n", | |
| " plt.show()\n", | |
| "\n", | |
| " def draw_x(self, xy):\n", | |
| " x, y = xy[0], xy[1]\n", | |
| " width, height = .3, .3\n", | |
| " span = .03\n", | |
| " verts = [(x+width/2-span, y), (x+width/2+span,y), (x+width/2+span,y+height/2-span), \n", | |
| " (x+width,y+height/2-span), (x+width, y+height/2+span), (x+width/2+span, y+height/2+span),\n", | |
| " (x+width/2+span, y+height), (x+width/2-span, y+height), (x+width/2-span, y+height/2+span),\n", | |
| " (x, y+height/2+span), (x, y+height/2-span), (x+width/2-span, y+height/2-span), \n", | |
| " (x+width/2-span, y)]\n", | |
| "\n", | |
| " codes = [mpath.Path.MOVETO, mpath.Path.LINETO, mpath.Path.LINETO, mpath.Path.LINETO, mpath.Path.LINETO,\n", | |
| " mpath.Path.LINETO, mpath.Path.LINETO, mpath.Path.LINETO, mpath.Path.LINETO,mpath.Path.LINETO, \n", | |
| " mpath.Path.LINETO, mpath.Path.LINETO, mpath.Path.CLOSEPOLY,]\n", | |
| "\n", | |
| " path = mpath.Path(verts, codes)\n", | |
| " #https://stackoverflow.com/questions/4285103/matplotlib-rotating-a-patch\n", | |
| " path = path.transformed(mtransforms.Affine2D().rotate_deg_around(x+width/2, y+height/2, 45))\n", | |
| " return [mpatches.PathPatch(path, fc=self.colors[2], ec = self.gridcolor)]\n", | |
| " \n", | |
| " \n", | |
| " def draw_o(self, xy):\n", | |
| " return [mpatches.Circle((xy[0]+.15, xy[1]+.15), radius=.13, fc=self.colors[1], ec=self.gridcolor),\n", | |
| " mpatches.Circle((xy[0]+.15, xy[1]+.15), radius=.07, fc=self.facecolor, ec=self.gridcolor)]\n", | |
| " \n", | |
| " def check_win(self):\n", | |
| " \"\"\"\n", | |
| " down, across, \\ diagonal, /diagonal \n", | |
| " \"\"\"\n", | |
| " return ((self.board==self.player).all(axis=0).any() | \n", | |
| " (self.board==self.player).all(axis=1).any() | \n", | |
| " (np.diag(self.board)==self.player).all()|\n", | |
| " (np.diag(np.flip(self.board,axis=1))== self.player).all())\n", | |
| " \n", | |
| " #https://jakevdp.github.io/blog/2012/12/06/minesweeper-in-matplotlib/\n", | |
| " # Function to be called when mouse is clicked\n", | |
| " # create a function to be bound to pick events: here the event has an\n", | |
| " # attribute `artist` which points to the object which was clicked\n", | |
| " def on_pick(self, event):\n", | |
| " xy = event.artist.get_xy()\n", | |
| " x, y = xy[0], xy[1]\n", | |
| " \n", | |
| " if self.game_over or (self.board[self.inds[x], self.inds[y]] is not None):\n", | |
| " return #don't overwrite board\n", | |
| " \n", | |
| " patches = self.draw_symbol[self.player](xy)\n", | |
| " [self.ax.add_patch(p) for p in patches]\n", | |
| " self.board[self.inds[x], self.inds[y]] = self.player\n", | |
| " self.plays += 1\n", | |
| " self.fig.canvas.draw()\n", | |
| "\n", | |
| " if self.plays > 4 and self.check_win():\n", | |
| " self.ax.set_title(f'Player {self.player} won!', fontsize=24)\n", | |
| " self.game_over = True\n", | |
| " self.fig.canvas.draw()\n", | |
| " elif self.plays == 9:\n", | |
| " self.ax.set_title(\"Draw\",fontsize=24)\n", | |
| " self.game_over = True\n", | |
| " self.fig.canvas.draw()\n", | |
| " \n", | |
| " self.player = next(self.players)\n", | |
| " return\n", | |
| " \n", | |
| " \n", | |
| " \n", | |
| " \n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "TicTacToe()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.7.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if the backend is changed from ipywidget to one of the other matplotlib backends, it should work in a terminal