Add colour support
This commit is contained in:
parent
8d3fce2829
commit
cd265375a3
@ -1,4 +1,6 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
import colorama
|
||||||
|
from colorama import Fore, Back, Style
|
||||||
|
|
||||||
class FieldType(Enum):
|
class FieldType(Enum):
|
||||||
empty = 1
|
empty = 1
|
||||||
@ -40,12 +42,12 @@ class Table(object):
|
|||||||
self.__fields[x - 1].append(Field(FieldType.water))
|
self.__fields[x - 1].append(Field(FieldType.water))
|
||||||
|
|
||||||
def __border_row(self):
|
def __border_row(self):
|
||||||
ret = '+'
|
ret = Back.WHITE + Fore.BLACK + '+'
|
||||||
|
|
||||||
for i in range(0, self.width):
|
for i in range(0, self.width):
|
||||||
ret += '--+'
|
ret += '--+'
|
||||||
|
|
||||||
ret += "\n"
|
ret += " \n" + Style.RESET_ALL
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@ -78,7 +80,7 @@ class Table(object):
|
|||||||
ret = self.__border_row()
|
ret = self.__border_row()
|
||||||
|
|
||||||
for row in range(0, self.height):
|
for row in range(0, self.height):
|
||||||
ret += '|'
|
ret += Back.WHITE + Fore.BLACK + '|'
|
||||||
|
|
||||||
for col in range(0, self.width):
|
for col in range(0, self.width):
|
||||||
field = self.__fields[row][col]
|
field = self.__fields[row][col]
|
||||||
@ -86,9 +88,9 @@ class Table(object):
|
|||||||
typ = field.hidden_type if field.fixed else field.player_type
|
typ = field.hidden_type if field.fixed else field.player_type
|
||||||
|
|
||||||
if typ == FieldType.empty:
|
if typ == FieldType.empty:
|
||||||
ret += ' '
|
ret += Back.WHITE + ' '
|
||||||
elif typ == FieldType.water:
|
elif typ == FieldType.water:
|
||||||
ret += '~~'
|
ret += Back.CYAN + Fore.BLUE + '~~'
|
||||||
elif typ == FieldType.ship:
|
elif typ == FieldType.ship:
|
||||||
# Check neighbours
|
# Check neighbours
|
||||||
|
|
||||||
@ -106,32 +108,36 @@ class Table(object):
|
|||||||
else n.is_ship(from_fixed=field.fixed) \
|
else n.is_ship(from_fixed=field.fixed) \
|
||||||
for n in neighbours]
|
for n in neighbours]
|
||||||
|
|
||||||
|
ret += Back.CYAN
|
||||||
|
|
||||||
# Ship above and below or left and right
|
# Ship above and below or left and right
|
||||||
if (neighs[0] and neighs[2]) \
|
if (neighs[0] and neighs[2]) \
|
||||||
or (neighs[1] and neighs[3]):
|
or (neighs[1] and neighs[3]):
|
||||||
ret += 'XX'
|
ret += Fore.RED + 'XX'
|
||||||
elif neighs[0]: # Ship above, not below
|
elif neighs[0]: # Ship above, not below
|
||||||
ret += 'vv'
|
ret += Fore.RED + 'vv'
|
||||||
elif neighs[2]: # Ship below, not above
|
elif neighs[2]: # Ship below, not above
|
||||||
ret += '^^'
|
ret += Fore.RED + '^^'
|
||||||
elif neighs[1]: # Ship right, not left
|
elif neighs[1]: # Ship right, not left
|
||||||
ret += ' <'
|
ret += Fore.RED + ' <'
|
||||||
elif neighs[3]: # Ship on left, not right
|
elif neighs[3]: # Ship on left, not right
|
||||||
ret += '> '
|
ret += Fore.RED + '> '
|
||||||
elif all_waters:
|
elif all_waters:
|
||||||
ret += 'oo'
|
ret += Fore.RED + 'oo'
|
||||||
else:
|
else:
|
||||||
ret += '??'
|
ret += Fore.BROWN + '??'
|
||||||
|
|
||||||
ret += '|'
|
ret += Back.WHITE + Fore.BLACK + '|'
|
||||||
|
|
||||||
ret += " {}\n".format(self.row_ship_count(row))
|
ret += " {:<3}\n".format(self.row_ship_count(row)) + Style.RESET_ALL
|
||||||
ret += self.__border_row()
|
ret += self.__border_row()
|
||||||
|
|
||||||
|
ret += Back.WHITE + Fore.BLACK
|
||||||
|
|
||||||
for col in range(0, self.width):
|
for col in range(0, self.width):
|
||||||
ret += " {:<2}".format(self.col_ship_count(col))
|
ret += " {:<2}".format(self.col_ship_count(col))
|
||||||
|
|
||||||
ret += "\n"
|
ret += " \n" + Style.RESET_ALL
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@ -174,6 +180,8 @@ class Table(object):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
colorama.init()
|
||||||
|
|
||||||
t = Table(6, 6)
|
t = Table(6, 6)
|
||||||
t.add_ship(1, 3, 1, False)
|
t.add_ship(1, 3, 1, False)
|
||||||
t.add_ship(2, 5, 2, False)
|
t.add_ship(2, 5, 2, False)
|
||||||
|
Loading…
Reference in New Issue
Block a user