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

Reduced code for Rock, Paper, Scissors #139

Open
wants to merge 5 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions c/Palindrome.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
A palindrome number is a number that is same after reverse.
For example 121, 34543, 343, 131, 48984 are the palindrome numbers.
*/
#include <stdio.h>
#include<stdio.h>
int main()
{
int n, m, rem, rev=0;
Expand All @@ -16,13 +16,7 @@ int main()
rev = rev * 10 +rem;
m = m / 10;
}
if(n == rev)
{
printf("%d is a Palindrome",n);
}
else
{
printf("%d is not a Palindrome",n);
}
n == rev ? printf("%d is a Palindrome",n) : printf("%d is not a Palindrome",n);

return 0;
}
17 changes: 11 additions & 6 deletions python/Celsius To Fahrenheit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Python Program to convert temperature in celsius to fahrenheit
# Python Program to convert temperature in celsius(take input from user) to fahrenheit

# change this value for a different result
celsius = 37.5
# Input: temperature in Celsius
celsius = float(input("Enter temperature in Celsius: "))

# calculate fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))
# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit

# Convert and display the result
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}°C is equal to {fahrenheit}°F")
68 changes: 21 additions & 47 deletions python/Rock,Paper,Scissors.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
from random import randint
import random

#create a list of play options
t = ["Rock", "Paper", "Scissors"]
def play():
options = ["rock", "paper", "scissors"]
computer_choice = random.choice(options)
user_choice = input("Enter rock, paper, or scissors: ").lower()

#assign a random play to the computer
computer = t[randint(0,2)]
your_score = 0
computer_score = 0
#set player to False
player = False
print(''' + "Welcome to Rock, Paper, Scissors! Type 'quit' to exit the game." + ''')
while player == False:
#set player to True
player = input("Rock, Paper, Scissors?")
if player == computer:
print("Tie!")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
computer_score += 1
else:
print("You win!", player, "smashes", computer)
your_score += 1
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cut", player)
computer_score += 1
else:
print("You win!", player, "covers", computer)
your_score += 1
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
computer_score += 1
else:
print("You win!", player, "cut", computer)
your_score += 1
if user_choice not in options:
print("Invalid choice! Please choose rock, paper, or scissors.")
return play() # Restart if invalid input

print(f"\nYou chose {user_choice}, and the computer chose {computer_choice}.")

if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
print("You win!")
else:
print("That's not a valid play. Check your spelling!")
if player == "quit":
print("Your score is", your_score)
print("The computer's score is", computer_score)
if your_score > computer_score:
print("You win!")
break

#player was set to True, but we want it to be False so the loop continues
player = False
computer = t[randint(0,2)]
print("Computer wins!")

# Run the game
play()
195 changes: 18 additions & 177 deletions python/snake.py
Original file line number Diff line number Diff line change
@@ -1,183 +1,24 @@
# Simple Snake Game in Python 3 for Beginners
import turtle
import time
import random

delay = 0.1
def play():
options = ["rock", "paper", "scissors"]
computer_choice = random.choice(options)
user_choice = input("Enter rock, paper, or scissors: ").lower()

# Score
score = 0
high_score = 0
if user_choice not in options:
print("Invalid choice! Please choose rock, paper, or scissors.")
return play() # Restart if invalid input

# Set up the screen
wn = turtle.Screen()
wn.title("Snake Game by @TokyoEdTech")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0) # Turns off the screen updates
print(f"\nYou chose {user_choice}, and the computer chose {computer_choice}.")

# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"
if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
print("You win!")
else:
print("Computer wins!")

# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)

segments = []

# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal"))

# Functions
def go_up():
if head.direction != "down":
head.direction = "up"

def go_down():
if head.direction != "up":
head.direction = "down"

def go_left():
if head.direction != "right":
head.direction = "left"

def go_right():
if head.direction != "left":
head.direction = "right"

def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)

if head.direction == "down":
y = head.ycor()
head.sety(y - 20)

if head.direction == "left":
x = head.xcor()
head.setx(x - 20)

if head.direction == "right":
x = head.xcor()
head.setx(x + 20)

# Keyboard bindings
wn.listen()
wn.onkeypress(go_up, "w")
wn.onkeypress(go_down, "s")
wn.onkeypress(go_left, "a")
wn.onkeypress(go_right, "d")

# Main game loop
while True:
wn.update()

# Check for a collision with the border
if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"

# Hide the segments
for segment in segments:
segment.goto(1000, 1000)

# Clear the segments list
segments.clear()

# Reset the score
score = 0

# Reset the delay
delay = 0.1

pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))


# Check for a collision with the food
if head.distance(food) < 20:
# Move the food to a random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x,y)

# Add a segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)

# Shorten the delay
delay -= 0.001

# Increase the score
score += 10

if score > high_score:
high_score = score

pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))

# Move the end segments first in reverse order
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)

# Move segment 0 to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x,y)

move()

# Check for head collision with the body segments
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0,0)
head.direction = "stop"

# Hide the segments
for segment in segments:
segment.goto(1000, 1000)

# Clear the segments list
segments.clear()

# Reset the score
score = 0

# Reset the delay
delay = 0.1

# Update the score display
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))

time.sleep(delay)

wn.mainloop()
# Run the game
play()