Question

In: Computer Science

Python we need to create a game using only the library graphic.py and use only Structure...

Python we need to create a game using only the library graphic.py and use only Structure Functions only.

Solutions

Expert Solution

#My Tic Tac Game

from itertools import permutations
from graphics import *


def fetchRes():
    # producing the permutations required for winning and storing them in the array
    arrRes = []
    squence = permutations(['7', '8', '9'])
    for x in list(squence):
        aa = ''.join(x)
        arrRes.append(aa)
    squence = permutations(['6', '5', '4'])
    for x in list(squence):
        aa = ''.join(x)
        arrRes.append(aa)
    squence = permutations(['3', '2', '1'])
    for x in list(squence):
        aa = ''.join(x)
        arrRes.append(aa)
    squence = permutations(['1', '4', '7'])
    for x in list(squence):
        aa = ''.join(x)
        arrRes.append(aa)
    squence = permutations(['2', '5', '8'])
    for x in list(squence):
        aa = ''.join(x)
        arrRes.append(aa)
    squence = permutations(['3', '6', '9'])
    for x in list(squence):
        aa = ''.join(x)
        arrRes.append(aa)
    squence = permutations(['1', '5', '9'])
    for x in list(squence):
        aa = ''.join(x)
        arrRes.append(aa)
    squence = permutations(['3', '5', '7'])
    for x in list(squence):
        aa = ''.join(x)
        arrRes.append(aa)
    return arrRes


def cheking(values_clck, combinRes):
    arrPlay = []
    combi = permutations(values_clck, 3)
    # produce all of the box permutations which is cliecked by the user or plyer
    for c in list(combi):
        aa = ''.join(c)
        arrPlay.append(aa)
    # comparing all of the winning and player permutations
    for x in arrPlay:
        if (x in combinRes):
            # if the produced permutation matches the winning permutation, it will return true
            return True


def main():
    # producing the permutations required for winning and storing them in the array
    combin_Res = fetchRes()
    # defining the screen for the game
    gameScr = GraphWin('My Tic Tac', 300, 350)  # Declaration for title and windows measurement
    gameScr.setBackground('white')
    # declaration for horizontle_lines
    st_line = Line(Point(10, 100), Point(280, 100))
    st_line.setWidth(3)
    st_line.draw(gameScr)
    st_line = Line(Point(10, 190), Point(280, 190))
    st_line.setWidth(3)
    st_line.draw(gameScr)
    # declaration for verticle_lines
    st_line = Line(Point(100, 10), Point(100, 280))
    st_line.setWidth(3)
    st_line.draw(gameScr)
    st_line = Line(Point(190, 10), Point(190, 280))
    st_line.setWidth(3)
    st_line.draw(gameScr)

    # declaring variables
    f_Arr = []
    t_Arr = []
    cont_1 = 0
    cont_2 = 0
    x = 10
    y = 10
    i = 0
    j = 8
    ply_1 = [' ']
    ply_2 = [' ']
    ppl = "X"

    # To store the boundaries I am using while-loop
    while j >= 0:
        f_Arr.append(Point(x, y))
        t_Arr.append(Point(x + 90, y + 90))
        x = x + 90
        j = j - 1
        i = i + 1
        if (i == 3):
            x = 10
            y = y + 90
            i = 0
    clk = True
    v_num = ""
    textOne = Text(Point(1, 1), ply_1)
    t2 = Text(Point(1, 1), ply_1)
    res_Fin = False
    count = 1
    box_In = False
    ptMid = Point(0, 0)

    # while_loop to fetch the co-ordinates where the player has pressed using mouse
    while count <= 9:

        check = gameScr.getMouse()

        if (check.x > f_Arr[0].x and check.x < t_Arr[0].x and check.y > f_Arr[0].y and check.y < t_Arr[0].y):
            v_num = "9"
            ptMid = Point(f_Arr[0].x + 45, f_Arr[0].y + 45)
            box_In = True
        if (check.x > f_Arr[1].x and check.x < t_Arr[1].x and check.y > f_Arr[1].y and check.y < t_Arr[1].y):
            v_num = "8"
            ptMid = Point(f_Arr[1].x + 45, f_Arr[1].y + 45)
            box_In = True
        if (check.x > f_Arr[2].x and check.x < t_Arr[2].x and check.y > f_Arr[2].y and check.y < t_Arr[2].y):
            v_num = "7"
            ptMid = Point(f_Arr[2].x + 45, f_Arr[2].y + 45)
            box_In = True
        if (check.x > f_Arr[3].x and check.x < t_Arr[3].x and check.y > f_Arr[3].y and check.y < t_Arr[3].y):
            v_num = "6"
            ptMid = Point(f_Arr[3].x + 45, f_Arr[3].y + 45)
            box_In = True
        if (check.x > f_Arr[4].x and check.x < t_Arr[4].x and check.y > f_Arr[4].y and check.y < t_Arr[4].y):
            v_num = "5"
            ptMid = Point(f_Arr[4].x + 45, f_Arr[4].y + 45)
            box_In = True
        if (check.x > f_Arr[5].x and check.x < t_Arr[5].x and check.y > f_Arr[5].y and check.y < t_Arr[5].y):
            v_num = "4"
            ptMid = Point(f_Arr[5].x + 45, f_Arr[5].y + 45)
            box_In = True
        if (check.x > f_Arr[6].x and check.x < t_Arr[6].x and check.y > f_Arr[6].y and check.y < t_Arr[6].y):
            v_num = "3"
            ptMid = Point(f_Arr[6].x + 45, f_Arr[6].y + 45)
            box_In = True
        if (check.x > f_Arr[7].x and check.x < t_Arr[7].x and check.y > f_Arr[7].y and check.y < t_Arr[7].y):
            v_num = "2"
            ptMid = Point(f_Arr[7].x + 45, f_Arr[7].y + 45)
            box_In = True
        if (check.x > f_Arr[8].x and check.x < t_Arr[8].x and check.y > f_Arr[8].y and check.y < t_Arr[8].y):
            v_num = "1"
            ptMid = Point(f_Arr[8].x + 45, f_Arr[8].y + 45)
            box_In = True
        # put point
        if box_In:
            myText = Text(ptMid, ppl)
            myText.setSize(36)
            myText.draw(gameScr)
            box_In = False
            count = count + 1
        # changing sign of the present plyer and inspecting the outcome
        if ppl == "X":
            ply_1.append(v_num)
            if len(ply_1) >= 3:
                res_Fin = cheking(ply_1, combin_Res)
            if res_Fin:
                textOne = Text(Point(gameScr.getWidth() / 2, 320), "Player " + ppl + " wins")
                textOne.setSize(20)
                textOne.setTextColor("black")
                textOne.draw(gameScr)
                clk = False
                break
            ppl = "O"
        else:
            cont_2 += 1
            ply_2.append(v_num)
            if len(ply_2) >= 3:
                res_Fin = cheking(ply_2, combin_Res)
            if res_Fin:
                textOne = Text(Point(gameScr.getWidth() / 2, 320), "Player " + ppl + " wins")
                textOne.setSize(20)
                textOne.setTextColor("black")
                textOne.draw(gameScr)
                clk = False
                break
            ppl = "X"

    # When game is drawn the text no one wins will be displayed
    if clk:
        textOne = Text(Point(gameScr.getWidth() / 2, 320), "No One wins :X")
        textOne.setSize(20)
        textOne.setTextColor("Black")
        textOne.draw(gameScr)

main() #calling main function

#Tic-Tac Toe game using python in graphics.py library

Output of Code (Game) :

Summary : Created a python game of tic-tac-toe using only the mentioned library, i.e, graphics.py. I've implemented the code using three functiond named as fetchRes, checking and main, where fetchRes produces winning permutations, checking is used for matching and finally main function is used for user-interface stuff. I have used graphics.py module/library to make this program/game.

P.S. Hope you are satisfied with the provided solution, please give a thumbs up, Good Day !


Related Solutions

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
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play....
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play. This will include, at least, the game board, the two types of pieces and the two sides. You will need to determine the best way to represent the relationship between them. 2) Set up one side of the board. Print the status of the board.
You need to create game with 4 or more outcomes using a Hypergeometric situation and adjust...
You need to create game with 4 or more outcomes using a Hypergeometric situation and adjust the point values so that a cost of 10 points would make the game profitable. Compare the method of calculating expected value with the table to the formula for hypergeometric presented in this activity. What are the pros and cons of each.
Please provide Python 3.7 implementation of Graph ADT using in-built structure List only (not using dict)....
Please provide Python 3.7 implementation of Graph ADT using in-built structure List only (not using dict). Basic structure of graph will be: Node = [] Edges = [ [ ] , [ ] ] Also provide method to print the graph along with all path
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
Using Python code create a program only with beginners code. You are taking online reservations at...
Using Python code create a program only with beginners code. You are taking online reservations at the The inn Ask for your client’s name and save in a variable Ask how many nights your client will be staying and save in a variable Room rental is $145 per night Sales tax is 8.5% Habitation tax is $5 per night (not subject to sales tax) Print out: Client’s name Room rate per night Number of nights Room cost (room rate *...
Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the...
Python: Solve following problems using Linked List Data Structure 2. Create a Queue class. In the queue class create enqueue, dequeue, first, empty, len and resize methods. The class should support circular queue and have the ability to resize the queue.
using xsd, Create a number guessing game You would need an input box and buttons Ask...
using xsd, Create a number guessing game You would need an input box and buttons Ask the user for a number Produce a random number from 1-50 Do the comparison ​a. Make sure to provide hints back to the user if the number enter is low or high ​​-this element should be automatically added ​b. Clear the value enter in the text ​c. Give them 3 tries Whether the user wins or loses, display all the guesses along with the...
using xml, Create a number guessing game You would need an input box and buttons Ask...
using xml, Create a number guessing game You would need an input box and buttons Ask the user for a number Produce a random number from 1-50 Do the comparison             a. Make sure to provide hints back to the user if the number enter is low or high                         -this element should be automatically added             b. Clear the value enter in the text             c. Give them 3 tries Whether the user wins or loses, display all the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT