Python代写:COMPSCI101 Stuck in the Mud

代写游戏Stuck in the Mud,练习基本的Python程序设计。

The Assignment

In this assignment, you will develop the Stuck in the Mud game which is a two person dice throwing game. The game uses 5 dice and each player has 3 turns. Note: initially the players only have one turn each.

One turn consists of:

The player rolls all five dice.

  • If any 2’s or 5’s are rolled, no points are scored for this throw and any 2’s or 5’s which are thrown are set aside (they are “stuck in the mud”).

  • If no 2’s or 5’s are rolled, add up the total of the dice and add this total to the running total for this turn.

  • Continue throwing the dice which have not been set aside in this way until all the dice are “stuck in the mud”. Once all the dice have been set aside, the turn finishes.

After three turns for each player the game ends and the player with the highest score wins.

Some example output using the completed program is shown at the end of this document.

The skeleton for this program is shown below. The main() function has already been defined for you and in the last part of this assignment you need to make changes to this function. The have_a_turn()function has also been defined for you and should not be changed in any way.

Download the skeleton program file, complete the ten missing functions and add code to the main() function.

You will need to change the name of the file to “YourUsernameA2.py”, e.g., afer023A2.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import random

def main():
display_welcome() #1
number_of_turns = 3
score_player1 = 0
score_player2 = 0
name_player1 = "Olivia"
name_player2 = "Ned"
turn_num = 1
first_player_num = random.randrange(1, number_of_turns + 1)

if first_player_num == 2:
temp = name_player1
name_player1 = name_player2
name_player2 = temp

score1 = have_one_turn(turn_num, name_player1)
score2 = have_one_turn(turn_num, name_player2)

score_player1 = score_player1 + score1
score_player2 = score_player2 + score2

if turn_num < number_of_turns:
display_turn_results(name_player1, score_player1,
name_player2, score_player2, False) #10
else:
display_turn_results(name_player1, score_player1,
name_player2, score_player2, True)

def have_one_turn(turn_num, player_name):
turn_score = 0
number_of_dice = 5
dice_set_aside = ""
print(player_name + "'s turn")

while number_of_dice > 0:
dice_string = get_random_dice_string(number_of_dice) #2
display_str = " The dice: " + get_dice_display_str(dice_string) #3

if contains_2s_or_5s(dice_string): #4
dice_set_aside = dice_set_aside + \
get_string_of_2s_and_5s(dice_string) #5
dice_set_aside_str = get_dice_display_str(dice_set_aside) #3
dice_string = get_2s_and_5s_removed(dice_string) #6
dice_to_roll_again_str = get_dice_display_str(dice_string)
display_str = display_str + " " + \
get_display_info(dice_to_roll_again_str, dice_set_aside_str)
number_of_dice = len(dice_string)
else:
turn_score = turn_score + sum_of_dice_values(dice_string) #8

display_str = display_str + "\n Score so far: " + str(turn_score)
print(display_str)

display_turn_score(turn_num, turn_score) #9
return turn_score

def display_welcome(): #1
print()

def get_random_dice_string(number_of_dice): #2
return ''

def get_dice_display_str(dice_string): #3
return ''

def contains_2s_or_5s(dice_string): #4
return True

def get_string_of_2s_and_5s(dice_string): #5
return ''

def get_2s_and_5s_removed(dice_string): #6
return ''

def get_display_info(dice_to_roll_again_str, dice_set_aside_str): #7
return ''

def sum_of_dice_values(dice_string): #8
return 0

def display_turn_score(turn_num, turn_score): #9
print()

def display_turn_results(first_player_name, score_player1, second_player_name,
score_player2, display_winner):
#10

print()

NOTE: Initially, when you run the program, the program does very little because the ten functions used in the program have not yet been completed. Once you have completed the first six functions you will start to see sensible output. As you complete each function, your program will do more and more.

Complete the following ten functions

display_welcome()

This function prints five lines of output: a blank line, the string “ Welcome to Stuck in the Mud by “ followed by your username enclosed inside two lines of 42 stars followed by one blank line. An output similar to the following is printed by this function:

******************************************
  Welcome to Stuck in the Mud by afer023
******************************************

get_random_dice_string(number_of_dice)

This function is passed one parameter: the number of random dice to be rolled. The function returns a string containing the required number of random dice values. You may assume that the number of dice to be rolled is always less than 6. For example, the output of the following code is shown in the textbox below on the right:

1
2
3
4
dice_str = get_random_dice_string(5)
print("1.", dice_str)
print("2.", get_random_dice_string(3))
print("3.", get_random_dice_string(4))

get_dice_display_str(dice_string)

This function is passed one parameter: a string containing up to five dice values (digits between 1 and 6 inclusive). The function returns a string made up of each dice value separated by a single space. Note that there should not be any spaces at the front or at the end of the string returned by this function. For example, the output of the following code is shown in the textbox below on the right:

1
2
3
4
5
dice_values = "55126"
dice_str = get_dice_display_str(dice_values)
print("1.", dice_str)
print("2.", get_dice_display_str("356"))
print("3.", get_dice_display_str("5611"))

contains_2s_or_5s(dice_string)

This function is passed one parameter: a string containing up to five dice values (digits between 1 and 6 inclusive). The function returns True if any of the digits in the parameter string are either a two or a five. Otherwise the function return False. For example, the output of the following code is shown in the textbox below on the right

1
2
3
4
5
dice_values = "55126"
result = contains_2s_or_5s(dice_values)
print("1.", result)
print("2.", contains_2s_or_5s("356"))
print("3.", contains_2s_or_5s("3611"))

get_string_of_2s_and_5s(dice_string)

This function is passed one parameter: a string containing up to five digits (digits between 1 and 6 inclusive). The function returns a string made up of any twos or fives in the parameter string. For example, the output of the following code is shown in the textbox below on the right

1
2
3
4
5
dice_values = "51256"
result = get_2s_and_5s_removed(dice_values)
print("1.", result)
print("2.", get_2s_and_5s_removed("14316"))
print("3.", get_2s_and_5s_removed("3215"))

get_2s_and_5s_removed(dice_string)

This function is passed one parameter: a string containing up to five digits (digits between 1 and 6 inclusive). The function returns a string made up of all the digits in the parameter string which are not twos or fives. For example, the output of the following code is shown in the textbox below on the right.

1
2
3
4
5
dice_values = "51256"
result = get_2s_and_5s_removed(dice_values)
print("1.", result)
print("2.", get_2s_and_5s_removed("14316"))
print("3.", get_2s_and_5s_removed("3215"))

get_display_info (dice_to_roll_again_str, dice_set_aside_str)

This function is passed two string parameters:

  • dice_to_roll_again_str: a string of the dice which will be rolled again,
  • dice_set_aside_str: a string of the dice which have been set aside so far this turn.

The function returns a string with the information about the player’s turn so far. The string returned depends on the length of the two parameter strings.

  • If both of the parameters have a length greater than 0, the function returns a string made up of “(Dice to roll again: “ followed by the dice_to_roll_again_str, followed by “, Dice set aside: “, followed by the dice_set_aside_str and finally a “)” (see output 1. below).

  • Otherwise, if the dice_to_roll_again_str parameter has a length greater than 0, the function returns the string “(Dice to roll again: “, followed by dice_to_roll_again_str string followed by a “)” (see output 2. below).

  • Otherwise, if the dice_set_aside_str parameter has a length greater than 0, the function returns the string “(Dice set aside: “, followed by the dice_set_aside_str string followed by a “)” (see output 3. below).

For example, the output of the following code is shown in the textbox lower down on this page:

1
2
3
4
5
6
dice_to_roll_again_str = "6 4 3"
dice_set_aside_str = "5 5"
result = get_display_info(dice_to_roll_again_str, dice_set_aside_str)
print("1.", result)
print("2.", get_display_info("6 6 1 4 2", ""))
print("3.", get_display_info("", "5 5 2 5 2"))

1. (Dice to roll again: 6 4 3, Dice set aside: 5 5)
2. (Dice to roll again: 6 6 1 4 2)
3. (Dice set aside: 5 5 2 5 2)

sum_of_dice_values(dice_string)

This function is passed one parameter: a string containing up to five digits (digits between 1 and 6 inclusive). The function returns the total of all the digit values in the parameter string.

For example, the output of the following code is shown in the textbox below on the right

1
2
3
4
5
dice_values = "41236"
result = sum_of_dice_values(dice_values)
print("1.", result)
print("2.", sum_of_dice_values("14316"))
print("3.", sum_of_dice_values("3313"))

display_turn_score(turn_num, turn_score)

This function is passed two integer parameters: the turn number and the score so far at the end of this turn. The function prints four lines of output:

  • A blank line.
  • A string made up of “ **** Turn “, followed by the turn number parameter, followed by “ score: “, followed by the score for this turn, followed by the string “ ****“.
  • A line of two blank spaces followed by 27 “-“ symbols.
  • A blank line.

For example, the output of the following code is shown in the textbox below on the right

display_turn_score(1, 21)
display_turn_score(1, 56)
display_turn_score(3, 87)

display_turn_results(first_player_name, score_player1, second_player_name, score_player2, display_winner)

This function is passed five parameters: the name of the first player, the first player’s score, the name of the second player, the second player’s score and a boolean indicating whether the function needs to display the winner or not. The function prints the following lines of output:

  • A blank line.
  • Two lines of 30 “-“ symbols.
  • A line of four blank spaces followed by the name of the first player, followed by “‘s score: “ and finally the first player’s score.
  • A line of four blank spaces followed by the name of the second player, followed by “‘s score: “ and finally the second player’s score.
  • If the last parameter has the value True, there is an extra line displayed showing the winner of the game. If both players have the same score this line displays “ The result is a draw!”, otherwise this line is made up of four blank spaces, followed by the name of the winner followed by “ is the winner!”
  • Two lines of 30 “-“ symbols.
  • Two blank lines.

For example, the output of the following code is shown in the textbox lower down on this page:

1
2
3
display_turn_results("Andre", 65, "Olivia", 78, True)
display_turn_results("Gill", 43, "Sebastian", 43, True)
display_turn_results("Terry", 32, "Guy", 48, False)

Make a change to the main() function

Currently the players only have one turn each and the winner of the game is never displayed. Change the main() function of this program so that each player has exactly three turns (and the turn numbers 1, 2 and 3 are correctly displayed in the output).

Example Ouput

Below is some output produced by the completed program. Your program must execute in the way described and the output should have the same format as the output below:

******************************************
  Welcome to Stuck in the Mud by afer023
******************************************

Olivia's turn
  The dice: 3 6 2 4 5 (Dice to roll again: 3 6 4, Dice set aside: 2 5)
    Score so far: 0
  The dice: 2 6 1 (Dice to roll again: 6 1, Dice set aside: 2 5 2)
    Score so far: 0
  The dice: 4 6
    Score so far: 10
  The dice: 2 1 (Dice to roll again: 1, Dice set aside: 2 5 2 2)
    Score so far: 10
  The dice: 2 (Dice set aside: 2 5 2 2 2)
    Score so far: 10

  **** Turn 1 score: 10 ****
  --------------------------

Ned's turn
  The dice: 1 5 1 1 2 (Dice to roll again: 1 1 1, Dice set aside: 5 2)
    Score so far: 0
  The dice: 2 4 4 (Dice to roll again: 4 4, Dice set aside: 5 2 2)
    Score so far: 0
  The dice: 3 2 (Dice to roll again: 3, Dice set aside: 5 2 2 2)
    Score so far: 0
  The dice: 6
    Score so far: 6
  The dice: 4
    Score so far: 10
  The dice: 3
    Score so far: 13
  The dice: 1
    Score so far: 14
  The dice: 6
    Score so far: 20
  The dice: 5 (Dice set aside: 5 2 2 2 5)
    Score so far: 20

  **** Turn 1 score: 20 ****
  ---------------------------
    Olivia's score: 10
    Ned's score: 20
  ---------------------------

Olivia's turn
  The dice: 2 1 2 1 4 (Dice to roll again: 1 1 4, Dice set aside: 2 2)
    Score so far: 0
  The dice: 2 5 1 (Dice to roll again: 1, Dice set aside: 2 2 2 5)
    Score so far: 0
  The dice: 4
    Score so far: 4
  The dice: 1
    Score so far: 5
  The dice: 3
    Score so far: 8
  The dice: 4
    Score so far: 12
  The dice: 6
    Score so far: 18
  The dice: 6
    Score so far: 24
  The dice: 6
    Score so far: 30
  The dice: 2 (Dice set aside: 2 2 2 5 2)
    Score so far: 30

  **** Turn 2 score: 30 ****
  --------------------------

Ned's turn
  The dice: 3 1 5 5 1 (Dice to roll again: 3 1 1, Dice set aside: 5 5)
    Score so far: 0
  The dice: 6 5 2 (Dice to roll again: 6, Dice set aside: 5 5 5 2)
    Score so far: 0
  The dice: 5 (Dice set aside: 5 5 5 2 5)
    Score so far: 0

  **** Turn 2 score: 0 ****
  ---------------------------

    Olivia's score: 40
    Ned's score: 20
  ---------------------------

Olivia's turn
  The dice: 6 6 1 2 2 (Dice to roll again: 6 6 1, Dice set aside: 2 2)
    Score so far: 0
  The dice: 3 4 3
    Score so far: 10
  The dice: 1 2 1 (Dice to roll again: 1 1, Dice set aside: 2 2 2)
    Score so far: 10
  The dice: 4 6
    Score so far: 20
  The dice: 6 6
    Score so far: 32
  The dice: 5 3 (Dice to roll again: 3, Dice set aside: 2 2 2 5)
    Score so far: 32
  The dice: 3
    Score so far: 35
  The dice: 3
    Score so far: 38
  The dice: 5 (Dice set aside: 2 2 2 5 5)
    Score so far: 38

  **** Turn 3 score: 38 ****
  --------------------------

Ned's turn
  The dice: 2 5 4 4 2 (Dice to roll again: 4 4, Dice set aside: 2 5 2)
    Score so far: 0
  The dice: 3 5 (Dice to roll again: 3, Dice set aside: 2 5 2 5)
    Score so far: 0
  The dice: 4
    Score so far: 4
  The dice: 5 (Dice set aside: 2 5 2 5 5)
    Score so far: 4

  **** Turn 3 score: 4 ****
  ---------------------------

  --------------------------
  --------------------------
    Olivia's score: 78
    Ned's score: 24
    Olivia is the winner!
  --------------------------
  --------------------------