Python Forum
Please help with this simple tic tac toe script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help with this simple tic tac toe script
#1
Please explain why variable"r" doesnt change and how to get tic tac toe game then ...
r = "g1" #zmienna pomocnicza do zarządzania kolejnymi ruchami graczy
def g1():

    while(True):
        r = "g1"
        if inp[0]=="1" and inp[1]=="1" and li[0][0]=="_": #to [0] to będzie pierwsza cyfra a [1] druga z podanych współrzędnych
            li[0][0]="X"
            r="g2" # i dont understand why variable"r" doesnt change its value to "g2" and how to get around this
            break
        elif inp[0]=="1" and inp[1]=="2" and li[0][1]=="_": #lub
            li[0][1]="X"
            r = "g2"
            break
        elif inp[0]=="1" and inp[1]=="3" and li[0][2]=="_": #lub
            li[0][2]="X"
            r = "g2"
            break
        #Doświadczony programista jakby zobaczył taką ilość elif to rzucił by "kur$@"
        elif inp[0]=="2" and inp[1]=="1" and li[1][0]=="_": #lub
            li[1][0]="X"
            r = "g2"
            break
        elif inp[0]=="2" and inp[1]=="2" and li[1][1]=="_": #lub
            li[1][1]="X"
            r = "g2"
            break
        elif inp[0]=="2" and inp[1]=="3" and li[1][2]=="_": #lub
            li[1][2]="X"
            r = "g2"
            break
        #ale ja nie jestem "doświadczonym"
        elif inp[0]=="3" and inp[1]=="1" and li[2][0]=="_": #lub
            li[2][0]="X"
            r = "g2"
            break
        elif inp[0]=="3" and inp[1]=="2" and li[2][1]=="_": #lub
            li[2][1]="X"
            r = "g2"
            break
        elif inp[0]=="3" and inp[1]=="3" and li[2][2]=="_": #lub
            li[2][2]="X"
            r = "g2"
            break
        else:
            print("Fatal Error")
            break
        break
def g2():
    r="g2"
    while (True):
        if inp[0] == "1" and inp[1] == "1" and li[0][
            0] == "_":  # to [0] to będzie pierwsza cyfra a [1] druga z podanych współrzędnych
            li[0][0] = "O"
            r = "g1"
            break
        elif inp[0] == "1" and inp[1] == "2" and li[0][1] == "_":  # lub
            li[0][1] = "O"
            r = "g1"
            break
        elif inp[0] == "1" and inp[1] == "3" and li[0][2] == "_":  # lub
            li[0][2] = "O"
            r = "g1"
            break

        elif inp[0] == "2" and inp[1] == "1" and li[1][0] == "_":  # lub
            li[1][0] = "O"
            r = "g1"
            break
        elif inp[0] == "2" and inp[1] == "2" and li[1][1] == "_":  # lub
            li[1][1] = "O"
            r = "g1"
            break
        elif inp[0] == "2" and inp[1] == "3" and li[1][2] == "_":  # lub
            li[1][2] = "O"
            r = "g1"
            break

        elif inp[0] == "3" and inp[1] == "1" and li[2][0] == "_":  # lub
            li[2][0] = "O"
            r = "g1"
            break
        elif inp[0] == "3" and inp[1] == "2" and li[2][1] == "_":  # lub
            li[2][1] = "O"
            r = "g1"
            break
        elif inp[0] == "3" and inp[1] == "3" and li[2][2] == "_":  # lub
            li[2][2] = "O"
            r = "g1"
            break
        else:
            print("Fatal Error")
            break
        break
    #koniec funkcji gR

li=[["_","_","_"],["_","_","_"],["_","_","_"]]# zajżyj do 50_1

#chyba o taki efekt chodziło
while(True):
    print("główna pętla")
    if r=="g1":
        inp=input("Gracz1[X]: podaj współrzędne:") #tutaj miałem error "subscritable" jak miałem int i troche nie rozumiem tego
        g1()
        print("TOP DJ TOP PJ")
        for i in li:  # bierze indeks li 0 0
            for j in i:  # później bierze indeks 0 1 dalej 0 2 a następnie 1 0 i tak dalej
                print(j, end=" ")
            print(" ")  # idzie do nowej lini po 3{0,1,2} kolejnych indeksach z li[0]
    else:
        inp=input("Gracz2[O]: podaj współrzędne:")
        g2()
        print("TOP KJ TOP VJ")
        for i in li:  # bierze indeks li 0 0
            for j in i:  # później bierze indeks 0 1 dalej 0 2 a następnie 1 0 i tak dalej
                print(j, end=" ")
            print(" ")  # idzie do nowej lini po 3{0,1,2} kolejnych indeksach z li[0]

    #i tu powtarza sie główna pętla while
#TLDR; można to byłoby napisać milion razy lepiej
PyCharm Python 3.8
Reply
#2
Omg I'm retard. Just add into defined function global r
def functionname():
    global r
I just didnt see it cause in Lua all var are by standard globals and you need to add 'local' before them to make them local
Reply
#3
Avoid using globals, as they lead to programs that are hard to follow and hard to maintain. Also, use meaningful names for your variables, functions, etc. Doing so makes your code easier to read and understand.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with a simple script Niko047 2 3,340 Jul-21-2017, 09:02 PM
Last Post: Niko047

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020