Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7932227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:57:06+00:00 2026-06-03T20:57:06+00:00

I am currently trying to write a program that is a connect four game

  • 0

I am currently trying to write a program that is a connect four game with zero gravity, meaning you can place the piece from anyside of the board, I am currently working on selecting a column and placing my piece from the top. Also my program ask the user how many blocks they would like to place on the board making the game harder. At the moment when I type for instance C8 its looks through the 8th row in the list on the 8th element in each list. Any thoughts?

My edited code is as follows:

#this imports random for my blocks 
import random




#this makes my board
table = [[ "   ","C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10"],
         [ " R1|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
         [ " R2|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
         [ " R3|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
         [ " R4|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
         [ " R5|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
         [ " R6|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
         [ " R7|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
         [ " R8|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
         [ " R9|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
       [ "R10|" ," |", " |", " |", " |", " |", " |", " |", " |", " |", " |"]]
#my two different player pieces
player1="#|"
player2="@|"
block="B|"
row=11
columns=11
#this is a function i can call to print the table
def printTable(table):
    for row in table:
        for value in row:
            print (value,end=' ')
        print ('\n')

#this ask the user how many blocks they want on their board then places it on there
def block(table):
    blockQuestion=(input("how many blocks would you like to put on the table? \n (please enter a number between 1 and 25)"))
    number=blockQuestion[:2]
    number=int(number)
    number=number-1
    print("this is number ",number)
    count = 0      
    number=int(number)
    while number >= count:
        x=random.randint(1,10)
        y=random.randint(1,10)
        print("x and y ", x,y)

        table[x][y]="B|"
        count +=1

    printTable(table)
    move()



#this is my function to ask the user for their move.
def move():
    move=input("Please choose where you would like to drop your marker \n (For instance C1, R5)")
    move.split()
    rorc=move[0:1]
    ans=move[1:3]
    answer=int(ans)

    print(rorc," ",answer)

    if "R" in move:
        row(answer)

    if "C" in move:
        col(answer)
#this is my function if the user wants to go by row
def row(answer):
    side=input("would you like to insert it from the right or left \n Please type R for right or L for left ")

    if "R" in side:

        try:
            table[answer].reverse()

            blockCheck=table[answer].index("B|")
            if blockCheck == 0:
                print ("you can not place a peice there because of the block")
                tryAgain()                   
        except:
            try:
                p1Check=table[answer].index("#|")
                if p1Check is 0:
                    print ("you can not place a peice there due to the opponents marker")
                    tryAgain()
            except:
                try:
                    p2Check=table[answer].index("@|")
                    if p2check is 0:
                        print ("you can not place a peice there due to the opponents marker")
                        tryAgain()
                except:
                    print('hi')
    try:
        tits=max(blockCheck,p1Check,p2Check)
        print("All three checks",tits)

    except:
        try:
            tits=max(blockCheck,p1Check)
            print("this is bc and p1c",tits)
        except:
            tits=(blockCheck)
            print("this is block check",tits)
    table[answer].reverse()
    table[answer][-tits]= (player1)
    printTable(table)
#this is my function if the user wants to go by columns
def col(answer):
    side=input("would you like to insert it from the top or bottom \nPlease type T for top or B for bottom")
    answer=int(answer)
    if "T" in side:
        try:   
            blockCheck=table[:][answer].index("B|")

            print("blockCheck ", blockCheck)
            if blockCheck == 1:
                print ("you can not place a peice there because of the block")
                tryAgain()
        except:
            try:
                p1Check=table[answer].index("#|")
                if p1Check is 1:
                    print ("you can not place a peice there due to the opponents marker")
                    tryAgain()
            except:
                try:
                    p2Check=table[answer].index("@|")
                    if p2check is 1:
                        print ("you can not place a peice there due to the opponents marker")
                        tryAgain()
                except:
                    print("whaa")       
        try:
            tits=min(blockCheck,p1Check,p2Check)
            print("All three checks",tits)

        except:
            try:
                tits=min(blockCheck,p1Check)
                print("this is bc and p1c",tits)
            except:
                try:
                    tits=(blockCheck)
                    print("this is block check",tits)
                except:
                    tits=11
        table[tits-1][answer]= (player2)
        printTable(table)
#function to restart the program to the move function
def tryagain():
    tryAgain=input('try again \nPlease type y or n ')
    if tryAgain == 'y':
        move()
    elif tryAgain =='n':
        bash





#calls the function to start the program 
block(table)

thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:57:08+00:00Added an answer on June 3, 2026 at 8:57 pm

    As you have no doubt already noticed, it’s very difficult to debug a long chain of if statements of the kind you have here. In fact, most well-written programs rarely contain anything as long as your move function. Linus Torvalds, in his style guide for the linux kernel, says this:

    Functions should be short and sweet, and do just one thing. They should fit on one or
    two screenfuls of text (the ISO/ANSI screen size is 80×24, as we all know), and do one
    thing and do that well.

    In other words, even (especially!) when you’re programming something as incredibly complex as a modern operating system kernel, your functions shouldn’t take up more than 48 lines of code — and he’s talking about c code there, which is a lot more verbose than Python. You’re breaking that rule pretty terribly here; your move function is more than 100 lines long!

    So my answer is not really an answer, but a meta-answer. Break that move function up into several smaller functions. For every kind of input you require, write a separate function that takes the input, makes sure it’s correct, and converts it into a value that your program can use. Shift as much of the above if logic into those functions as possible. Then have your move function call those functions, and apply whatever top-level logic is necessary. I see at least three, and probably five or six different functions glommed together in that one giant function.

    Once you’ve divided it up, test each function individually with print statements to make sure they’re doing what you expect. Once you’ve done that, and you’re sure they’re behaving as expected, you’ll know that your problem is isolated in the top-level move function; from there, it shouldn’t be hard to debug at all — for us or for you!


    Your edits have made things better. But it’s clear to me that you don’t have an intuitive sense of how functions ought to, well, function. Consider how you’ve refactored the above code. You’ve written a function. Then at the end of that function, you call another function. Then at the end of that function you call another. You’re chaining functions over and over again. If I drew a call diagram of your code above, it would look kind of like this:

    block
     |
     -> move
         |
         -> row | col
             |
             -> ...keep on calling new functions...          
    

    That’s not how it’s supposed to work. Instead, you should have a single main function that calls other functions and gets the values they return. Consider this simple example:

    def get_val():
        x = None
        while x is None:
            x = raw_input('Give me a value: ')
            try:
                x = int(x)
            except ValueError:
                print('Value must be an integer')
                x = None
        return x
    
    def transform_val(x):
        return x * 2
    
    def main():
        val = get_val()
        val = transform_val(val)
        print(val)
    

    You see that the two functions above don’t call other functions. They just do something and return. The call diagram would look like this:

    main
     |
     -> get_val
      <- |
     |
     -> transform_val
      <- |
     |
    

    You see that in this case, main is at the “top level” of the program, and it’s in control of everything. Restructure your code to look like that. You should have a main function that calls other functions that operate on your table. Since table is mutable, these mostly needn’t return anything, but they definitely shouldn’t end with another function call. Move that down into the main function:

    def main():
        # define some variables here
        block(table)
        while no_winner(table):
            move(table)
    

    Here no_winner looks at the table to see if someone has won, or if no more moves are possible; it does return something — True or False, depending on whether another move is possible. move takes input from the user and transforms the table based on it. Now the loop contains all the top-level logic you need. Then at the bottom of your program, do this to initiate your main program. (This is just the standard way to invoke main in Python; don’t worry about what __name__ means for now:

    if __name__ == '__main__':
        main()
    

    I know I’m still not answering your actual question; but I think this stuff is actually way more important than the specific bug you’re looking for. I’ll start trying to find it now though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to write a program that alphabetizes strings. I have one
I'm trying to write a program that uses sockets to connect with other instances
it's a week that I'm trying to write a program that download correctly from
I'm trying to create a program that can be used by other people. Currently,
I am currently trying to write an addin for PowerPoint that whenever any PowerPoint
I am currently trying to get a script to write output from other started
I am currently trying to write a very simple app that sends an object
I'm currently trying to write a voicechat program in python. All tips/trick is welcome
I am trying to write a program that scan an ip range and detect
I'm trying to write a simple program that will compare the files in separate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.