Advertisement
JAS_Software

Bot Quest with Stats

May 16th, 2021
87
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,table):
  28.     #clear all records from the picked table
  29.     try:
  30.         sql = "DELETE FROM {}".format(table)        
  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 getCardNames(conn):
  47.     try:
  48.         result = conn.execute("SELECT name FROM computer")
  49.         return result.fetchall()
  50.     except Exception as ex:
  51.         print('Failed to get table - {}'.format(ex))
  52.         return ''
  53.  
  54.    
  55. def displayRecords(conn):
  56.     cursor = conn.execute('SELECT * FROM picked')
  57.     records = cursor.fetchall()
  58.     for record in records:
  59.         print(record)
  60.        
  61. def pickAnotherCard(conn):
  62.     response = input('Pick Another Card? ').strip().upper()
  63.     if response == 'D':
  64.         displayRecords(conn)
  65.         response = 'Y'
  66.     return (response == 'Y')
  67.  
  68. def pickCard():
  69.     response = input('Pick Another Card? ').strip().upper()
  70.     return (response == 'Y')
  71.  
  72. def numberOfRounds():
  73.     rounds = -1
  74.     while rounds < 0:
  75.         try:
  76.             rounds = int(input('Enter rounds to play (3 - 25000): ').strip())
  77.             if rounds < 3 or rounds > 25000:
  78.                 print('Invalid Input')
  79.                 rounds = -1
  80.         except:
  81.             print('Invalid Input')
  82.             rounds = -1
  83.     return rounds
  84.        
  85. def getPlayerNames():
  86.     name1 = input('Enter Player Name 1:').strip()
  87.     if name1 == '':
  88.         name1 = 'Player 1'
  89.  
  90.     name2 = input('Enter Player Name 2:').strip()
  91.     if name2 == '':
  92.         name2 = 'Player 2'
  93.     return name1,name2
  94.  
  95.  
  96. def addCard(conn,name):
  97.     #add card to the picked table
  98.     try:
  99.         sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name,time())        
  100.         cursor = conn.execute(sql)
  101.         conn.commit()
  102.         print('Added {}'.format(name))
  103.     except Exception as ex:
  104.         print('Failed to enter card {} - {}'.format(name, ex))
  105.  
  106. def getLastCard(conn):
  107.     try:
  108.         result = conn.execute("SELECT name FROM picked ORDER BY time DESC")
  109.         return result.fetchone()[0]
  110.     except Exception as ex:
  111.         #print('Error getting last card picked, table empty - {}'.format(ex))
  112.         return ''
  113.    
  114. def getLastCards(conn):
  115.     try:
  116.         result = conn.execute("SELECT name FROM picked ORDER BY time DESC")
  117.         return result.fetchmany()
  118.     except Exception as ex:
  119.         #print('Error getting last card picked, table empty - {}'.format(ex))
  120.         return ''
  121.        
  122. def buildCardString(card):
  123.     details = ''
  124.     details += '----------' + '\n'
  125.     details += 'Name:' + '\t' + card[0] + '\n'
  126.     details += 'Cores:' + '\t' + str(card[1]) + '\n'
  127.     details += 'Speed:' + '\t' + str(card[2]) + '\n'
  128.     details += 'Ram:' + '\t' + str(card[3]) + '\n'
  129.     details += 'Cost:' + '\t' + str(card[4]) + '\n'
  130.     details += '----------' + '\n'
  131.     return details
  132.    
  133. def chooseStat():
  134.     response = 0
  135.     while response == 0:
  136.         try:
  137.             response = int(input('Choose Stat (1>Cores  2>Speed  3>Ram  4>Cost): '))
  138.             if response < 1 or response > 4:
  139.                 response = 0
  140.         except:
  141.             response = 0
  142.     return response
  143.  
  144. def addCard(conn,name):
  145.     #add card to the picked table
  146.     try:
  147.         sql = "INSERT INTO picked(name, time) VALUES ('{}', {})".format(name,time())        
  148.         cursor = conn.execute(sql)
  149.         conn.commit()
  150.         #print('Added {}'.format(name))
  151.     except Exception as ex:
  152.         print('Failed to enter card {} - {}'.format(name, ex))
  153.  
  154. def addResult(conn,card1,card2,winner):
  155.     try:
  156.         sql = "INSERT INTO result(card1, card2, winner) VALUES ('{}', '{}', '{}')".format(card1,card2,winner)
  157.         #print(sql)
  158.         cursor = conn.execute(sql)
  159.         conn.commit()
  160.         print('Added Result {}'.format(winner))
  161.     except Exception as ex:
  162.         print('Failed to enter result for {} v {}'.format(card1, card2))
  163.    
  164. def getRoundResult(stat1,stat2):
  165.     if(stat1 > stat2):
  166.         result = 1
  167.     elif(stat2 > stat1):
  168.         result = 2
  169.     else:
  170.         result = 0
  171.     return result
  172.  
  173. def roundResult(win1,win2,currentPlayer, stat1,stat2,name1,name2):
  174.     if(stat1 > stat2):
  175.         win1 += 1
  176.         currentPlayer = 1
  177.         print('*** PLAYER {} WON ***'.format(name1))
  178.     elif(stat2 > stat1):
  179.         win2 += 1
  180.         currentPlayer = 2
  181.         print('*** PLAYER {} WON ***'.format(name2))
  182.     else:
  183.         print('*** DRAW ***')
  184.     print('*** CURRENT SCORE {} v {} ***'.format(win1,win2) + '\n')
  185.     return win1,win2,currentPlayer
  186.  
  187. def finalResult(win1,win2,name1,name2):
  188.     if(win1 > win2):
  189.         print('*** PLAYER {} WON ***'.format(name1) + '\n')
  190.     elif(win2 > win1):
  191.         print('*** PLAYER {} WON ***'.format(name2) + '\n')
  192.     else:
  193.         print('*** GAME IS A DRAW ***' + '\n')
  194.        
  195. def displayRecords(conn):
  196.     cursor = conn.execute('SELECT * FROM result')
  197.     records = cursor.fetchall()
  198.     for record in records:
  199.         print(record)
  200.        
  201. def alreadyPicked(picked,lastCards):
  202.     if len(lastCards) > 0:
  203.         for card in lastCards:
  204.             if card == picked:
  205.                 return True
  206.     return False
  207.  
  208. def mainLoop(conn,rounds,name1,name2):
  209.     #pick random cards and add to picked table
  210.     cards = getCards(conn)
  211.     win1 = 0
  212.     win2 = 0
  213.     currentPlayer = 1
  214.     for round in range(rounds):
  215.         print('ROUND {}'.format(round + 1))
  216.         print('CURRENT PICKER - PLAYER {}'.format(name1))
  217.         card1 = cards[randint(0, len(cards) - 1)]
  218.         lastCards = getLastCards(conn)
  219.         while alreadyPicked(card1[0],lastCards): # == getLastCard(conn):
  220.             card1 = cards[randint(0, len(cards) - 1)]
  221.         addCard(conn,card1[0])
  222.    
  223.         card2 = cards[randint(0, len(cards) - 1)]
  224.         while card2[0] == card1[0] or alreadyPicked(card2[0],lastCards): # == getLastCard(conn):
  225.             card2 = cards[randint(0, len(cards) - 1)]
  226.         addCard(conn,card2[0])
  227.    
  228.         if currentPlayer == 1:
  229.             print('CARD FOR {}'.format(name1))
  230.             print(buildCardString(card1))
  231.             #stat = chooseStat()
  232.             stat = randint(1, 4)
  233.             print('CARD FOR {}'.format(name2))
  234.             print(buildCardString(card2)+'\n')
  235.             #sleep(2)
  236.             print('Statistic for {} > {} - {}'.format(name1,stat,card1[stat]))
  237.             print('Statistic for {} > {} - {}'.format(name2,stat,card2[stat]))
  238.             #sleep(2)
  239.         else:
  240.             print('CARD FOR {}'.format(name1))
  241.             print(buildCardString(card1))
  242.             #sleep(2)
  243.             print('CARD FOR {}'.format(name2))
  244.             print(buildCardString(card2)+'\n')
  245.             print('{} choosing stat'.format(name2))
  246.             stat = randint(1, 4)
  247.             #sleep(2)
  248.             print('Statistic for {} > {} - {}'.format(name1,stat,card1[stat]))
  249.             print('Statistic for {} > {} - {}'.format(name2,stat,card2[stat]))
  250.             #sleep(2)
  251.            
  252.         result = getRoundResult(card1[stat],card2[stat])
  253.         if result == 1:
  254.             currentPlayer = 1
  255.             win1 += 1
  256.             print('*** PLAYER {} WON ***'.format(name1))
  257.             addResult(conn,card1[0],card2[0],card1[0])
  258.         elif result == 2:
  259.             currentPlayer = 2
  260.             win2 += 1
  261.             print('*** PLAYER {} WON ***'.format(name2))
  262.             addResult(conn,card1[0],card2[0],card2[0])
  263.         else:
  264.             print('*** DRAW ***')
  265.         print('*** CURRENT SCORE {} v {} ***'.format(win1,win2) + '\n')
  266.         #sleep(3)
  267.        
  268.     finalResult(win1,win2,name1,name2)
  269.  
  270. def getUse(conn,card):
  271.     try:
  272.         sql = "SELECT * FROM result WHERE card1 = '{}' OR card2 = '{}'".format(card,card)
  273.         cursor = conn.execute(sql)
  274.         records = cursor.fetchall()
  275.         return len(records)
  276.     except Exception as ex:
  277.         return 0
  278.  
  279. def getWins(conn,card):
  280.     try:
  281.         sql = "SELECT * FROM result WHERE winner = '{}'".format(card)
  282.         cursor = conn.execute(sql)
  283.         records = cursor.fetchall()
  284.         return len(records)
  285.     except Exception as ex:
  286.         return 0
  287.    
  288. def reportStatistics(conn):
  289.     cards = getCardNames(conn)    
  290.     for card in cards:
  291.         uses = getUse(conn,card[0])
  292.         wins = getWins(conn,card[0])
  293.         if uses > 0:
  294.             rate = "{:.2%}".format(wins/uses)
  295.         else:
  296.             rate = '0'
  297.         print("{}:\t  uses = {}\t  wins = {}\t win rate = {}".format(card[0],uses,wins,rate))
  298.        
  299.    
  300. #Main Program
  301. conn = getConnection()
  302. if conn != '':
  303.     #displayRecords(conn)
  304.     #reportStatistics(conn)
  305.     clearTable(conn,'picked')
  306.     #clearTable(conn,'result')
  307.     rounds = numberOfRounds()
  308.     name1,name2 = getPlayerNames()
  309.     mainLoop(conn,rounds,name1,name2)
  310.     reportStatistics(conn)
  311.     closeConnection(conn)
  312.  
  313.  
  314.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement