Question

In: Computer Science

please create a tic tac toe game with python using only graphics.py library

please create a tic tac toe game with python using only graphics.py library

Solutions

Expert Solution

Import the graphics.py(version-5.0) in the same directory as of TicTacToe.py and run the file using the command python3 TicTacToe.py

from graphics import *
import sys

def get_box(px,py):
if(px > 0 and px <= 100 and py > 0 and py <= 100 ):
return 0

if(px > 100 and px <= 200 and py > 0 and py <= 100 ):
return 1

if(px > 200 and px <= 300 and py > 0 and py <= 100 ):
return 2

if(px > 0 and px <= 100 and py > 100 and py <= 200 ):
return 3

if(px > 100 and px <= 200 and py > 100 and py <= 200 ):
return 4

if(px > 200 and px <= 300 and py > 100 and py <= 200 ):
return 5

if(px > 0 and px <= 100 and py > 200 and py <= 300 ):
return 6

if(px > 100 and px <= 200 and py > 200 and py <= 300 ):
return 7

if(px > 200 and px <= 300 and py > 200 and py <= 300 ):
return 8

def tic_tac_toe_O(entries,win, p2x, p2y):
box = get_box(p2x,p2y)
if(box == 0):
center = Point(50,50)
entries[0][0] = 2
elif(box == 1):
center = Point(150,50)
entries[1][0] = 2
elif(box == 2):
center = Point(250,50)
entries[2][0] = 2
elif(box == 3):
center = Point(50,150)
entries[0][1] = 2
elif(box == 4):
center = Point(150,150)
entries[1][1] = 2
elif(box == 5):
center = Point(250,150)
entries[2][1] = 2
elif(box == 6):
center = Point(50,250)
entries[0][2] = 2
elif(box == 7):
center = Point(150,250)
entries[1][2] = 2
elif(box == 8):
center = Point(250,250)
entries[2][2] = 2
  
circle = Circle(center,50)
circle.setOutline('green')
circle.setWidth(5)
circle.draw(win)


def tic_tac_toe_X(entries,win, p1x, p1y):
  
box = get_box(p1x,p1y)
if(box == 0):
line1 = Line(Point(0,0),Point(100,100))
line2 = Line(Point(0,100),Point(100,0))
entries[0][0] = 1

elif(box == 1):
line1 = Line(Point(100,0),Point(200,100))
line2 = Line(Point(100,100),Point(200,0))
entries[1][0] = 1

elif(box == 2):
line1 = Line(Point(200,0),Point(300,100))
line2 = Line(Point(200,100),Point(300,0))
entries[2][0] = 1

elif(box == 3):
line1 = Line(Point(0,100),Point(100,200))
line2 = Line(Point(0,200),Point(100,100))
entries[0][1] = 1

elif(box == 4):
line1 = Line(Point(100,100),Point(200,200))
line2 = Line(Point(100,200),Point(200,100))
entries[1][1] = 1

elif(box == 5):
line1 = Line(Point(200,100),Point(300,200))
line2 = Line(Point(200,200),Point(300,100))
entries[2][1] = 1

elif(box == 6):
line1 = Line(Point(0,200),Point(100,300))
line2 = Line(Point(0,300),Point(100,200))
entries[0][2] = 1

elif(box == 7):
line1 = Line(Point(100,200),Point(200,300))
line2 = Line(Point(100,300),Point(200,200))
entries[1][2] = 1

elif(box == 8):
line1 = Line(Point(200,200),Point(300,300))
line2 = Line(Point(200,300),Point(300,200))
entries[2][2] = 1

line1.setFill('red')
line2.setFill('red')
line1.setWidth(5)
line2.setWidth(5)
line1.draw(win)
line2.draw(win)

def check(entries):
if(entries[0][0] == 1 and entries[0][1] == 1 and entries[0][2] == 1):
line = Line(Point(50,0),Point(50,300))
line.setFill('red')
line.setWidth(5)
line.draw(win)
return True
elif(entries[1][0] == 1 and entries[1][1] == 1 and entries[1][2] == 1):
line = Line(Point(150,0),Point(150,300))
line.setFill('red')
line.setWidth(5)
line.draw(win)
return True
elif(entries[2][0] == 1 and entries[2][1] == 1 and entries[2][2] == 1):
line = Line(Point(250,0),Point(250,300))
line.setFill('red')
line.setWidth(5)
line.draw(win)
return True
elif(entries[0][0] == 1 and entries[1][0] == 1 and entries[2][0] == 1):
line = Line(Point(0,50),Point(300,50))
line.setFill('red')
line.setWidth(5)
line.draw(win)
return True
elif(entries[0][1] == 1 and entries[1][1] == 1 and entries[2][1] == 1):
line = Line(Point(0,150),Point(300,150))
line.setFill('red')
line.setWidth(5)
line.draw(win)
return True
elif(entries[0][2] == 1 and entries[1][2] == 1 and entries[2][2] == 1):
line = Line(Point(0,250),Point(300,250))
line.setFill('red')
line.setWidth(5)
line.draw(win)
return True
elif(entries[0][0] == 1 and entries[1][1] == 1 and entries[2][2] == 1):
line = Line(Point(0,0),Point(300,300))
line.setFill('red')
line.setWidth(5)
line.draw(win)
return True
elif(entries[0][2] == 1 and entries[1][1] == 1 and entries[2][0] == 1):
line = Line(Point(0,300),Point(300,0))
line.setFill('red')
line.setWidth(5)
line.draw(win)
return True

elif(entries[0][0] == 2 and entries[0][1] == 2 and entries[0][2] == 2):
line = Line(Point(50,0),Point(50,300))
line.setFill('green')
line.setWidth(5)
line.draw(win)
return True
elif(entries[1][0] == 2 and entries[1][1] == 2 and entries[1][2] == 2):
line = Line(Point(150,0),Point(150,300))
line.setFill('green')
line.setWidth(5)
line.draw(win)
return True
elif(entries[2][0] == 2 and entries[2][1] == 2 and entries[2][2] == 2):
line = Line(Point(250,0),Point(250,300))
line.setFill('green')
line.setWidth(5)
line.draw(win)
return True
elif(entries[0][0] == 2 and entries[1][0] == 2 and entries[2][0] == 2):
line = Line(Point(0,50),Point(300,50))
line.setFill('green')
line.setWidth(5)
line.draw(win)
return True
elif(entries[0][1] == 2 and entries[1][1] == 2 and entries[2][1] == 2):
line = Line(Point(0,150),Point(300,150))
line.setFill('green')
line.setWidth(5)
line.draw(win)
return True
elif(entries[0][2] == 2 and entries[1][2] == 2 and entries[2][2] == 2):
line = Line(Point(0,250),Point(300,250))
line.setFill('green')
line.setWidth(5)
line.draw(win)
return True
elif(entries[0][0] == 2 and entries[1][1] == 2 and entries[2][2] == 2):
line = Line(Point(0,0),Point(300,300))
line.setFill('green')
line.setWidth(5)
line.draw(win)
return True
elif(entries[0][2] == 2 and entries[1][1] == 2 and entries[2][0] == 2):
line = Line(Point(0,300),Point(300,0))
line.setFill('green')
line.setWidth(5)
line.draw(win)
return True

def main():

global win
global boxsize
entries = [[-1 for x in range(3)] for y in range(3)]

windowsize = 300
squares = 3
boxsize = 100

win = GraphWin("Tic Tac Toe", windowsize, windowsize)

for i in range(squares - 1):
hline = Line(Point(0, (windowsize/squares) * (i + 1)), Point(windowsize, (windowsize/squares) * (i + 1)))
hline.draw(win)
vline = Line(Point((windowsize/squares) * (i + 1), 0), Point((windowsize/squares) * (i + 1), windowsize))
vline.draw(win)

for i in range(1,10):
print("Player 1: click a square.")
p1mouse = win.getMouse()
p1x = p1mouse.getX()
p1y = p1mouse.getY()
tic_tac_toe_X(entries,win, p1x, p1y)
if(check(entries) == True):
print("Player 1 is the winner.")
break
  
print("Player 2: click a Circle.")
p2mouse = win.getMouse()
p2x = p2mouse.getX()
p2y = p2mouse.getY()
tic_tac_toe_O(entries,win, p2x, p2y)
if(check(entries) == True):
print("Player 2 is the winner.")
break

print("Enter 1 to play again, 0 to Exit : ")
x = int(input())
if(x==1):
win.close()
main()
else:
sys.exit()

main()


Related Solutions

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
Program Description, Interfaces, and Functionality To create a Python tic-tac-toe game, use Python's randint function from...
Program Description, Interfaces, and Functionality To create a Python tic-tac-toe game, use Python's randint function from the random module to control the computer's moves. To determine if the game has been won or is a tie/draw, write at least the func- tions: def move(), def check_cols(), def check_diag1(), def check_diag2(), def check_rows(), and def print_board(). Four of the functions will check if a winning state exist. To win tic-tac-toe, a user needs to have three of the same game pieces,...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
If anyone can please write a code for a 5x5 tic tac toe game in matlab...
If anyone can please write a code for a 5x5 tic tac toe game in matlab I would greatly appreciate it. Its extra credit. PLEASE HELP ME :(
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please...
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please provide code for checking diagonal win. function checkWinVertical(player, row, col) { var counter = 0; //each button var btn = document.getElementsByTagName("button"); for (counterC = 0; counterC < col; counterC++){ for (countR = 0; countR < row; countR++){ if (player != btn[counter].innerHTML){ counter += 1; break; } else if (countR + 1 == col) { for (countW = 0; countW < col; countW++){ btn[counter -...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
In Python: Please complete the following two tasks in Tic Tac Toe: 1. Allow the user...
In Python: Please complete the following two tasks in Tic Tac Toe: 1. Allow the user to choose heads/tails to see if the user goes first or the computer and alter your code accordingly. 2. Allow the user to play again. Add a simple strategy for the AI to play against the user.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT