Assignment 1

Program Instructions:

You are to implement (code in Python) a computer game as outlined in the following description. Use simple functions, selection, repetition and lists.

NB: The following functionality items must be included in your program:

  • Game loop so that the player can play more than one round
  • Betting system – amounts in multiples of 5, balance of $0 forces quit
  • Error handling for incorrect user input
  • Appropriate comments (each function should have a comment describing its purpose)
  • Display balance history on exit

Program Features:

Short description:

You are required to prepare a plan for a game of Ultimate Ninja Battle Combat!!! (The exclamation points are important). The player will be fighting against the computer, and the winner gets bragging rights.You will create a plan for a game played between the player and the computer.

The player will start the game with a balance of $100. The player will choose a move, and so will the computer, and the winner of that round will be announced followed by the updated balance (depending on whether the player won or lost and how much he or she bet). The player should not be able to bet more than they have in their balance.

The player will choose their move from a list of six possibilities, and the computer will randomly choose a move as well. Once the player has chosen their move the computers move will be revealed, and the winner declared. The possible moves are:

  1. Punch of Fury
  2. Kick of Punishment
  3. Sword of Justice
  4. Shuriken of Vengeance
  5. Nunchucks of Anger
  6. Knife of Freedom

The chart below shows how each move performs against the others.

Computer’s Move
Punch of Fury Kick of Punishment Sword of Justice Shuriken of Vengeance Nunchucks of Anger Knife of Freedom
Player’s Move Punch of Fury Tie
No Winner
Player Loses Player Loses Player Wins Player Wins Player Loses
Kick of Punishment Player Wins Tie
No Winner
Player Loses Player Loses Player Wins Player Loses
Sword of Justice Player Wins Player Wins Tie
No Winner
Player Loses Player Loses Player Wins
Shuriken of Vengeance Player Loses Player Wins Player Wins Tie
No Winner
Player Loses Player Wins
Nunchucks of Anger Player Loses Player Loses Player Wins Player Wins Tie
No Winner
Player Loses
  Knife of Freedom Player Wins Player Wins Player Loses Player Loses Player Wins Tie
No Winner

 

Once the player chooses their move your game will check the computers move and see who wins.

You are welcome to choose other names for the attacks as long as they make sense. For instance, you may have “Knife of Assessment” rather than “Knife of Freedom”.

At the end of the round the winner is declared and the updated current balance is displayed.

The menu will then be presented again, allowing the player to get Information, Play or Quit. The player will be forced to quit if the player balance reaches $0.

When the game quits, the game should display “Goodbye [player name]. Your final balance is $ [player’s final balance]. The game will then display the player’s balance history (their balance at the end of each round).

Detailed Instructions:

  1. Ask the player’s name and welcome them to the game using their name. The player’s beginning balance of $100 should be displayed.
  2. Show game menu(I)nstructions
    (P)lay game
    (Q)uit game

    ALL other choices will produce an error. The menu will be displayed after every menu choice, except (Q).

    If the player chooses ‘I’ display the following instructions:

Welcome to Ultimate Ninja Battle Combat!!! You will be fighting against the computer, and the winner gets bragging rights. For each turn you will be asked to use one of the 6 attacks below:

(1) Punch of Fury

(2) Kick of Punishment

(3) Sword of Justice

(4) Shuriken of Vengeance

(5) Nunchucks of Anger

(6) Knife of Freedom.

Choose wisely.

If the player chooses ‘P’ from the menu display the player’s total and then ask the player how much they want to bet. Inform the player that the bet amount must be in multiples of 5. The player should not be able to bet more than their current balance.

  1. Next, ask the player to choose an attack.(1) Punch of Fury
    (2) Kick of Punishment
    (3) Sword of Justice
    (4) Shuriken of Vengeance
    (5) Nunchucks of Anger
    (6) Knife of Freedom.
  1. Once the player has entered a number the game should display the message “You chose: ” and their move. Make sure you use the name of the move not just the number they entered. If they entered anything other than a number between 1 and 6 then display an error message and ask the user for their move until they enter a valid value.
  2. Display the message “The computer chose: ” and the computer’s move.
  3. Display the result of the combat, showing that either the computer or the player won.
  4. Display the game menu again. The game menu will keep being displayed until the player chooses quit.
  5. If the player chooses “P” but has a $0 balance, the game will quit.
  1. If the player chooses ‘Q’ from the menu then display the quit message “Goodbye! [player name]. Your final balance is [player balance]”.
  1. On a new line display:“Your balance history is:
    Starting Balance: $100

After round 1: $55
After round 2: $75

After every menu option other than ‘Q’ the program will return to the menu.

  1. If the user enters anything other than the options above then display an error message.

Implementation:

Python file:
You are to divide your solution into functions, following the principles shown in class. These may involve one for each menu option (other than Quit) as well as functions for parts of the program (e.g. calculating the current player balance could be implemented as a function).

General Principles:

You will be focusing on basic planning and implementation using selections, repetition, and functions. Lists may be used where they are feasible.

Use the techniques and patterns that you have learned and seen demonstrated in class.

  • Coding standards:
  • Variables:
    • Wherever possible, variables should be set at the top of the function
    • Although Python does not use constants you may set variables to values that will remain the same for the life of the program. These should be named in all caps
      • You should be able to modify these variables to adjust the various values of items such as WIN, LOSE, STARTING_BALANCE, etc. This is an important aspect of this assignment, so consider carefully how to use constants. Remember to use these variables everywhere you can.
      • You will need to decide the best position to place these variables.
    • You should AVOID the use of global variables unless they are needed in multiple places in the application
    • In other words, you should pass the values to any functions that need them
      • And any constants only needed in one function should be ONLY in that function
  • Identifier Names
    • Variables should be named appropriately using either camelCase or under_scores
    • No variables should be a single character such as “x” or “y” unless they are used in a loop as an index. Variable names should be meaningful, and describe their use and/or purpose
    • Any list variable should be named with plurals to show multiple data is held (e.g. prices versus price)
    • Function names should begin with a verb (they are active), for example getValue() is better than value(). Good verbs include – ‘get’, ‘display’, ‘calculate’, ‘check’ etc (not all these are good for this problem…)
  • Comments:
    • You will need to add at LEAST one comment for each function that you define including main
    • The comment should either be placed directly above or below the function header
    • The comment should describe: (a) the purpose of the function, (b) any inputs and (c) any returned values
    • Additional comments should be placed at any position where the functionality of the program may be unclear
    • All code MUSTbe properly indented
  • Error handling:
    • Note that menu choice and other character selection should handle upper and lower case letters.
      • Look into the upper() or lower() functions in Python
    • You also must make sure that any functions that get input from the user do error checking to ensure that the input is within the expected range (greater than zero, etc).
  • Output
    • Make your output display as close as possible to the given examples, especially formatting and layout. Use Google to find out how to display tabs and other special characters using Python 3. 

Solution 

michael.py

import random

#this is the function that will be called if user gives ‘i’ as input. This function gives all the instructions needed.

def instructions():

print (“Welcome to Ultimate Ninja Battle Combat!!! You will be fighting against the computer, and the winner gets bragging rights. For each turn you will be asked to use one of the 6 attacks below: “)

print (‘(1) Punch of Fury’)

print (‘(2) Kick of Punishment’)

print (‘(3) Sword of Justice’)

print (‘(4) Shuriken of Vengeance’)

print (‘(5) Nunchucks of Anger’)

print (‘(6) Knife of Freedom’)

print (‘Choose wisely.’)

print (‘Your current balance is ${}’.format(current_balance))

#This is the fucntion that will be called if user chooses to play a game, takes global variable current balance as input and prints balance

defplay_game():

globalcurrent_balance

amount =  eval(input(‘Please enter the amount to bet. All bets must be multiples of 5.’))

print (‘You choose to bet ${}’.format(amount))

#if amount is not a multiple of 5 or greater than the current balance, keep taking input till valid amount is not entered

if (amount%5 != 0 or amount >current_balance ):

while (amount%5 != 0 or amount >current_balance):

print (‘That is not a valid amount. You bet must be a multiple of 5, and be within your means’)

amount =  eval(input(‘Please enter the amount to bet. All bets must be multiples of 5.’))

print (‘{}, you must choose one of the following attacks:’.format(name))

print (‘(1) Punch of Fury’)

print (‘(2) Kick of Punishment’)

print (‘(3) Sword of Justice’)

print (‘(4) Shuriken of Vengeance’)

print (‘(5) Nunchucks of Anger’)

print (‘(6) Knife of Freedom’)

choice = eval(input())

#Take the computer’s choice as a random imput

computers_choice = random.randint(1,6)

#storing all the moves in a list

moves = [‘Punch of Fury’, ‘Kick of Punishment’, ‘Sword of Justice’,  ‘Shuriken of Vengeance’, ‘Nunchucks of Anger’, ‘Knife of Freedom’ ]

#making a 2D array which tells which move beats which, -1 indicates player has lost, 0 means tie and 1 means player has won

moves_matrix = [[0, -1, -1, 1, 1, -1], [1,0,-1,-1,1,-1], [1,1,0,-1,-1,1],[-1,1,1,0,-1,1], [-1,-1,1,1,0,-1], [1,1,-1,-1,1,0]]

print (‘You chose: {}’.format(moves[choice-1]))

print (‘Computer chose: {}’.format(moves[computers_choice-1]))

if (moves_matrix[choice -1][computers_choice – 1] == -1):

print(‘Unfortunately, {}, you lost’.format(name))

current_balance = current_balance – amount

elif (moves_matrix[choice-1][computers_choice-1] == 1):

print(‘Congratulations, you won {}’.format(name))

current_balance = current_balance + amount

else:

print (‘It was a tie, {}’.format(name))

print (‘Your current balance is ${}’.format(current_balance))

#appending balance after each round to a list

balance_after_each_round.append(current_balance)

if __name__ == ‘__main__’:

current_balance = 100

balance_after_each_round = []

print (‘Welcome to Ultimate Ninja Battle Combat!!!’)

name = input (‘Please enter your name: ‘)

print (‘Welcome, {}’.format(name))

print (‘Your current balance is $100’)

inpt = input(‘Please choose from the following menu:\n(I)nstructions\n(P)lay game\n(Q)uit game’).lower()

#if user input is not valid

ifinpt not in [‘i’, ‘p’, ‘q’]:

print (‘Please choose a valid option’)

inpt = input(‘Please choose from the following menu:\n(I)nstructions\n(P)lay game\n(Q)uit game’).lower()

#till input is not ‘q’ and current balance is greater than 0

while (inpt!= ‘q’ and current_balance>0):

if (inpt == ‘i’):

instructions()

elif (inpt == ‘p’):

play_game()

#taking input again

inpt = input(‘Please choose from the following menu:\n(I)nstructions\n(P)lay game\n(Q)uit game’).lower()

ifinpt not in [‘i’, ‘p’, ‘q’]:

print (‘Please choose a valid option’)

inpt = input(‘Please choose from the following menu:\n(I)nstructions\n(P)lay game\n(Q)uit game’).lower()

print (‘Goodbye {}. Your final balance is ${}’.format(name, current_balance))

print (‘Your balance history is :\nStarting Balance: $100’)

#print balance after each round using the list

for i in range(len(balance_after_each_round)):

print (‘After round {}: {}’.format(i+1, balance_after_each_round[i]))