Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Battle of cards Game
- # ST 2019
- # Programming 103: Saving and Structuring Data
- # A Raspberry Pi Foundation Course
- 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 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()
- def insert_result(name1, name2, winner):
- insert_sql = "INSERT INTO result(card1, card2, winner) VALUES ('{}', '{}', '{}')".format(name1, name2, winner)
- conn.execute(insert_sql)
- conn.commit()
- def read_last_picked():
- result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
- return result.fetchone()
- def read_last2_picked():
- result = conn.execute("SELECT * FROM picked ORDER BY time DESC")
- return result.fetchone(), result.fetchone()
- def print_card(datos):
- maxw = 35
- print("\n")
- print("+" + maxw*"-" + "+")
- print("|" + '{:^{maxw}}'.format(datos[0], maxw=maxw) + "|")
- print("|" + maxw*" " + "|")
- print("|" + '{:<18}'.format(" Cores") + '{:>10}'.format(datos[1]) + '{:<4}'.format(" ") + " |")
- print("|" + '{:<18}'.format(" CPU speed") + '{:>10}'.format(datos[2]) + '{:<4}'.format(" GHz") + " |")
- print("|" + '{:<18}'.format(" RAM") + '{:>10}'.format(datos[3]) + '{:<4}'.format(" MB") + " |")
- print("|" + '{:<18}'.format(" Cost") + '{:>10}'.format("$ " + str(datos[4])) + '{:<4}'.format(" ") + " |")
- print("|" + maxw*" " + "|")
- print("+" + maxw*"-" + "+")
- def print_score(score_p1, score_p2):
- if player == "1":
- yourscore = score_p1
- opponentscore = score_p2
- elif player == "2":
- yourscore = score_p2
- opponentscore = score_p1
- print("\n<Your score: {}> \t\t <Opponent's score: {}>\n".format(yourscore, opponentscore))
- return yourscore, opponentscore
- def statistics(name):
- data = conn.execute("SELECT * FROM result ORDER BY winner")
- table = data.fetchall()
- num_wins = 0
- num_duels = 0
- for computer in table:
- if computer[0] == name or computer[1] == name:
- num_duels = num_duels + 1
- if computer[2] == name:
- num_wins = num_wins + 1
- print("\nThe card ยซ" + name + "ยป :")
- if num_duels == 0:
- print("Sorry... statistics about this card are not available")
- return
- else:
- percentage_success = round(num_wins / num_duels * 100, 2)
- print("Has come victorious in " + str(num_wins) + " out of " + str(num_duels) + " duels")
- print("Percentage of success: " + str(percentage_success) + " %")
- print(40 * "-")
- print("\n*** WELCOME TO BATTLE OF CARDS ***\n")
- player = ''
- while player == '' or not(player in ['1', '2']):
- player = input("Are you player (1) or (2) >")
- choosing_player = "1"
- score_p1 = 0
- score_p2 = 0
- winner = "d"
- num_rounds = 5
- while winner == "d" and score_p1 + score_p2 < num_rounds:
- print_score(score_p1, score_p2)
- input("Press enter to pick a card when both players are ready >")
- card = pick_card()
- print_card(card)
- print("Player " + choosing_player + " picks.")
- winner = ''
- while winner == '' or not(winner in ['y', 'n', 'd']):
- winner = input("Did you win? (Y)es, (N)o, (D)raw >").lower()
- card1, card2 = read_last2_picked()
- if card[0] == card1[0]:
- name1 = card[0]
- name2 = card2[0]
- elif card[0] == card2[0]:
- name1 = card[0]
- name2 = card1[0]
- if winner == "y":
- choosing_player = player
- if player == "1":
- score_p1 = score_p1 + 1
- insert_result(name1, name2, name1)
- elif player == "2":
- score_p2 = score_p2 + 1
- elif winner == "n":
- if player == "1":
- score_p2 = score_p2 + 1
- insert_result(name1, name2, name2)
- elif player == "2":
- score_p1 = score_p1 + 1
- choosing_player = "2" if player == "1" else "1"
- if winner != "d":
- statistics(card1[0])
- statistics(card2[0])
- winner = "d"
- yourscore, opponentscore = print_score(score_p1, score_p2)
- if yourscore > opponentscore:
- print("CONGRATULATIONS! You won!\n")
- else:
- print("You lost... Better luck next time!\n")
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement