![]() |
Please help with this simple tic tac toe script - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Please help with this simple tic tac toe script (/thread-24769.html) |
Please help with this simple tic tac toe script - ErtYwek - Mar-03-2020 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 lepiejPyCharm Python 3.8 RE: Please help with this simple tic tac toe script - ErtYwek - Mar-03-2020 Omg I'm retard. Just add into defined function global r def functionname(): global rI 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 RE: Please help with this simple tic tac toe script - ndc85430 - Mar-06-2020 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. |