Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-Programs into testing
  • Loading branch information
NitkarshChourasia committed Dec 27, 2023
2 parents bf0e246 + 7e7df4c commit d719073
Show file tree
Hide file tree
Showing 54 changed files with 3,307 additions and 103 deletions.
104 changes: 104 additions & 0 deletions AI Game/Tic-Tac-Toe-AI/tictactoe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import tkinter as tk #provides a library of basic elements of GUI widgets
from tkinter import messagebox #provides a different set of dialogues that are used to display message boxes
import random

def check_winner(board, player):
# Check rows, columns, and diagonals for a win
for i in range(3):
if all(board[i][j] == player for j in range(3)) or all(board[j][i] == player for j in range(3)):
return True
if all(board[i][i] == player for i in range(3)) or all(board[i][2 - i] == player for i in range(3)):
return True
return False

def is_board_full(board):
return all(all(cell != ' ' for cell in row) for row in board)

def minimax(board, depth, is_maximizing):
if check_winner(board, 'X'):
return -1
if check_winner(board, 'O'):
return 1
if is_board_full(board): #if game is full, terminate
return 0

if is_maximizing: #recursive approach that fills board with Os
max_eval = float('-inf')
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'O'
eval = minimax(board, depth + 1, False) #recursion
board[i][j] = ' '
max_eval = max(max_eval, eval)
return max_eval
else: #recursive approach that fills board with Xs
min_eval = float('inf')
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'X'
eval = minimax(board, depth + 1, True) #recursion
board[i][j] = ' '
min_eval = min(min_eval, eval)
return min_eval

#determines the best move for the current player and returns a tuple representing the position
def best_move(board):
best_val = float('-inf')
best_move = None

for i in range(3):
for j in range(3):
if board[i][j] == ' ':
board[i][j] = 'O'
move_val = minimax(board, 0, False)
board[i][j] = ' '
if move_val > best_val:
best_val = move_val
best_move = (i, j)

return best_move

def make_move(row, col):
if board[row][col] == ' ':
board[row][col] = 'X'
buttons[row][col].config(text='X')
if check_winner(board, 'X'):
messagebox.showinfo("Tic-Tac-Toe", "You win!")
root.quit()
elif is_board_full(board):
messagebox.showinfo("Tic-Tac-Toe", "It's a draw!")
root.quit()
else:
ai_move()
else:
messagebox.showerror("Error", "Invalid move")

#AI's turn to play
def ai_move():
row, col = best_move(board)
board[row][col] = 'O'
buttons[row][col].config(text='O')
if check_winner(board, 'O'):
messagebox.showinfo("Tic-Tac-Toe", "AI wins!")
root.quit()
elif is_board_full(board):
messagebox.showinfo("Tic-Tac-Toe", "It's a draw!")
root.quit()

root = tk.Tk()
root.title("Tic-Tac-Toe")

board = [[' ' for _ in range(3)] for _ in range(3)]
buttons = []

for i in range(3):
row_buttons = []
for j in range(3):
button = tk.Button(root, text=' ', font=('normal', 30), width=5, height=2, command=lambda row=i, col=j: make_move(row, col))
button.grid(row=i, column=j)
row_buttons.append(button)
buttons.append(row_buttons)

root.mainloop()
112 changes: 47 additions & 65 deletions Caesar Cipher Encoder & Decoder.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,67 @@
#PROJECT1
#CAESAR CIPHER DECODER
# PROJECT1
# CAESAR CIPHER ENCODER/DECODER

#Author: InTruder
#Cloned from: https://github.com/InTruder-Sec/caesar-cipher
# Author: InTruder
# Cloned from: https://github.com/InTruder-Sec/caesar-cipher

# Improved by: OfficialAhmed (https://github.com/OfficialAhmed)

def get_int() -> int:
"""
Get integer, otherwise redo
"""

try:
key = int(input("Enter number of characters you want to shift: "))
except:
print("Enter an integer")
key = get_int()

return key

def main():

print("[>] CAESAR CIPHER DECODER!!! \n")
print("[1] Encrypt\n[2] Decrypt")
try:
func = int(input("Choose one of the above(example for encode enter 1): "))
except:
print("\n[>] Invalid input")
exit()

if func == 2:
decode()
else:
if func == 1:
match input("Choose one of the above(example for encode enter 1): "):

case "1":
encode()
else:
print("\n[>] Invalid input")
exit()

case "2":
decode()

case _:
print("\n[>] Invalid input. Choose 1 or 2")
main()


def encode():
text = input("Enter text to encode: ")
key = input("Enter number of characters you want to shift: ")

encoded_cipher = ""
try:
key = int(key)
except:
print("Only intigers between 0 to 25 are allowed. Try again :)")
exit()
if key > 25:
print("Only intigers between 0 to 25 are allowed. Try again :)")
exit()
else:
key = key
text = text.upper()
text = input("Enter text to encode: ")
key = get_int()

for char in text:
ascii = ord(char)
if ascii > 90:
new_ascii = ascii
else:
if ascii < 65:
new_ascii = ascii
else:
new_ascii = ascii + key
if new_ascii > 90:
new_ascii = new_ascii - 26
else:
new_ascii = new_ascii
encoded = chr(new_ascii)
encoded_cipher = encoded_cipher + encoded
print("Encoded text: " + encoded_cipher)

ascii = ord(char) + key
encoded_cipher += chr(ascii)

print(f"Encoded text: {encoded_cipher}")


def decode():

decoded_cipher = ""
cipher = input("\n[>] Enter your cipher text: ")
print("Posiblities of cipher text are: \n")
cipher = cipher.lower()
for i in range(1, 26):
decoded = ""
decoded_cipher = ""
for char in cipher:
ascii = ord(char)
if ascii < 97:
new_ascii = ascii
else:
if ascii > 122:
new_ascii = ascii
else:
new_ascii = ascii - int(i)
if new_ascii < 97:
new_ascii = new_ascii + 26
else:
new_ascii = new_ascii
decoded = chr(new_ascii)
decoded_cipher = decoded_cipher + decoded
print("\n" + decoded_cipher)
key = get_int()

for character in cipher:
ascii = ord(character) - key
decoded_cipher += chr(ascii)

print(decoded_cipher)


if __name__ == '__main__':
Expand Down
95 changes: 72 additions & 23 deletions Calculator with simple ui.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,73 @@
# Program make a simple calculator

# This function adds two numbers
def add(x, y):
return x + y

# This function subtracts two numbers
def subtract(x, y):
return x - y
class Calculator:
def __init__(self):
pass

# This function multiplies two numbers
def multiply(x, y):
return x * y
def add(self, num1, num2):
"""
This function adds two numbers.
# This function divides two numbers
def divide(x, y):
return x / y
Examples:
>>> add(2, 3)
5
>>> add(5, 9)
14
>>> add(-1, 2)
1
"""
return num1 + num2

def subtract(self, num1, num2):
"""
This function subtracts two numbers.
Examples:
>>> subtract(5, 3)
2
>>> subtract(9, 5)
4
>>> subtract(4, 9)
-5
"""
return num1 - num2

def multiply(self, num1, num2):
"""
This function multiplies two numbers.
Examples:
>>> multiply(4, 2)
8
>>> multiply(3, 3)
9
>>> multiply(9, 9)
81
"""
return num1 * num2

def divide(self, num1, num2):
"""
This function divides two numbers.
Examples:
>>> divide(4, 4)
1
>>> divide(6, 3)
2
>>> divide(9, 1)
9
"""
if num2 == 0:
print("Cannot divide by zero")
else:
return num1 / num2


calculator = Calculator()


print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
Expand All @@ -28,22 +78,21 @@ def divide(x, y):
choice = input("Enter choice(1/2/3/4): ")

# Check if choice is one of the four options
if choice in ('1', '2', '3', '4'):
if choice in ("1", "2", "3", "4"):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
if choice == "1":
print(calculator.add(num1, num2))

elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == "2":
print(calculator.subtract(num1, num2))

elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == "3":
print(calculator.multiply(num1, num2))

elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
elif choice == "4":
print(calculator.divide(num1, num2))
break
else:
print("Invalid Input")

Loading

0 comments on commit d719073

Please sign in to comment.