Guest User

Untitled

a guest
Mar 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import tkinter
  2. import random
  3.  
  4. class Game(object):
  5. '''
  6. Enter the class docstring here
  7. '''
  8. tile_size = 100
  9.  
  10. wins = [
  11. {1, 2, 3}, {4, 5, 6}, {7, 8, 9},
  12. {1, 4, 7}, {2, 5, 8}, {3, 6, 9},
  13. {1, 5, 9}, {3, 5, 7},
  14. ]
  15.  
  16. board = [[None, None, None],
  17. [None, None, None],
  18. [None, None, None]]
  19.  
  20. def __init__(self, parent):
  21. parent.title('Tic Tac Toe')
  22. self.parent = parent
  23. restart_button = tkinter.Button(self.parent, text='RESTART',width=20,command=self.restart)
  24. restart_button.grid()
  25. self.canvas = tkinter.Canvas(self.parent,width=self.tile_size * 3,height=self.tile_size * 3)
  26. self.canvas.grid()
  27. for row in range(3):
  28. for column in range(3):
  29. self.canvas.create_rectangle(self.tile_size * column,self.tile_size * row,self.tile_size * (column + 1),
  30. self.tile_size * (row + 1),fill='white')
  31. self.canvas.bind("<Button-1>", self.play)
  32.  
  33. def restart(self):
  34. for shape in self.canvas.find_all():
  35. self.canvas.itemconfigure(shape, fill='white')# This method is invoked when the user clicks on the RESTART
  36. self.board =[[None, None, None],
  37. [None, None, None],
  38. [None, None, None]]
  39.  
  40. def play(self, event):
  41. x,y = event.x, event.y
  42. shape = self.canvas.find_closest(x, y)
  43. self.canvas.itemconfigure(shape, fill='red')
  44. new_x = x // self.tile_size
  45. new_y = y // self.tile_size
  46. if new_x == 0 and new_y == 0:
  47. self.board [0][0] = 'user'
  48. elif new_x == 1 and new_y == 0:
  49. self.board[0][1] = 'user'
  50. elif new_x == 2 and new_y == 0:
  51. self.board [0][2] = 'user'
  52. elif new_x == 0 and new_y == 1:
  53. self.board [1][0] = 'user'
  54. elif new_x == 1 and new_y == 1:
  55. self.board [1][1] = 'user'
  56. elif new_x == 2 and new_y == 1:
  57. self.board [1][2] = 'user'
  58. elif new_x == 2 and new_y == 2:
  59. self.board [2][2] = 'user'
  60. elif new_x == 1 and new_y == 2:
  61. self.board [2][1] = 'user'
  62. elif new_x == 0 and new_y == 2:
  63. self.board [2][0] = 'user'
  64.  
  65. counter = 1
  66. i = 1
  67. while i == 1:
  68. first_value = random.randint(0, 2)
  69. second_value = random.randint(0, 2)
  70. if counter == 4:
  71. break
  72. elif self.board[second_value][first_value] == None:
  73. first_val = first_value * 100
  74. second_val = second_value * 100
  75. self.board[second_value][first_value] = 'comp'
  76. shape = self.canvas.find_closest(first_val,second_val)
  77. self.canvas.itemconfigure(shape, fill='blue')
  78. i = 0
  79. counter += 1
  80. else:
  81. i = 1
  82. print(self.board)
  83.  
  84.  
  85. def main():
  86.  
  87. root = tkinter.Tk()# Instantiate a root window
  88. my_game = Game(root)# Instantiate a Game object
  89. root.mainloop()# Enter the main event loop
  90.  
  91. if __name__ == '__main__':
  92. main()
Add Comment
Please, Sign In to add comment