Advertisement
Guest User

Top Trumps Game

a guest
Jan 20th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. import sqlite3
  2. from random import randint
  3. from time import time
  4. conn = sqlite3.connect("computer_cards.db")
  5.  
  6. def read_all_cards():
  7.     result = conn.execute("SELECT * FROM computer")
  8.     return result.fetchall()
  9.  
  10. def insert_picked(name):
  11.     insert_sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name, time())
  12.     conn.execute(insert_sql)
  13.     conn.commit()
  14.  
  15. def insert_round_cards(card1, card2, winner):
  16.     insert_round_sql = "INSERT INTO result(card1, card2, winner) VALUES ('{}', '{}', '{}')".format(card1, card2, winner)
  17.     conn.execute(insert_round_sql)
  18.     conn.commit()
  19.  
  20. def read_last_picked():
  21.     result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  22.     return result.fetchone()
  23.  
  24. def read_last_round():
  25.     round_results = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  26.     return round_results.fetchmany(2)
  27.  
  28. def pick_card():
  29.     cards = read_all_cards()
  30.  
  31.     last_picked_card = read_last_picked()
  32.  
  33.     last_round_card = read_last_round()
  34.    
  35.     random_card = cards[randint(0, len(cards) - 1)]
  36.  
  37.     random_card2 = cards[randint(0, len(cards) - 1)]
  38.  
  39.     while random_card[0] == last_picked_card[0]:
  40.         random_card = cards[randint(0, len(cards) - 1)]
  41.  
  42.     while random_card2[0] == last_picked_card[0]:
  43.         random_card2 = cards[randint(0, len(cards) - 1)]
  44.  
  45.     insert_picked(random_card[0])
  46.    
  47.     return random_card, random_card2
  48.  
  49. def count_winners():
  50.     count_sql = conn.execute("SELECT winner, COUNT(*) FROM result GROUP BY winner")
  51.     return count_sql.fetchall()
  52.    
  53. player = input("Are you player (1) or (2) >")
  54.  
  55. choosing_player = "1"
  56.  
  57. input("Press enter to pick a card when both players are ready >")
  58.  
  59. card = pick_card()
  60. print(card)
  61. card_text = "{}, cores={}, speed={}GHz, RAM={}MB, cost={}£".format(card[0][0], card[0][1], card[0][2], card[0][3], card[0][4])
  62. print(card_text)
  63.  
  64. print("Player " + choosing_player + " picks.")
  65.  
  66. winner = input("Did you win? (Y)es, (N)o, (D)raw >").lower()
  67.  
  68. if winner == "y":
  69.     choosing_player = player
  70.     insert_round_cards(card[0][0],card[1][0],card[0][0])
  71. elif winner == "n":
  72.     choosing_player = "2" if player == "1" else "1"
  73.     insert_round_cards(card[0][0],card[1][0],card[1][0])
  74. elif winner == "d":
  75.     choosing_player = player
  76.     insert_round_cards(card[0][0],card[1][0],"draw")
  77.  
  78. winner_table = count_winners()
  79. print(winner_table)
  80.  
  81. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement