Advertisement
Guest User

Cards Game Project

a guest
Nov 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.74 KB | None | 0 0
  1. # Battle of cards Game
  2. # ST 2019
  3. # Programming 103: Saving and Structuring Data
  4. # A Raspberry Pi Foundation Course
  5.  
  6. import sqlite3
  7. from random import randint
  8. from time import time
  9. conn = sqlite3.connect("computer_cards.db")
  10.  
  11. def read_all_cards():
  12.     result = conn.execute("SELECT * FROM computer")
  13.     return result.fetchall()
  14.  
  15. def pick_card():
  16.     cards = read_all_cards()
  17.     last_picked_card = read_last_picked()
  18.     random_card = cards[randint(0, len(cards) - 1)]
  19.  
  20.     while random_card[0] == last_picked_card[0]:
  21.         random_card = cards[randint(0, len(cards) - 1)]
  22.  
  23.     insert_picked(random_card[0])
  24.     return random_card
  25.  
  26. def insert_picked(name):
  27.     insert_sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name, time())
  28.     conn.execute(insert_sql)
  29.     conn.commit()
  30.  
  31. def insert_result(name1, name2, winner):
  32.     insert_sql = "INSERT INTO result(card1, card2, winner) VALUES ('{}', '{}', '{}')".format(name1, name2, winner)
  33.     conn.execute(insert_sql)
  34.     conn.commit()
  35.  
  36. def read_last_picked():
  37.     result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  38.     return result.fetchone()
  39.  
  40. def read_last2_picked():
  41.     result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  42.     return result.fetchone(), result.fetchone()
  43.  
  44. def print_card(datos):
  45.     maxw = 35
  46.     print("\n")
  47.     print("+" + maxw*"-" + "+")
  48.     print("|" + '{:^{maxw}}'.format(datos[0], maxw=maxw) + "|")
  49.     print("|" + maxw*" " + "|")
  50.     print("|" + '{:<18}'.format("    Cores") + '{:>10}'.format(datos[1]) +  '{:<4}'.format(" ") + "   |")
  51.     print("|" + '{:<18}'.format("    CPU speed") + '{:>10}'.format(datos[2]) + '{:<4}'.format(" GHz") + "   |")
  52.     print("|" + '{:<18}'.format("    RAM") + '{:>10}'.format(datos[3]) + '{:<4}'.format(" MB") + "   |")
  53.     print("|" + '{:<18}'.format("    Cost") + '{:>10}'.format("$ " + str(datos[4])) +  '{:<4}'.format(" ") + "   |")
  54.     print("|" + maxw*" " + "|")
  55.     print("+" + maxw*"-" + "+")
  56.  
  57. def print_score(score_p1, score_p2):
  58.     if player == "1":
  59.         yourscore = score_p1
  60.         opponentscore = score_p2
  61.     elif player == "2":
  62.         yourscore = score_p2
  63.         opponentscore = score_p1
  64.    
  65.     print("\n<Your score: {}> \t\t <Opponent's score: {}>\n".format(yourscore, opponentscore))
  66.     return yourscore, opponentscore
  67.  
  68. def statistics(name):
  69.     data = conn.execute("SELECT * FROM result ORDER BY winner")
  70.     table = data.fetchall()
  71.     num_wins = 0
  72.     num_duels = 0
  73.     for computer in table:
  74.         if computer[0] == name or computer[1] == name:
  75.             num_duels = num_duels + 1
  76.         if computer[2] == name:
  77.             num_wins = num_wins + 1
  78.    
  79.     print("\nThe card ยซ" + name + "ยป :")
  80.     if num_duels == 0:
  81.         print("Sorry... statistics about this card are not available")
  82.         return
  83.     else:
  84.         percentage_success = round(num_wins / num_duels * 100, 2)        
  85.         print("Has come victorious in " + str(num_wins) + " out of " + str(num_duels) + " duels")
  86.         print("Percentage of success: " + str(percentage_success) + " %")
  87.         print(40 * "-")
  88.    
  89.    
  90.    
  91. print("\n*** WELCOME TO BATTLE OF CARDS ***\n")
  92.  
  93. player = ''
  94. while player == '' or not(player in ['1', '2']):
  95.     player = input("Are you player (1) or (2) >")
  96.  
  97. choosing_player = "1"
  98. score_p1 = 0
  99. score_p2 = 0
  100. winner = "d"
  101. num_rounds = 5
  102.  
  103.  
  104. while winner == "d" and score_p1 + score_p2 < num_rounds:
  105.     print_score(score_p1, score_p2)
  106.     input("Press enter to pick a card when both players are ready >")
  107.     card = pick_card()
  108.     print_card(card)
  109.     print("Player " + choosing_player + " picks.")
  110.     winner = ''
  111.     while winner == '' or not(winner in ['y', 'n', 'd']):
  112.         winner = input("Did you win? (Y)es, (N)o, (D)raw >").lower()
  113.     card1, card2 = read_last2_picked()
  114.     if card[0] == card1[0]:
  115.         name1 = card[0]
  116.         name2 = card2[0]
  117.     elif card[0] == card2[0]:
  118.         name1 = card[0]
  119.         name2 = card1[0]
  120.     if winner == "y":
  121.         choosing_player = player
  122.         if player == "1":
  123.             score_p1 = score_p1 + 1
  124.             insert_result(name1, name2, name1)
  125.         elif player == "2":
  126.             score_p2 = score_p2 + 1
  127.     elif winner == "n":
  128.         if player == "1":
  129.             score_p2 = score_p2 + 1
  130.             insert_result(name1, name2, name2)
  131.         elif player == "2":
  132.             score_p1 = score_p1 + 1
  133.         choosing_player = "2" if player == "1" else "1"
  134.     if winner != "d":
  135.         statistics(card1[0])
  136.         statistics(card2[0])
  137.     winner = "d"
  138.        
  139.  
  140. yourscore, opponentscore = print_score(score_p1, score_p2)
  141. if yourscore > opponentscore:
  142.     print("CONGRATULATIONS! You won!\n")
  143. else:
  144.     print("You lost... Better luck next time!\n")
  145. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement