import random
# Constants
RED = "\033[0;91m"
YELLOW = "\033[1;93m"
RESET = "\033[0m"
class Player:
def __init__(self, letter, x, y):
self.letter = letter
self.x = x
self.y = y
self.score = 0
def __str__(self):
return f'{YELLOW}{self.letter}{RESET}'
class Food:
def __init__(self, amount):
self.amount = amount
def __str__(self):
return self.amount
class Board:
def __init__(self, size):
self.size = size
self.board = [['·']*size for _ in range(size)]
def __str__(self):
res = ""
for row in self.board:
res += ' '.join([RED + e + RESET if e.isupper() else e for e in row]) + "\n"
return res
def addPlayer(self, player):
if(player.x <= self.size and player.y <= self.size):
self.board[player.y][player.x] = player
if __name__ == "__main__":
p = Player("A",0,0)
print(p)
b = Board(5)
print(b)
A
· · · · ·
· · · · ·
· · · · ·
· · · · ·
· · · · ·
class Tablero:
def __init__(self):
self.board = [[1,2,3],[4,5,6],[7,8,9]]
def __str__(self):
return " ".join( map( str, myList ))
t = Tablero()
print(t)
[1, 2, 3] [4, 5, 6] [7, 8, 9]
class Tablero:
def __init__(self):
self.board = [[1,2,3],[4,5,6],[7,8,9]]
def __str__(self):
res = ""
for row in self.board:
res += ' '.join(map(str, row)) + "\n"
return res
t = Tablero()
print(t)
1 2 3 4 5 6 7 8 9
RED = "\033[0;91m"
RESET = "\033[0m"
class Tablero:
def __init__(self):
self.board = [[1,2,3],[4,5,6],[7,8,9]]
def __str__(self):
res = ""
for row in self.board:
res += ' '.join([RED + str(element) + RESET for element in row]) + "\n"
return res
t = Tablero()
print(t)
print()
print(t.board)
1 2 3 4 5 6 7 8 9 [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
RED = "\033[0;91m"
RESET = "\033[0m"
class Tablero:
def __init__(self):
self.board = [['1','·','3'],['·','A','C'],['·','8','B']]
def __str__(self):
res = ""
for row in self.board:
res += ' '.join([RED + e + RESET if e.isupper() else e for e in row]) + "\n"
return res
t = Tablero()
print(t)
print()
print(t.board)
1 · 3 · A C · 8 B [['1', '·', '3'], ['·', 'A', 'C'], ['·', '8', 'B']]
import random
# Constants
RED = "\033[0;92m"
RESET = "\033[0m"
class Player:
def __init__(self, letter, x, y):
self.letter = letter
self.x = x
self.y = y
self.score = 0
def __str__(self):
return f'{RED}{self.letter}{RESET}'
class Board:
def __init__(self, size):
self.size = size
self.matrix = [['·']*size for _ in range(size)]
self.players = []
def addPlayer(self, p):
if (p.x <= self.size and p.y <= self.size):
self.matrix[p.x][p.y] = p
self.players.append(self.matrix[p.x][p.y])
else:
print(f"{RED}Error:\nPlayer position out of map{RESET}")
def updatePlayer(self, p, x, y):
if (x <= self.size and y <= self.size):
self.matrix[p.x][p.y] = '·'
self.matrix[x][y] = p
p.x = x
p.y = y
else:
print(f"{RED}Error:\nPlayer position out of map{RESET}")
def printBoard(self):
for row in self.matrix:
print(*row)
print()
def create_players(player_num, DIM, my_board):
for i in range(player_num):
while True:
new_x = random.randint(0, DIM-1)
new_y = random.randint(0, DIM-1)
if my_board.matrix[new_x][new_y] == '·':
break
my_board.addPlayer(Player(chr(65 + i), new_x, new_y))
if __name__ == "__main__":
size = 6
player_num = 26
my_board = Board(size)
create_players(player_num, size, my_board)
my_board.printBoard()
print()
my_board.players[0].letter = "@" # sustituye la A por una @
my_board.printBoard()
F Q G T X W · E · P L · S · Y N V Z · · · H B · J D A U · K I M R C O · F Q G T X W · E · P L · S · Y N V Z · · · H B · J D @ U · K I M R C O ·