Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Dice Game
- #TO:DO Sort highscore. Allow player to roll.
- # Import
- import random
- import csv
- import string
- from collections import OrderedDict
- import operator
- # Variables
- external = 'Namescore.csv'
- external2 = 'Highscore.csv'# faster if replacing exterior file name
- namescore =[]# # Scoreboard
- scoreboard = []
- roundcnt = 1 # Number of Rounds the players have played
- p1score = 0
- p2score = 0
- accept_char = string.digits + string.ascii_letters + " " + "'" # Accepted characters
- valid_name = False
- r1 = 0 # First Roll
- r2 = 0 # Second Roll
- turn_finished = False
- first_roll = False
- second_roll = False
- roll_count = 1
- # Functions
- def addtofile():# Writes to the external file.
- # Writes to highscore file.
- myfile = open(external, "a")
- myfile2 = open(external2, 'a')
- record = winner + '\n'
- record2 = winscore
- # record2 = winscore + "\n" # Newline character
- myfile.append(record)
- myfile2.append(record2)
- myfile.close()
- myfile2.close()
- def swap(array, index1, index2): # Function swaps numbers within the list.
- temp = array[index1]
- array[index1] = array[index2+1]
- array[index2+1] = temp
- return array
- def roll(score, player): # Player to roll themselves
- global turn_finished
- turn_finished = False
- while not turn_finished:
- global r1
- global r2
- global roll_count
- print("\n",player+"'s turn","\n")
- p_roll = input("Press any key to roll")
- roll = random.randint(1,6)
- print("Rolled",roll)
- rolling()
- a = rolling()
- if a == True:
- r1 = roll
- roll_count += 1
- elif a == True:
- r2 = roll
- roll_count -= 1
- score += roll
- bonus(player, score, r1 ,r2)
- return score
- turn_finished = True
- def rolling(): # Determines the first and second roll for roll function
- if roll_count == 1:
- first_roll = True
- second_roll = False
- return first_roll
- else:
- first_roll = False
- second_roll = True
- return second_roll
- def bonus(player, score,roll1,roll2): # Calculates bonuses
- if roll1 == roll2: # Extra Roll
- roll = random.randint(1,6)
- score+= roll
- print(player,"'s rolls were equal!",player," gets an extra roll!")
- if score % 2 == 0: # Adds 10 to Player's score if Player's score is an even number
- score += 10
- print("\n",player,"'s score is even! 10 points added!","\n")
- elif score % 2 != 0: # Deducts 10 from the Player's score if the Player's score is an odd number.
- score = score - 5
- print("\n",player,"'s score is odd. 5 points subtracted.","\n")
- # Main Program
- # Input Validation
- while True: # Player 1 Validation Tests
- p1_usernm = str(input("Player 1 Name: "))
- valid_name = True
- if len(p1_usernm) == 0: # Length Check
- valid_name = False
- elif len(p1_usernm) > 30: # Range Check
- valid_name = False
- for character in p1_usernm: # Character Check
- if character not in accept_char:
- valid_name = False
- if valid_name: # if valid_name is True
- break
- print("The Player's name is invalid. Please try again. ")
- while True: # Player 2 Validation Tests
- p2_usernm = str(input("Player 2 Name: "))
- valid_name = True
- if len(p2_usernm) == 0:
- valid_name = False
- elif len(p2_usernm) > 30:
- valid_name = False
- for character in p2_usernm:
- if character not in accept_char:
- valid_name = False
- if valid_name:
- break
- print("The Player's name is invalid. Please try again")
- # Rolling Dice
- while roundcnt <= 5: # Rolling Dice
- print("\n","ROUND",+roundcnt,"\n")
- roll(p1score, p1_usernm)
- score_add = roll(p1score, p1_usernm)
- p1score += score_add
- roll(p2score, p2_usernm)
- score_add = roll(p2score, p2_usernm)
- p2score += score_add
- roundcnt += 1
- # Win Conditions - Max score of 110 (60 from normal rolling, 50 from bonus)
- if p1score == p2score: # Draw Conditon
- roll1 = random.randint(1,6)
- p1score += roll1
- roll1 = random.randint(1,6)
- p2score += roll1
- if p1score > p2score: # Player 1 Victory
- print("Player 1 has Won! Congratulations! Player's 1 Score:", p1score,"Player 2's Score:",p2score)
- winner = str(p1_usernm) # Setting up for writing
- winscore = p1score
- else: # Player 2 Victory
- print("Player 2 has Won! Congratulations! Player 2's Score:", p2score,"Player's 1 Score:", p1score,)
- winner = str(p2_usernm)
- winscore = p2score
- # Writing to External file
- addtofile() # Writes to highscore_table
- print("Highscore added! Thanks for playing!")
- with open(external, newline = '') as f: # Appends data from namescore file to list.
- reader = csv.reader(f, delimiter = '/') # Stackoverflow code: https://bit.ly/2ARJSZS
- for row in reader:
- namescore.append(row)
- with open(external2, newline = '') as f: # Appends data from scoreboard file to list.
- reader = csv.reader(f, delimiter = '/') # Stackoverflow code: https://bit.ly/2ARJSZS
- for row in reader:
- scoreboard.append(row)
- 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
- namescore = [j for sub in namescore for j in sub] # Turns 2 Dimensiona list into a 1 Dimensional list
- numbers = [ int(x) for x in scoreboard ]
- results = (int, scoreboard)
- scoreboard = results
- # Create a zip object from two lists - Sauce: https://thispointer.com/python-how-to-convert-a-list-to-dictionary/
- zipbObj = zip(namescore, results)
- # Create a dictionary from zip object
- dictOfWords = dict(zipbObj)
- print(dictOfWords)
- #### highscore[i] = [int(n) for n in row.split(',')]
- #### i += 1
- ##
- ##for n in range(len(highscore)): # Cycles through the list
- ## for m in range(n):
- ## if highscore[m] >= highscore[m+1]: # If one index is greater or equal to another. Swap
- ## swap(highscore, m, m) # m refers to indexes in the list.
- ##
- ##for i in range(len(highscore)):
- ## for j in range(len(highscore[i])):
- ## print(highscore[i][j], end=' ')
- ## if highscore[i][j] == int:
- ## score_scoreboard.append(highscore)
- ##
- ##print("\n","Highscore Table:",sorted(highscore),"\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement