{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyMyBQ3K1wKSNVEkzZaugD/b", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YTHPSHvYHco6", "outputId": "a9b9f7db-47d1-45e9-fce9-7bb38bd47fef" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[1;93mA\u001b[0m\n", "· · · · ·\n", "· · · · ·\n", "· · · · ·\n", "· · · · ·\n", "· · · · ·\n", "\n" ] } ], "source": [ "import random\n", "\n", "# Constants\n", "RED = \"\\033[0;91m\"\n", "YELLOW = \"\\033[1;93m\"\n", "RESET = \"\\033[0m\"\n", "\n", "class Player:\n", " def __init__(self, letter, x, y):\n", " self.letter = letter\n", " self.x = x\n", " self.y = y\n", " self.score = 0\n", "\n", " def __str__(self):\n", " return f'{YELLOW}{self.letter}{RESET}'\n", "\n", "\n", "class Food:\n", " def __init__(self, amount):\n", " self.amount = amount\n", "\n", " def __str__(self):\n", " return self.amount\n", "\n", "\n", "class Board:\n", " def __init__(self, size):\n", " self.size = size\n", " self.board = [['·']*size for _ in range(size)]\n", "\n", " def __str__(self):\n", " res = \"\"\n", " for row in self.board:\n", " res += ' '.join([RED + e + RESET if e.isupper() else e for e in row]) + \"\\n\"\n", " return res\n", "\n", " def addPlayer(self, player):\n", " if(player.x <= self.size and player.y <= self.size):\n", " self.board[player.y][player.x] = player\n", "\n", "\n", "\n", "if __name__ == \"__main__\":\n", " p = Player(\"A\",0,0)\n", " print(p)\n", " b = Board(5)\n", " print(b)" ] }, { "cell_type": "code", "source": [ "class Tablero:\n", " def __init__(self):\n", " self.board = [[1,2,3],[4,5,6],[7,8,9]]\n", " def __str__(self):\n", " return \" \".join( map( str, myList ))\n", "\n", "t = Tablero()\n", "print(t)" ], "metadata": { "id": "GfznpZM4cUOw", "outputId": "8cb47308-5468-4dba-9845-24a995e12714", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[1, 2, 3] [4, 5, 6] [7, 8, 9]\n" ] } ] }, { "cell_type": "code", "source": [ "class Tablero:\n", " def __init__(self):\n", " self.board = [[1,2,3],[4,5,6],[7,8,9]]\n", " def __str__(self):\n", " res = \"\"\n", " for row in self.board:\n", " res += ' '.join(map(str, row)) + \"\\n\"\n", " return res\n", "\n", "t = Tablero()\n", "print(t)" ], "metadata": { "id": "t74m0KUidPUD", "outputId": "5e4033b0-5b5d-4eb4-95ae-3b44079a2dfe", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1 2 3\n", "4 5 6\n", "7 8 9\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "RED = \"\\033[0;91m\"\n", "RESET = \"\\033[0m\"\n", "\n", "class Tablero:\n", " def __init__(self):\n", " self.board = [[1,2,3],[4,5,6],[7,8,9]]\n", " def __str__(self):\n", " res = \"\"\n", " for row in self.board:\n", " res += ' '.join([RED + str(element) + RESET for element in row]) + \"\\n\"\n", " return res\n", "\n", "t = Tablero()\n", "print(t)\n", "print()\n", "print(t.board)" ], "metadata": { "id": "3txMzetXeX2O", "outputId": "f9a9abd0-1bbb-4ced-ea7d-818b701c69cf", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[0;91m1\u001b[0m \u001b[0;91m2\u001b[0m \u001b[0;91m3\u001b[0m\n", "\u001b[0;91m4\u001b[0m \u001b[0;91m5\u001b[0m \u001b[0;91m6\u001b[0m\n", "\u001b[0;91m7\u001b[0m \u001b[0;91m8\u001b[0m \u001b[0;91m9\u001b[0m\n", "\n", "\n", "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n" ] } ] }, { "cell_type": "code", "source": [ "RED = \"\\033[0;91m\"\n", "RESET = \"\\033[0m\"\n", "\n", "class Tablero:\n", " def __init__(self):\n", " self.board = [['1','·','3'],['·','A','C'],['·','8','B']]\n", " def __str__(self):\n", " res = \"\"\n", " for row in self.board:\n", " res += ' '.join([RED + e + RESET if e.isupper() else e for e in row]) + \"\\n\"\n", " return res\n", "\n", "t = Tablero()\n", "print(t)\n", "print()\n", "print(t.board)" ], "metadata": { "id": "O5fF4o8-U016", "outputId": "0772fba5-6ac3-4833-e21a-585665803aca", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1 · 3\n", "· \u001b[0;91mA\u001b[0m \u001b[0;91mC\u001b[0m\n", "· 8 \u001b[0;91mB\u001b[0m\n", "\n", "\n", "[['1', '·', '3'], ['·', 'A', 'C'], ['·', '8', 'B']]\n" ] } ] }, { "cell_type": "code", "source": [ "import random\n", "\n", "# Constants\n", "RED = \"\\033[0;92m\"\n", "RESET = \"\\033[0m\"\n", "\n", "class Player:\n", " def __init__(self, letter, x, y):\n", " self.letter = letter\n", " self.x = x\n", " self.y = y\n", " self.score = 0\n", "\n", " def __str__(self):\n", " return f'{RED}{self.letter}{RESET}'\n", "\n", "\n", "class Board:\n", " def __init__(self, size):\n", " self.size = size\n", " self.matrix = [['·']*size for _ in range(size)]\n", " self.players = []\n", "\n", " def addPlayer(self, p):\n", " if (p.x <= self.size and p.y <= self.size):\n", " self.matrix[p.x][p.y] = p\n", " self.players.append(self.matrix[p.x][p.y])\n", " else:\n", " print(f\"{RED}Error:\\nPlayer position out of map{RESET}\")\n", "\n", " def updatePlayer(self, p, x, y):\n", " if (x <= self.size and y <= self.size):\n", " self.matrix[p.x][p.y] = '·'\n", " self.matrix[x][y] = p\n", " p.x = x\n", " p.y = y\n", " else:\n", " print(f\"{RED}Error:\\nPlayer position out of map{RESET}\")\n", "\n", " def printBoard(self):\n", " for row in self.matrix:\n", " print(*row)\n", " print()\n", "\n", "\n", "def create_players(player_num, DIM, my_board):\n", " for i in range(player_num):\n", " while True:\n", " new_x = random.randint(0, DIM-1)\n", " new_y = random.randint(0, DIM-1)\n", " if my_board.matrix[new_x][new_y] == '·':\n", " break\n", " my_board.addPlayer(Player(chr(65 + i), new_x, new_y))\n", "\n", "\n", "if __name__ == \"__main__\":\n", " size = 6\n", " player_num = 26\n", " my_board = Board(size)\n", " create_players(player_num, size, my_board)\n", " my_board.printBoard()\n", " print()\n", " my_board.players[0].letter = \"@\" # sustituye la A por una @\n", " my_board.printBoard()\n", "\n", "" ], "metadata": { "id": "Bl1vzMJIZ0X_", "outputId": "d0d1e8ac-716b-4eeb-ee0c-e2e19be3fc45", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": 38, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[0;92mF\u001b[0m \u001b[0;92mQ\u001b[0m \u001b[0;92mG\u001b[0m \u001b[0;92mT\u001b[0m \u001b[0;92mX\u001b[0m \u001b[0;92mW\u001b[0m\n", "· \u001b[0;92mE\u001b[0m · \u001b[0;92mP\u001b[0m \u001b[0;92mL\u001b[0m ·\n", "\u001b[0;92mS\u001b[0m · \u001b[0;92mY\u001b[0m \u001b[0;92mN\u001b[0m \u001b[0;92mV\u001b[0m \u001b[0;92mZ\u001b[0m\n", "· · · \u001b[0;92mH\u001b[0m \u001b[0;92mB\u001b[0m ·\n", "\u001b[0;92mJ\u001b[0m \u001b[0;92mD\u001b[0m \u001b[0;92mA\u001b[0m \u001b[0;92mU\u001b[0m · \u001b[0;92mK\u001b[0m\n", "\u001b[0;92mI\u001b[0m \u001b[0;92mM\u001b[0m \u001b[0;92mR\u001b[0m \u001b[0;92mC\u001b[0m \u001b[0;92mO\u001b[0m ·\n", "\n", "\n", "\u001b[0;92mF\u001b[0m \u001b[0;92mQ\u001b[0m \u001b[0;92mG\u001b[0m \u001b[0;92mT\u001b[0m \u001b[0;92mX\u001b[0m \u001b[0;92mW\u001b[0m\n", "· \u001b[0;92mE\u001b[0m · \u001b[0;92mP\u001b[0m \u001b[0;92mL\u001b[0m ·\n", "\u001b[0;92mS\u001b[0m · \u001b[0;92mY\u001b[0m \u001b[0;92mN\u001b[0m \u001b[0;92mV\u001b[0m \u001b[0;92mZ\u001b[0m\n", "· · · \u001b[0;92mH\u001b[0m \u001b[0;92mB\u001b[0m ·\n", "\u001b[0;92mJ\u001b[0m \u001b[0;92mD\u001b[0m \u001b[0;92m@\u001b[0m \u001b[0;92mU\u001b[0m · \u001b[0;92mK\u001b[0m\n", "\u001b[0;92mI\u001b[0m \u001b[0;92mM\u001b[0m \u001b[0;92mR\u001b[0m \u001b[0;92mC\u001b[0m \u001b[0;92mO\u001b[0m ·\n", "\n" ] } ] } ] }