Skip to content

Commit 7e7df4c

Browse files
Merge branch 'geekcomputers:master' into testing
2 parents a519e72 + a143ffa commit 7e7df4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+3307
-103
lines changed

AI Game/Tic-Tac-Toe-AI/tictactoe.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import tkinter as tk #provides a library of basic elements of GUI widgets
2+
from tkinter import messagebox #provides a different set of dialogues that are used to display message boxes
3+
import random
4+
5+
def check_winner(board, player):
6+
# Check rows, columns, and diagonals for a win
7+
for i in range(3):
8+
if all(board[i][j] == player for j in range(3)) or all(board[j][i] == player for j in range(3)):
9+
return True
10+
if all(board[i][i] == player for i in range(3)) or all(board[i][2 - i] == player for i in range(3)):
11+
return True
12+
return False
13+
14+
def is_board_full(board):
15+
return all(all(cell != ' ' for cell in row) for row in board)
16+
17+
def minimax(board, depth, is_maximizing):
18+
if check_winner(board, 'X'):
19+
return -1
20+
if check_winner(board, 'O'):
21+
return 1
22+
if is_board_full(board): #if game is full, terminate
23+
return 0
24+
25+
if is_maximizing: #recursive approach that fills board with Os
26+
max_eval = float('-inf')
27+
for i in range(3):
28+
for j in range(3):
29+
if board[i][j] == ' ':
30+
board[i][j] = 'O'
31+
eval = minimax(board, depth + 1, False) #recursion
32+
board[i][j] = ' '
33+
max_eval = max(max_eval, eval)
34+
return max_eval
35+
else: #recursive approach that fills board with Xs
36+
min_eval = float('inf')
37+
for i in range(3):
38+
for j in range(3):
39+
if board[i][j] == ' ':
40+
board[i][j] = 'X'
41+
eval = minimax(board, depth + 1, True) #recursion
42+
board[i][j] = ' '
43+
min_eval = min(min_eval, eval)
44+
return min_eval
45+
46+
#determines the best move for the current player and returns a tuple representing the position
47+
def best_move(board):
48+
best_val = float('-inf')
49+
best_move = None
50+
51+
for i in range(3):
52+
for j in range(3):
53+
if board[i][j] == ' ':
54+
board[i][j] = 'O'
55+
move_val = minimax(board, 0, False)
56+
board[i][j] = ' '
57+
if move_val > best_val:
58+
best_val = move_val
59+
best_move = (i, j)
60+
61+
return best_move
62+
63+
def make_move(row, col):
64+
if board[row][col] == ' ':
65+
board[row][col] = 'X'
66+
buttons[row][col].config(text='X')
67+
if check_winner(board, 'X'):
68+
messagebox.showinfo("Tic-Tac-Toe", "You win!")
69+
root.quit()
70+
elif is_board_full(board):
71+
messagebox.showinfo("Tic-Tac-Toe", "It's a draw!")
72+
root.quit()
73+
else:
74+
ai_move()
75+
else:
76+
messagebox.showerror("Error", "Invalid move")
77+
78+
#AI's turn to play
79+
def ai_move():
80+
row, col = best_move(board)
81+
board[row][col] = 'O'
82+
buttons[row][col].config(text='O')
83+
if check_winner(board, 'O'):
84+
messagebox.showinfo("Tic-Tac-Toe", "AI wins!")
85+
root.quit()
86+
elif is_board_full(board):
87+
messagebox.showinfo("Tic-Tac-Toe", "It's a draw!")
88+
root.quit()
89+
90+
root = tk.Tk()
91+
root.title("Tic-Tac-Toe")
92+
93+
board = [[' ' for _ in range(3)] for _ in range(3)]
94+
buttons = []
95+
96+
for i in range(3):
97+
row_buttons = []
98+
for j in range(3):
99+
button = tk.Button(root, text=' ', font=('normal', 30), width=5, height=2, command=lambda row=i, col=j: make_move(row, col))
100+
button.grid(row=i, column=j)
101+
row_buttons.append(button)
102+
buttons.append(row_buttons)
103+
104+
root.mainloop()

Caesar Cipher Encoder & Decoder.py

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,67 @@
1-
#PROJECT1
2-
#CAESAR CIPHER DECODER
1+
# PROJECT1
2+
# CAESAR CIPHER ENCODER/DECODER
33

4-
#Author: InTruder
5-
#Cloned from: https://github.com/InTruder-Sec/caesar-cipher
4+
# Author: InTruder
5+
# Cloned from: https://github.com/InTruder-Sec/caesar-cipher
66

7+
# Improved by: OfficialAhmed (https://github.com/OfficialAhmed)
8+
9+
def get_int() -> int:
10+
"""
11+
Get integer, otherwise redo
12+
"""
13+
14+
try:
15+
key = int(input("Enter number of characters you want to shift: "))
16+
except:
17+
print("Enter an integer")
18+
key = get_int()
19+
20+
return key
721

822
def main():
23+
924
print("[>] CAESAR CIPHER DECODER!!! \n")
1025
print("[1] Encrypt\n[2] Decrypt")
11-
try:
12-
func = int(input("Choose one of the above(example for encode enter 1): "))
13-
except:
14-
print("\n[>] Invalid input")
15-
exit()
1626

17-
if func == 2:
18-
decode()
19-
else:
20-
if func == 1:
27+
match input("Choose one of the above(example for encode enter 1): "):
28+
29+
case "1":
2130
encode()
22-
else:
23-
print("\n[>] Invalid input")
24-
exit()
31+
32+
case "2":
33+
decode()
34+
35+
case _:
36+
print("\n[>] Invalid input. Choose 1 or 2")
37+
main()
38+
2539

2640
def encode():
27-
text = input("Enter text to encode: ")
28-
key = input("Enter number of characters you want to shift: ")
41+
2942
encoded_cipher = ""
30-
try:
31-
key = int(key)
32-
except:
33-
print("Only intigers between 0 to 25 are allowed. Try again :)")
34-
exit()
35-
if key > 25:
36-
print("Only intigers between 0 to 25 are allowed. Try again :)")
37-
exit()
38-
else:
39-
key = key
40-
text = text.upper()
43+
text = input("Enter text to encode: ")
44+
key = get_int()
45+
4146
for char in text:
42-
ascii = ord(char)
43-
if ascii > 90:
44-
new_ascii = ascii
45-
else:
46-
if ascii < 65:
47-
new_ascii = ascii
48-
else:
49-
new_ascii = ascii + key
50-
if new_ascii > 90:
51-
new_ascii = new_ascii - 26
52-
else:
53-
new_ascii = new_ascii
54-
encoded = chr(new_ascii)
55-
encoded_cipher = encoded_cipher + encoded
56-
print("Encoded text: " + encoded_cipher)
47+
48+
ascii = ord(char) + key
49+
encoded_cipher += chr(ascii)
5750

51+
print(f"Encoded text: {encoded_cipher}")
5852

5953

6054
def decode():
55+
56+
decoded_cipher = ""
6157
cipher = input("\n[>] Enter your cipher text: ")
62-
print("Posiblities of cipher text are: \n")
63-
cipher = cipher.lower()
64-
for i in range(1, 26):
65-
decoded = ""
66-
decoded_cipher = ""
67-
for char in cipher:
68-
ascii = ord(char)
69-
if ascii < 97:
70-
new_ascii = ascii
71-
else:
72-
if ascii > 122:
73-
new_ascii = ascii
74-
else:
75-
new_ascii = ascii - int(i)
76-
if new_ascii < 97:
77-
new_ascii = new_ascii + 26
78-
else:
79-
new_ascii = new_ascii
80-
decoded = chr(new_ascii)
81-
decoded_cipher = decoded_cipher + decoded
82-
print("\n" + decoded_cipher)
58+
key = get_int()
59+
60+
for character in cipher:
61+
ascii = ord(character) - key
62+
decoded_cipher += chr(ascii)
63+
64+
print(decoded_cipher)
8365

8466

8567
if __name__ == '__main__':

Calculator with simple ui.py

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,73 @@
11
# Program make a simple calculator
22

3-
# This function adds two numbers
4-
def add(x, y):
5-
return x + y
63

7-
# This function subtracts two numbers
8-
def subtract(x, y):
9-
return x - y
4+
class Calculator:
5+
def __init__(self):
6+
pass
107

11-
# This function multiplies two numbers
12-
def multiply(x, y):
13-
return x * y
8+
def add(self, num1, num2):
9+
"""
10+
This function adds two numbers.
1411
15-
# This function divides two numbers
16-
def divide(x, y):
17-
return x / y
12+
Examples:
13+
>>> add(2, 3)
14+
5
15+
>>> add(5, 9)
16+
14
17+
>>> add(-1, 2)
18+
1
19+
"""
20+
return num1 + num2
21+
22+
def subtract(self, num1, num2):
23+
"""
24+
This function subtracts two numbers.
25+
26+
Examples:
27+
>>> subtract(5, 3)
28+
2
29+
>>> subtract(9, 5)
30+
4
31+
>>> subtract(4, 9)
32+
-5
33+
"""
34+
return num1 - num2
35+
36+
def multiply(self, num1, num2):
37+
"""
38+
This function multiplies two numbers.
39+
40+
Examples:
41+
>>> multiply(4, 2)
42+
8
43+
>>> multiply(3, 3)
44+
9
45+
>>> multiply(9, 9)
46+
81
47+
"""
48+
return num1 * num2
49+
50+
def divide(self, num1, num2):
51+
"""
52+
This function divides two numbers.
53+
54+
Examples:
55+
>>> divide(4, 4)
56+
1
57+
>>> divide(6, 3)
58+
2
59+
>>> divide(9, 1)
60+
9
61+
"""
62+
if num2 == 0:
63+
print("Cannot divide by zero")
64+
else:
65+
return num1 / num2
66+
67+
68+
calculator = Calculator()
1869

1970

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

3080
# Check if choice is one of the four options
31-
if choice in ('1', '2', '3', '4'):
81+
if choice in ("1", "2", "3", "4"):
3282
num1 = float(input("Enter first number: "))
3383
num2 = float(input("Enter second number: "))
3484

35-
if choice == '1':
36-
print(num1, "+", num2, "=", add(num1, num2))
85+
if choice == "1":
86+
print(calculator.add(num1, num2))
3787

38-
elif choice == '2':
39-
print(num1, "-", num2, "=", subtract(num1, num2))
88+
elif choice == "2":
89+
print(calculator.subtract(num1, num2))
4090

41-
elif choice == '3':
42-
print(num1, "*", num2, "=", multiply(num1, num2))
91+
elif choice == "3":
92+
print(calculator.multiply(num1, num2))
4393

44-
elif choice == '4':
45-
print(num1, "/", num2, "=", divide(num1, num2))
94+
elif choice == "4":
95+
print(calculator.divide(num1, num2))
4696
break
4797
else:
4898
print("Invalid Input")
49-

0 commit comments

Comments
 (0)