简体   繁体   中英

Python connect four, cannot use libraries

I want to make connect four in python 3.7, and we got stuck pretty quickly, we're really new so thats why. We bassicly want to drop the 1 and 2, for player 1 and 2, down in the grid below, but when we give input as player 1 and 2 on the same column, it doesn't work. I would love for someone to help us out as we've been stuck on this for a long time, thanks in advance! ps. We don't want to use any plugins or additions to python, just regular if, def, while etc statements.

ROW_COUNT = 6
COLUMN_COUNT = 7

row6 = [0, 0, 0, 0, 0, 0, 0]
row5 = [0, 0, 0, 0, 0, 0, 0]
row4 = [0, 0, 0, 0, 0, 0, 0]
row3 = [0, 0, 0, 0, 0, 0, 0]
row2 = [0, 0, 0, 0, 0, 0, 0]
row1 = [0, 0, 0, 0, 0, 0, 0]
Board=[row6, row5, row4, row3, row2, row1]

def drop_piece(Board, row, Column, piece):
    Board[row][Column] = piece

def is_valid_location(Board, Column):
    return Board[0][Column] ==0

def get_next_open_row(Board, Column):
    for r in range(ROW_COUNT):
        if Board[r][Column]==0:
            return r

gameOver = False
turn = 0
while not gameOver:
    if turn == 0:
        Column = int(input("Player 1, Make your turn(0-6):"))
        if is_valid_location(Board ,Column):
            row = get_next_open_row(Board, Column)
            drop_piece(Board, row, Column, 1)
            turn = turn + 1
    else:
        Column = int(input("Player 2, Make your turn(0-6):"))
        if is_valid_location(Board, Column):
            row = get_next_open_row(Board, Column)
            drop_piece(Board, row, Column, 2)
        turn = turn - 1

    print(row1)
    print(row2)
    print(row3)
    print(row4)
    print(row5)
    print(row6)

This should get you going again, still need the gameOver part written.

One issue is you defined the display as:

row6 = [0, 0, 0, 0, 0, 0, 0]
row5 = [0, 0, 0, 0, 0, 0, 0]
row4 = [0, 0, 0, 0, 0, 0, 0]
row3 = [0, 0, 0, 0, 0, 0, 0]
row2 = [0, 0, 0, 0, 0, 0, 0]
row1 = [0, 0, 0, 0, 0, 0, 0]
Board=[row6, row5, row4, row3, row2, row1]

But, printing the output with

print(row1) #Prints First
print(row2)
print(row3)
print(row4)
print(row5)
print(row6) #Prints Last

Makes it appear on the screen as:

row1 = [0, 0, 0, 0, 0, 0, 0]
row2 = [0, 0, 0, 0, 0, 0, 0]
row3 = [0, 0, 0, 0, 0, 0, 0]
row4 = [0, 0, 0, 0, 0, 0, 0]
row5 = [0, 0, 0, 0, 0, 0, 0]
row6 = [0, 0, 0, 0, 0, 0, 0]

Another is your toggle is easily broken:

if turn == 0:
    if is_valid_location(Board ,Column):
        turn = turn + 1
else:
    turn = turn - 1

This means the only way turn can increment is if turn is 0 and the Column is a valid location, otherwise it will continue to be player 2's turn and keep decrementing.

This is my code, the display is now made dynamically, based on the rows and columns set in the count variables, the toggle is boolean based, and only switches the player, added validation for the users choice, and the printing is done in a loop:

ROW_COUNT = 6
COLUMN_COUNT = 7

Board = [] # Define the Board list
# Appends a list of 0s COLUMN_COUNT long, to the Board list, ROW_COUNT amount of times.
for x in range(ROW_COUNT): Board.append(list([0] * COLUMN_COUNT))

def drop_piece(Board, row, Column, piece):
    Board[row][Column] = piece

def is_valid_location(Board, Column):
    # Checks that the top row for the selected column is 0
    return Board[-1][Column] == 0

def get_next_open_row(Board, Column):
    for r in range(ROW_COUNT):
        if Board[r][Column]==0:
            return r

gameOver = False

turn = True

while not gameOver:
    # Boolean toggle True = player 1, False = player 2
    if turn: player = 1
    else: player = 2
    Column = int(input("Player " + str(player) +", Make your turn(0-6):"))
    if is_valid_location(Board, Column):
        row = get_next_open_row(Board, Column)
        drop_piece(Board, row, Column, player)
        turn = not turn
    else: print("Invalid selection")

    # prints the reversed Board, since printing needs to be last printed first
    for row in reversed(Board):
        print(row)

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