0

In my program, the user can build buildings on specific coordinates on a 5x5 map. Once a building has been placed, the user can no longer build on that coordinate. I want to print these coordinates as red when they're used up. How can I achieve this? Is there a subroutine I can write and call to make it easier?

The map is based on a 2D list that looks like this:

#       x=0   x=1   x=2   x=3   x=4
map = [["a1", "a2", "a3", "a4", "a5"],  # y = 0
       ["b1", "b2", "b3", "b4", "b5"],  # y = 1
       ["c1", "c2", "c3", "c4", "c5"],  # y = 2
       ["d1", "d2", "d3", "d4", "d5"],  # y = 3
       ["e1", "e2", "e3", "e4", "e5"]]  # y = 4

When the user builds on a coordinate, it's appended to a list called "constructed" so that it can't be done a second time.

The map is printed here, this is where I want to color values that are also in "constructed":

    print("Select a coordinate to build on.")
    print()
    print(map[0])
    print(map[1])
    print(map[2])
    print(map[3])
    print(map[4])
Kukul
  • 13
  • 1
  • Does this answer your question? [How do I print colored text to the terminal?](https://stackoverflow.com/questions/287871/how-do-i-print-colored-text-to-the-terminal) – Woodford Jul 21 '23 at 22:05

2 Answers2

1
  1. map is a builtin function in python so i would recommend renaming your local variable to mymap or something else.

  2. here is a handy constructor for the map you provided

mymap = [[f"{i}{j}" for j in  range(1,6)] for i in 'abcde']
  1. 2-d list aside, it sounds like you seek more of a dictionary structure to hold values of colors based on these map coordinates. Let's start by initializing something with just False as a placeholder for 'not constructed'
mymap = {f"{i}{j}": False for i in 'abcde' for j in  range(1,6)}
  1. let's pick a notation for 'colors', like RGBA. We can express this as a tuple in our mymap from (3).
mymap = {f"{i}{j}": (0,0,0,0) for i in 'abcde' for j in  range(1,6)}

you are left with a structure like this, which holds your coordinate notation, and a value for a color.

{'a1': (0, 0, 0, 0),
 'a2': (0, 0, 0, 0),
 'a3': (0, 0, 0, 0),
 'a4': (0, 0, 0, 0),
 'a5': (0, 0, 0, 0),
 'b1': (0, 0, 0, 0),
 'b2': (0, 0, 0, 0),
 'b3': (0, 0, 0, 0),
 'b4': (0, 0, 0, 0),
 'b5': (0, 0, 0, 0),
 'c1': (0, 0, 0, 0),
 'c2': (0, 0, 0, 0),
 'c3': (0, 0, 0, 0),
 'c4': (0, 0, 0, 0),
 'c5': (0, 0, 0, 0),
 'd1': (0, 0, 0, 0),
 'd2': (0, 0, 0, 0),
 'd3': (0, 0, 0, 0),
 'd4': (0, 0, 0, 0),
 'd5': (0, 0, 0, 0),
 'e1': (0, 0, 0, 0),
 'e2': (0, 0, 0, 0),
 'e3': (0, 0, 0, 0),
 'e4': (0, 0, 0, 0),
 'e5': (0, 0, 0, 0)}

hoping this helps you aim at your solution a little better.

Zombro
  • 311
  • 1
  • 9
0

You could do something like this:

def print_map(map, constructed):
    for row in map:
        for coordinate in row:
            if coordinate in constructed:
                print("\033[0;31m" + coordinate + "\033[0m", end=" ")
            else:
                print(coordinate, end=" ")
        print()

This function takes the map and the constructed list as parameters and then iterates through each coordinate, checking if anything has been built on it and uses ANSI escape codes to print the constructed coordinates in colour. Building a dedicated function also has the advantage of making it easier to call a single function to print your entire map instead of printing it row by row using manual indexes since the function can adapt to a changed map size.

Also it isn't advised to use the variable name map since it will override the in-built map function.

Mridul
  • 127
  • 7