timber101

Trumps

Jan 20th, 2021
648
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #  pick a random card from cards database
  2.  
  3. # Trumps type game
  4.  
  5. from random import randint
  6. from time import time
  7. import sqlite3
  8.  
  9. connex = sqlite3.connect("computer_cards.db") # mu connection to the computer_cards database
  10.  
  11. """
  12. def update_winner_loser(p1, p2, pw):
  13.    insert_sql = "INSERT INTO result (p1, p2, pw) VALUES ('{}','{}','{}')".format(p1,p2,pw)
  14.    connex.execute(insert_sql)
  15.    connex.commit()
  16. """
  17. # function to insert the card picked and time in the picked table
  18. def insert_picked(name):
  19.     insert_sql = "INSERT INTO picked(name, time) VALUES('{}',{})".format(name, time())
  20.     connex.execute(insert_sql)
  21.     connex.commit()
  22.  
  23. # function to read the last card added to the picked table
  24. def read_last_picked():
  25.     last_picked = connex.execute("SELECT * FROM picked ORDER BY time DESC")
  26.     return last_picked.fetchone()
  27.  
  28. # function to pick a random number between 1 and the length of the table computer
  29. def pick_rdm_index():  # this is going to count rows in our table
  30.     rowcntsql = connex.execute("SELECT count (*) FROM computer") # counts lenght of computer table
  31.     rowcntval = (rowcntsql.fetchall())  #comes back as a list with tuple inside
  32.     #print(rowcntval) # testing
  33.     lastrow = (rowcntval[0][0]) # visit the tuple on index 0 and bring back the list item at index [0]
  34.     #print(type(lastrow)) # checking its an intger
  35.     #print(lastrow)
  36.     return (randint(1,lastrow)) # needs to start at row 1 as it is a basic list
  37.  
  38. # function to pick a random card, call the pick_rdm_index function above
  39. def pick_random_card():
  40.     last_picked_card = read_last_picked() # gets the last card picked
  41.     rndrow = pick_rdm_index() # gets random index
  42.     #print(rndrow) for testing
  43.     sel_card_sql = "SELECT * FROM computer WHERE rowid = {} ".format(rndrow) # get the row that corresponds with the random index
  44.     cardtgt = connex.execute(sel_card_sql)
  45.     cardpicked=cardtgt.fetchone()
  46.     #print(last_picked_card, cardpicked,"1") #testing while loop works
  47.     while cardpicked[0] == last_picked_card[0]: # loop to test last card picked is not then picked again
  48.         rndrow = pick_rdm_index()
  49.         sel_card_sql = "SELECT * FROM computer WHERE rowid = {} ".format(rndrow)
  50.         cardtgt = connex.execute(sel_card_sql)
  51.         cardpicked=cardtgt.fetchone()
  52.         #print(last_picked_card, cardpicked,"2") # tsting while loop works
  53.  
  54.     #print(cardpicked)
  55.     #print(cardpicked[0])
  56.     insert_picked(cardpicked[0]) # puts the card picked into the picked table
  57.     return cardpicked
  58.  
  59. # function to get the last 2 cards picked and the winning card into the results table, if a draw ignore
  60. def write_results(first,winner):
  61.     last_picked = connex.execute("SELECT * FROM picked ORDER BY time DESC")
  62.     last_card_picked = last_picked.fetchone()
  63.     next_last_card_picked = last_picked.fetchone()
  64.     print(first,winner)
  65.     if first == "1":
  66.         if winner == "W":
  67.             insert_last_card_sql ="INSERT INTO result (card1, card2, winner) VALUES ('{}','{}', '{}')".format(next_last_card_picked[0],last_card_picked[0], next_last_card_picked[0])
  68.             connex.execute(insert_last_card_sql)
  69.             connex.commit()
  70.         elif winner == "L":
  71.             insert_last_card_sql ="INSERT INTO result (card1, card2, winner) VALUES ('{}','{}', '{}')".format(next_last_card_picked[0],last_card_picked[0], last_card_picked[0])
  72.             connex.execute(insert_last_card_sql)
  73.             connex.commit()
  74.     elif first == "2":
  75.         if winner == "W":
  76.             insert_last_card_sql ="INSERT INTO result (card1, card2, winner) VALUES ('{}','{}', '{}')".format(next_last_card_picked[0],last_card_picked[0], next_last_card_picked[0])
  77.             connex.execute(insert_last_card_sql)
  78.             connex.commit()
  79.         elif winner == "L":
  80.             insert_last_card_sql ="INSERT INTO result (card1, card2, winner) VALUES ('{}','{}', '{}')".format(next_last_card_picked[0],last_card_picked[0], last_card_picked[0])
  81.             connex.execute(insert_last_card_sql)
  82.             connex.commit()
  83.  
  84. #  the game
  85.  
  86. player = input("Are you player (1) or player (2) >> ")
  87.  
  88. choosing_player = "1"
  89.  
  90. for round in range(3):
  91.     input("Presss enter to pick a card when both players are ready >> ")
  92.     card = pick_random_card()
  93.     card_text = "{}, cores = {}, speed={}Ghz, RAM = {}MB, cost ={}$".format(card[0],card[1],card[2],card[3],card[4])
  94.     print(card_text)
  95.     winner = input("Did you win? (Y)es, (N)o, (D)raw >> ").lower()
  96.     if winner == "y":
  97.         if player == "1":
  98.             write_results("1","W")
  99.             choosing_player = player
  100.         elif player =="2":
  101.             write_results("2", "W")
  102.             choosing_player = player  
  103.  
  104.     elif winner == "n":
  105.         if player == "1":
  106.             write_results("2", "L")
  107.             choosing_player = "2" if player =="1" else "1"
  108.     elif player =="2":
  109.             write_results("1","L")
  110.             choosing_player = "2" if player =="1" else "1"
  111.     elif winner == "d":
  112.         pass
  113.     print(f"Player {choosing_player} picks")
  114.  
  115. #print("interesting Stat", in)
  116. most_wins_computer_name_sql = "SELECT winner, COUNT(winner) qty FROM result  GROUP BY winner HAVING COUNT (winner)=( SELECT MAX(mycount) FROM ( SELECT winner, COUNT(winner) mycount FROM result GROUP BY winner));"
  117. intstat = connex.execute(most_wins_computer_name_sql)
  118. output=intstat.fetchone()
  119. print(f"Interesting Statistic the card that won the most was {output[0]} with {output[1]} items")
RAW Paste Data