Advertisement
Mori007

assigndbcards

Mar 2nd, 2021
1,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. # Game on dbcards
  2. import sqlite3
  3. from random import randint
  4. from time import time
  5.  
  6. conn = sqlite3.connect("d:/Pystrukturdata/databasenya/computer_cards.db")
  7.  
  8. def read_all_cards():
  9.     result = conn.execute("SELECT * FROM computer")
  10.     return result.fetchall()
  11.  
  12.  
  13. def pick_card():
  14.     cards = read_all_cards()
  15.     last_picked_card = read_last_picked()
  16.     random_card = cards[randint(0, len(cards) - 1)]
  17.     while random_card[0] == last_picked_card[0]:
  18.         random_card = cards[randint(0, len(cards) - 1)]
  19.     insert_picked(random_card[0])
  20.     return random_card
  21.  
  22. def insert_picked(name):
  23.     insert_sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name, time())
  24.     conn.execute(insert_sql)
  25.     conn.commit()
  26.  
  27. # Function to write the the 'results' table containing card1, card2, winner
  28. def insert_card_played(card_p1, card_p2, the_winner):
  29.     insert_sql = f"INSERT INTO result(card1, card2, winner) VALUES ('{card_p1}', '{card_p2}', '{the_winner}')"
  30.     conn.execute(insert_sql)
  31.     conn.commit()
  32.  
  33. # Function reading the last data
  34. def read_last_picked():
  35.     result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  36.     return result.fetchone()
  37.  
  38. # Function reading many data
  39. def read_card_just_played():
  40.     result = conn.execute("SELECT name FROM picked ORDER BY time DESC")
  41.     return result.fetchmany(2)
  42.    
  43. # Function update players
  44. def update_card_played(won, card):
  45.     # gets last two card picked, check who has what, update who has won.
  46.     cards_in_play = read_card_just_played()
  47.    
  48.     if cards_in_play[0][0] == card:
  49.         if won:
  50.             insert_card_played(card, cards_in_play[1][0], card)
  51.         else:
  52.             insert_card_played(card, cards_in_play[1][0], cards_in_play[1][0])
  53.     else:
  54.         if won:
  55.             insert_card_played(card, cards_in_play[0][0], card)
  56.         else:
  57.             insert_card_played(card, cards_in_play[0][0], cards_in_play[0][0])  
  58.  
  59. # Function of start game player
  60. def playing():
  61.     player = input("Are you player (1) or (2) >")
  62.     choosing_player = "1"
  63.  
  64.     # List of start score each player
  65.     score =[0,0]
  66.    
  67.     for round in range(5):
  68.         input("Press enter to pick a card when both players are ready >")
  69.         card = pick_card()
  70.         card_text = "{}, cores={}, speed={}GHz, RAM={}MB, cost={}$".format(card[0], card[1], card[2], card[3], card[4])
  71.         print("************************************************************************")
  72.         print(card_text)
  73.         print("************************************************************************")
  74.         print("Player " + choosing_player + " picks.")
  75.         winner = input("Did you win? (Y)es, (N)o, (D)raw >").lower()
  76.        
  77.         # Taking score for each players and update score
  78.         if winner == "y":
  79.             choosing_player = player
  80.             if player =="1":  
  81.                 score[0] += 1
  82.                 update_card_played("won", card[0])
  83.             else:
  84.                 score[1] += 1
  85.            
  86.         elif winner == "n":
  87.             choosing_player = "2" if player == "1" else "1"  
  88.             if player =="1":
  89.                 score[1] += 1
  90.                 update_card_played("lost", card[0])
  91.             else:
  92.                 score[0] += 1
  93.                
  94.         elif winner == "d":
  95.             pass
  96.      
  97. # Display the score each player
  98.     print("---RESULT SCORE------")
  99.     print(f" Player 1 ===> {score[0]}")
  100.     print(f" Player 2 ===> {score[1]}")
  101.     print("---------------------")
  102.    
  103. playing()
  104.  
  105. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement