Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Game on dbcards
- import sqlite3
- from random import randint
- from time import time
- conn = sqlite3.connect("d:/Pystrukturdata/databasenya/computer_cards.db")
- def read_all_cards():
- result = conn.execute("SELECT * FROM computer")
- return result.fetchall()
- def pick_card():
- cards = read_all_cards()
- last_picked_card = read_last_picked()
- random_card = cards[randint(0, len(cards) - 1)]
- while random_card[0] == last_picked_card[0]:
- random_card = cards[randint(0, len(cards) - 1)]
- insert_picked(random_card[0])
- return random_card
- def insert_picked(name):
- insert_sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name, time())
- conn.execute(insert_sql)
- conn.commit()
- # Function to write the the 'results' table containing card1, card2, winner
- def insert_card_played(card_p1, card_p2, the_winner):
- insert_sql = f"INSERT INTO result(card1, card2, winner) VALUES ('{card_p1}', '{card_p2}', '{the_winner}')"
- conn.execute(insert_sql)
- conn.commit()
- # Function reading the last data
- def read_last_picked():
- result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
- return result.fetchone()
- # Function reading many data
- def read_card_just_played():
- result = conn.execute("SELECT name FROM picked ORDER BY time DESC")
- return result.fetchmany(2)
- # Function update players
- def update_card_played(won, card):
- # gets last two card picked, check who has what, update who has won.
- cards_in_play = read_card_just_played()
- if cards_in_play[0][0] == card:
- if won:
- insert_card_played(card, cards_in_play[1][0], card)
- else:
- insert_card_played(card, cards_in_play[1][0], cards_in_play[1][0])
- else:
- if won:
- insert_card_played(card, cards_in_play[0][0], card)
- else:
- insert_card_played(card, cards_in_play[0][0], cards_in_play[0][0])
- # Function of start game player
- def playing():
- player = input("Are you player (1) or (2) >")
- choosing_player = "1"
- # List of start score each player
- score =[0,0]
- for round in range(5):
- input("Press enter to pick a card when both players are ready >")
- card = pick_card()
- card_text = "{}, cores={}, speed={}GHz, RAM={}MB, cost={}$".format(card[0], card[1], card[2], card[3], card[4])
- print("************************************************************************")
- print(card_text)
- print("************************************************************************")
- print("Player " + choosing_player + " picks.")
- winner = input("Did you win? (Y)es, (N)o, (D)raw >").lower()
- # Taking score for each players and update score
- if winner == "y":
- choosing_player = player
- if player =="1":
- score[0] += 1
- update_card_played("won", card[0])
- else:
- score[1] += 1
- elif winner == "n":
- choosing_player = "2" if player == "1" else "1"
- if player =="1":
- score[1] += 1
- update_card_played("lost", card[0])
- else:
- score[0] += 1
- elif winner == "d":
- pass
- # Display the score each player
- print("---RESULT SCORE------")
- print(f" Player 1 ===> {score[0]}")
- print(f" Player 2 ===> {score[1]}")
- print("---------------------")
- playing()
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement