xavicano

Untitled

May 17th, 2020
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. import sqlite3
  2. from time import time
  3. from random import randint
  4.  
  5. conn = sqlite3.connect("computer_cards.db")
  6.  
  7. def read_all_cards():
  8.     result = conn.execute("SELECT * FROM computer")
  9.     return result.fetchall()
  10.    
  11. def pick_card():
  12.     cards = read_all_cards()
  13.     last_picked_card = read_last_picked()
  14.     random_card = cards[randint(0, len(cards) - 1)]
  15.     while random_card[0] == last_picked_card[0]:
  16.         random_card = cards[randint(0, len(cards) - 1)]
  17.     insert_picked(random_card[0])
  18.     return random_card
  19.    
  20. def insert_picked(name):
  21.     insert_sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name, time())
  22.     conn.execute(insert_sql)
  23.     conn.commit()
  24.    
  25. def insert_winner(name):
  26.     result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  27.     winner= result.fetchone()
  28.     ##print("Winner =",winner)
  29.     loser=result.fetchone()
  30.     ##print("Winner =",loser)
  31.     ##print("Card1 =",name)
  32.     insert_sql="INSERT INTO result(card1,card2,winner) VALUES ('{}','{}','{}')".format(name,loser[0],winner[0])
  33.     conn.execute(insert_sql)
  34.     conn.commit()
  35.    
  36. def stat_winner(name):
  37.     stat=0
  38.     result = conn.execute("SELECT * FROM result WHERE winner='{}'".format(name))
  39.     print("The winner card has won {} times up to now.".format(len(result.fetchall())))
  40.    
  41.    
  42. def read_last_picked():
  43.     result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  44.     return result.fetchone()
  45.    
  46. choosing_player = "1"
  47.  
  48. player = input("Are you player (1) or (2) >")
  49.  
  50. for round in range(5):
  51.     input("Press enter to pick a card when both players are ready >")
  52.  
  53.     card = pick_card()
  54.     card_text = "{}, cores={}, speed={}GHz, RAM={}MB, cost={}$".format(card[0], card[1], card[2], card[3], card[4])
  55.     print(card_text)
  56.     print("___________________________________________")
  57.     print("|   {:<10}{:^20}".format("Name = ",card[0]))
  58.     print("|   {:<10}{:^5}".format("Cores = ",card[1]))
  59.     print("|   {:<10}{:^5}  GHz".format("CPU SPeed= ", card[2]))
  60.     print("|   {:<10}{:^5}  MB".format("RAM = ", card[3]))
  61.     print("|   {:<10}{:^5}  €".format("Cost = ",card[4]))
  62.     print("___________________________________________")
  63.     print("Player " + choosing_player + " picks.")
  64.     winner = input("Did you win? (Y)es, (N)o, (D)raw >").lower()
  65.     if winner == "y":
  66.         choosing_player = player
  67.         insert_winner(card[0])
  68.         stat_winner(card[0])
  69.     elif winner == "n":
  70.         choosing_player = "2" if player == "1" else "1"
  71.  
  72.  
  73.  
  74. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment