Professional Documents
Culture Documents
# 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}")
win_or_tie(player_turn)
if __name__ == "__main__":
main()
@author: sharm
"""
class Application(Frame):
""" Main class for calculator"""
def create_widgets(self):
""" Create all the buttons for calculator. """
# User input stored as an Entry widget.
self.user_input.insert(0, "0")
# 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 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 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()
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()