__author__ = 'Bob' #Python 3 code ,so probably wont run on FIDDLE import random GD=[[0,"||"]] Xs=[] Os=[] Free=[1,2,3,4,5,6,7,8,9] board_size=9 c1=[] c2=[] c3=[] d1=[] r1=[[1,2,3]] r2=[] r3=[] d2=[] check_list=[r1,r2,r3,c1,c2,c3,d1,d2] won=False for i in range(1,(boardsize+1)): u=[int(i),"?"] GD.append(u)#creates new list of number 1-9 and ? value , and each of these is appended to GD list (now list of lists) def print_board(): print(" ",GD[1][1],v,GD[2][1],v,GD[3][1],"\n",'='*13,"")#prints first line , box 1-3 plus divider line print(" ",GD[4][1],v,GD[5][1],v,GD[6][1],"\n",'='*13,"")#prints 2nd line , box 4-6 plus divider line print(" ",GD[7][1],v,GD[8][1],v,GD[9][1],"\n")#prints last line , box 7-9 '''lots of list comprehensions and COPIES of lists next''' def refresh_memory(gamepiece): for i in check_list: i=[]#clear out list on new check global check_list #removed if status=="create": for i in GD:#for each list in GD x=(i[0])#i[0] checks value if x==1: d1.append(x)#1 for example r1.append(x) c1.append(x) if x==2: c2.append(x) r1.append(x) if x == 3: c3.append(x) r1.append(x) d2.append(x) if x == 4: r2.append(i) c1.append(i) if x ==5: c2.append(x) r2.append(x) d1.append(i) d2.append(x) if x ==6: c3.append(x) r2.append(x) if x == 7: d2.append(x) r3.append(x) c1.append(x) if x == 8: append(x) r3.append(x) if x == 9: c3.append(x) r3.append(x) d1.append(x) def board_check(status): for i in check_list: count_x = 0 count_o = 0 if status == "player": print("checking player") for j in i: if j[1]=="X": count_x+=1 #count xs in list of lists print(count_x) if count_x == "3": game_end("player") if status=="cpu": for j in i: if count_o==2 and count_x==0: cpu_move(i,"atk") if count_x==2 and count_o==0: cpu_move(i,"def") else: if j[1] == "O": count_o += 1 if j[1]=="X": count_x += 1 #make random cpu move def game_end(winner): global won print ("\n\n\r",winner," won") won=True quit() def player_move(): print_board() P1=int((input("enter a number between 1 and nine to pick a square >>"))) if P1 in Free: GD[P1][1]="X" Xs.append(P1) Free.remove(P1) board_check("player") else: print("not available , try again") def cpu_move(line,move): print("CPU_MOVING") for i in line: if i[1]=="?": print(GD[i]) print (i) #place O here if move=="atk": print (move) #game_end("cpu") else: print (move) print("*"*30) def main_new(): refresh_memory("create") while len(Free)>0 and won=="False: player_move() main_new()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question