def test_board_full_4(board4): """Testing the board fulness""" root = tkinter.Tk() canvas = tkinter.Canvas(root) t = RawTurtle(canvas) screen = t.getscreen() board = Board(None, screen) root.destroy()
def board0(): root = tkinter.Tk() canvas = tkinter.Canvas(root) t = RawTurtle(canvas) screen = t.getscreen() board = Board(None, screen) board.reset() # root.destroy() return board
def test_minimax_param(board, evaluation): """Testing the minimax""" root = tkinter.Tk() canvas = tkinter.Canvas(root) t = RawTurtle(canvas) screen = t.getscreen() tkboard = Board(None, screen) tkboard.reset() for i in range(3): for j in range(3): if board[i][j] == "O": tkboard[i][j] = O(canvas) elif board[i][j] == "X": tkboard[i][j] = X(canvas) # root.destroy() assert minimax(1, tkboard, 4) == evaluation
class Robot: def __init__(self, scene, robot_id): self.robot_id = robot_id self.done = False self.signal = None self.turtle = RawTurtle(scene.canvas) self.scene = scene self.scr = self.turtle.getscreen() self.scr.setworldcoordinates(0, scene.height, scene.width, 0) self.turtle.penup() self.reset() def reset(self): positions = [((15,15), 45), ((self.scene.width-15, 15), 135), ((15, self.scene.height-15), 315), ((self.scene.height-15, self.scene.width-15), 135)] self.turtle.speed(0) self.turtle.setpos(positions[self.robot_id][0]) print positions[self.robot_id] self.turtle.left(positions[self.robot_id][1]) self.turtle.forward(20) self.turtle.speed("normal") def process(self): ##sets variables for checking collision turtle_x = self.turtle.xcor() turtle_y = self.turtle.ycor() t_heading = self.turtle.heading() ##variables used to check right xr = turtle_x + 15*math.cos(math.radians(self.turtle.heading()+30)) yr = turtle_y + 15*math.sin(math.radians(self.turtle.heading()+30)) ##variables used to check left xl = turtle_x + 15*math.cos(math.radians(self.turtle.heading()-30)) yl = turtle_y + 15*math.sin(math.radians(self.turtle.heading()-30)) ##turns the robot at window boundries st_orient = [0, 90, 180, 270] bounce_d = 20 if turtle_x - bounce_d < 0: self.turtle.setheading(180-t_heading) if turtle_x + bounce_d > self.scene.width: self.turtle.setheading(180-t_heading) if turtle_y - bounce_d < 0: self.turtle.setheading(360-t_heading) if turtle_y + bounce_d > self.scene.height: self.turtle.setheading(360-t_heading) ##check collisions left = self.scene.canvas.find_overlapping(xl-1,yl-1,xl+1,yl+1) right = self.scene.canvas.find_overlapping(xr-1,yr-1,xr+1,yr+1) if self.goal_id in left + right: self.done = True elif left: ##turn away self.turtle.left(10) elif right: ##turn away self.turtle.right(10) else: ##else move forward self.turtle.forward(5) ##if goal not reached: if self.signal: if self.signal == "STOP": self.signal = None return elif not self.done: self.scene.master.after(20, self.process)
"Circle Gasket": circFrac, "Seasonal Tree": circTreeDraw } ## contruct the Tk window root = Tk() root.title("Turtle Fractals") ## setup turtle screen wide = root.winfo_screenwidth() high = root.winfo_screenheight() canvas = Canvas(root, width=wide, height=high) canvas.pack() pen = RawTurtle(canvas) screen = pen.getscreen() pen.speed(0) pen.width(3) screen.colormode(255) screen.bgcolor((199, 248, 255)) ## define handlers def onClearScreen(): # clear the canvas screen.resetscreen() pen.speed(0) pen.width(3) screen.colormode(255) screen.bgcolor((199, 248, 255))
def buildWindow(self): canvas = ScrolledCanvas(self, 600, 600, 600, 600) canvas.pack(side=tkinter.LEFT) t = RawTurtle(canvas) screen = t.getscreen() screen.tracer(100000) screen.setworldcoordinates(screenMin, screenMin, screenMax, screenMax) screen.bgcolor("white") t.hideturtle() frame = tkinter.Frame(self) frame.pack(side=tkinter.RIGHT, fill=tkinter.BOTH) board = Board(None, screen) def drawGrid(): screen.clear() screen.tracer(1000000) screen.setworldcoordinates(screenMin, screenMin, screenMax, screenMax) screen.bgcolor("white") screen.tracer(0) t = RawTurtle(canvas) t.hideturtle() t.penup() t.width(10) t.color("black") for i in range(2): t.penup() t.goto(i * 100 + 100, 10) t.pendown() t.goto(i * 100 + 100, 290) t.penup() t.goto(10, i * 100 + 100) t.pendown() t.goto(290, i * 100 + 100) screen.update() drawGrid() def newGame(): # drawGrid() self.turn = HUMAN board.reset() self.locked = False screen.update() def startHandler(): newGame() btn_Start = tkinter.Button(frame, text="New Game", command=startHandler) btn_Start.pack() tkvar = tkinter.StringVar(self) tkvar.set(self.level) def levelHandler(*args): self.level = tkvar.get() lbl_Level = tkinter.Label(frame, text="AI level") lbl_Level.pack() dd_Level = tkinter.OptionMenu(frame, tkvar, command=levelHandler, *AILVLS) dd_Level.pack() def quitHandler(): self.master.quit() btn_Quit = tkinter.Button(frame, text="Quit", command=quitHandler) btn_Quit.pack() def computerTurn(): """ The locked variable prevents another event from being processed while the computer is making up its mind. """ self.locked = True maxMove = None # Call Minimax to find the best move to make. # After writing this code, the maxMove tuple should # contain the best move for the computer. For instance, # if the best move is in the first row and third column # then maxMove would be (0,2). # TODO: IMPLEMENT THE DESCRIBED LOGIC row, col = maxMove board[row][col] = X(canvas) self.locked = False def mouseClick(x, y): if not self.locked: row = int(y // 100) col = int(x // 100) if board[row][col].eval() == 0: board[row][col] = O(canvas) self.turn = COMPUTER board.drawXOs() if not board.full() and not abs(board.eval()) == 1: computerTurn() self.turn = HUMAN board.drawXOs() else: self.locked = True if board.eval() == 1: tkinter.messagebox.showwarning( "Game Over", "Expectedly, Machine wins." ) elif board.eval() == -1: tkinter.messagebox.showerror( "Game Over", "Suprisingly, Human wins." ) elif board.full(): tkinter.messagebox.showinfo("Game Over", "It was a tie.") screen.onclick(mouseClick) screen.listen()
class Robot: def __init__(self, scene, robot_id): #sets variables self.robot_id = robot_id self.turtle = RawTurtle(scene.canvas) self.scene = scene self.scr = self.turtle.getscreen() self.scr.setworldcoordinates(0, scene.height, scene.width, 0) self.turtle.penup() if robot_id == 1: self.turtle.color("blue") else: self.turtle.color("red") #create turtles sprite ## self.turtle.register_shape("ship",((-7,-2),(-6,-1),(-3,0),(-3,1),(-2,5), ## (0,7),(2,5),(3,1),(3,0),(6,-1),(7,-2), ## (3,-1),(3,-2),(2,-7),(-2,-7),(-3,-2), ## (-3,-1),(-2,-1),(-2,-2),(-1,-6),(1,-6), ## (2,-2),(2,-1),(-2,-1),(-2,0),(2,0), ## (2,1),(1,4),(0,5),(-1,4),(-2,1), ## (-2,-1),(-3,-1)) ## ) ## self.turtle.shape("ship") ## self.turtle.shapesize(2,2) #place robot using reset self.reset() def reset(self): #set start positions for robots positions = [((15,15), 45), ((self.scene.width-15, 15), 135), ((15, self.scene.height-15), 315), ((self.scene.height-15, self.scene.width-15), 135)] #move robot to starting possition self.turtle.speed(0) self.turtle.setpos(positions[self.robot_id][0]) #print positions[self.robot_id] self.turtle.left(positions[self.robot_id][1]) self.turtle.forward(20) self.turtle.speed(0) self.turtle.screen.bgcolor("sky blue") def orientate(self, landmarks, canvas, goal): ##sd = shortest distance ##x1,x2,y1,y2 = circles corners ##lx,ly = length x/y ##h = hypothinus ##ln = landmark number sd = 40000000 ln = 0 for ID in landmarks: if canvas.itemcget(ID, "fill") == "dark green": ln+=1 x1,y1,x2,y2 = canvas.coords(ID) lx = ((x1+x2)/2) - self.turtle.xcor() ly = ((y1+y2)/2) - self.turtle.ycor() h = math.sqrt(lx*lx + ly*ly) if h < sd: sd = h stored_ID = ID stored_x = lx stored_y = ly if ln == 0: stored_ID = goal x1,y1,x2,y2 = canvas.coords(goal) lx = ((x1+x2)/2) - self.turtle.xcor() ly = ((y1+y2)/2) - self.turtle.ycor() sd = math.sqrt(lx*lx + ly*ly) stored_x = ((x1+x2)/2) - self.turtle.xcor() stored_y = ((y1+y2)/2) - self.turtle.ycor() if sd < 37: return stored_ID if stored_x < 0: if stored_y < 0: new_heading = 180 + math.degrees(math.atan((-stored_y)/(-stored_x))) else: new_heading = 180 - math.degrees(math.atan(stored_y/(-stored_x))) elif stored_y < 0: new_heading = 360 - math.degrees(math.atan((-stored_y)/stored_x)) else: new_heading = math.degrees(math.atan(stored_y/stored_x)) self.turtle.seth(new_heading) return False def collisions_move(self, speed, depth): ##breaks the recursion if the robots get to close if depth > 10: return ##sets variables for checking collision turtle_x = self.turtle.xcor() turtle_y = self.turtle.ycor() t_heading = self.turtle.heading() ##variables used to check right xr = turtle_x + 15*math.cos(math.radians(self.turtle.heading()+30)) yr = turtle_y + 15*math.sin(math.radians(self.turtle.heading()+30)) ##variables used to check left xl = turtle_x + 15*math.cos(math.radians(self.turtle.heading()-30)) yl = turtle_y + 15*math.sin(math.radians(self.turtle.heading()-30)) ##check for the collision left = self.scene.canvas.find_overlapping(xl-1,yl-1,xl+1,yl+1) right = self.scene.canvas.find_overlapping(xr-1,yr-1,xr+1,yr+1) if left: ##turn away self.turtle.left(20) self.collisions_move(speed, depth+1) #self.turtle.forward(speed/5) elif right: ##turn away self.turtle.right(20) self.collisions_move(speed, depth+1) #self.turtle.forward(speed/5) else: ##else move forward self.turtle.forward(speed) return
def build_window(self): """Build a window.""" canvas = ScrolledCanvas(self, 600, 600, 600, 600) canvas.pack(side=tkinter.LEFT) t = RawTurtle(canvas) screen = t.getscreen() screen.tracer(100000) screen.setworldcoordinates(SCREENMIN, SCREENMIN, SCREENMAX, SCREENMAX) screen.bgcolor("white") t.hideturtle() frame = tkinter.Frame(self) frame.pack(side=tkinter.RIGHT, fill=tkinter.BOTH) board = Board(None, screen) def draw_grid(): screen.clear() screen.tracer(1000000) screen.setworldcoordinates(SCREENMIN, SCREENMIN, SCREENMAX, SCREENMAX) screen.bgcolor("white") screen.tracer(0) t = RawTurtle(canvas) t.hideturtle() t.penup() t.width(10) t.color("black") for i in range(2): t.penup() t.goto(i * 100 + 100, 10) t.pendown() t.goto(i * 100 + 100, 290) t.penup() t.goto(10, i * 100 + 100) t.pendown() t.goto(290, i * 100 + 100) screen.update() draw_grid() def new_game(): # draw_grid() self.turn = HUMAN board.reset() self.locked = False screen.update() def start_handler(): new_game() btn_start = tkinter.Button(frame, text="New Game", command=start_handler) btn_start.pack() tkvar = tkinter.StringVar(self) tkvar.set(self.level) def level_handler(*args): self.level = tkvar.get() lbl_level = tkinter.Label(frame, text="AI Level") lbl_level.pack() dd_level = tkinter.OptionMenu(frame, tkvar, command=level_handler, *AI_LEVELS) dd_level.pack() def quit_handler(): self.master.quit() btn_quit = tkinter.Button(frame, text="Quit", command=quit_handler) btn_quit.pack() def computer_turn(): """ The locked variable prevents another event from being processed while the computer is making up its mind. """ self.locked = True max_move = None # Call Minimax to find the best move to make. # After writing this code, the maxMove tuple should # contain the best move for the computer. For instance, # if the best move is in the first row and third column # then max_move would be (0,2). depth = AI_LEVELS[self.level] best_move = -float("inf") for i, j in board.available(): temp = board.clone() temp.items[i][j] = X(None) value = minimax(HUMAN, temp, depth) if value > best_move: best_move = value row, col = i, j max_move = (row, col) row, col = max_move board[row][col] = X(canvas) self.locked = False def mouse_click(x, y): """Defines what happens on a mouse click.""" if not self.locked: row = int(y // 100) col = int(x // 100) if board[row][col].eval() == 0: board[row][col] = O(canvas) self.turn = COMPUTER board.draw_xos() if not board.is_full() and not abs(board.eval()) == 1: computer_turn() self.turn = HUMAN board.draw_xos() else: self.locked = True if board.eval() == 1: tkinter.messagebox.showwarning("Game Over", "Expectedly, Machine wins.") elif board.eval() == -1: tkinter.messagebox.showerror("Game Over", "Suprisingly, Human wins.") elif board.is_full(): tkinter.messagebox.showinfo("Game Over", "It was a tie.") screen.onclick(mouse_click) screen.listen()