Mini Games Source Code
while True:
print('\n')
print('Hi!..')
name = input('What is your name:')
print('Good luck..!', name)
print('\n')
print('Welcome to our PYTHON PROGRAMMED Mini Games')
print('\n')
print('What do you want to play??')
print('\n')
print('############################')
print('Number guessing game....(1)')
print('Or')
print('Word guessing game....(2)')
print('or')
print('Tic Tac Toe....(3)')
print('or')
print('Stone Paper Scissors....(4)')
print('############################')
print('\n')
x = input('Enter your choice:')
if x == '1':
#Creating Number Guessing Game
import random
import math
# Taking Inputs
lower = int(input("Enter Lower bound: "))
# Taking Inputs
upper = int(input("Enter Upper bound: "))
# generating random number between
# the lower and upper
x = random.randint(lower, upper)
print("\n\tYou've only ",
round(math.log(upper - lower + 1, 2)),
" chances to guess the integer!\n")
# Initializing the number of guesses.
count = 0
# for calculation of minimum number of
# guesses depends upon range
while count < math.log(upper - lower + 1, 2):
count += 1
# taking guessing number as input
guess = int(input("Guess a number:- "))
# Condition testing
if x == guess:
print("Congratulations you did it in ",
count, " try")
print('*************************************')
# Once guessed, loop will break
break
elif x > guess:
print("You guessed too small!")
print('**************************')
elif x < guess:
print("You Guessed too high!")
print('**************************')
# If Guessing is more than required guesses,
# shows this output.
if count >= math.log(upper - lower + 1, 2):
print("\nThe number is %d" % x)
print("\tBetter Luck Next time!")
print('**********************************************************************************************************')
elif x=='2':
#Creating Word Guessing Game
import random
# library that we use in order to choose
# on random words from a list of words
# Here the user is asked to enter the name first
words = ['rainbow', 'computer', 'science', 'programming',
'python', 'mathematics', 'player', 'condition',
'reverse', 'adhiyamaan', 'board', 'technology', 'college',
'engineering', 'creative', 'lepoard', 'vibgyor', 'software',
'brain', 'autonomous','department','lecture', 'student']
# Function will choose one random
# word from this list of words
word = random.choice(words)
print("Guess the characters")
guesses = ''
# any number of turns can be used here
turns = 12
while turns > 0:
# counts the number of times a user fails
failed = 0
# all characters from the input
# word taking one at a time.
for char in word:
# comparing that character with
# the character in guesses
if char in guesses:
print(char, end=" ")
else:
print("_")
# for every failure 1 will be
# incremented in failure
failed += 1
if failed == 0:
# user will win the game if failure is 0
# and 'You Win' will be given as output
print("Congradulations You Win!")
# this print the correct word
print("The word is: ", word)
print('\n')
print("*******************************************************************************************")
break
# if user has input the wrong alphabet then
# it will ask user to enter another alphabet
print()
guess = input("guess a character:")
# every input character will be stored in guesses
guesses += guess
# check input with the character in word
if guess not in word:
turns -= 1
# if the character doesn’t match the word
# then “Wrong” will be given as output
print("Wrong")
print("**********************************")
# this will print the number of
# turns left for the user
print("You have", + turns, 'more guesses')
if turns == 0:
print("You Lose")
print("The word is: ", word)
print('\n')
print("*******************************************************************************")
elif x=='3':
#Creating Tic Tac Toe
import time
# Initialize board
board = {1: ' ', 2: ' ', 3: ' ',
4: ' ', 5: ' ', 6: ' ',
7: ' ', 8: ' ', 9: ' '}
# Initialize variables
count = 0 # counter to track number of filled slots
winner = False # boolean to check if anyone won
play = True # boolen to check if the game should continue
tie = False # boolean to check if there is a tie
curr_player = '' # variable to store current player identifier
player_details = [] # list to store player identifier and marker
# Helper functions
def get_player_details(curr_player):
"""Function to get player identifier and marker"""
if curr_player == 'A':
return ['B', 'O']
else:
return ['A', 'X']
def print_board():
"""Function to print the board"""
for i in board:
print(i, ':', board[i], ' ', end='')
if i % 3 == 0:
print()
def win_game(marker, player_id):
"""Function to check for winning combination"""
if board[1] == marker and board[2] == marker and board[3] == marker or \
board[4] == marker and board[5] == marker and board[6] == marker or \
board[7] == marker and board[8] == marker and board[9] == marker or \
board[1] == marker and board[4] == marker and board[7] == marker or \
board[2] == marker and board[5] == marker and board[8] == marker or \
board[3] == marker and board[6] == marker and board[9] == marker or \
board[1] == marker and board[5] == marker and board[9] == marker or \
board[3] == marker and board[5] == marker and board[7] == marker:
print_board()
time.sleep(1)
print("Player", player_id, "wins!")
return True
else:
return False
def insert_input(slot_num, marker):
"""Function for capturing user inputs"""
while board[slot_num] != ' ':
print("spot taken, pick another no.")
slot_num = int(input())
board[slot_num] = marker
def play_again():
"""Function to check if player wants to play again"""
print("Do you want to play again? (press Y for Yes) ")
play_again = input()
if play_again.upper() == 'Y':
for z in board:
board[z] = ' '
return True
else:
print("Thanks for playing. See you next time!")
print('\n')
print("**********************************************************************************")
return False
# Main program
while play:
print_board()
player_details = get_player_details(curr_player)
curr_player = player_details[0]
print("Player {}: Enter a number between 1 and 9".format(curr_player))
input_slot = int(input())
# Inserting 'X' or 'O' in desired spot
insert_input(input_slot, player_details[1])
count += 1
# Check if anybody won
winner = win_game(player_details[1], curr_player)
if count == 9 and not winner:
print("It's a tie!!")
tie = True
print_board()
# Check if players want to play again
if winner or tie:
play = play_again()
if play:
curr_player = ''
count = 0
elif x=='4':
#Creating Stone paper Scissors
import random
stone = '''
___
---' __)
(___)
(___)
(__)
---._(__)
'''
paper = '''
____
---' _)_____
____)
_____)
_____)
---.______)
'''
scissors = '''
___
---' _)______
______)
_____)
____)
(__)
---._(__)
'''
game_images = [stone, paper, scissors]
user_choice = int(
input(
"What do you choose? Type 0 for Stone, 1 for Paper or 2 for Scissors.\n"
))
if user_choice >= 3 or user_choice < 0:
print("You typed an invalid number, you lose!")
else:
print(game_images[user_choice])
computer_choice = random.randint(0, 2)
print(f"Computer chose: {computer_choice}")
print(game_images[computer_choice])
if user_choice >= 3 or user_choice < 0:
print("You typed an invalid number, you lose!")
elif user_choice == 0 and computer_choice == 2:
print("You win!")
elif computer_choice == 0 and user_choice == 2:
print("You lose")
elif computer_choice > user_choice:
print("You lose")
elif user_choice > computer_choice:
print("You win!")
elif computer_choice == user_choice:
print("It's a draw")
print('**********************************************************************************************')
else:
print('\n')
print("Invalid Input! ")
print('\n')
print("Thank you for playing ",name+'!')
print('************************************************************************************************************************')
Comments
Post a Comment