Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint #Import randint that belong to random numbers
- import pprint #To print dictionary better
- #Creating variables
- players = ("player" , "computer") #stores all player names
- active_player = randint(0, 1) #number for the player in turn
- #Dictionary that stores all dice in the game
- game = {"player" : {"active" :[], "saved" : []}, "computer" : {"active" : [], "saved" : []}}
- def player_score(d):
- """
- a funktion that calculates the result for a player
- :param d: Dictionary with two lists, active and saved
- :return: The sum of the dice's value in both lists
- """
- def change_active_player(save=False):
- global active_player # Telling that we want to use active_player inside the function
- if save:
- # .extend allows us to add a list to another
- game[players[active_player]]['saved'].extend(game[players[active_player]]['active'])
- # Empty the active list
- game[players[active_player]]['active'] = []
- # Change aktive player, can be written: active_player (active_player + 1) % 2
- if active_player == 1: #If the value for avtive_player is 1---
- active_player = 0 #...set it to 0
- else: #...otherwise
- active_player = 1 #...set it to 1
- print("Player changes")
- def standings():
- """
- Prints the stance
- :return: strict with the stand
- """
- s = "{} has hit {} (sum {} point)".format(players[active_player],
- game[players[active_player]]['active'], sum(game[players[active_player]]['active']))
- s += "\nTotal you have {} points and the computer has {} points".format(player_score(game['player']),
- player_score(game['computer']))
- return s
- #prints the welcome message
- input("""#######################################
- # Welcome to the dice game of 40 #
- #######################################
- Game of 40 is a dice game where you first have to reach at least 40 points.
- If you hit a six, you lose the points you have not saved and the tour goes
- over to the opponent. You can save your points at any time and let the luck
- go over to the opponent.
- The player who starts the game is: {}
- Press enter to start the game.""".format(players[active_player]))
- #Run n endless loop
- while True:
- #Rolls a dice
- dice = randint(1,6)
- #Prints the value of the dice
- print("\nDice value: {}".format(dice))
- #If dice value is 6
- if dice == 6:
- change_active_player()
- else:
- # Stores the dice in the active player list
- game[players[active_player]]["active"].append(dice)
- if active_player == 1: #If the value for active_player is 1...
- active_player = 0 #...Set it to 0
- else: #...otherwise
- active_player = 1 #...set it to 1
- print("Player changed")
- # Prints the stand
- print(standings())
- # Prints whose turn it is to play.
- print("Player to hit: {}".format(players[active_player]))
- # Logic for the different players
- if players[active_player] == 'computer': #Computer playes
- #If the computer has more than two dice in its active-list...
- if len(game[players[active_player]]['active']) > 2:
- change_active_player(True) #...then these must be saved and the turn goes to the player
- else:
- input("Press to display the computers next dice")
- else: # Player playes
- input("Press enter to hit a dice again")
- else:
- while True:
- val = input("Choose [K] to throw th dice again or [S] to save your dices: ")
- if val.upper() == 'K' : #Hit the dice
- break
- elif val.upper() == 'S': #Save your dice
- change_active_player(True) #To save dice and leave the turn
- break
- else:
- print("Incorrect entry, try again")
- # Prints game
- #pprint.pprint(game)
- #Prints whose turn it is to play
- print("Player to hit: {}".format(players[active_player]))
- #Wait for input before running the loop again
- input("Press enter to hit a dice again.")
- #If someone has scored more than 39 then they have won...
- if player_score(game['player']) > 39 or player_score(game['computer']) > 39:
- break #...Break loop
- #prints game
- pprint.pprint(game)
- #Wait for input before we run the loop again
- input("Press the entrance button to roll a dice")
- #The game ends and winner is played
- #Check first who won, since no one can get at least 40, we know someone has won
- if player_score(game['player']) > player_score(game['computer']):
- print("Congratulations! you have beaten your computer with {} vs {} points.".format(player_score(game['player']),
- player_score(game['computer'])))
- else:
- print("You have lost to the computer with {} vs {} points.".format(player_score(game['computer']),
- player_score(game['player'])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement