Last active
April 20, 2016 07:30
-
-
Save greut/3a6415471b033c0eb671ee5bb201041f to your computer and use it in GitHub Desktop.
Tic Tac Toe
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": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
" 1 2 3 \n", | |
" 4 5 6 \n", | |
" 7 8 9 \n", | |
"\n", | |
"☻ plays in: 5\n", | |
"\n", | |
" 1 2 3 \n", | |
" 4 ☻ 6 \n", | |
" 7 8 9 \n", | |
"\n", | |
"☺ plays in: 3\n", | |
"\n", | |
" 1 2 ☺ \n", | |
" 4 ☻ 6 \n", | |
" 7 8 9 \n", | |
"\n", | |
"☻ plays in: 9\n", | |
"\n", | |
" 1 2 ☺ \n", | |
" 4 ☻ 6 \n", | |
" 7 8 ☻ \n", | |
"\n", | |
"☺ plays in: 2\n", | |
"\n", | |
" 1 ☺ ☺ \n", | |
" 4 ☻ 6 \n", | |
" 7 8 ☻ \n", | |
"\n", | |
"☻ plays in: 1\n", | |
"\n", | |
" ☻ ☺ ☺ \n", | |
" 4 ☻ 6 \n", | |
" 7 8 ☻ \n", | |
"\n", | |
"☻ wins!\n" | |
] | |
} | |
], | |
"source": [ | |
"class TicTacToe:\n", | |
" \"\"\"Tic Tac Toe board.\"\"\"\n", | |
"\n", | |
" def __init__(self, board=None):\n", | |
" # this element behaves like an immutable one, but nothing really\n", | |
" # enforces it (like using __new__).\n", | |
" self._board = tuple(board) if board else tuple(range(1, 10))\n", | |
"\n", | |
" def __str__(self, split=3):\n", | |
" return \"\\n\".join(\"\".join(\"{:^5}\".format(x)\n", | |
" for x in self._board[i:i+split])\n", | |
" for i in range(0, len(self._board), split))\n", | |
"\n", | |
" def play(self, player, position):\n", | |
" # throws a ValueError if it's not a int\n", | |
" pos = int(position, 10)\n", | |
" # throws a ValueError if the element doesn't exist in the list.\n", | |
" self._board.index(pos)\n", | |
" return TicTacToe(player if pos == x else x for x in self._board)\n", | |
"\n", | |
" @property\n", | |
" def positions(self):\n", | |
" b = self._board\n", | |
" yield b[0::3] # first col\n", | |
" yield b[1::3] # second col\n", | |
" yield b[2::3] # third col\n", | |
" yield b[0:3] # first row\n", | |
" yield b[3:6] # second row\n", | |
" yield b[7:9] # third row\n", | |
" yield b[0::4] # left (back slash) diagonal\n", | |
" yield b[2::2][:3] # right (forward slash) diagonal\n", | |
" \n", | |
" def is_winning(self, player):\n", | |
" win = tuple([player] * 3)\n", | |
" return any(position == win for position in self.positions)\n", | |
"\n", | |
"\n", | |
"# You can skin the board.\n", | |
"players = ('☻', '☺')\n", | |
"turn = 0\n", | |
"board = TicTacToe()\n", | |
"\n", | |
"while True:\n", | |
" player = players[turn]\n", | |
" print(\"\", board, \"\", sep=\"\\n\")\n", | |
" \n", | |
" position = input(\"{} plays in: \".format(player))\n", | |
"\n", | |
" try:\n", | |
" board = board.play(player, position) \n", | |
" except ValueError:\n", | |
" print(\"Derp!\")\n", | |
" break\n", | |
"\n", | |
" if board.is_winning(player):\n", | |
" print(\"\", board, \"\", sep=\"\\n\")\n", | |
" print(\"{} wins!\".format(players[turn]))\n", | |
" break\n", | |
"\n", | |
" # Next turn\n", | |
" turn = (turn + 1) % len(players)" | |
] | |
} | |
], | |
"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.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment