fork download
  1. import random
  2. import mysql.connector as connector
  3.  
  4. # Global scores
  5. score = 0
  6. score1 = 0
  7.  
  8. # ASCII art for numbers
  9. s1 = '''
  10. _
  11. | |
  12. |_|_______
  13. | | | | | |
  14. | |_|_|_|_|
  15. |_ |
  16. |_| |
  17. \_________/'''
  18.  
  19. s2 = '''
  20. _ _
  21. | | |
  22. |_|_|_____
  23. | | | | | |
  24. | | |_|_|_|
  25. |_ |
  26. |_| |
  27. \_________/'''
  28.  
  29. s3 = '''
  30. _ _ _
  31. | | | |
  32. |_|_|_|___
  33. | | | | | |
  34. | | | |_|_|
  35. |_ |
  36. |_| |
  37. \_________/'''
  38.  
  39. s4 = '''
  40. _ _ _ _
  41. | | | | |
  42. |_|_|_|_|_
  43. | | | | | |
  44. | | | | |_|
  45. |_ |
  46. |_| |
  47. \_________/'''
  48.  
  49. s5 = '''
  50. _ _ _ _ _
  51. | | | | | |
  52. |_|_|_|_|_|
  53. | | | | | |
  54. __| | | | | |
  55. |__ |
  56. | |
  57. \_________/'''
  58.  
  59. s6 = '''
  60. _
  61. | |
  62. |_|______
  63. | |_____|
  64. | |_____|
  65. | |_____|
  66. | |_____|
  67. \________/'''
  68.  
  69. s7 = '''
  70. _
  71. | |
  72. |_|__________
  73. | |_________|
  74. | |_____|
  75. | |_____|
  76. | |_____|
  77. \________/ '''
  78.  
  79. s8 = '''
  80. _
  81. | |
  82. |_|__________
  83. | |_________|
  84. | |_________|
  85. | |_____|
  86. | |_____|
  87. \________/ '''
  88.  
  89. s9 = '''
  90. _ _
  91. | | | |
  92. |_|_____|_|
  93. | | | | | |
  94. | |_|_|_|_|
  95. |_ |
  96. |_| |
  97. \_________/'''
  98.  
  99. s10 = '''
  100. _ _ _ _ _
  101. | | | | | |
  102. |_|_|_|_|_|
  103. | _ |
  104. __| /| | | |
  105. |__ | | | |
  106. | _|_ |_| |
  107. \_________/'''
  108.  
  109. # Mapping of numbers to ASCII art
  110. d = {1: s1, 2: s2, 3: s3, 4: s4, 5: s5, 6: s6, 7: s7, 8: s8, 9: s9, 10: s10}
  111.  
  112.  
  113. # Function to print user and PC moves side by side
  114. def p(a, b):
  115. l1 = a.split('\n')[1:]
  116. l2 = b.split('\n')[1:]
  117. print('You' + (' ' * 15) + 'PC')
  118. for i in range(len(l1)):
  119. print(l1[i], l2[i], sep=' ' * 15)
  120.  
  121.  
  122. # User batting
  123. def batting():
  124. global score
  125. score = 0
  126. while True:
  127. try:
  128. a = int(input("Enter your number (1-10): "))
  129. if a < 1 or a > 10:
  130. raise ValueError
  131. b = random.randint(1, 10)
  132. p(d[a], d[b])
  133. if a == b:
  134. print('OUT! Your final score:', score)
  135. break
  136. else:
  137. score += a
  138. except ValueError:
  139. print('Invalid input! Enter a number between 1 and 10.')
  140.  
  141.  
  142. # User bowling
  143. def bowl():
  144. global score1
  145. score1 = 0
  146. while True:
  147. try:
  148. a = int(input("Enter your number (1-10): "))
  149. if a < 1 or a > 10:
  150. raise ValueError
  151. b = random.randint(1, 10)
  152. p(d[a], d[b])
  153. if a == b:
  154. print('PC is OUT! PC score:', score1)
  155. break
  156. else:
  157. score1 += b
  158. except ValueError:
  159. print('Invalid input! Enter a number between 1 and 10.')
  160.  
  161.  
  162. # MySQL connection setup
  163. try:
  164. db = connector.connect(
  165. host='localhost',
  166. user='root',
  167. passwd='cqcdrdo3$'
  168. )
  169. curs = db.cursor()
  170. curs.execute('CREATE DATABASE IF NOT EXISTS handcricket')
  171. curs.execute('USE handcricket')
  172. curs.execute('''CREATE TABLE IF NOT EXISTS storage1
  173. (name VARCHAR(255), score INT, victory VARCHAR(10))''')
  174. except Exception as e:
  175. print("Database connection failed:", e)
  176. exit()
  177.  
  178.  
  179. # Main game loop
  180. print('🎮 Welcome to Hand Cricket 🎮')
  181.  
  182. while True:
  183. name = input("Enter your name: ").strip()
  184. if not name:
  185. name = "Player"
  186.  
  187. try:
  188. toss = int(input("Toss Time! Enter 1 for Even or 2 for Odd: "))
  189. if toss not in [1, 2]:
  190. raise ValueError
  191.  
  192. a = int(input("Enter your toss number (1-10): "))
  193. if a < 1 or a > 10:
  194. raise ValueError
  195. b = random.randint(1, 10)
  196. p(d[a], d[b])
  197. total = a + b
  198.  
  199. user_won_toss = (toss == 1 and total % 2 == 0) or (toss == 2 and total % 2 != 0)
  200.  
  201. if user_won_toss:
  202. print('You won the toss!')
  203. choice = input("Enter 1 to Bat first or 2 to Bowl first: ")
  204. if choice == '1':
  205. print('🟢 First Innings: You Bat 🏏')
  206. batting()
  207. print('🟡 Second Innings: You Bowl 🎯')
  208. bowl()
  209. else:
  210. print('🟢 First Innings: You Bowl 🎯')
  211. bowl()
  212. print('🟡 Second Innings: You Bat 🏏')
  213. batting()
  214. else:
  215. print('PC won the toss!')
  216. pc_choice = random.choice(['bat', 'bowl'])
  217. print('PC chose to', pc_choice)
  218. if pc_choice == 'bat':
  219. print('🟢 First Innings: PC Bats 🎯')
  220. bowl()
  221. print('🟡 Second Innings: You Bat 🏏')
  222. batting()
  223. else:
  224. print('🟢 First Innings: You Bat 🏏')
  225. batting()
  226. print('🟡 Second Innings: PC Bats 🎯')
  227. bowl()
  228.  
  229. # Match result
  230. if score > score1:
  231. print(f'🏆 {name} won by {score - score1} runs!')
  232. result = 'yes'
  233. elif score == score1:
  234. print('🤝 Match Drawn!')
  235. result = 'draw'
  236. else:
  237. print(f'💥 PC won by {score1 - score} runs!')
  238. result = 'no'
  239.  
  240. # Store in database
  241. curs.execute('INSERT INTO storage1 (name, score, victory) VALUES (%s, %s, %s)',
  242. (name, score, result))
  243. db.commit()
  244.  
  245. # Fetch and show highest score
  246. curs.execute('SELECT name, score, victory FROM storage1 ORDER BY score DESC LIMIT 1')
  247. top = curs.fetchone()
  248. print(f'Your score: {score}')
  249. print(f'Highest score: {top}')
  250.  
  251. # Play again?
  252. q = input("\nEnter 1 to play again or any other key to exit: ")
  253. if q != '1':
  254. print("Thanks for playing! 👋")
  255. break
  256. except ValueError:
  257. print('Invalid input, try again!')
  258. except Exception as e:
  259. print('Error:', e)
  260.  
Success #stdin #stdout 0.03s 25484KB
stdin
Standard input is empty
stdout
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)