Advertisement
davidhellam

Python: tic-tac-toe 2

Aug 13th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. from guizero import App, Box, Text, Picture, info
  2.  
  3. app = App(title="Os and Xs", width = 480, height = 320, bg=(204,204,204), layout ="grid")
  4. blank = "25fb.png"
  5. X = "274c.png"
  6. O = "2b55.png"
  7. msquare = [0,2,7,6,9,5,1,4,3,8]
  8. Xmoves=[]
  9. Omoves=[]
  10.  
  11. def testwin(set,letter):
  12.     total = 0    
  13.     if (len(set) == 3):
  14.         for count in range(3):
  15.             total = total+set[count]  
  16.     if (len(set) == 4):
  17.         if (set[1]+set[2]+set[3]==15 or set[0]+set[1]+set[3]==15 or set[0]+set[2]+set[3]==15):
  18.             total = 15
  19.     if (len(set) == 5):
  20.         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):
  21.             total=15
  22.     if total == 15:
  23.         info("We Have a Winner!",letter+" wins this game!")    
  24.  
  25. def error():
  26.     info("illegal move","Sorry, that box is already taken!")
  27.        
  28. def move(event_data):
  29.     boxclicked = event_data.widget
  30.     square=boxclicked.master.grid[0]+3*boxclicked.master.grid[1]-2
  31.     if boxclicked.image == blank:
  32.         boxclicked.image= img_turn.image
  33.         if img_turn.image == X:
  34.             Xmoves.append(msquare[square])
  35.             testwin(Xmoves,"X")
  36.             img_turn.image=O
  37.         else:
  38.             Omoves.append(msquare[square])
  39.             testwin(Omoves,"O")
  40.             img_turn.image=X
  41.     else:
  42.         error()
  43.    
  44. box = [[]]
  45. img_box=[[]]
  46. header = Box(app, width=480, height=20, grid=[0,0,5,1])
  47. for x in range (0,3):
  48.     for y in range (1,4):
  49.         n = x*3+y
  50.         box.append(Box(app,width=72,height=72,border=1, grid=[x,y]))
  51. spacer1 = Box(app,width=181,height=219, grid=[3,1,1,3])
  52. spacer2 = Box(app,width=80, height=146, grid=[4,1,1,2])
  53. turn = Box(app,width=80,height=73,grid=[4,3])
  54. header.bg=(0,0,0)
  55. #spacer1.bg=(255,0,0)
  56. #spacer2.bg=(255,0,0)
  57. txt_header=Text(header,text="Tic-Tac-Toe")
  58. txt_header.text_color=(255,255,255)
  59. txt_spacer2=Text(spacer2,align="bottom",text="Turn:")
  60. img_turn=Picture(turn,O)
  61.  
  62. for count in range(1,10):
  63.     img_box.append(Picture(box[count],blank))
  64.  
  65. for count in range(1,10):
  66.     img_box[count].when_clicked = move
  67.  
  68. app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement