i am a beginner of using python,and i am doing an project about "Tic-Tac-Toe".
well,the project needs very basci code.....however,i could not get it....
The following conditons are the project requested:
1.When the computer randonm selected a available number,so which means computer doesn have logic....the winner are user.....
2.user and computer can tie...we dont care computer wins.
3.use "List"
:confused:

However,i only can not figure out.....am I stupid??stupid logic??:S
can anyone help me?It is urgent.....:(
Thank you guys!:sweat:

import random
L=['1','2','3','4','5','6','7','8','9']
AL=['1','2','3','4','5','6','7','8','9']
s='-------\n|1|2|3|\n-------\n|4|5|6|\n-------\n|7|8|9|\n-------'
print(s)
u='O'
c='X'
first= random.randint(1,2)
while True:    
    if first ==1:
        uc=input("Enter your number,your number will be changed to sign 'o': ")
        L[int(uc)-1]=u
        AL.remove(uc)
        print(AL)
        print(L)
        s=s.replace(uc,u)
        print(s)
        cc=random.randint(0,len(AL)-1)
        print("Computer Choice: ",cc)
        L[int(cc)-1]=c
        AL.remove (AL[cc])
        #AL.pop(int(cc)-1)
        print(AL)
        print(L)
        s=s.replace(L[cc],c)
        print(s)
    else:
        cc=random.randint(0,len(AL)-1)
        L[int(cc)-1]=c
        AL.remove(AL[cc])
        print(AL)
        s=s.replace(L[cc],c)
        print(s)
        uc=input("Enter your number,your number will be changed to sign 'o': ")
        L[int(uc)-1]=u
        AL.remove(int(uc)-1)
        print(AL)
        s=s.replace(uc,u)
        print(s)

    if L[0]==L[1]==L[2]:
        print("Congraduation!You have won the game!")
        break
    elif L[3]==L[4]==L[5]:
        print("Congraduation!You have won the game!")
        break
    elif L[6]==L[7]==L[8]:
        print("Congraduation!You have won the game!")
        break
    elif L[0]==L[3]==L[6]:
        print("Congraduation!You have won the game!")
        break
    elif L[1]==L[4]==L[7]:
        print("Congraduation!You have won the game!")
        break
    elif L[2]==L[5]==L[8]:
        print("Congraduation!You have won the game!")
        break
    elif L[0]==L[4]==L[8]:
        print("Congraduation!You have won the game!")
        break
    elif L[2]==L[4]==L[6]:
        print("Congraduation!You have won the game!")
        break

Recommended Answers

All 4 Replies

anyone can help me......???

canyvr i think tonyjv's link got a cooked dinner for you ;)

However,i only can not figure out.....am I stupid??stupid logic??
can anyone help me?It is urgent..

No one cares if you are stupid or are stupid twice and have waited 'till the last minute. Ask a specific question.

For starters, break this into functions. For example, use one function to choose a square. If the player is the user, ask for an input from the squares available, and if it is the computer, do a random choice and return the selection.

Also, this line
cc=random.randint(0,len(AL)-1)
returns an integer in the range, but the squares will not all be available. Use
random.choice(AL) instead (and print these things while testing so you know if it is working).

Finally, you should have a separate function to check for a winner and can use

def winner_check(L):
    winner_list = [ [0, 1, 2], [3, 4, 5], [6, 7, 8] ] ## etc
    for each_list in winner_list:
        print "checking", each_list[0], each_list[1], each_list[2] 
        if L[each_list[0]] == L[each_list[1]] == L[each_list[2]]:
            print("Congraduation!You have won the game!")
            return True
    return False

def tie_check(L, AL):
    ## no squares left and no winner
    if (len(AL)==0) and (not winner_check(AL)):
        print "Tie"
        ## exit program

If you break the code up into functions and use variable names that are descriptive, (available_squares instead of AL), so the code is readable you will probably get more help. No on wants to clean up your mess.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.