import random
import mysql.connector as connector
# Global scores
score = 0
score1 = 0
# ASCII art for numbers
s1 = '''
_
| |
|_|_______
| | | | | |
| |_|_|_|_|
|_ |
|_| |
\_________/'''
s2 = '''
_ _
| | |
|_|_|_____
| | | | | |
| | |_|_|_|
|_ |
|_| |
\_________/'''
s3 = '''
_ _ _
| | | |
|_|_|_|___
| | | | | |
| | | |_|_|
|_ |
|_| |
\_________/'''
s4 = '''
_ _ _ _
| | | | |
|_|_|_|_|_
| | | | | |
| | | | |_|
|_ |
|_| |
\_________/'''
s5 = '''
_ _ _ _ _
| | | | | |
|_|_|_|_|_|
| | | | | |
__| | | | | |
|__ |
| |
\_________/'''
s6 = '''
_
| |
|_|______
| |_____|
| |_____|
| |_____|
| |_____|
\________/'''
s7 = '''
_
| |
|_|__________
| |_________|
| |_____|
| |_____|
| |_____|
\________/ '''
s8 = '''
_
| |
|_|__________
| |_________|
| |_________|
| |_____|
| |_____|
\________/ '''
s9 = '''
_ _
| | | |
|_|_____|_|
| | | | | |
| |_|_|_|_|
|_ |
|_| |
\_________/'''
s10 = '''
_ _ _ _ _
| | | | | |
|_|_|_|_|_|
| _ |
__| /| | | |
|__ | | | |
| _|_ |_| |
\_________/'''
# Mapping of numbers to ASCII art
d = {1: s1, 2: s2, 3: s3, 4: s4, 5: s5, 6: s6, 7: s7, 8: s8, 9: s9, 10: s10}
# Function to print user and PC moves side by side
def p(a, b):
l1 = a.split('\n')[1:]
l2 = b.split('\n')[1:]
print('You' + (' ' * 15) + 'PC')
for i in range(len(l1)):
print(l1[i], l2[i], sep=' ' * 15)
# User batting
def batting():
global score
score = 0
while True:
try:
a = int(input("Enter your number (1-10): "))
if a < 1 or a > 10:
raise ValueError
b = random.randint(1, 10)
p(d[a], d[b])
if a == b:
print('OUT! Your final score:', score)
break
else:
score += a
except ValueError:
print('Invalid input! Enter a number between 1 and 10.')
# User bowling
def bowl():
global score1
score1 = 0
while True:
try:
a = int(input("Enter your number (1-10): "))
if a < 1 or a > 10:
raise ValueError
b = random.randint(1, 10)
p(d[a], d[b])
if a == b:
print('PC is OUT! PC score:', score1)
break
else:
score1 += b
except ValueError:
print('Invalid input! Enter a number between 1 and 10.')
# MySQL connection setup
try:
db = connector.connect(
host='localhost',
user='root',
passwd='cqcdrdo3$'
)
curs = db.cursor()
curs.execute('CREATE DATABASE IF NOT EXISTS handcricket')
curs.execute('USE handcricket')
curs.execute('''CREATE TABLE IF NOT EXISTS storage1
(name VARCHAR(255), score INT, victory VARCHAR(10))''')
except Exception as e:
print("Database connection failed:", e)
exit()
# Main game loop
print('🎮 Welcome to Hand Cricket 🎮')
while True:
name = input("Enter your name: ").strip()
if not name:
name = "Player"
try:
toss = int(input("Toss Time! Enter 1 for Even or 2 for Odd: "))
if toss not in [1, 2]:
raise ValueError
a = int(input("Enter your toss number (1-10): "))
if a < 1 or a > 10:
raise ValueError
b = random.randint(1, 10)
p(d[a], d[b])
total = a + b
user_won_toss = (toss == 1 and total % 2 == 0) or (toss == 2 and total % 2 != 0)
if user_won_toss:
print('You won the toss!')
choice = input("Enter 1 to Bat first or 2 to Bowl first: ")
if choice == '1':
print('🟢 First Innings: You Bat 🏏')
batting()
print('🟡 Second Innings: You Bowl 🎯')
bowl()
else:
print('🟢 First Innings: You Bowl 🎯')
bowl()
print('🟡 Second Innings: You Bat 🏏')
batting()
else:
print('PC won the toss!')
pc_choice = random.choice(['bat', 'bowl'])
print('PC chose to', pc_choice)
if pc_choice == 'bat':
print('🟢 First Innings: PC Bats 🎯')
bowl()
print('🟡 Second Innings: You Bat 🏏')
batting()
else:
print('🟢 First Innings: You Bat 🏏')
batting()
print('🟡 Second Innings: PC Bats 🎯')
bowl()
# Match result
if score > score1:
print(f'🏆 {name} won by {score - score1} runs!')
result = 'yes'
elif score == score1:
print('🤝 Match Drawn!')
result = 'draw'
else:
print(f'💥 PC won by {score1 - score} runs!')
result = 'no'
# Store in database
curs.execute('INSERT INTO storage1 (name, score, victory) VALUES (%s, %s, %s)',
(name, score, result))
db.commit()
# Fetch and show highest score
curs.execute('SELECT name, score, victory FROM storage1 ORDER BY score DESC LIMIT 1')
top = curs.fetchone()
print(f'Your score: {score}')
print(f'Highest score: {top}')
# Play again?
q = input("\nEnter 1 to play again or any other key to exit: ")
if q != '1':
print("Thanks for playing! 👋")
break
except ValueError:
print('Invalid input, try again!')
except Exception as e:
print('Error:', e)