diff --git a/amstrong.py b/amstrong.py new file mode 100644 index 0000000..883e34c --- /dev/null +++ b/amstrong.py @@ -0,0 +1,7 @@ +a=input("enter number") #153 +l=len(a) +sum=0 +for i in a: + pow=i**3 + sum=sum+pow +print(sum) \ No newline at end of file diff --git a/spinner.py b/spinner.py new file mode 100644 index 0000000..fe7a650 --- /dev/null +++ b/spinner.py @@ -0,0 +1,36 @@ +from turtle import * +state = {'turn': 0} +def spinner(): + clear() + angle = state['turn']/10 + right(angle) + forward(100) + dot(120, 'red') + back(100) + right(120) + forward(100) + dot(120, 'green') + back(100) + right(120) + forward(100) + dot(120, 'blue') + back(100) + right(120) + update() +def animate(): + if state['turn']>0: + state['turn']-=1 + + spinner() + ontimer(animate, 20) +def flick(): + state['turn']+=10 + +setup(420, 420, 370, 0) +hideturtle() +tracer(False) +width(20) +onkey(flick, 'space') +listen() +animate() +done() \ No newline at end of file diff --git a/tic tac toe.py b/tic tac toe.py new file mode 100644 index 0000000..0c6eaf9 --- /dev/null +++ b/tic tac toe.py @@ -0,0 +1,200 @@ +from tkinter import * +import numpy as np + +board_size = 800 +symbol_size = (board_size / 3 - board_size / 8) / 2 +symbol_thickness = 50 +X_ = 'red' +O = 'pink' +Green_color = '#7BC043' + + +class Tic_Tac_Toe(): + # ------------------------------------------------------------------ + # Initialization Functions: + # ------------------------------------------------------------------ + def __init__(self): + self.window = Tk() + self.window.title('Tic-Tac-Toe') + self.canvas = Canvas(self.window, width=board_size, height=board_size) + self.canvas.pack() + # Input from user in form of clicks + self.window.bind('', self.click) + + self.initialize_board() + self.player_X_turns = True + self.board_status = np.zeros(shape=(3, 3)) + self.X_starts = True + self.reset_board = False + self.gameover = False + self.tie = False + self.X_wins = False + self.O_wins = False + + self.X_score = 0 + self.O_score = 0 + self.tie_score = 0 + + def mainloop(self): + self.window.mainloop() + + def initialize_board(self): + for i in range(2): + self.canvas.create_line((i + 1) * board_size / 3, 0, (i + 1) * board_size / 3, board_size) + + for i in range(2): + self.canvas.create_line(0, (i + 1) * board_size / 3, board_size, (i + 1) * board_size / 3) + + def play_again(self): + self.initialize_board() + self.X_starts = not self.X_starts + self.player_X_turns = self.X_starts + self.board_status = np.zeros(shape=(3, 3)) + + # ------------------------------------------------------------------ + # The modules required to draw required game based object on canvas + # ------------------------------------------------------------------ + + def draw_O(self, logical_position): + logical_position = np.array(logical_position) + # logical_position = grid value on the board + # grid_position = actual pixel values of the center of the grid + grid_pos = self.convert_logical_to_grid_position(logical_position) + self.canvas.create_oval(grid_pos[0] - symbol_size, grid_pos[1] - symbol_size, + grid_pos[0] + symbol_size, grid_pos[1] + symbol_size, width=symbol_thickness, + outline=O) + + def draw_X(self, logical_position): + grid_pos = self.convert_logical_to_grid_position(logical_position) + self.canvas.create_line(grid_pos[0] - symbol_size, grid_pos[1] - symbol_size, + grid_pos[0] + symbol_size, grid_pos[1] + symbol_size, width=symbol_thickness, + fill=X_) + self.canvas.create_line(grid_pos[0] - symbol_size, grid_pos[1] + symbol_size, + grid_pos[0] + symbol_size, grid_pos[1] - symbol_size, width=symbol_thickness, + fill=X_) + + def display_gameover(self): + + if self.X_wins: + self.X_score += 1 + text = 'Winner: Player 1 (X)' + color = X_ + elif self.O_wins: + self.O_score += 1 + text = 'Winner: Player 2 (O)' + color = O + else: + self.tie_score += 1 + text = 'Its a tie' + color = 'gray' + + self.canvas.delete("all") + self.canvas.create_text(board_size / 2, board_size / 3, font="cmr 60 bold", fill=color, text=text) + + score_text = 'Scores \n' + self.canvas.create_text(board_size / 2, 5 * board_size / 8, font="cmr 40 bold", fill=Green_color, + text=score_text) + + score_text = 'Player 1 (X) : ' + str(self.X_score) + '\n' + score_text += 'Player 2 (O): ' + str(self.O_score) + '\n' + score_text += 'Tie : ' + str(self.tie_score) + self.canvas.create_text(board_size / 2, 3 * board_size / 4, font="cmr 30 bold", fill=Green_color, + text=score_text) + self.reset_board = True + + score_text = 'Click to play again \n' + self.canvas.create_text(board_size / 2, 15 * board_size / 16, font="cmr 20 bold", fill="gray", + text=score_text) + + # ------------------------------------------------------------------ + # The modules required to carry out game logic + # ------------------------------------------------------------------ + + def convert_logical_to_grid_position(self, logical_position): + logical_position = np.array(logical_position, dtype=int) + return (board_size / 3) * logical_position + board_size / 6 + + def convert_grid_to_logical_position(self, grid_position): + grid_position = np.array(grid_position) + return np.array(grid_position // (board_size / 3), dtype=int) + + def is_grid_occupied(self, logical_position): + if self.board_status[logical_position[0]][logical_position[1]] == 0: + return False + else: + return True + + def is_winner(self, player): + + player = -1 if player == 'X' else 1 + + # Three in a row + for i in range(3): + if self.board_status[i][0] == self.board_status[i][1] == self.board_status[i][2] == player: + return True + if self.board_status[0][i] == self.board_status[1][i] == self.board_status[2][i] == player: + return True + + # Diagonals + if self.board_status[0][0] == self.board_status[1][1] == self.board_status[2][2] == player: + return True + + if self.board_status[0][2] == self.board_status[1][1] == self.board_status[2][0] == player: + return True + + return False + + def is_tie(self): + + r, c = np.where(self.board_status == 0) + tie = False + if len(r) == 0: + tie = True + + return tie + + def is_gameover(self): + # Either someone wins or all grid occupied + self.X_wins = self.is_winner('X') + if not self.X_wins: + self.O_wins = self.is_winner('O') + + if not self.O_wins: + self.tie = self.is_tie() + + gameover = self.X_wins or self.O_wins or self.tie + + if self.X_wins: + print('X wins') + if self.O_wins: + print('O wins') + if self.tie: + print('Its a tie') + + return gameover + + def click(self, event): + grid_position = [event.x, event.y] + logical_position = self.convert_grid_to_logical_position(grid_position) + if not self.reset_board: + if self.player_X_turns: + if not self.is_grid_occupied(logical_position): + self.draw_X(logical_position) + self.board_status[logical_position[0]][logical_position[1]] = -1 + self.player_X_turns = not self.player_X_turns + else: + if not self.is_grid_occupied(logical_position): + self.draw_O(logical_position) + self.board_status[logical_position[0]][logical_position[1]] = 1 + self.player_X_turns = not self.player_X_turns + # Check if game is concluded + if self.is_gameover(): + self.display_gameover() + # print('Done') + else: # Play Again + self.canvas.delete("all") + self.play_again() + self.reset_board = False + +game_instance = Tic_Tac_Toe() +game_instance.mainloop() \ No newline at end of file