Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle, time, random, winsound
- from settings import *
- class Game(turtle.Turtle):
- def __init__(self):
- super().__init__()
- self.penup()
- self.hideturtle()
- self.speed(2)
- self.x_text_pos = 60
- self.y_text_pos = 150
- self.row = 3
- self.columns = 3
- self.space = 5
- self.distance = 80
- self.board = [" "] * 9
- self.spot = 0
- self.x = 0
- self.y = 0
- self.move = "p"
- self.corners = [0,2,6,8]
- self.winpos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],
- [1,4,7],[2,5,8],[0,4,8],[2,4,6]]
- self.semipos = [[0,1,2],[1,2,0],[0,2,1],[3,4,5],
- [3,5,4],[4,5,3],[6,7,8],[6,8,7],
- [7,8,6],[0,3,6],[0,6,3],[3,6,0],[1,4,7],
- [1,7,4],[4,7,1],[2,5,8],[2,8,5],[5,8,2],
- [0,4,8],[0,8,4],[4,8,0],[2,4,6],[2,6,4],[4,6,2]]
- self.count = 0
- def get_opening(self):
- if self.count == 0:
- comp_spot = random.choice(self.corners)
- return comp_spot
- if self.count == 1:
- if self.board[4] == " ":
- comp_spot = 4
- return comp_spot
- if self.board[4] != " ":
- comp_spot=random.choice(self.corners)
- return comp_spot
- def draw_board(self):
- self.pencolor("green")
- self.shape("square")
- self.shapesize(self.distance /20)
- self.setpos(-150,150)
- for r in range(self.row):
- for c in range(self.columns):
- self.stamp()
- self.forward(self.distance)
- self.back(self.distance * self.row)
- self.right(90)
- self.forward(self.distance)
- self.left(90)
- def get_spot(self,x,y):
- self.spot = None
- if x == -160 and y == 160:
- self.spot=0
- if x == -80 and y == 160:
- self.spot=1
- if x == -0 and y == 160:
- self.spot=2
- if x == -160 and y == 80:
- self.spot=3
- if x == -80 and y == 80:
- self.spot=4
- if x == -0 and y == 80:
- self.spot=5
- if x == -160 and y == 0:
- self.spot=6
- if x == -80 and y == 0:
- self.spot=7
- if x == -0 and y == 0:
- self.spot=8
- return self.spot
- def user(self,x,y):
- try:
- self.move = "x"
- if self.count == 9:
- font = ("arial", "18", "bold")
- self.color("blue")
- self.goto(-100,-150)
- self.write("The game is a tie!", font = font)
- time.sleep(2)
- self.undo()
- main()
- else:
- x = int(self.distance * round (x/self.distance))
- y = int(self.distance * round (y/self.distance))
- self.get_spot(x,y)
- if self.board[self.spot] != " ":
- font = ("arial", "18", "bold")
- self.color("red")
- self.goto(-100,-150)
- self.write("Click on an empty spot.", font = font)
- time.sleep(1)
- self.undo()
- else:
- self.board[self.spot] = self.move
- if self.check_win():
- self.kereszt(x,y)
- font = ("arial","18","bold")
- self.color("blue")
- self.goto(-100,-150)
- self.write("YOU WIN!", font = font)
- time.sleep(2)
- self.undo()
- main()
- else:
- self.count += 1
- self.kereszt(x,y)
- self.computer()
- except TypeError:
- font = ("arial", "18", "bold")
- self.color("red")
- self.goto(-100, -150)
- self.write("Click on the board." , font = font)
- time.sleep(1)
- self.undo()
- def kereszt(self,x,y):
- self.shape("classic")
- self.showturtle()
- self.goto(x-10, y+10)
- self.setheading(-45)
- self.color("red")
- self.pensize(2)
- self.down()
- self.forward(60)
- self.up()
- self.goto(x+30, y+10)
- self.setheading(-135)
- self.down()
- self.forward(60)
- self.up()
- self.hideturtle()
- def check_win(self):
- for f,s,t in self.winpos:
- if self.board[f] == self.board [s] == self.board [t] and self.board[f] != " ":
- return True
- def info(self):
- self.pencolor("blue")
- self.goto(self.x_text_pos, self.y_text_pos)
- self.write(self.board)
- def computer(self):
- self.move="o"
- if self.count == 9:
- font = ("arial", "18", "bold")
- self.color("blue")
- self.goto(-100,-150)
- self.write("The game is a tie!", font = font)
- time.sleep(1)
- self.undo()
- main()
- else:
- comp_spot=self.get_opening()
- if comp_spot == None:
- for f,s,t in self.semipos:
- if self.board[f] == "o" and self.board[s] == "o":
- if self.board[t] == " ":
- comp_spot = t
- if comp_spot == None:
- for f,s,t in self.semipos:
- if self.board[f] == "x" and self.board[s] == "x":
- if self.board[t] == " ":
- comp_spot = t
- if comp_spot == None:
- comp_spot=random.randrange(0,9)
- while self.board[comp_spot] != " ":
- comp_spot=random.randrange(0,9)
- if self.board[comp_spot] == " ":
- break
- if comp_spot == 0:
- self.x = -160
- self.y = 160
- if comp_spot == 1:
- self.x = -80
- self.y = 160
- if comp_spot == 2:
- self.x = -0
- self.y = 160
- if comp_spot ==3:
- self.x = -160
- self.y = 80
- if comp_spot ==4:
- self.x = -80
- self.y = 80
- if comp_spot ==5:
- self.x = 0
- self.y = 80
- if comp_spot ==6:
- self.x = -160
- self.y = 0
- if comp_spot ==7:
- self.x = -80
- self.y = 0
- if comp_spot ==8:
- self.x = 0
- self.y = 0
- print(self.x, self.y)
- self.shape("circle")
- self.shapesize(2.5)
- self.pencolor("yellow")
- self.fillcolor("black")
- self.goto(self.x + 10, self.y - 10)
- self.board[comp_spot] = self.move
- self.count +=1
- self.stamp()
- self.info()
- time.sleep(1)
- self.undo()
- for f,s,t in self.winpos:
- if self.board[f] == self.board[s] == self.board[t] and self.board[f] != " ":
- font = ("arial", "18", "bold")
- self.color("blue")
- self.goto(-100,-150)
- self.write("COMPUTER WINS!", font = font)
- time.sleep(2)
- self.undo()
- main()
- if self.count == 9:
- font = ("arial", "18", "bold")
- self.color("blue")
- self.goto(-100,-150)
- self.write("The game is a tie!", font = font)
- time.sleep(2)
- self.undo()
- main()
- def clear_board(self):
- self.clear()
- def main():
- turn = random.randrange(0,2)
- if turn == 0:
- game = Game()
- game.clear_board()
- game.draw_board()
- font = ("arial","18","bold")
- game.goto(-100, -150)
- game.color("yellow")
- game.write("You begin.", font = font)
- winsound.Beep(1500,200)
- time.sleep(1)
- game.undo()
- turtle.onscreenclick(game.user)
- else:
- game = Game()
- game.clear_board()
- game.draw_board()
- font = ("arial","18","bold")
- game.goto(-100, -150)
- game.color("yellow")
- game.write("Computer begins", font = font)
- winsound.Beep(1500,200)
- time.sleep(1)
- game.undo()
- turtle.onscreenclick(game.user)
- game.computer()
- turtle.mainloop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement