-3

I have a Connect Four game with a board that I made with a bunch of print functions, which is incredibly inefficient. Here it is:

    print("   0   1   2   3   4   5   6")
    print("   " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " | " + board[0][3] + " | " + board[0][
        4] + " | " + board[0][5] + " | " + board[0][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " | " + board[1][3] + " | " + board[1][
        4] + " | " + board[1][5] + " | " + board[1][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " | " + board[2][3] + " | " + board[2][
        4] + " | " + board[2][5] + " | " + board[2][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[3][0] + " | " + board[3][1] + " | " + board[3][2] + " | " + board[3][3] + " | " + board[3][
        4] + " | " + board[3][5] + " | " + board[3][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[4][0] + " | " + board[4][1] + " | " + board[4][2] + " | " + board[4][3] + " | " + board[4][
        4] + " | " + board[4][5] + " | " + board[4][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[5][0] + " | " + board[5][1] + " | " + board[5][2] + " | " + board[5][3] + " | " + board[5][
        4] + " | " + board[5][5] + " | " + board[5][6])
    print("  ---+---+---+---+---+---+---")
    print("   " + board[6][0] + " | " + board[6][1] + " | " + board[6][2] + " | " + board[6][3] + " | " + board[6][
        4] + " | " + board[6][5] + " | " + board[6][6])
    print()

I want to know how I can replace my board using for loops such that the code will look neater and easier to change. Here is the rest of my code if you need any references.

https://paste.pythondiscord.com/buriritebe.md

Thank you!

Vix
  • 192
  • 9
  • 2
    Since you already know about `for` loops you should at least give it a go and update your question with the results. – quamrana Jan 23 '21 at 18:39
  • 2
    If your code is working and you are looking for advice to improve it, it might be a better fit for https://codereview.stackexchange.com/ – Thierry Lathuille Jan 23 '21 at 18:40

2 Answers2

3

When you're trying to roll your code into loops, look for where you're repeating yourself. For instance, it looks like every line print is the same with a different x coordinate. That's your outer loop. Within every line, each cell is the same with a different y coordinate. That's your inner loop. Loops can be in the form of comprehensions, too. Also, the string.join() method is your friend because the separator between cells and between rows is always gonna be the same. Python programmers love one-line solutions. I'm sure there's an even tighter way to write this, but you want to be careful not to roll things too tight because it makes changing your code in the future more difficult. Here's one way you could tighten it up:

def generate_line(x, board):
    return "   " + " | ".join(board[x][y] for y in range(7)) + "\n"
board = [['x','o','x','o','x','o','x'] for x in range(7)]
border = "  ---+---+---+---+---+---+---\n"
lines = [generate_line(x, board) for x in range(7)]
print(border.join(lines))
luthervespers
  • 258
  • 1
  • 2
  • 10
  • 1
    Nice balance of brevity and clarity, and uses clear function/variable names to make the intention easier to read. One minor suggestion: `lines = (generate_line(x, board) for x in range(7))` (with parentheses instead of brackets) makes it a [generator expression](https://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehensions) instead of a list comprehension. It's unlikely to matter here, but it's a good habit to get into, when possible, because it will be more efficient when processing longer data. – CrazyChucky Jan 23 '21 at 19:26
  • 1
    It might be, but when you want to use `join`, using a list comprehension is faster than a generator expression. See for example https://stackoverflow.com/a/32462254/550094 – Thierry Lathuille Jan 23 '21 at 20:34
  • Whoah! I was under the impression the opposite was true. That's a great find. [This question](https://stackoverflow.com/questions/34822676/joining-strings-generator-or-list-comprehension)'s answers are also informative. – CrazyChucky Jan 23 '21 at 21:29
1

This should do the trick:

print("   0   1   2   3   4   5   6")
for row in range(7):
    print("   " + board[row][0] + " | " + board[row][1] + " | " + board[row][2] + " | " + board[row][3] + " | " + board[row][4] + " | " + board[row][5] + " | " + board[row][6])
    if row != 6:
        print("  ---+---+---+---+---+---+---")
print()

and if you want to remove the long print

print("   0   1   2   3   4   5   6")
for row in range(7):
    line = "  "
    for col in range(7):
        line += (" " + board[row][col] + " |")
    print(line[:-1])
    if row != 6:
        print("  ---+---+---+---+---+---+---")
print()
Nicklas
  • 19
  • 5