Advertisement
johnpentyrch

pi103assignment

May 20th, 2020
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 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.     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_result(acard1,acard2,awin):
  26.     insert_sql = "INSERT INTO result(card1,card2,winner) VALUES ('{}', '{}', '{}')".format(acard1[0], acard2[0], awin[0])
  27.     conn.execute(insert_sql)
  28.     conn.commit()
  29.  
  30. def read_last_picked():
  31.     result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  32.     return result.fetchone()
  33. def show_card(acard):
  34.     print('''
  35.    |---------------------------------------|
  36.    |{}                |
  37.    |~~~~~~~~~~~~~~~~~~~~~~~~~~--------------|
  38.    |   Cores     {}                           |
  39.    |   CPU Speed {} Ghz                     |
  40.    |   Ram       {} Mb                    |
  41.    |   cost    $ {} bn                      |
  42.    |                                           |
  43.    +-------------------------------------------|
  44.    '''.format(acard[0], acard[1], acard[2], acard[3], acard[4]))
  45.  
  46.  
  47.    
  48. rounds=int(input('how many round do you want to play?'))
  49. p1_name=input('Enter the first players name ')
  50. p2_name=input('Enter the second players name ')
  51. last_winner=1 #set so player 1 goes first
  52.  
  53. for i in range(rounds):
  54.     if last_winner == 1:
  55.         fplayer_name=p1_name
  56.         splayer_name=p2_name
  57.     else :
  58.         fplayer_name=p2_name
  59.         splayer_name=p1_name
  60.     print('{} it is you go now '.format(fplayer_name))
  61.     card = pick_card()
  62.     print('\n',fplayer_name,' your card ')
  63.     show_card(card)
  64.     try:
  65.         field_picked=int(input('Enter the number of the parameter you want to  play! \n\t(1) #Cores, \n\t(2) CPU Speed, \n\t(3) Ram, \n\t(4) Cost \n\t>'))
  66.     except:
  67.         field_picked=0
  68.     while field_picked < 1 or field_picked > 5:
  69.         try:
  70.             field_picked=int(input('Choose the parameter you want to  play! \n\t(1) #Cores, \n\t(2) CPU Speed, \n\t(3) Ram, \n\t(4) Cost \n\t>'))
  71.         except:
  72.             field_picked=0  
  73.     card2 = pick_card()
  74.     print('\n',splayer_name, ' your card')
  75.     show_card(card2)
  76.     if card[field_picked] > card2[field_picked] :
  77.         print(fplayer_name,' You win ')
  78.         last_winner=1 if last_winner==1 else 0
  79.         insert_result(card,card2,card)
  80.     elif card[field_picked] < card2[field_picked] :
  81.         print(fplayer_name,' You lose ')
  82.         last_winner=0 if last_winner==1 else 1
  83.         insert_result(card,card2,card2)
  84.     else :
  85.         print('Draw - Nobody wins')
  86.     input('Press enter to continue\n-------------------\n')
  87. ##Now produce some statistics
  88. ##We select and group the cards in the 'winner' field of the result table
  89. print('\n\n\nAnalysis of Winning Cards')
  90. print('CARD \t\t\t\tWINS')
  91. result = conn.execute("SELECT winner, COUNT(winner)  AS CN FROM result GROUP BY winner ORDER BY CN DESC")
  92. results=result.fetchall()
  93. for res in results:
  94.     print(res[0],'   \tWon ',res[1], ' times')
  95. print('There were ',len(results), ' Winning cards')
  96. result = conn.execute("SELECT COUNT(winner) FROM result")
  97. results=result.fetchone()
  98.  
  99. print('Out of ',results[0], 'wins')
  100. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement