Advertisement
JAS_Software

BotQuest game

May 16th, 2021
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sqlite3
  2. from random import randint
  3. from time import time
  4. from time import sleep
  5. import sys
  6.  
  7.  
  8. def getConnection():
  9.     #create connection to the database
  10.     print('Creating connection to the Database')
  11.     conn = ''
  12.     try:
  13.         conn = sqlite3.connect('computer_cards.db')
  14.     except Exception as ex:
  15.         print('Failed to establish connection to database - {}'.format(ex))
  16.         conn = ''
  17.     return conn
  18.  
  19. def closeConnection(conn):
  20.     #close connection to database
  21.     print('Closing connection to the Database')
  22.     try:
  23.         conn.close()
  24.     except Exception as ex:
  25.         print('Error closing connection {}'.format(ex))
  26.  
  27. def clearTable(conn):
  28.     #clear all records from the picked table
  29.     try:
  30.         sql = "DELETE FROM picked"        
  31.         cursor = conn.execute(sql)
  32.         conn.commit()
  33.         print('Rows deleted - {}'.format(cursor.rowcount))
  34.     except Exception as ex:
  35.         print('Failed to clear table - {}'.format(ex))
  36.        
  37. def getCards(conn):
  38.     #create list of all card names from the computer table
  39.     try:
  40.         result = conn.execute("SELECT * FROM computer")
  41.         return result.fetchall()
  42.     except Exception as ex:
  43.         print('Failed to get table - {}'.format(ex))
  44.         return ''
  45.        
  46. def displayRecords(conn):
  47.     cursor = conn.execute('SELECT * FROM picked')
  48.     records = cursor.fetchall()
  49.     for record in records:
  50.         print(record)
  51.        
  52. def pickAnotherCard(conn):
  53.     response = input('Pick Another Card? ').strip().upper()
  54.     if response == 'D':
  55.         displayRecords(conn)
  56.         response = 'Y'
  57.     return (response == 'Y')
  58.  
  59. def pickCard():
  60.     response = input('Pick Another Card? ').strip().upper()
  61.     return (response == 'Y')
  62.  
  63. def numberOfRounds():
  64.     rounds = -1
  65.     while rounds < 0:
  66.         try:
  67.             rounds = int(input('Enter rounds to play (3 - 20): ').strip())
  68.             if rounds < 3 or rounds > 20:
  69.                 print('Invalid Input')
  70.                 rounds = -1
  71.         except:
  72.             print('Invalid Input')
  73.             rounds = -1
  74.     return rounds
  75.        
  76. def getPlayerNames():
  77.     name1 = input('Enter Player Name 1:').strip()
  78.     if name1 == '':
  79.         name1 = 'Player 1'
  80.  
  81.     name2 = input('Enter Player Name 2:').strip()
  82.     if name2 == '':
  83.         name2 = 'Player 2'
  84.     return name1,name2
  85.  
  86.  
  87. def addCard(conn,name):
  88.     #add card to the picked table
  89.     try:
  90.         sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name,time())        
  91.         cursor = conn.execute(sql)
  92.         conn.commit()
  93.         print('Added {}'.format(name))
  94.     except Exception as ex:
  95.         print('Failed to enter card {} - {}'.format(name, ex))
  96.  
  97. def getLastCard(conn):
  98.     try:
  99.         result = conn.execute("SELECT name FROM picked ORDER BY time DESC")
  100.         return result.fetchone()[0]
  101.     except Exception as ex:
  102.         #print('Error getting last card picked, table empty - {}'.format(ex))
  103.         return ''
  104.    
  105. def buildCardString(card):
  106.     details = ''
  107.     details += '----------' + '\n'
  108.     details += 'Name:' + '\t' + card[0] + '\n'
  109.     details += 'Cores:' + '\t' + str(card[1]) + '\n'
  110.     details += 'Speed:' + '\t' + str(card[2]) + '\n'
  111.     details += 'Ram:' + '\t' + str(card[3]) + '\n'
  112.     details += 'Cost:' + '\t' + str(card[4]) + '\n'
  113.     details += '----------' + '\n'
  114.     return details
  115.    
  116. def chooseStat():
  117.     response = 0
  118.     while response == 0:
  119.         try:
  120.             response = int(input('Choose Stat (1>Cores  2>Speed  3>Ram  4>Cost): '))
  121.             if response < 1 or response > 4:
  122.                 response = 0
  123.         except:
  124.             response = 0
  125.     return response
  126.  
  127. def roundResult(win1,win2,currentPlayer, stat1,stat2,name1,name2):
  128.     if(stat1 > stat2):
  129.         win1 += 1
  130.         currentPlayer = 1
  131.         print('*** PLAYER {} WON ***'.format(name1))
  132.     elif(stat2 > stat1):
  133.         win2 += 1
  134.         currentPlayer = 2
  135.         print('*** PLAYER {} WON ***'.format(name2))
  136.     else:
  137.         print('*** DRAW ***')
  138.     print('*** CURRENT SCORE {} v {} ***'.format(win1,win2) + '\n')
  139.     return win1,win2,currentPlayer
  140.  
  141. def finalResult(win1,win2,name1,name2):
  142.     if(win1 > win2):
  143.         print('*** PLAYER {} WON ***'.format(name1) + '\n')
  144.     elif(win2 > win1):
  145.         print('*** PLAYER {} WON ***'.format(name2) + '\n')
  146.     else:
  147.         print('*** GAME IS A DRAW ***' + '\n')
  148.        
  149.    
  150. def mainLoop(conn,rounds,name1,name2):
  151.     #pick random cards and add to picked table
  152.     cards = getCards(conn)
  153.     win1 = 0
  154.     win2 = 0
  155.     currentPlayer = 1
  156.     for round in range(rounds):
  157.         print('ROUND {}'.format(round + 1))
  158.         print('CURRENT PICKER - PLAYER {}'.format(name1))
  159.         card1 = cards[randint(0, len(cards) - 1)]
  160.         while card1[0] == getLastCard(conn):
  161.             card1 = cards[randint(0, len(cards) - 1)]
  162.        
  163.         card2 = cards[randint(0, len(cards) - 1)]
  164.         while card2[0] == card1[0] or card2[0] == getLastCard(conn):
  165.             card2 = cards[randint(0, len(cards) - 1)]
  166.  
  167.         if currentPlayer == 1:
  168.             print('CARD FOR {}'.format(name1))
  169.             print(buildCardString(card1))
  170.             stat = chooseStat()
  171.             print('CARD FOR {}'.format(name2))
  172.             print(buildCardString(card2)+'\n')
  173.             sleep(2)
  174.             print('Statistic for {} > {} - {}'.format(name1,stat,card1[stat]))
  175.             print('Statistic for {} > {} - {}'.format(name2,stat,card2[stat]))
  176.             sleep(2)
  177.         else:
  178.             print('CARD FOR {}'.format(name1))
  179.             print(buildCardString(card1))
  180.             sleep(2)
  181.             print('CARD FOR {}'.format(name2))
  182.             print(buildCardString(card2)+'\n')
  183.             print('{} choosing stat'.format(name2))
  184.             stat = randint(1, 4)
  185.             sleep(2)
  186.             print('Statistic for {} > {} - {}'.format(name1,stat,card1[stat]))
  187.             print('Statistic for {} > {} - {}'.format(name2,stat,card2[stat]))
  188.             sleep(2)
  189.         win1,win2,currentPlayer = roundResult(win1,win2,currentPlayer,card1[stat],card2[stat],name1,name2)
  190.         sleep(3)
  191.        
  192.     finalResult(win1,win2,name1,name2)
  193.  
  194. #Main Program
  195. conn = getConnection()
  196. if conn != '':
  197.     clearTable(conn)
  198.     rounds = numberOfRounds()
  199.     name1,name2 = getPlayerNames()
  200.     mainLoop(conn,rounds,name1,name2)
  201.     closeConnection(conn)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement