View the step-by-step solution to:

def check_game_over(board): signature: listCstr) > bool Given the current state of the board, determine if the game is over, by checking for a...

Construct the functions in the attachments. Based on the tic/tac toe game code below. Construct an artificial intelligence for this tic/tac/toe game using the "do_computer_move" function in the attachments, make sure the computer controls the game piece, "O". Determine how the game is over using the "check_game_over" function in the attachments by checking for a three in a row pattern. Depending on who is the winner: it must display a prompt in the middle of the screen as shown in "the final output example" attachments. The "user_clicks" functions presented in the code below must be put inside the "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 in the attachments that runs the entire game. Use the time module, random module, and turtle module wherever needed.


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 turtle
import time
import 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)



User move function .jpgClickhandler function and Main function.jpgCheck game over function and computer move function.PNGIMG-1448.JPGFinal output example.JPG

Check game over function and computer move function.PNG

def check_game_over(board):
signature: listCstr) —> bool
Given the current state of the board, determine
if the game is over, by checking for
a three—in—a—row pattern in horizontal,
vertical, or diagonal lines; and also if the
game has reached a stalemate, achieved when
the board is full and no further move is possible.
If there is a winner or if there is a stalemante, display
an appropriate message to the user and clear the board
in preparation for the next round. If the game is over,
return True, otherwise False. II II II pass # your code here def do_computer_move(board):
signature: listCstr) —> NoneType
Given a list representing the state of the board,
select a position for the computer's move and
update the board with an X in an appropriate
position. The algorithm for selecting the
computer's move shall be as Follows: if it is
possible for the computer to win in one move,
it must do so. If the human player is able
to win in the next move, the computer must
try to block it. Otherwise, the computer's
next move may be any random, valid position
(selected with the random.randint function). II II II pass # your code here

Clickhandler function and Main function.jpg

def clickhandler(x, y):
signature: int, int -> NoneType
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.
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.
turtle.tracer(0,0)
turtle.hideturtle()
turtle.onscreenclickCclickhandler)
draw_board(the_board)
turtle.mainloop() main()

Final output example.JPG

IMG-1448.JPG

User move function .jpg

def do_user_move(board, x, y):
signature: list(str), int, int —> bool
The current state of the board is given as
a parameter, as well as the x,y screen coordinate
indicating where the user clicked. This function
should update the board state variable
with an 0 in the corresponding position. Your
code will need to translate the screen coordinate
(a pixel position where the user clicked) into the
corresponding board position (a value between 0 and
8 inclusive, identifying one of the 9 board positions).
The function returns a bool, indicating if
the operation was successful: if the user
clicks on a position that is already occupied
or outside of the board area, the move is
invalid, and the function should return False,
otherise True. print("user clicked at
pass # your code here +str(x)+","+str(y))

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question