Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added rock paper scissor game #1980

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions DS&Algo Programs in Python/Level_order_traversal_in_a_binary_tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class TreeNode:
def __init__(self, key):
self.left = None
self.right = None
self.val = key

def level_order_traversal(root):
if root is None:
return

# Create an empty queue for level order traversal
queue = []

# Enqueue the root node
queue.append(root)

while queue:
# Dequeue a node from the queue
node = queue.pop(0)

# Print the node's value
print(node.val, end=' ')

# Enqueue the left child
if node.left is not None:
queue.append(node.left)

# Enqueue the right child
if node.right is not None:
queue.append(node.right)

# Sample binary tree:
# 1
# / \
# 2 3
# / \
# 4 5

root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)

print("Level Order Traversal:")
level_order_traversal(root)
42 changes: 42 additions & 0 deletions Game/RockPaperScissor/gameRockPaperScissors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import random

def get_user_choice():
user_choice = input("Choose Rock, Paper, or Scissors: ").lower()
while user_choice not in ["rock", "paper", "scissors"]:
print("Invalid choice. Please choose Rock, Paper, or Scissors.")
user_choice = input("Choose Rock, Paper, or Scissors: ").lower()
return user_choice

def get_computer_choice():
return random.choice(["rock", "paper", "scissors"])

def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "It's a tie!"
if (
(user_choice == "rock" and computer_choice == "scissors") or
(user_choice == "paper" and computer_choice == "rock") or
(user_choice == "scissors" and computer_choice == "paper")
):
return "You win!"
else:
return "Computer wins!"

def main():
print("Welcome to Rock, Paper, Scissors!")
while True:
user_choice = get_user_choice()
computer_choice = get_computer_choice()

print(f"You chose {user_choice}")
print(f"Computer chose {computer_choice}")

result = determine_winner(user_choice, computer_choice)
print(result)

play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != "yes":
break

if __name__ == "__main__":
main()
42 changes: 42 additions & 0 deletions Game/RockPaperScissor/gameRockPapterScissors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import random

def get_user_choice():
user_choice = input("Choose Rock, Paper, or Scissors: ").lower()
while user_choice not in ["rock", "paper", "scissors"]:
print("Invalid choice. Please choose Rock, Paper, or Scissors.")
user_choice = input("Choose Rock, Paper, or Scissors: ").lower()
return user_choice

def get_computer_choice():
return random.choice(["rock", "paper", "scissors"])

def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "It's a tie!"
if (
(user_choice == "rock" and computer_choice == "scissors") or
(user_choice == "paper" and computer_choice == "rock") or
(user_choice == "scissors" and computer_choice == "paper")
):
return "You win!"
else:
return "Computer wins!"

def main():
print("Welcome to Rock, Paper, Scissors!")
while True:
user_choice = get_user_choice()
computer_choice = get_computer_choice()

print(f"You chose {user_choice}")
print(f"Computer chose {computer_choice}")

result = determine_winner(user_choice, computer_choice)
print(result)

play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != "yes":
break

if __name__ == "__main__":
main()