Question

Assignment 2 – The two player game of Poaka The game of Poaka is a dice...

Assignment 2 – The two player game of Poaka The game of Poaka is a dice game played by two players. Each player takes turns at rolling the dice. At each turn, the player repeatedly rolls a dice until either the player rolls a 1 (in which case the player scores 0 for their turn) or the player chooses to end their turn (in which case the player scores the total of all their dice rolls).
At any time during a player's turn, the player is faced with two options: To roll or not to roll:   
CompSci 101, 2019 - 2 - Assignment Two

a) If the player chooses to roll and the player rolls a • 1 - the player scores zero for their turn and it becomes the next player's turn. • 2, 3, 4, 5 or 6 – the dice value is added to the player's turn total and the player's turn continues.
b) If the player chooses to end their turn - the turn total is added to the player's score and it is now the turn of the other player.

The first player to score 30 or more points wins the game.

For example, The first player, Jack, begins a turn with a roll of 5. Jack could end his turn and score 5 points, but he chooses to roll again. Jack rolls a 2, and could end his turn with a turn total of 7 points, but chooses to roll again. Jack rolls a 1, and must end his turn with a zero score. The next player, Jill, rolls the sequence 4-5-3-5-5, after which she chooses to end her turn, and adds her turn total of 22 points to her score, and so on.

Assignment 2 Section A (26 marks) Download the Python skeleton program (SkeletonA2.py) from the CompSci 101 assignments website:
https://www.cs.auckland.ac.nz/courses/compsci101s2c/assignments/
and rename the SkeletonA2.py file to "YourUsernameA2.py", e.g., afer023A2.py. Develop the solution to each of the fourteen functions in the SkeletonA2.py program. Once you are happy that a function executes correctly, submit the whole function to Coderunner2:
https://coderunner2.auckland.ac.nz/moodle/

When you press the Check button in Coderunner2, you will receive immediate feedback telling you if you have passed all the tests for that function. You can submit as many times as you need. You need to submit each function separately. Your marks for Section A of Assignment 2 are obtained through Coderunner2. When you have successfully completed all the functions and your code passes all the tests click the "Finish attempt" button. 1. welcome_to_game(player1, player2) This function is passed two strings as parameters, the names of the two players. The function prints six lines of output:
• a blank line. • 42 "*" symbols. • 5 spaces, followed by "Welcome to POAKA by " followed by your username. • 5 spaces, followed by the first player's name, followed by " versus ", followed by the second player's name. • 42 "*" symbols. • a blank line.

  
*** IMPORTANT: when checking this function in Coderunner2 you MUST put "by afer023" (NOT your own username). This is so your output matches the expected output but in your actual Assignment 2 program put your own username (not afer023).

CompSci 101, 2019 - 3 - Assignment Two
For example, the code:
welcome_to_game("Jack", "Jill")
prints (the first and the last blank lines are not shown here):

****************************************** Welcome to POAKA by afer023 Jack versus Jill ****************************************** <--------------------------->
2. get_starting_player_number() This function returns a random number which is either the number 1 or the number 2. <--------------------------->
3. get_player_name(player_number, name_player1, name_player2) This function is passed three parameters: a number (either a 1 or a 2) and two strings (the names of the two players). If the first parameter (player_number) is a 1, the function returns the second parameter string (name_player1), otherwise the function returns the third parameter string (name_player2). For example, the code:
print("1.", get_player_name(2, "Jack", "Jill")) print("2.", get_player_name(1, "Jack", "Jill"))
prints:

1. Jill 2. Jack
<---------------------------> 4. get_player_score(player_number, score_player1, score_player2) This function is passed three integer parameters: a number (either a 1 or a 2) and two integer values (the current scores of the two players). If the first parameter (player_number) is the number 1, the function returns the second parameter(score_player1), otherwise the function returns the third parameter(score_player2). For example, the code:
print("1.", get_player_score(2, 13, 21)) print("2.", get_player_score(1, 13, 21))
prints:

1. 21 2. 13
<---------------------------> 5. get_other_player_name_and_score(player_number, name_player1, name_player2, score_player1, score_player2) This function is passed five parameters: a number (either a 1 or a 2), two strings (the names of the two players) and two integer values (the current scores of the two players). If the first parameter (player_number) is the number 1, the function returns the string made up of the second player's name followed by "'s score: " followed by the score of the second player, otherwise
CompSci 101, 2019 - 4 - Assignment Two
the function returns the string made up of the first player's name followed by "'s score: " followed by the score of the first player. For example, the code:

print("1.", get_other_player_name_and_score(2, "Jack", "Jill", 13, 21)) print("2.", get_other_player_name_and_score(1, "Jack", "Jill", 13, 21))
prints:

1. Jack's score: 13 2. Jill's score: 21
<---------------------------> 6. display_player_turn_info(player_name, player_score, other_player_info) This function is passed three parameters: a string (the player's name), an integer (the player's score) and a string (describing the other player's name and score). The function prints four lines of output:
• a blank line. • 42 "*" symbols. • the first parameter (player_name), followed by "'s turn (score: ", followed by the second parameter (player_score), followed by ") " and finally the third parameter (other_player_info) inside parentheses (round brackets). • a blank line.
For example, the code:
display_player_turn_info("Jack", 22,"Jill's score: 16")
prints (the first and the last blank lines of the output are not shown here):

****************************************** Jack's turn (score: 22) (Jill's score: 16) <---------------------------> 7. dice_rolls_contains_a_one(dice_string) This function is passed a string containing dice values as a parameter. If the dice values string contains a "1" the function returns True, otherwise the function returns False. For example, the code:
print("1.", dice_rolls_contains_a_one("2251")) print("2.", dice_rolls_contains_a_one("3256"))
prints:

1. True 2. False
<---------------------------> 8. get_sum_of_dice(dice_string) This function is passed a string containing any number of digits as a parameter. The function returns the sum of the digits which make up the parameter string. Note: you will need to use a loop for this function.
CompSci 101, 2019 - 5 - Assignment Two
For example, the code:
print("1.", get_sum_of_dice("22565")) print("2.", get_sum_of_dice("366"))
prints:

1. 20 2. 15
<---------------------------> 9. game_has_been_won(player1_score, player2_score, winning_score) This function is passed three integer values as parameters: the current scores of the two players and the number of points needed to win the game. If either of the players' scores are greater than or equal to the points needed to win the game the function returns True, otherwise the function returns False. For example, the code:
print("1.", game_has_been_won(32, 23, 30)) print("2.", game_has_been_won(5, 34, 30)) print("3.", game_has_been_won(3, 20, 30))
prints:

1. True 2. True 3. False
<---------------------------> 10. get_other_player_number(current_player_number) This function is passed an integer as a parameter (current_player_number). If the parameter is a 1 the function returns 2, otherwise the function returns 1. For example, the code:
print("1.", get_other_player_number(2)) print("2.", get_other_player_number(1))


prints:

1. 1 2. 2
<---------------------------> 11. get_winner_number(score_player1, score_player2) This function is passed two integer values as parameters. If the first parameter (score_player1) is greater than the second parameter (score_player2) the function returns 1, otherwise (even if the two scores are equal) the function returns 2. For example, the code:
print("1.", get_winner_number(32, 21)) print("2.", get_winner_number(12, 33))


prints:

1. 1 2. 2
<--------------------------->
CompSci 101, 2019 - 6 - Assignment Two
12. congratulate_winner(winner_name) This function is passed a string as a parameter (the name of the winner). The function prints five lines of output:
• a blank line. • 42 "*" symbols. • The string " Well done ", followed by the name of the winner and finally the string "! You have won!". • 42 "*" symbols. • a blank line.
For example, the code:
congratulate_winner("Jack")
prints (the first and the last blank lines of the output are not shown here):

****************************************** Well done Jack! You have won! ******************************************

<--------------------------->
13. wants_to_play_again() This function asks the user if they would like to play again using the prompt: " Would you like to play again (y or n): ". If the user enters "y" the function return True, otherwise the function returns False. <---------------------------> 14. display_final_results(wins_player1, wins_player2, name_player1, name_player2) This function is passed four parameters (the number of games won by each player followed by each player's name). The function prints either seven lines of output if each of the two players has won the same number of games, or the function prints eight lines of output:
• a blank line. • a blank line. • 42 "*" symbols.
If both players have won the same number of games, the fourth line of output is: • The string " Everyone is a winner! -", followed by the number of games won by both players. Otherwise, the fourth and the fifth lines of output are: • The string " The winner: ", followed by the name of the winning player in uppercase characters, followed by " – " and finally, the number of games won by the winning player. • The string " Runner up: ", followed by the name of the runner up followed by " – " and finally, the number of games won by the runner up.
• 42 "*" symbols. • a blank line. • a blank line.
For example, the code:
5 spaces
CompSci 101, 2019 - 7 - Assignment Two
display_final_results(2, 3, "Jack", "Jill")
prints (the first blank lines and the last two blank lines of the output are not shown here):
****************************************** The winner: JILL - 3 Runner up: Jack - 2 ******************************************

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code


import random

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# You should not make any changes
# to the main() function
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def main():
player2_is_automated = False #Change to True when doing Section C
name_player1 = "Jack"
name_player2 = "Jill" #Change to "Computer" when doing Section C

wins_player1 = 0
wins_player2 = 0

game_has_ended = False

welcome_to_game(name_player1, name_player2) #1.

while not game_has_ended:
   game_winner = play_one_game(name_player1, name_player2, player2_is_automated)

   if game_winner == 1:
       wins_player1 += 1
   else:
       wins_player2 += 1

   if not wants_to_play_again(): #13.
       game_has_ended = True

display_final_results(wins_player1, wins_player2, name_player1, name_player2) #14.

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Section A - Complete the
# following 14 functions.
# Submit each completed function
# to Coderunner.
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#------------------------------------
# 1. welcome_to_game()
#------------------------------------
def welcome_to_game(player1, player2):
number_of_stars = 42
print()
print("*"*number_of_stars,end="")
print(" Welcome to POAKA by afer023",end="")
print(" {} versus {}".format(player1,player2),end="")
print("*"*number_of_stars,end="")
print()
#------------------------------------
# 2. get_starting_player_number()
#------------------------------------
def get_starting_player_number():
   return random.randint(1,3)
#------------------------------------
# 3. get_player_name()
#------------------------------------
def get_player_name(player_number, name_player1, name_player2):
if player_number==1:
return name_player1
return name_player2
#------------------------------------
# 4. get_player_score()
#------------------------------------
def get_player_score(player_number, score_player1, score_player2):
if player_number==1:
return score_player1
return score_player2

#------------------------------------
# 5. get_other_player_name_and_score()
#------------------------------------
def get_other_player_name_and_score(player_number, name_player1, name_player2, score_player1, score_player2):
if player_number==1:
return name_player1+"'s score "+str(score_player1)
return name_player2+"'s score "+str(score_player2)
#------------------------------------
# 6. display_player_turn_info()
#------------------------------------
def display_player_turn_info(player_name, player_score, other_player_info):
number_of_stars = 42
print()
print("*"*number_of_stars,end=" ")
print("{}'s turn (score: {}) ({})\n".format(player_name,player_score,other_player_info))
#------------------------------------
# 7. dice_roll_contains_a_one()
#------------------------------------
def dice_rolls_contains_a_one(dice_string):
if "1" in dice_string:
return True
return False
#------------------------------------
# 8. get_sum_of_dice()
#------------------------------------
def get_sum_of_dice(dice_string):
sum=0
for digit in dice_string:
sum+=int(digit)
return sum
#------------------------------------
# 9. game_has_been_won()
#------------------------------------
def game_has_been_won(player1_score, player2_score, game_score):
if(player1_score>=game_score or player2_score>=game_score):
return True
return False
#------------------------------------
# 10. get_other_player_number()
#------------------------------------
def get_other_player_number(current_player_number):
if(current_player_number==1):
return 2
return 1
#------------------------------------
# 11. get_winner_number()
#------------------------------------
def get_winner_number(score_player1, score_player2):
if(score_player1>score_player2):
return 1
return 2
#------------------------------------
# 12. get_other_player_number()
#------------------------------------
def congratulate_winner(winner_name):
number_of_stars = 42
print()
print("*"*number_of_stars,end=" ")
print("Well done {}! You have won! ".format(winner_name),end="")
print("*"*number_of_stars)
print()
#------------------------------------
# 13. wants_to_play_again()
#------------------------------------
def wants_to_play_again():
again=input("Would you like to play again (y or n): ")
if(again=='y'):
return True
return False
#------------------------------------
# 14. display_final_results()
#------------------------------------
def display_final_results(wins_player1, wins_player2, name_player1, name_player2):
number_of_stars = 42
print()
print()
print("*"*number_of_stars,end="")
if(wins_player1==wins_player2):
print("Everyone is a winner! - {}".format(wins_player1))
elif(wins_player1>wins_player2):
print(" The winner: {} - {} Runner up: {} - {} ".format(name_player1.upper(),wins_player1,name_player2,wins_player2),end="")
else:
print(" The winner: {} - {} Runner up: {} - {} ".format(name_player2.upper(),wins_player2,name_player1,wins_player1),end="")
print("*"*number_of_stars)
print()
print()
#------------------------------------
# Section C: Final part of the assignment
# 15. computer_wants_roll_again()
#------------------------------------
def computer_wants_roll_again(dice_string_so_far, score_player1, score_player2, points_needed_to_win):
return False

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# You should not make any changes
# to any code below this line.
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# get_string_of_dice_rolls()
def get_string_of_dice_rolls():
dice_string = ""
user_input = input(" ")
while user_input == "":
dice_roll = str(random.randrange(1, 7))
dice_roll_display = " Your dice: " + dice_roll + " "
dice_string = dice_string + dice_roll
if dice_roll == "1":
print(dice_roll_display, "Ooops! You rolled a one!")
return dice_string
user_input = input(dice_roll_display)
return dice_string

def play_one_game(name_player1, name_player2, player2_is_automated):
points_needed_to_win = 30

score_player1 = 0
score_player2 = 0

winnner_number = 0
current_score = 0
game_has_ended = False

current_player_number = get_starting_player_number() #2.

while not game_has_ended:
current_player_name = get_player_name(current_player_number, name_player1, name_player2) #3.
current_player_score = get_player_score(current_player_number, score_player1, score_player2) #4.
other_player_info = get_other_player_name_and_score(current_player_number, name_player1, name_player2, score_player1, score_player2) #5.
display_player_turn_info(current_player_name, current_player_score, other_player_info) #6.

if player2_is_automated and current_player_number == 2:
turn_score = computer_has_a_turn(current_player_name, score_player1, score_player2, points_needed_to_win)
else:
turn_score = have_a_turn(current_player_name)

if current_player_number == 1:
score_player1 = score_player1 + turn_score
else:
score_player2 = score_player2 + turn_score

if game_has_been_won(score_player1, score_player2, points_needed_to_win): #9.
game_has_ended = True
else:
current_player_number = get_other_player_number(current_player_number) #10.


winnning_player_number = get_winner_number(score_player1, score_player2) #11.
name_winner = get_player_name(winnning_player_number, name_player1, name_player2)
congratulate_winner(name_winner) #12.

return winnning_player_number

def computer_has_a_turn(current_player_name, score_player1, score_player2, points_needed_to_win):
score_this_turn = 0
print(current_player_name + ", keep on pressing ENTER to continue \n rolling the dice (anything else\n to end your turn)")
string_of_dice = ""
wants_to_roll_again = True
while wants_to_roll_again:
dice_roll = str(random.randrange(1, 7))

dice_roll_display = " Your dice: " + dice_roll + " "
print(dice_roll_display)
string_of_dice = string_of_dice + dice_roll
if dice_roll == "1":
print(" Ooops! You rolled a one!")
return 0

wants_to_roll_again = computer_wants_roll_again(string_of_dice, score_player1, score_player2,
points_needed_to_win) #15.
score_this_turn = get_sum_of_dice(string_of_dice) #8.
return score_this_turn

def have_a_turn(player_name):
   score_this_turn = 0
   print(player_name + ", keep on pressing ENTER to continue \n rolling the dice (anything else\n to end your turn)")
   string_of_dice = get_string_of_dice_rolls()

   if dice_rolls_contains_a_one(string_of_dice): #7.
       return 0

   score_this_turn = get_sum_of_dice(string_of_dice) #8.
   return score_this_turn

main()

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Assignment 2 – The two player game of Poaka The game of Poaka is a dice...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The game of Pig is a simple two-player dice game in which the first player to...

    The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die: If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn. If the player rolls 2 through 6, then he or she can either ROLL AGAIN or HOLD:      At this point, the sum of all rolls...

  • // This program creates a simulated player who takes one turn in the // Pig dice...

    // This program creates a simulated player who takes one turn in the // Pig dice game. The simulated player keeps rolling the die until // the total for the turn is 20 or greater, or until a 1 is rolled. // If a 1 is rolled, the player's score for the turn is 0. Otherwise // the player's score is the sum of the rolls for the turn. // ///////////////////////////////////////////////////////////////////// #include<iostream> #include<cstdlib> #include<time.h> using namespace std; int randNumGen(int upper,...

  • C or C++ Project Overview Wild, Wild, West! Dice Game The Wild, Wild, West! is an...

    C or C++ Project Overview Wild, Wild, West! Dice Game The Wild, Wild, West! is an elimination dice game. It can be played by any number of players, but it is ideal to have three or more players. Wild, Wild, West! Requires the use of two dice. At the start of the game, each player receives a paper that will track the number of lives that player has. Each player starts the game with 6 lives. In the first round,...

  • For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two p...

    For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn,...

  • Two player Dice game In C++ The game of ancient game of horse, not the basketball...

    Two player Dice game In C++ The game of ancient game of horse, not the basketball version, is a two player game in which the first player to reach a score of 100 wins. Players take alternating turns. On each player’s turn he/she rolls a six-sided dice. After each roll: a. If the player rolls a 3-6 then he/she can either Roll again or Hold. If the player holds then the player gets all of the points summed up during...

  • 6.32 In the game of Chuck-a-luck, three dice are rolled. The player selects a number between...

    6.32 In the game of Chuck-a-luck, three dice are rolled. The player selects a number between 1 and 6. If the player's number comes up on exactly one die, the player wins $1. If the player's number comes up on exactly two dice, the player wins $2. If the player's number comes up on all three dice, the player wins $3. If the player's number does not come up, the player loses $1. Let X denote the player's win or...

  • In python: ScoreFinder is a function that takes in two lists and a string. The first...

    In python: ScoreFinder is a function that takes in two lists and a string. The first list is a list of strings (player names), the second list is a list of floats (player scores), and the string is a name (player to find). If the player to find exists in the list of player names, then print the name of that player along with their associated score (which is in the second list at the same index). If the player...

  • The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players...

    The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 (”pig”) is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player’s turn, the player is faced with two decisions: roll If the player rolls a 1: the player scores nothing and it becomes the...

  • python code( using functions please)! Rules of the game: - This game requires a dice. The...

    python code( using functions please)! Rules of the game: - This game requires a dice. The goal of the game is to collect the correct number of coins to create a dollar. Players take coins based on a roll of the dice. The numbers on the dice correlate to the coin values as follows:  1—penny  2—nickel  3—dime  4—quarter  5— ( pick a random card from 1 to 4): o 1 = penny o 2 =...

  • can someone help me fix my jeopardy dice game I am having a hard time figuring...

    can someone help me fix my jeopardy dice game I am having a hard time figuring out what i am doing wrong #include #include using namespace std; //this function makes a number between 1 and 6 come out just like a dice int rollDie() { return (rand() % 6+1); } //this function asks the user with a printed statement if they want to roll the dice and gives instructions for yes and no (y/n) void askYoNs(){ cout<<"Do you want to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT