Skip to content

Instantly share code, notes, and snippets.

@korniichuk
Last active June 9, 2023 15:28
Show Gist options
  • Save korniichuk/1ea269da7dbdc329e6a82fe9e2a0a904 to your computer and use it in GitHub Desktop.
Save korniichuk/1ea269da7dbdc329e6a82fe9e2a0a904 to your computer and use it in GitHub Desktop.
Playing card (09.06.2023)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"\n",
"class Player:\n",
" def __init__(self, cards):\n",
" self.cards = cards\n",
" self.score = 0\n",
" self.win_pile = set()\n",
"\n",
"\n",
"class CardGame:\n",
"\n",
" cards = [i for i in range(1, 53)] # [1 .. 52]\n",
"\n",
" def __init__(self):\n",
" self._status = 'in progress'\n",
" random.shuffle(self.cards)\n",
" self.player1 = Player(self.cards[0::2])\n",
" self.player2 = Player(self.cards[1::2])\n",
"\n",
" @property\n",
" def status(self):\n",
" return self._status\n",
"\n",
" def game(self):\n",
" if self._status == 'in progress':\n",
" card1 = self.player1.cards.pop()\n",
" card2 = self.player2.cards.pop()\n",
" if card1 > card2:\n",
" self.player1.score += 2\n",
" self.player1.win_pile.add(card1)\n",
" self.player1.win_pile.add(card2)\n",
" else:\n",
" self.player2.score += 2\n",
" self.player2.win_pile.add(card1)\n",
" self.player2.win_pile.add(card2)\n",
" if len(self.player1.cards) == 0:\n",
" if self.player1.score > self.player2.score:\n",
" self._status = \"Player 1 is winner\"\n",
" elif self.player2.score > self.player1.score:\n",
" self._status = \"Player 2 is winner\"\n",
" else:\n",
" if max(self.player1.win_pile) > max(self.player2.win_pile):\n",
" self._status = \"Player 1 is winner\"\n",
" else:\n",
" self._status = \"Player 2 is winner\"\n",
"\n",
" def statistics(self):\n",
" print(f\"\\nPlayer 1:\\n cards: {self.player1.cards}\\n \"\n",
" f\"score: {self.player1.score}\\n \"\n",
" f\"win pile: {self.player1.win_pile}\\n\")\n",
" print(f\"Player 2:\\n cards: {self.player2.cards}\\n \"\n",
" f\"score: {self.player2.score}\\n \"\n",
" f\"win pile: {self.player2.win_pile}\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"card_game = CardGame()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Player 1:\n",
" cards: [20, 41, 31, 6, 3, 47, 23, 11, 29, 46, 24, 19, 40, 18, 17, 30, 43, 35, 9, 36, 22, 7, 37, 26, 49, 39]\n",
" score: 0\n",
" win pile: set()\n",
"\n",
"Player 2:\n",
" cards: [10, 27, 33, 1, 28, 25, 32, 48, 45, 44, 13, 14, 42, 16, 4, 2, 51, 5, 34, 8, 52, 15, 12, 38, 50, 21]\n",
" score: 0\n",
" win pile: set()\n",
"\n",
"Status: in progress\n"
]
}
],
"source": [
"card_game.statistics()\n",
"print(f\"Status: {card_game.status}\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Player 1:\n",
" cards: []\n",
" score: 28\n",
" win pile: {1, 2, 4, 5, 6, 8, 10, 12, 13, 14, 16, 17, 18, 19, 20, 21, 24, 25, 27, 30, 35, 36, 37, 39, 41, 44, 46, 47}\n",
"\n",
"Player 2:\n",
" cards: []\n",
" score: 24\n",
" win pile: {3, 7, 9, 11, 15, 22, 23, 26, 28, 29, 31, 32, 33, 34, 38, 40, 42, 43, 45, 48, 49, 50, 51, 52}\n",
"\n",
"Status: Player 1 is winner\n"
]
}
],
"source": [
"for i in range(1, 27):\n",
" card_game.game()\n",
"\n",
"card_game.statistics()\n",
"print(f\"Status: {card_game.status}\")"
]
}
],
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment