Click here to Skip to main content
16,016,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 20-Oct-21 20:10pm

1 solution

When you "drop" a token, you need to scan the column and find the first "used slot". The simplest way to do that is to expand the storage area by one in all directions (so that the board matrix for a 3 x 3 board is 5 x 5) and fill the "edges" with a "player three" value:
3 3 3 3 3
3 0 0 0 3
3 0 0 0 3
3 0 0 0 3
3 3 3 3 3
Now, to drop a token in column 1, you can use the row number as an index and us a loop to "scan" the column form row 1 onward looking for a non-zero value. When you find it, you can insert your token in the previous row. Assuming it's player 1's turn:
3 3 3 3 3
3 x 0 0 3
3 0 0 0 3
3 0 0 0 3
3 3 3 3 3
Zero - ignore.
3 3 3 3 3
3 0 0 0 3
3 x 0 0 3
3 0 0 0 3
3 3 3 3 3
Zero - ignore.
3 3 3 3 3
3 x 0 0 3
3 0 0 0 3
3 x 0 0 3
3 3 3 3 3
Zero - ignore.
3 3 3 3 3
3 x 0 0 3
3 0 0 0 3
3 0 0 0 3
3 x 3 3 3
Non-zero - it fits in the previous row:
3 3 3 3 3
3 0 0 0 3
3 0 0 0 3
3 1 0 0 3
3 3 3 3 3
If player 2 then drops a token in the same column, it will find a non-zero earlier, and fill the row above that.

Checking works the same way: have four loops which count player "n" tokens in a line: first horizontally, then vertically, then diagonally right from the bottom up, then diagonally right from the top down. If a count reaches the "win value" (4 in Connect4) then that player has won. If none do, the game isn't over.
Put that into a function so you can pass the Player N value as a parameter, and call it twice, once for each player.

Try it on paper first, and you'll see what I mean.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900