Advertisement
here2share

# Tk_connect4.py

Jul 24th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. # Tk_connect4.py
  2.  
  3. # ZZZ might soon make quite a different game from this basic code
  4.  
  5. from Tkinter import *
  6. import random
  7.  
  8. main = Tk()
  9.  
  10. c = Canvas(main, width=400, height=500)
  11. c.pack()
  12.  
  13. for z in range(5):
  14.     z*=100
  15.     c.create_line(z+2, 2, z+2, 400+2)
  16.     c.create_line(2, z+2, 400+2, z+2)
  17.  
  18. grid = [
  19.     "0", "1", "2", "3",
  20.     "4", "5", "6", "7",
  21.     "8", "9", "10", "11",
  22.     "12", "13", "14", "15"
  23. ]
  24. # Klikk prosedyre
  25. def click(event):
  26.     across = int(c.canvasx(event.x) / 100)
  27.     down = int(c.canvasy(event.y) / 100)
  28.     square = across + (down * 4)
  29.     print across, down
  30.  
  31.     if grid[square] == "X" or grid[square] == "O":
  32.         return
  33.  
  34.     if winner(grid):
  35.         return
  36.  
  37.     grid[square] = "O"
  38.     draw_shape("O", across, down)
  39.  
  40.     if winner(grid):
  41.         return
  42.  
  43.     play_move()
  44.  
  45. def o4xy(x, y):
  46.     return x * 100 + 2, y * 100 + 2, (x+1) * 100 + 2, (y+1) * 100 + 2
  47.  
  48. def draw_shape(shape, across, down):
  49.     if shape == "O":
  50.         c.create_oval(o4xy(across, down))
  51.     else:
  52.         c.create_line(o4xy(across, down))
  53.         c.create_line(across * 100 + 2, (down+1) * 100 + 2,
  54.             (across+1) * 100 + 2, down * 100 + 2)
  55.  
  56. # Dette gjer at datamaskinen faktisk har ein hjerne, og ikkje bare velger tilfeldige ruter
  57. def winner(grid):
  58.     for across in range(4):
  59.         row = across * 4
  60.         line = grid[row] + grid[row+1] + grid[row+2] + grid[row+3]
  61.         if line == "XXXX" or line == "OOOO":
  62.             return True
  63.  
  64.     for down in range(4):
  65.         line = grid[down] + grid[down+4] + grid[down+8] + grid[down+12]
  66.         if line == "XXXX" or line == "OOOO":
  67.             return True
  68.  
  69.     line = grid[0] + grid[5] + grid[10] + grid[15]
  70.     if line == "XXXX" or line == "OOOO":
  71.         return True
  72.  
  73.     line = grid[3] + grid[6] + grid[9] + grid[12]
  74.     if line == "XXXX" or line == "OOOO":
  75.         return True
  76.  
  77. # Finner ledige ruter
  78. def free_squares():
  79.     output = []
  80.     for position, square in enumerate(grid):
  81.         if square != "X" and square != "O":
  82.             output.append(position)
  83.     return output
  84.  
  85. def play_move():
  86.     moves = free_squares()
  87.     square = random.choice(moves)
  88.  
  89. # Hvis et blokkerende trekk eksisterer kjører den det
  90.     for possible in moves:
  91.         new_grid = list(grid)
  92.         new_grid[possible] = "O"
  93.         if winner(new_grid):
  94.             square = possible
  95.             break
  96.  
  97. # Hvis et vinnende trekk eksisterer kjører den det
  98.     for possible in moves:
  99.         new_grid = list(grid)
  100.         new_grid[possible] = "X"
  101.         if winner(new_grid):
  102.             square = possible
  103.             break
  104.  
  105.     down = square // 4
  106.     across = square % 4
  107.  
  108.     grid[square] = "X"
  109.     draw_shape("X", across, down)
  110.  
  111. c.bind("<Double-Button-1>", click)
  112.  
  113. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement