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...
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 :(
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...
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.
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the...
PLEASE READ VERY CAREFULLY write a client.py and server.py file for tic-tac-toe IN PYTHON with the following restrictions (SO WRITE TWO FILES THAT PLAY PYTHON THROUGH A SOCKET) Use a 5 x 5 grid (dimensions are subject to change, so use constants for NUM_ROWS and NUM_COLS) Use 'X' for player 1 and 'O' for player 2 (symbols and the number of players is subject to change, so use constants) Each player can make 1 move per turn before having to...
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 a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human...
Python Code Needed Two-Player, Two-Dimensional Tic-Tac-Toe Write a script to play two-dimensional Tic-Tac-Toe between two human players who alternate entering their moves on the same computer. Create a 3-by-3 two-dimensional array. Each player indicates their moves by entering a pair of numbers representing the row and column indices of the square in which they want to place their mark, either an 'X' or an 'O'. When the first player moves, place an 'X' in the specified square. When the second...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT