Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from guizero import App, Box, Text, Picture, info
- app = App(title="Os and Xs", width = 480, height = 320, bg=(204,204,204), layout ="grid")
- blank = "25fb.png"
- X = "274c.png"
- O = "2b55.png"
- msquare = [0,2,7,6,9,5,1,4,3,8]
- Xmoves=[]
- Omoves=[]
- def testwin(set,letter):
- total = 0
- if (len(set) == 3):
- for count in range(3):
- total = total+set[count]
- if (len(set) == 4):
- if (set[1]+set[2]+set[3]==15 or set[0]+set[1]+set[3]==15 or set[0]+set[2]+set[3]==15):
- total = 15
- if (len(set) == 5):
- if(set[2]+set[3]+set[4]==15 or set[0]+set[1]+set[4]==15 or set[0]+set[2]+set[4]==15 or set[1]+set[3]+set[4]==15 or set[1]+set[2]+set[4]==15 or set[0]+set[3]+set[4]==15):
- total=15
- if total == 15:
- info("We Have a Winner!",letter+" wins this game!")
- def error():
- info("illegal move","Sorry, that box is already taken!")
- def move(event_data):
- boxclicked = event_data.widget
- square=boxclicked.master.grid[0]+3*boxclicked.master.grid[1]-2
- if boxclicked.image == blank:
- boxclicked.image= img_turn.image
- if img_turn.image == X:
- Xmoves.append(msquare[square])
- testwin(Xmoves,"X")
- img_turn.image=O
- else:
- Omoves.append(msquare[square])
- testwin(Omoves,"O")
- img_turn.image=X
- else:
- error()
- box = [[]]
- img_box=[[]]
- header = Box(app, width=480, height=20, grid=[0,0,5,1])
- for x in range (0,3):
- for y in range (1,4):
- n = x*3+y
- box.append(Box(app,width=72,height=72,border=1, grid=[x,y]))
- spacer1 = Box(app,width=181,height=219, grid=[3,1,1,3])
- spacer2 = Box(app,width=80, height=146, grid=[4,1,1,2])
- turn = Box(app,width=80,height=73,grid=[4,3])
- header.bg=(0,0,0)
- #spacer1.bg=(255,0,0)
- #spacer2.bg=(255,0,0)
- txt_header=Text(header,text="Tic-Tac-Toe")
- txt_header.text_color=(255,255,255)
- txt_spacer2=Text(spacer2,align="bottom",text="Turn:")
- img_turn=Picture(turn,O)
- for count in range(1,10):
- img_box.append(Picture(box[count],blank))
- for count in range(1,10):
- img_box[count].when_clicked = move
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement