Skip to content
Advertisement

How can I print another version of the board without actually changing it?

Suppose the

JavaScript

and I want to have a function that print the board in a readable version which would be like

JavaScript

This method does not change the actual board. It prints 'X' for 1, 'O' for 2, and empty space (' ') for 0.

Advertisement

Answer

You can use a translation dictionary and a nested list comprehension:

JavaScript

Note that you can also use a string instead of a dictionary as @jasonharper mentioned but it will only work for this case, where keys start with 0 and are sequential:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement