I'm developing a
Connect 4 Python program. When I'm attempting to play the game and trying to "stack" the tokens, instead of doing so they just replace each other after each turn if the other/same user picks the same column. For example, this will be the output when the first user inputs '1' as in place in the first column:
[1, 2, 3, 4, 5, 6, 7]
---------------------
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0]
However, if the second player tries to place another token in the first column, instead of stacking/being shown above it will show this:
[1, 2, 3, 4, 5, 6, 7]
---------------------
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[2, 0, 0, 0, 0, 0, 0]
How am I able to fix it so the
tokens can stack up on each other and therefore check when a player has
matched four tokens either horizontally, vertically, or diagonally? Additionally, since this is an assignment I was told I _can't_ use libraries. Any help/guidance would be much appreciated. My full code is shown below:
def create_board():
#Description: The first function in our program will create the board for our game
#Parameters: There will be no parameters in this function
#Return Value (RV): The return value will be the board
matriz = []
for i in range(6):
renglon = []
for r in range(7):
data = 0
renglon.append(data)
matriz.append(renglon)
return matriz
def print_board(matrix):
#Description: This function will print the board and the welcoming message
#Parameters: The parameter used for this function will be the variable that stores the matrix above
#RV: The return value will be the board printed
matrix = matrix
numeros = [1,2,3,4,5,6,7]
print("\n")
print(" ",numeros)
print(" ---------------------")
for i in range(len(matrix)):
print(" ",matrix[i])
print("\n")
pass
def turns(turn,player2,player1):
#Description: This function will 'moderate' the turns in which the players go
#Parameters: The parameters are the names of the players
#RV: The return value will be turn of the next player and so on
turn = turn
if turn%2==0:
turn = player2
elif turn%2!=0:
turn = player1
return turn
pass
def moves(matrix,turn,playernumber):
#Description: This function will be in charge of updating the board matrix after a player's turn
#Parameters: The parameter used for this function will be the variable for the board as the game goes on
#RV: The return value will be the updated board with the new token
matrix = matrix
turn = turn
row = -1
line = turn - 1
matrix[row][line] = playernumber
print_board(matrix)
for i in range(-1,-7,-1):
print(i)
if matrix[i][line]==1 or matrix[i][line]==2:
matrix[row][line] = playernumber
else:
matrix[row][line] = playernumber
pass
def winner(matrix):
#Description: This function will indicate the winner when a player is able to place 4 tokens in a row.
#Parameters: The parameter will be the constantly-changing board along with the number of moves.
#RV: The return value will simply print who of the two players won
pass
def main():
#Description: The main function is the place to input the names and moves of the players
#Parameters: No parameters will be needed in the main function
#RV: This function will return the work of the functions above
print("****************************************\n WELCOME TO CONNECT 4\nBe the first player to connect 4 of the \nsame tokens in a row (either verticaly,\nhorizontaly or diagonaly) wins.\n****************************************")
mtx = create_board()
print_board(mtx)
P1 = str(input("Player 1 Name: "))
while len(P1)<3:
P1 = str(input("Player 1 Name: "))
if len(P1)>=3:
P2 = str(input("Player 2 Name: "))
while len(P2)<3:
P2 = str(input("Player 2 Name: "))
if len(P2)>=3:
for i in range(42):
t = turns(i,P1,P2)
print("\n")
print(t)
c = int()
if t == P1:
c = 1
elif t == P2:
c = 2
mv = int(input("Choose your play (value from 1 to 7): "))
while mv>=8 or mv<1:
print("Try Again")
mv = int(input("Choose your play (value from 1 to 7): "))
else:
moves(mtx,mv,c)
main()
What I have tried:
I've attempted adding more functions as to check available slots and diagonal, horizontal, vertical examining but I can't seem to make it work.