Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.29 KB | None | 0 0
  1. # Dice Game
  2.  
  3.  
  4. #TO:DO Sort highscore. Allow player to roll.
  5.  
  6. # Import
  7.  
  8. import random
  9. import csv
  10. import string
  11. from collections import OrderedDict
  12. import operator
  13.  
  14. # Variables
  15.  
  16. external = 'Namescore.csv'
  17. external2 = 'Highscore.csv'# faster if replacing exterior file name
  18. namescore =[]# # Scoreboard
  19. scoreboard = []
  20. roundcnt = 1 # Number of Rounds the players have played
  21. p1score = 0
  22. p2score = 0
  23. accept_char = string.digits + string.ascii_letters + " " + "'" # Accepted characters
  24. valid_name = False
  25. r1 = 0 # First Roll
  26. r2 = 0 # Second Roll
  27. turn_finished = False
  28. first_roll = False
  29. second_roll = False
  30. roll_count = 1
  31.  
  32. # Functions
  33.  
  34. def addtofile():# Writes to the external file.
  35.     # Writes to highscore file.
  36.     myfile = open(external, "a")
  37.     myfile2 = open(external2, 'a')
  38.     record = winner + '\n'
  39.     record2 = winscore
  40.     # record2 = winscore + "\n" # Newline character
  41.     myfile.append(record)
  42.     myfile2.append(record2)
  43.     myfile.close()
  44.     myfile2.close()
  45.  
  46. def swap(array, index1, index2): # Function swaps numbers within the list.
  47.     temp = array[index1]
  48.     array[index1] = array[index2+1]
  49.     array[index2+1] = temp
  50.     return array
  51.  
  52. def roll(score, player): # Player to roll themselves
  53.     global turn_finished
  54.     turn_finished = False
  55.     while not turn_finished:
  56.         global r1
  57.         global r2
  58.         global roll_count
  59.         print("\n",player+"'s turn","\n")
  60.         p_roll = input("Press any key to roll")
  61.         roll = random.randint(1,6)
  62.         print("Rolled",roll)
  63.         rolling()
  64.         a = rolling()
  65.         if a == True:
  66.             r1 = roll
  67.             roll_count += 1
  68.         elif a == True:
  69.             r2 = roll
  70.             roll_count -= 1
  71.         score += roll
  72.         bonus(player, score, r1 ,r2)
  73.         return score
  74.         turn_finished = True
  75.  
  76. def rolling(): # Determines the first and second roll for roll function
  77.     if roll_count == 1:
  78.         first_roll = True
  79.         second_roll = False
  80.         return first_roll
  81.     else:
  82.         first_roll = False
  83.         second_roll = True
  84.         return second_roll
  85.  
  86.  
  87. def bonus(player, score,roll1,roll2): # Calculates bonuses
  88.     if roll1 == roll2: # Extra Roll
  89.         roll = random.randint(1,6)
  90.         score+= roll
  91.         print(player,"'s rolls were equal!",player," gets an extra roll!")
  92.     if score % 2 == 0: # Adds 10 to Player's score if Player's score is an even number
  93.         score += 10
  94.         print("\n",player,"'s score is even! 10 points added!","\n")
  95.     elif score % 2 != 0: # Deducts 10 from the Player's score if the Player's score is an odd number.
  96.         score = score - 5
  97.         print("\n",player,"'s score is odd. 5 points subtracted.","\n")
  98.  
  99.  
  100. # Main Program
  101.  
  102. # Input Validation
  103.  
  104. while True: # Player 1 Validation Tests
  105.   p1_usernm = str(input("Player 1 Name: "))
  106.   valid_name = True
  107.   if len(p1_usernm) == 0: # Length Check
  108.     valid_name = False
  109.   elif len(p1_usernm) > 30: # Range Check
  110.     valid_name = False
  111.   for character in p1_usernm: # Character Check
  112.     if character not in accept_char:
  113.         valid_name = False
  114.   if valid_name: # if valid_name is True
  115.     break
  116.   print("The Player's name is invalid. Please try again. ")
  117.  
  118.  
  119. while True: # Player 2 Validation Tests
  120.   p2_usernm = str(input("Player 2 Name: "))
  121.   valid_name = True
  122.   if len(p2_usernm) == 0:
  123.     valid_name = False
  124.   elif len(p2_usernm) > 30:
  125.     valid_name = False
  126.   for character in p2_usernm:
  127.     if character not in accept_char:
  128.         valid_name = False
  129.   if valid_name:
  130.     break
  131.   print("The Player's name is invalid. Please try again")
  132.  
  133. # Rolling Dice
  134.  
  135. while roundcnt <= 5: # Rolling Dice
  136.     print("\n","ROUND",+roundcnt,"\n")
  137.     roll(p1score, p1_usernm)
  138.     score_add = roll(p1score, p1_usernm)
  139.     p1score += score_add
  140.     roll(p2score, p2_usernm)
  141.     score_add = roll(p2score, p2_usernm)
  142.     p2score += score_add
  143.     roundcnt += 1
  144.  
  145. # Win Conditions - Max score of 110 (60 from normal rolling, 50 from bonus)
  146.  
  147. if p1score == p2score: # Draw Conditon
  148.     roll1 = random.randint(1,6)
  149.     p1score += roll1
  150.     roll1 = random.randint(1,6)
  151.     p2score += roll1
  152. if p1score > p2score: # Player 1 Victory
  153.     print("Player 1 has Won! Congratulations! Player's 1 Score:", p1score,"Player 2's Score:",p2score)
  154.     winner = str(p1_usernm) # Setting up for writing
  155.     winscore = p1score
  156. else: # Player 2 Victory
  157.     print("Player 2 has Won! Congratulations! Player 2's Score:", p2score,"Player's 1 Score:", p1score,)
  158.     winner = str(p2_usernm)
  159.     winscore = p2score
  160.  
  161. # Writing to External file
  162.  
  163. addtofile() # Writes to highscore_table
  164. print("Highscore added! Thanks for playing!")
  165.  
  166. with open(external, newline = '') as f: # Appends data from namescore file to list.
  167.     reader = csv.reader(f, delimiter = '/') # Stackoverflow code: https://bit.ly/2ARJSZS
  168.     for row in reader:
  169.         namescore.append(row)
  170.  
  171. with open(external2, newline = '') as f: # Appends data from scoreboard file to list.
  172.     reader = csv.reader(f, delimiter = '/') # Stackoverflow code: https://bit.ly/2ARJSZS
  173.     for row in reader:
  174.         scoreboard.append(row)
  175.  
  176.  
  177. scoreboard = [j for sub in scoreboard for j in sub] # Sauce: https://stackoverflow.com/questions/29244286/how-to-flatten-a-2d-list-to-1d-without-using-numpy
  178. namescore = [j for sub in namescore for j in sub] # Turns 2 Dimensiona list into a 1 Dimensional list
  179. numbers = [ int(x) for x in scoreboard ]
  180. results = (int, scoreboard)
  181. scoreboard = results
  182.  
  183.  
  184. # Create a zip object from two lists - Sauce: https://thispointer.com/python-how-to-convert-a-list-to-dictionary/
  185. zipbObj = zip(namescore, results)
  186. # Create a dictionary from zip object
  187. dictOfWords = dict(zipbObj)
  188.  
  189. print(dictOfWords)
  190.  
  191. ####        highscore[i] = [int(n) for n in row.split(',')]
  192. ####        i += 1
  193. ##
  194. ##for n in range(len(highscore)): # Cycles through the list
  195. ##    for m in range(n):
  196. ##        if highscore[m] >= highscore[m+1]: # If one index is greater or equal to another. Swap
  197. ##            swap(highscore, m, m) # m refers to indexes in the list.
  198. ##
  199. ##for i in range(len(highscore)):
  200. ##    for j in range(len(highscore[i])):
  201. ##        print(highscore[i][j], end=' ')
  202. ##        if highscore[i][j] == int:
  203. ##            score_scoreboard.append(highscore)
  204. ##
  205. ##print("\n","Highscore Table:",sorted(highscore),"\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement