Advertisement
bensimmo

Untitled

Jan 16th, 2021
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.86 KB | None | 0 0
  1. import sqlite3
  2. from random import randint
  3. from time import time
  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.    
  15.     random_card = cards[randint(0, len(cards) - 1)]
  16.     while random_card[0] == last_picked_card[0]:
  17.         random_card = cards[randint(0, len(cards) -1)]
  18.    
  19.     insert_picked(random_card[0])
  20.     return random_card
  21.  
  22. def insert_picked(name):
  23.     insert_sql = f"INSERT INTO picked(name, time) VALUES ( '{name}', {time()} )"
  24.     conn.execute(insert_sql)
  25.     conn.commit()
  26.  
  27. # new code
  28. def insert_card_played(card_p1, card_p2, the_winner):
  29.     # function to write the the 'results' table containing card1, card2, winner
  30.     insert_sql = f"INSERT INTO result(card1, card2, winner) VALUES ('{card_p1}', '{card_p2}', '{the_winner}')"
  31.    
  32.     conn.execute(insert_sql)
  33.     conn.commit()
  34.  
  35. # new code
  36. def update_card_played(won, card):
  37.     # gets last two card picked, check who has what, update who has won.
  38.     cards_in_play = read_card_just_played()
  39.    
  40.     if cards_in_play[0][0] == card:
  41.         if won:
  42.             insert_card_played(card, cards_in_play[1][0], card)
  43.         else:
  44.             insert_card_played(card, cards_in_play[1][0], cards_in_play[1][0])
  45.     else:
  46.         if won:
  47.             insert_card_played(card, cards_in_play[0][0], card)
  48.         else:
  49.             insert_card_played(card, cards_in_play[0][0], cards_in_play[0][0])  
  50.            
  51.  
  52. def read_last_picked():
  53.     result = conn.execute("SELECT name FROM picked ORDER BY time DESC")
  54.     return result.fetchone()
  55.  
  56. def read_card_just_played():
  57.     result = conn.execute("SELECT name FROM picked ORDER BY time DESC")
  58.     return result.fetchmany(2)
  59.    
  60. def display_card(card_text):
  61.     print(
  62.     f"+----------------------------------+\n"
  63.     f"| {card_text[0]:^31}  |\n"
  64.     f"|                                  |\n"
  65.     f"|   Cores         {card_text[1]:<15}  |\n"
  66.     f"|   CPU speed     {card_text[2]:<10} GHz   |\n"
  67.     f"|   RAM           {card_text[3]:<10} MB    |\n"
  68.     f"|   Cost         ${card_text[4]:<15}  |\n"
  69.     f"|                                  |\n"
  70.     f"+----------------------------------+"
  71.     )
  72.    
  73. def play_game():
  74.     print("Welcome to Top Trump Computers")
  75.     print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
  76.    
  77.     player = input("Are you player (1) or player (2)?>")
  78.     player_name = input("What is your name? >")
  79.    
  80.     choosing_player = "1"   # traking who goes first
  81.  
  82.     score = [0,0]
  83.     for round in range(5):
  84.         input("Both players ready? Press Enter to pick card a card. >")
  85.         card = pick_card()  # picks card, and checks it's not already picked
  86.         card_text = f"{card[0]}, cores={card[1]}, speed={card[2]}GHz, RAM={card[3]}MB, cost={card[4]}$"
  87.         print(display_card(card))
  88.        
  89.         print(f"Player {choosing_player} picks.")
  90.         winner = input("Did you win? (Y)es, (N)o, (D)raw >").lower()
  91.    
  92.        # new code, updates played cards (only if player1), increases score
  93.         if winner == "y":
  94.             choosing_player = player
  95.             if player == "1":  #Ternary operator didn't work here ?
  96.                 score[0] += 1
  97.                 update_card_played("won", card[0]) # only Player_1 updates the database
  98.             else:
  99.                 score[1] += 1
  100.            
  101.         elif winner == "n":
  102.             choosing_player = "2" if player == "1" else "1"  #using a Ternary operaor  
  103.             if player =="1":
  104.                 score[1] += 1
  105.                 update_card_played("lost", card[0])
  106.             else:
  107.                 score[0] += 1
  108.        
  109.            
  110.     print(f"Player 1 Scored {score[0]}")
  111.     print(f"Player 2 Scored {score[1]}")
  112.  
  113. play_game()
  114.  
  115. conn.close()
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement