Question
Based on the half-written tic/tac toe game code below. Construct code for the do_computer_move function in the attachments. Follow what the algorithm and and signature
Based on the half-written tic/tac toe game code below. Construct code for the "do_computer_move" function in the attachments. Follow what the algorithm and and signature says. Make sure the computer controls the game piece, "O". Same goes for the "check_game_over" function in the attachments. Lastly, the "user_clicks" functions presented in the code below must be put inside the "do_user_move" function in the attachments and it must return a bool. In this function, make sure the user still controls the game piece, "X". For all functions, it must follow the signature and algorithm stated.
Lastly, all the functions must correspond to the function calls in the "clickhandler" function and the "main" function that runs the entire game in the attachments . Use the time module, random module, and turtle module wherever needed. Please make sure the turtle can go fast, especially when the user clicks on the board to make their move and when the computer makes its move. But do not use turtle.speed as it is inside the prohibited methods section. The "main" function provided in the attachments uses turtle.tracer to make it go faster. Everything must be in the appropriate functions in the attachments. Do NOT create your own functions.
Prohibited methods: These are inadequate with the code already written at the bottom:
turtle.speed
turtle.pen() or turtle.Turtle
turtle.screen or turtle.getscreen
turtle.shape, turtle.shapsize, turtle.resizemode, turtle.turtlesize
tkinter or any other built-in functions besides turtle, time or random
Here is my code:
import turtleimport timeimport random pieces = ["_", "_", "_", "_", "_", "_", "_", "_", "_"] turn = "X" def drawgame(brd): # draw board turtle.setup(600, 600) turtle.bgcolor("silver") turtle.color("white") turtle.hideturtle() turtle.speed('fastest') turtle.width(10) turtle.up() # Horizontal bars turtle.goto(-300, 100) turtle.down() turtle.forward(600) turtle.up() turtle.goto(-300, -100) turtle.down() turtle.forward(600) turtle.up() # Vertical bars turtle.goto(-100, 300) turtle.setheading(-90) turtle.down() turtle.forward(600) turtle.up() turtle.goto(100, 300) turtle.down() turtle.forward(600) turtle.up() turtle.color("blue") x, y = -300, 300 for pos in pieces: if pos == "X": # Draw X turtle.up() turtle.goto(x + 20, y - 20) turtle.setheading(-45) turtle.down() turtle.forward(226) turtle.up() turtle.goto(x + 180, y - 20) turtle.setheading(-135) turtle.down() turtle.forward(226) turtle.up() elif pos == "O": #Draw O turtle.up() turtle.goto(x + 100, y - 180) turtle.setheading(0) turtle.down() turtle.circle(80) turtle.up() x += 200 if x > 100: x = -300 y -= 200 def clicked(x, y): global turn, pieces turtle.onscreenclick(None) # disabling handler when inside handler column = (x + 300) // 200 row = (y - 300) // -200 square = int(row * 3 + column) print("User clicked ", x, ",", y, " at square ", square) if pieces[square] == "_": pieces[square] = turn if turn == "X": turn = "O" else: turn = "X" drawgame(pieces) else: print("That square is already taken") turtle.onscreenclick(clicked)
def clickhandler(x, y): signature: int, int -> None Type This function is called by turtle in response to a user click. The parameters are the screen coordinates indicating where the click happened. The function will call other functions. You do not need to modify this function, but you do need to understand it. 11 11 11 if do_user_move(the_board, x, y): draw_board(the_board) if not check_game_over (the_board): do_computer_move(the_board) draw_board(the_board) check_game_over (the_board) def main(): signature: () -> NoneType Runs the tic-tac-toe game. You shouldn't need to modify this function. 11 11 11 turtle.tracer(0,0) turtle.hideturtle() turtle.onscreenclick(clickhandler) draw_board(the_board) turtle.mainloop() main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Okay I made adjustments to the docomputermove and checkgameover and integrate the userclick function ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started