Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.11 KB | None | 0 0
  1. import turtle, time, random, winsound
  2.  
  3. from settings import *
  4.  
  5. class Game(turtle.Turtle):
  6.     def __init__(self):
  7.         super().__init__()
  8.        
  9.         self.penup()
  10.         self.hideturtle()
  11.         self.speed(2)
  12.         self.x_text_pos = 60
  13.         self.y_text_pos = 150
  14.         self.row = 3
  15.         self.columns = 3
  16.         self.space = 5
  17.         self.distance = 80
  18.         self.board = [" "] * 9
  19.         self.spot = 0
  20.         self.x = 0
  21.         self.y = 0
  22.         self.move = "p"
  23.         self.corners = [0,2,6,8]
  24.         self.winpos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],
  25.                       [1,4,7],[2,5,8],[0,4,8],[2,4,6]]
  26.         self.semipos = [[0,1,2],[1,2,0],[0,2,1],[3,4,5],
  27.                         [3,5,4],[4,5,3],[6,7,8],[6,8,7],
  28.                         [7,8,6],[0,3,6],[0,6,3],[3,6,0],[1,4,7],
  29.                         [1,7,4],[4,7,1],[2,5,8],[2,8,5],[5,8,2],
  30.                         [0,4,8],[0,8,4],[4,8,0],[2,4,6],[2,6,4],[4,6,2]]
  31.         self.count = 0
  32.        
  33.        
  34.     def get_opening(self):
  35.         if self.count == 0:
  36.             comp_spot = random.choice(self.corners)
  37.             return comp_spot
  38.         if self.count == 1:
  39.             if self.board[4] == " ":
  40.                 comp_spot = 4
  41.                 return comp_spot
  42.             if self.board[4] != " ":
  43.                 comp_spot=random.choice(self.corners)
  44.                 return comp_spot
  45.        
  46.        
  47.                
  48.        
  49.     def draw_board(self):
  50.         self.pencolor("green")
  51.         self.shape("square")
  52.         self.shapesize(self.distance /20)
  53.         self.setpos(-150,150)
  54.         for r in range(self.row):
  55.             for c in range(self.columns):
  56.                 self.stamp()
  57.                 self.forward(self.distance)
  58.             self.back(self.distance * self.row)
  59.             self.right(90)
  60.             self.forward(self.distance)
  61.             self.left(90)
  62.            
  63.     def get_spot(self,x,y):
  64.         self.spot = None
  65.         if x == -160 and y == 160:
  66.             self.spot=0
  67.         if x == -80 and y == 160:
  68.             self.spot=1
  69.         if x == -0 and y == 160:
  70.             self.spot=2
  71.         if x == -160 and y == 80:
  72.             self.spot=3
  73.         if x == -80 and y == 80:
  74.             self.spot=4
  75.         if x == -0 and y == 80:
  76.             self.spot=5
  77.         if x == -160 and y == 0:
  78.             self.spot=6
  79.         if x == -80 and y == 0:
  80.             self.spot=7
  81.         if x == -0 and y == 0:
  82.             self.spot=8
  83.         return self.spot
  84.    
  85.    
  86.    
  87.     def user(self,x,y):
  88.         try:            
  89.             self.move = "x"
  90.             if self.count == 9:
  91.                 font = ("arial", "18", "bold")
  92.                 self.color("blue")
  93.                 self.goto(-100,-150)
  94.                 self.write("The game is a tie!", font = font)
  95.                 time.sleep(2)
  96.                 self.undo()
  97.                 main()
  98.             else:
  99.                 x = int(self.distance * round (x/self.distance))
  100.                 y = int(self.distance * round (y/self.distance))
  101.                 self.get_spot(x,y)
  102.                 if self.board[self.spot] != " ":
  103.                     font = ("arial", "18", "bold")
  104.                     self.color("red")
  105.                     self.goto(-100,-150)
  106.                     self.write("Click on an empty spot.", font = font)
  107.                     time.sleep(1)
  108.                     self.undo()
  109.                 else:
  110.                     self.board[self.spot] = self.move
  111.                     if self.check_win():
  112.                         self.kereszt(x,y)
  113.                         font = ("arial","18","bold")
  114.                         self.color("blue")
  115.                         self.goto(-100,-150)
  116.                         self.write("YOU WIN!", font = font)
  117.                         time.sleep(2)
  118.                         self.undo()
  119.                         main()
  120.                     else:                        
  121.                         self.count += 1
  122.                         self.kereszt(x,y)
  123.                         self.computer()
  124.        
  125.         except TypeError:
  126.             font = ("arial", "18", "bold")
  127.             self.color("red")
  128.             self.goto(-100, -150)
  129.             self.write("Click on the board." , font = font)
  130.             time.sleep(1)
  131.             self.undo()
  132.    
  133.    
  134.     def kereszt(self,x,y):
  135.         self.shape("classic")
  136.         self.showturtle()
  137.         self.goto(x-10, y+10)
  138.         self.setheading(-45)
  139.         self.color("red")
  140.         self.pensize(2)
  141.         self.down()
  142.         self.forward(60)
  143.         self.up()
  144.         self.goto(x+30, y+10)
  145.         self.setheading(-135)
  146.         self.down()
  147.         self.forward(60)
  148.         self.up()
  149.         self.hideturtle()
  150.        
  151.    
  152.     def check_win(self):
  153.         for f,s,t in self.winpos:
  154.             if self.board[f] == self.board [s] == self.board [t] and self.board[f] != " ":
  155.                 return True
  156.        
  157.     def info(self):
  158.         self.pencolor("blue")
  159.         self.goto(self.x_text_pos, self.y_text_pos)
  160.         self.write(self.board)
  161.            
  162.     def computer(self):
  163.         self.move="o"
  164.         if self.count == 9:
  165.                 font = ("arial", "18", "bold")
  166.                 self.color("blue")
  167.                 self.goto(-100,-150)
  168.                 self.write("The game is a tie!", font = font)
  169.                 time.sleep(1)
  170.                 self.undo()
  171.                 main()
  172.         else:
  173.             comp_spot=self.get_opening()          
  174.        
  175.             if comp_spot == None:
  176.                 for f,s,t in self.semipos:
  177.                     if self.board[f] == "o" and self.board[s] == "o":
  178.                         if self.board[t] == " ":
  179.                             comp_spot = t
  180.                
  181.             if comp_spot == None:
  182.                 for f,s,t in self.semipos:
  183.                     if self.board[f] == "x" and self.board[s] == "x":
  184.                         if self.board[t] == " ":
  185.                             comp_spot = t
  186.            
  187.             if comp_spot == None:
  188.                 comp_spot=random.randrange(0,9)
  189.                 while self.board[comp_spot] != " ":
  190.                     comp_spot=random.randrange(0,9)
  191.                     if self.board[comp_spot] == " ":
  192.                         break
  193.            
  194.             if comp_spot == 0:
  195.                 self.x = -160
  196.                 self.y = 160
  197.             if comp_spot == 1:
  198.                 self.x = -80
  199.                 self.y = 160
  200.             if comp_spot == 2:
  201.                 self.x = -0
  202.                 self.y = 160
  203.             if comp_spot ==3:
  204.                 self.x = -160
  205.                 self.y = 80
  206.             if comp_spot ==4:
  207.                 self.x = -80
  208.                 self.y = 80
  209.             if comp_spot ==5:
  210.                 self.x = 0
  211.                 self.y = 80
  212.             if comp_spot ==6:
  213.                 self.x = -160
  214.                 self.y = 0
  215.             if comp_spot ==7:
  216.                 self.x = -80
  217.                 self.y = 0
  218.             if comp_spot ==8:
  219.                 self.x = 0
  220.                 self.y = 0
  221.             print(self.x, self.y)
  222.             self.shape("circle")
  223.             self.shapesize(2.5)
  224.             self.pencolor("yellow")
  225.             self.fillcolor("black")
  226.             self.goto(self.x + 10, self.y - 10)
  227.             self.board[comp_spot] = self.move
  228.             self.count +=1
  229.             self.stamp()
  230.             self.info()
  231.             time.sleep(1)
  232.             self.undo()
  233.             for f,s,t in self.winpos:
  234.                 if self.board[f] == self.board[s] == self.board[t] and self.board[f] != " ":
  235.                     font = ("arial", "18", "bold")
  236.                     self.color("blue")
  237.                     self.goto(-100,-150)
  238.                     self.write("COMPUTER WINS!", font = font)
  239.                     time.sleep(2)
  240.                     self.undo()
  241.                     main()
  242.             if self.count == 9:
  243.                 font = ("arial", "18", "bold")
  244.                 self.color("blue")
  245.                 self.goto(-100,-150)
  246.                 self.write("The game is a tie!", font = font)
  247.                 time.sleep(2)
  248.                 self.undo()
  249.                 main()
  250.                    
  251.                
  252.     def clear_board(self):
  253.         self.clear()
  254.    
  255.    
  256. def main():
  257.     turn = random.randrange(0,2)
  258.     if turn == 0:
  259.         game = Game()
  260.         game.clear_board()
  261.         game.draw_board()
  262.         font = ("arial","18","bold")
  263.         game.goto(-100, -150)
  264.         game.color("yellow")
  265.         game.write("You begin.", font = font)
  266.         winsound.Beep(1500,200)
  267.         time.sleep(1)
  268.         game.undo()
  269.         turtle.onscreenclick(game.user)  
  270.    
  271.     else:
  272.         game = Game()
  273.         game.clear_board()
  274.         game.draw_board()
  275.         font = ("arial","18","bold")
  276.         game.goto(-100, -150)
  277.         game.color("yellow")
  278.         game.write("Computer begins", font = font)
  279.         winsound.Beep(1500,200)
  280.         time.sleep(1)
  281.         game.undo()
  282.         turtle.onscreenclick(game.user)  
  283.         game.computer()
  284.    
  285.     turtle.mainloop()
  286.        
  287. if __name__ == "__main__":
  288.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement