Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- from random import randint
- from time import time
- conn = sqlite3.connect("computer_cards.db")
- def read_all_cards():
- result = conn.execute("SELECT * FROM computer")
- return result.fetchall()
- def insert_picked(name):
- insert_sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name, time())
- conn.execute(insert_sql)
- conn.commit()
- def insert_round_cards(card1, card2, winner):
- insert_round_sql = "INSERT INTO result(card1, card2, winner) VALUES ('{}', '{}', '{}')".format(card1, card2, winner)
- conn.execute(insert_round_sql)
- conn.commit()
- def read_last_picked():
- result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
- return result.fetchone()
- def read_last_round():
- round_results = conn.execute("SELECT * FROM picked ORDER BY time DESC")
- return round_results.fetchmany(2)
- def pick_card():
- cards = read_all_cards()
- last_picked_card = read_last_picked()
- last_round_card = read_last_round()
- random_card = cards[randint(0, len(cards) - 1)]
- random_card2 = cards[randint(0, len(cards) - 1)]
- while random_card[0] == last_picked_card[0]:
- random_card = cards[randint(0, len(cards) - 1)]
- while random_card2[0] == last_picked_card[0]:
- random_card2 = cards[randint(0, len(cards) - 1)]
- insert_picked(random_card[0])
- return random_card, random_card2
- def count_winners():
- count_sql = conn.execute("SELECT winner, COUNT(*) FROM result GROUP BY winner")
- return count_sql.fetchall()
- player = input("Are you player (1) or (2) >")
- choosing_player = "1"
- input("Press enter to pick a card when both players are ready >")
- card = pick_card()
- print(card)
- card_text = "{}, cores={}, speed={}GHz, RAM={}MB, cost={}£".format(card[0][0], card[0][1], card[0][2], card[0][3], card[0][4])
- print(card_text)
- print("Player " + choosing_player + " picks.")
- winner = input("Did you win? (Y)es, (N)o, (D)raw >").lower()
- if winner == "y":
- choosing_player = player
- insert_round_cards(card[0][0],card[1][0],card[0][0])
- elif winner == "n":
- choosing_player = "2" if player == "1" else "1"
- insert_round_cards(card[0][0],card[1][0],card[1][0])
- elif winner == "d":
- choosing_player = player
- insert_round_cards(card[0][0],card[1][0],"draw")
- winner_table = count_winners()
- print(winner_table)
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement