Advertisement
TonyMo

3_11_Log_count_wins.py

Apr 28th, 2021
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.60 KB | None | 0 0
  1. # 3 11 LoggingWins.py
  2.  
  3. """
  4. You now have all the functions you need to interact with the database to make the game.
  5. What is missing is the logic which controls the flow and applies the rules of the game.
  6.  
  7. To recap, the rules of the game are:
  8.  
  9.    A random card is drawn for each player.
  10.    Player 1 picks a statistic, and the values are compared.
  11.      Whoever has the higher value wins that round.
  12.    Whoever wins the round picks the statistic in the next round.
  13.    If the round is tied, both players draw a new card
  14.      and the most recent winner still picks the statistic.
  15.  
  16. Each player will be running the game separately,
  17. so the first thing it should do is
  18. establish who is player 1 and who is player 2.
  19. """
  20. """ 3 11
  21. Your assignment is to:
  22.    Modify the game to Update the Result Table with
  23.    1. the Two Cards which were played against each other
  24.    2. and which was the Winner
  25.    Use the data you have stored to provide some interesting statistics
  26.    (e.g. how many times the winning card has been used) after each round
  27.    Document your changes, either as comments in your code or as a separate document
  28. """
  29.  
  30. import sqlite3
  31. from random import randint
  32. from time import time
  33.  
  34. conn = sqlite3.connect("computer_cards.db")
  35.  
  36.  
  37. def read_all_cards():
  38.     result = conn.execute("SELECT * FROM computer")
  39.     return result.fetchall()
  40.  
  41.  
  42. def pick_card():
  43.     cards = read_all_cards()
  44.     last_picked_card = read_last_picked()
  45.     random_card = cards[randint(0, len(cards) - 1)]
  46.     while random_card[0] == last_picked_card[0]:
  47.         random_card = cards[randint(0, len(cards) - 1)]
  48.     insert_picked(random_card[0])
  49.     return random_card
  50.  
  51.  
  52. def insert_picked(name):
  53.     insert_sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name, time())
  54.     conn.execute(insert_sql)
  55.     conn.commit()
  56.  
  57.  
  58. def read_last_picked():
  59.     result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
  60.     return result.fetchone()
  61.  
  62.  
  63. def winning_result(card1, card2, winning_card):
  64.     insert_sql = ("INSERT INTO result(card1, card2, winning_card) VALUES ('{}', '{}', '{}')"
  65.                   .format(card1, card2, winning_card))
  66.  
  67.     conn.execute(insert_sql)
  68.  
  69.     conn.commit()
  70.    
  71.    
  72. """def count_winning_card():
  73.    result = conn.execute("SELECT * FROM result GROUP BY winning_card; winning_card count(*)")
  74.    return result.fetchall()"""
  75.  
  76.  
  77. # ___________________ end of setup
  78.  
  79. #  Main game  Basically from L T's script
  80. player1 = input("Player 1 please enter your name > ")
  81. player2 = input("Player 2 please enter your name > ")
  82. choosing_player = "1"
  83. other_player = "2"
  84. player1_score = 0
  85. player2_score = 0
  86.  
  87. for round in range(2):  # Default is 5
  88.  
  89.     input("Press enter to pick a card when both players are ready > ")
  90.  
  91.     #  Select cards and allow choosing player to select the category
  92.     if choosing_player == "1":
  93.         card1 = pick_card()
  94.         card1_text = ("{}, cores={}, speed={}GHz, RAM={}MB, cost={}$"
  95.                       .format(card1[0], card1[1], card1[2], card1[3], card1[4]))
  96.         print("Player 1 draws card:", card1_text)
  97.  
  98.         print("Player 1 picks category.")
  99.         category = input('Enter the category: (C)ores, (S)peed, (R)am or (P)rice > ')              
  100.  
  101.         card2 = pick_card()
  102.         card2_text = ("{}, cores={}, speed={}GHz, RAM={}MB, cost={}$"
  103.                       .format(card2[0], card2[1], card2[2], card2[3], card2[4]))
  104.         print("Player 2 card:", card2_text)
  105.  
  106.     else:
  107.         card2 = pick_card()
  108.         card2_text = ("{}, cores={}, speed={}GHz, RAM={}MB, cost={}$"
  109.                       .format(card2[0], card2[1], card2[2], card2[3], card2[4]))
  110.         print("Player 2 card:", card2_text)
  111.  
  112.         print("Player 2 pick a category.")
  113.         category = input('Enter the category: (C)ores, (S)peed, (R)am or (P)rice > ')              
  114.  
  115.         card1 = pick_card()
  116.         card1_text = ("{}, cores={}, speed={}GHz, RAM={}MB, cost={}$"
  117.                       .format(card1[0], card1[1], card1[2], card1[3], card1[4]))
  118.         print("Player 1 card:", card1_text)
  119.  
  120.     #  Check for the winner
  121.     compare = {"C": 1, "S": 2, "R": 3, "P": 4}
  122.     index = compare[category]
  123.     if card1[index] > card2[index]:
  124.         print("Player 1 wins")
  125.         player1_score += 1
  126.         choosing_player = "1"
  127.         winner = card1[0]
  128.         winning_result(card1[0], card2[0], winner)
  129.     elif card2[index] > card1[index]:
  130.         print("Player 2 wins")
  131.         player2_score += 1
  132.         choosing_player = "2"
  133.         winner = card2[0]
  134.         winning_result(card1[0], card2[0], winner)
  135.     else:
  136.         print("It's a draw")
  137.  
  138.  
  139. #  Print final scores
  140. print(player1, "scored", player1_score)
  141. print(player2, "scored", player2_score)
  142.  
  143. #  Count the Winning  Cards
  144. # Count the number of times each winning card occurs.
  145. # ref: https://www.sqlitetutorial.net/sqlite-count-function/
  146. # ref: https://www.fosslinux.com/42798/basics-of-working-with-the-sqlite-databases-in-python.html
  147. cur = conn.cursor()
  148. cur.execute("SELECT winning_card, COUNT(*) "
  149.             "FROM result "
  150.             "GROUP BY winning_card "
  151.             "ORDER BY COUNT(*) DESC")
  152. table = cur.fetchall()
  153. print("Winning card,Count")
  154. for i in table:
  155.     print(i)
  156.  
  157. cur.close()
  158. conn.commit()
  159. conn.close()
  160.  
  161.  
  162. """>>>
  163. Player 2 wins
  164. A scored 0
  165. B scored 2
  166.  
  167. Winning card,Count
  168. ('Raspberry Pi 4 Model B 2GB', 6)
  169. ('Raspberry Pi 4 Model B 1GB', 5)
  170. ('Raspberry Pi 4 Model B 4GB', 4)
  171. ('Raspberry Pi 3 Model B+', 4)
  172. ('Raspberry Pi 3 Model B', 3)
  173. ('Raspberry Pi 3 Model A+', 2)
  174. ('Raspberry Pi 2 Model B', 2)
  175. ('Raspberry Pi Zero W', 1)
  176. ('Raspberry Pi 1 Model B 256MB', 1)
  177.  
  178. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement