0% found this document useful (0 votes)
31 views7 pages

# Display Welcome and Instructions

The document contains code for a Tic Tac Toe game that includes functions for displaying the board, determining player pieces, checking for a winner, getting player moves, modifying the board, and switching turns. The main function runs the game by initializing the board, displaying it, getting player pieces, checking for a winner after each turn or if it's a tie, and congratulating the winner or announcing a tie.

Uploaded by

hiten8411jdrravi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views7 pages

# Display Welcome and Instructions

The document contains code for a Tic Tac Toe game that includes functions for displaying the board, determining player pieces, checking for a winner, getting player moves, modifying the board, and switching turns. The main function runs the game by initializing the board, displaying it, getting player pieces, checking for a winner after each turn or if it's a tie, and congratulating the winner or announcing a tie.

Uploaded by

hiten8411jdrravi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

# Display welcome and instructions

# Display board
# Who gets X or O ?
# While no one has won and it isn't a tie loop
# get player's move X or O
# Update the board with the new move
# and check legal move, so move that hasn't been taken
#Switch Turns into X or O
#Congratulate Winner X or O or say it's a Tie

def display_welcome():
"""Display Welcome to Tic Tac Toe """
print(
"""
Hi and welcome to this Tic Tac Toe game_board.
You can win by haveing three X or 3 O in a row.
This can be horizontal, vertical or diagonal

"""
)

def display_board(game_board):
"""display the board"""
for x in range(3):
print("+---+---+---+" )
for y in range(3):
print("|", game_board[x*3+y], end = " ")
print("|")
print("+---+---+---+" )

def determine_pieces():
"""Determine who gets X or O"""
answer = None
answer = input("Do you wish to start with X or O ").upper()
while not answer in ['X','O']:
answer = input("Sorry this needs to be X or O ").upper()
return answer

def determine_winner(game_board):
"""check if there is a win, draw"""
# check rows
for i in range(3):
if game_board[i*3] == game_board[i*3+1] == game_board[i*3+2] and
game_board[i*3] in ('X', 'O'):
return game_board[i*3]
# check columns
for i in range(3):
if game_board[i] == game_board[i+3] == game_board[i+6] and game_board[i] in
('X', 'O'):
return game_board[i]
# check diagonals
if game_board[0] == game_board[4] == game_board[8] and game_board[0] in ('X',
'O'):
return game_board[0]
if game_board[2] == game_board[4] == game_board[6] and game_board[2] in ('X',
'O'):
return game_board[2]
# check tie
if all(x in ('X', 'O') for x in game_board):
return 'Tie'
# no winner or tie
return False

def get_move(game_board):
""" Get the player's move """
move = int(input(" Please make a move between 1 and 9 "))
while not legal_move(game_board,move):
move = int(input( " That move is not legal try again "))
return move

def legal_move(game_board,player_move):
"""Check if the move is legal"""
legal = True
if game_board[player_move -1] in ['X','O']:
legal = False
return legal

def modify_board(game_board,user_piece,move):
""" Modify the board positions into X or O"""
game_board[move - 1] = user_piece

def switch_turns(user_piece):
""" Change player's piece into X or O"""
if user_piece == 'X':
user_piece = 'O'
else:
user_piece = 'X'
return user_piece

def win_or_tie(user_piece):
""" display win for X or O or a tie"""
if user_piece == "TIE":
print("This is a TIE, but feel free to play again")
else:
print(f"Yes the player with {user_piece} won !!" )

def main():
""" game_board starting point """
game_board = [1,2,3,4,5,6,7,8,9]
player_turn = None
display_welcome()
display_board(game_board)
player_turn = determine_pieces()
print(f"ok you start with {player_turn}")

while not determine_winner(game_board):


player_move = get_move(game_board)
modify_board(game_board,player_turn,player_move)
display_board(game_board)
if not determine_winner(game_board):
player_turn = switch_turns(player_turn)

win_or_tie(player_turn)

if __name__ == "__main__":
main()

# -*- coding: utf-8 -*-


"""
Created on Wed Jul 26 16:09:56 2023

@author: sharm
"""

from tkinter import *

class Application(Frame):
""" Main class for calculator"""

def __init__(self, master):


""" Initialise the Frame. """
super(Application, self).__init__(master)
self.task = ""
self.UserIn = StringVar()
self.grid()
self.create_widgets()

def create_widgets(self):
""" Create all the buttons for calculator. """
# User input stored as an Entry widget.

self.user_input = Entry(self, bg = "#5BC8AC", bd = 29,


insertwidth = 4, width = 24,
font = ("Verdana", 20, "bold"), textvariable = self.UserIn, justify =
RIGHT)
self.user_input.grid(columnspan = 4)

self.user_input.insert(0, "0")

# Button for value 7


self.button1 = Button(self, bg = "#98DBC6", bd = 12,
text = "7", padx = 33, pady = 25, font = ("Helvetica", 20, "bold"),
command = lambda : self.buttonClick(7))
self.button1.grid(row = 2, column = 0, sticky = W)
# Button for value 8
self.button2 = Button(self, bg = "#98DBC6", bd = 12,
text = "8", padx = 35, pady = 25,
command = lambda : self.buttonClick(8), font = ("Helvetica", 20, "bold"))
self.button2.grid(row = 2, column = 1, sticky = W)

# Button for value 9


self.button3 = Button(self, bg = "#98DBC6", bd = 12,
text = "9", padx = 33, pady = 25,
command = lambda : self.buttonClick(9), font = ("Helvetica", 20, "bold"))
self.button3.grid(row = 2, column = 2, sticky = W)

# Button for value 4


self.button4 = Button(self, bg = "#98DBC6", bd = 12,
text = "4", padx = 33, pady = 25,
command = lambda : self.buttonClick(4), font = ("Helvetica", 20, "bold"))
self.button4.grid(row = 3, column = 0, sticky = W)

# Button for value 5


self.button5 = Button(self, bg = "#98DBC6", bd = 12,
text = "5", padx = 35, pady = 25,
command = lambda : self.buttonClick(5), font = ("Helvetica", 20, "bold"))
self.button5.grid(row = 3, column = 1, sticky = W)

# Button for value 6


self.button6 = Button(self, bg = "#98DBC6", bd = 12,
text = "6", padx = 33, pady = 25,
command = lambda : self.buttonClick(6), font = ("Helvetica", 20, "bold"))
self.button6.grid(row = 3, column = 2, sticky = W)

# Button for value 1


self.button7 = Button(self, bg = "#98DBC6", bd = 12,
text = "1", padx = 33, pady = 25,
command = lambda : self.buttonClick(1), font = ("Helvetica", 20, "bold"))
self.button7.grid(row = 4, column = 0, sticky = W)

# Button for value 2


self.button8 = Button(self, bg = "#98DBC6", bd = 12,
text = "2", padx = 35, pady = 25,
command = lambda : self.buttonClick(2), font = ("Helvetica", 20, "bold"))
self.button8.grid(row = 4, column = 1, sticky = W)

# Button for value 3


self.button9 = Button(self, bg = "#98DBC6", bd = 12,
text = "3", padx = 33, pady = 25,
command = lambda : self.buttonClick(3), font = ("Helvetica", 20, "bold"))
self.button9.grid(row = 4, column = 2, sticky = W)

# Button for value 0


self.button9 = Button(self, bg = "#98DBC6", bd = 12,
text = "0", padx = 33, pady = 25,
command = lambda : self.buttonClick(0), font = ("Helvetica", 20, "bold"))
self.button9.grid(row = 5, column = 0, sticky = W)

# Operator buttons
# Addition button
self.Addbutton = Button(self, bg = "#98DBC6", bd = 12,
text = "+", padx = 36, pady = 25,
command = lambda : self.buttonClick("+"), font = ("Helvetica", 20, "bold"))
self.Addbutton.grid(row = 2, column = 3, sticky = W)

# Subtraction button
self.Subbutton = Button(self, bg = "#98DBC6", bd = 12,
text = "-", padx = 39, pady = 25,
command = lambda : self.buttonClick("-"), font = ("Helvetica", 20, "bold"))
self.Subbutton.grid(row = 3, column = 3, sticky = W)

# Multiplication button
self.Multbutton = Button(self, bg = "#98DBC6", bd = 12,
text = "*", padx = 38, pady = 25,
command = lambda : self.buttonClick("*"), font = ("Helvetica", 20, "bold"))
self.Multbutton.grid(row = 4, column = 3, sticky = W)

# Division button
self.Divbutton = Button(self, bg = "#98DBC6", bd = 12,
text = "/", padx = 39, pady = 25,
command = lambda : self.buttonClick("/"), font = ("Helvetica", 20, "bold"))
self.Divbutton.grid(row = 5, column = 3, sticky = W)

# Equal button
self.Equalbutton = Button(self, bg = "#E6D72A", bd = 12,
text = "=", padx = 100, pady = 25,
command = self.CalculateTask, font = ("Helvetica", 20, "bold"))
self.Equalbutton.grid(row = 5, column = 1, sticky = W, columnspan = 2)

# Clear Button
self.Clearbutton = Button(self, bg = "#E6D72A", bd = 12,
text = "AC", font = ("Helvetica", 20, "bold"), width = 28, padx = 7,
command = self.ClearDisplay)
self.Clearbutton.grid(row = 1, columnspan = 4, sticky = W)

def buttonClick(self, number):


self.task = str(self.task) + str(number)
self.UserIn.set(self.task)

def CalculateTask(self):
self.data = self.user_input.get()
try:
self.answer = eval(self.data)
self.displayText(self.answer)
self.task = self.answer

except SyntaxError as e:
self.displayText("Invalid Syntax!")
self.task = ""

def displayText(self, value):


self.user_input.delete(0, END)
self.user_input.insert(0, value)

def ClearDisplay(self):
self.task = ""
self.user_input.delete(0, END)
self.user_input.insert(0, "0")

calculator = Tk()
calculator.title("Calculator")
app = Application(calculator)
# Make window fixed (cannot be resized)
calculator.resizable(width = False, height = False)

calculator.mainloop()

from datetime import datetime


import tkinter as tk
from threading import Thread
import time

class clock():
def __init__(self):
self.display = tk.Tk()
def start(self):
def get():
self.display.geometry("215x62")
self.display.title("Clock")
while True:
try:
now = datetime.now()
current_time = now.strftime("%H:%M %p")
lbl = tk.Label(self.display, text=str(current_time),
background = 'black', font = ("Helvetica", 37),
foreground = 'red')
lbl.place(x=0, y=0)
time.sleep(0.1)
except:
break
receive_thread = Thread(target=get)
receive_thread.start()
self.display.mainloop()
clock = clock()
clock.start()

You might also like