简体   繁体   中英

How to Replace Connect Four Code with a For Loop

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!

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))

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM