Advertisement
TonyMo

3 10 Making the game.py

Apr 23rd, 2021
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. # 3 10 Making the game for two players
  2. #  Run two instances on two IDE s
  3. #  Else develop this to run two players on one computer.
  4.  
  5. import sqlite3
  6. from random import randint
  7. from time import time
  8.  
  9. conn = sqlite3.connect("computer_cards.db")
  10.  
  11.  
  12. def read_all_cards():
  13.     result = conn.execute("SELECT * FROM computer")
  14.     return result.fetchall()
  15.  
  16.  
  17. def pick_card():
  18.     cards = read_all_cards()
  19.     last_picked_card = read_last_picked()
  20.     random_card = cards[randint(0, len(cards) - 1)]
  21.     while random_card[0] == last_picked_card[0]:
  22.         random_card = cards[randint(0, len(cards) - 1)]
  23.     insert_picked(random_card[0])
  24.     return random_card
  25.  
  26.  
  27. def insert_picked(name):
  28.     insert_sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name, time())
  29.     conn.execute(insert_sql)
  30.     conn.commit()
  31.    
  32.    
  33. def read_last_picked():
  34.     result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  35.     return result.fetchone()
  36.  
  37.  
  38. player = input("Are you player (1) or (2) > ")
  39.  
  40. choosing_player = "1" # for a start.
  41.  
  42. for round in range(5):    # Default =5 Change if you want
  43.     input("Press enter to pick a card when both players are ready > ")
  44.  
  45.     card = pick_card()
  46.     card_text = ("{}, cores={}, speed={}GHz, RAM={}MB, cost={}$"
  47.                  .format(card[0], card[1], card[2], card[3], card[4]))
  48.  
  49.     print(card)
  50.     print("Player " + choosing_player + " picks a statistic.")
  51.  
  52.     winner = input("Did you win? (Y)es, (N)o, (D)raw > ").lower()
  53.  
  54.     if winner == "y":
  55.         choosing_player = player
  56.     elif winner == "n":
  57.         choosing_player = "2" if player == "1" else "1"
  58.    
  59.  
  60. conn.close()
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement