Skip to content

Renderers module

Module with classes and methods to handle UI Provide the classes handle human players.

This module allows the handle CLI arguments and options.

The module contains the following classes: - ConsoleRenderer(Renderer) - A class to handler render UI in the console.

The module contains the following functions: - clear_screen() -> None: - Clear console, like command reset on modern Linux systems. - blink(text: str) -> str: - Modify information to be print to console and add slow blinking - print_blinking(cells: Iterable[str], positions: Iterable[int]) -> None: - Add blinking ANSI code to positions in cells. - print_solid(cells: Iterable[str]) -> None: - Render game UI. It uses textwrap and format to replace on the correct position..

ConsoleRenderer

Bases: Renderer

A class to handler render UI in the console. Extend abstract class Renderar for the creation of visual and state rendering

Methods:

Name Description
render

GameState) -> None: Renders a new UI depending on game state.

Source code in src\frontend\console\renderers.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class ConsoleRenderer(Renderer):
    """A class to handler render UI in the console. Extend abstract class Renderar
        for the creation of visual and state rendering

    Methods:
        render(self, game_state: GameState) -> None:
            Renders a new UI depending on game state.
    """
    def render(self, game_state: GameState) -> None:
        """Renders a new UI depending on game state.

        Args:
            game_state (GameState): current GameState, consisting of a current Grid (9 elemets that
                that can be X, O or spaces) and a starting Mark (default X).
        """
        clear_screen()
        if game_state.winner:
            print_blinking(game_state.grid.cells, game_state.winning_cells)
            print(f"{game_state.winner} wins \N{party popper}")
        else:
            print_solid(game_state.grid.cells)
            if game_state.tie:
                print("No one wins this time \N{neutral face}")

    def placeholder2(self) -> None:
        """This is a placeholder
        """

placeholder2()

This is a placeholder

Source code in src\frontend\console\renderers.py
49
50
51
def placeholder2(self) -> None:
    """This is a placeholder
    """

render(game_state)

Renders a new UI depending on game state.

Parameters:

Name Type Description Default
game_state GameState

current GameState, consisting of a current Grid (9 elemets that that can be X, O or spaces) and a starting Mark (default X).

required
Source code in src\frontend\console\renderers.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def render(self, game_state: GameState) -> None:
    """Renders a new UI depending on game state.

    Args:
        game_state (GameState): current GameState, consisting of a current Grid (9 elemets that
            that can be X, O or spaces) and a starting Mark (default X).
    """
    clear_screen()
    if game_state.winner:
        print_blinking(game_state.grid.cells, game_state.winning_cells)
        print(f"{game_state.winner} wins \N{party popper}")
    else:
        print_solid(game_state.grid.cells)
        if game_state.tie:
            print("No one wins this time \N{neutral face}")

Modify information to be print to console and add slow blinking [5m slow blink and [0m reset.

Source code in src\frontend\console\renderers.py
57
58
59
60
61
def blink(text: str) -> str:
    """Modify information to be print to console and add slow blinking [5m slow blink
    and [0m reset.
    """
    return f"\033[5m{text}\033[0m"

clear_screen()

Clear console, like command reset on modern Linux systems.

Source code in src\frontend\console\renderers.py
52
53
54
55
def clear_screen() -> None:
    """Clear console, like command reset on modern Linux systems.
    """
    print("\033c", end="")

print_blinking(cells, positions)

Add blinking ANSI code to positions in cells.

Parameters:

Name Type Description Default
cells Iterable[str]

Lits of all cells

required
positions Iterable[int]

List of positions

required
Source code in src\frontend\console\renderers.py
63
64
65
66
67
68
69
70
71
72
73
def print_blinking(cells: Iterable[str], positions: Iterable[int]) -> None:
    """Add blinking ANSI code to positions in cells.

    Args:
        cells (Iterable[str]): Lits of all cells
        positions (Iterable[int]): List of positions
    """
    mutable_cells = list(cells)
    for position in positions:
        mutable_cells[position] = blink(mutable_cells[position])
    print_solid(mutable_cells)

print_solid(cells)

Render game UI. It uses textwrap and format to replace on the correct position.

Parameters:

Name Type Description Default
cells Iterable[str]

description

required
Source code in src\frontend\console\renderers.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def print_solid(cells: Iterable[str]) -> None:
    """Render game UI. It uses textwrap and format to replace on the correct position.

    Args:
        cells (Iterable[str]): _description_
    """

    print(
        textwrap.dedent(
            """\
             A   B   C
           ------------
        1 ┆  {0} │ {1} │ {2}
          ┆ ───┼───┼───
        2 ┆  {3} │ {4} │ {5}
          ┆ ───┼───┼───
        3 ┆  {6} │ {7} │ {8}
    """
        ).format(*cells)
    )