CSCI1200 Homework 5 — Game of Chance solution

$35.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

Objectives:

– Understand how to use while loops.
– Understand how to use conditionals inside of while loops.
– Understand how to use nested while loops. (A while loop inside of another while loop)
– Understand how to call functions so that you can reuse code.
– Understand how to use functions from modules.

Turn In:
– hw5_lastname_code.ipynb
You should turn this file in on moodle under the “Homework 5 – Game of Chance” link.

Instructions:

Your job is to write a program that:
1. Displays a menu to the user with an appropriate set of games
2. Asks them which game they want to play
3. Asks for information and allows them to play the game
4. Then, control flow goes back to the menu for game selection again

We have given you some starter code (hw5_starter.ipynb). This starter code has all the
functions you will need defined and the general skeleton for your main function already written.

You must:
● Replace the comments in main to appropriately assign variables so that the while loop
only stops looping when the user chooses to exit in the menu.
● Fill in the code in the display_menu function so that it displays the menu as in the
example below and then prompts the user for a choice, which it returns.

● Fill in the code for the three games (St. Petersburg lottery, First to a Word, and Game of
21) in the appropriate function we have defined for you. Below we will include a brief
description of each game (instructions are also included in print statements in the starter
code) and we will provide you with some tips.

● You may assume that the user will only enter valid input.

Bonuses (choose one of the following two, up to +3 points)
1) Implement a fourth game of your choosing. This bonus game must include a while loop.
Bonus points will be assigned based on the complexity of the game included.
2) Implement functions that appropriately re-prompt the user for new input if they don’t
enter what you expect everywhere where you ask for user input. Make sure to do this by
writing functions and calling the functions where appropriate (re-use your code!).

Game Description and Tips:

St. Petersburg Lottery- (10 points)

● (Description) You are being asked to implement a variation on a historically important
game known as the St. Petersburg Lottery (read this article if you are curious about the
original game).

In the version you are implementing in this assignment, the player will
select any number greater than 0. Up to the number of times they chose, a “virtual coin”
will be flipped that randomly chooses 0 or 1. If a 1 comes up before the last flip, then
they lose. If a 1 does not appear at all in the given drawing, then the user loses. They
only win if 1 is chosen for the first time exactly on the last flip.

● (Coding Tips) You should use a while loop to “flip the coin” up to the number of times the
user specifies. You should use the random.randint(0,1) function to randomly choose 0 or
1. Set the logical expression in the loop in such a way that it stops when either a 1
appears or they have flipped the user specified number of times. From this you will be
able to know whether they have lost the game.

First to a Word- (12 points)

● (Description) You are being asked to implement a two player game. In this game players
will take turns choosing letters one at a time until a word is formed. After each letter is
chosen users will have a chance to decide whether or not a word has been formed.

When a word is formed, the player who played the last letter wins. Only words of length
greater than 2 should be allowed to be stopped on. The starting player is chosen at
random.

● (Coding Tips) You should use a while loop that only stops looping when the players
agree that a word has been chosen. Associate each player with a number and use
random.randint(0,1) to randomly choose a player that starts.

Game of 21- (10 points)

● (Description) You are being asked to implement the game you played during the first lab.
In this game two players alternate choosing numbers. Starting from 1, the players take
turns saying a number that is 1, 2, or 3 greater than the previous number. The first player
always starts by saying “1”. The player who says “21” loses. You may not say a number
greater than 21. The starting player is chosen at random.

● (Coding Tips) You should use a while loop that only stops when the total number in the
game is greater than or equal to 21. You should only ask players to choose numbers that
increase the total by 1, 2, or 3 and should not allow them to ever directly affect the total.
Associate each player with a number and use random.randint(0,1) to randomly choose a
player that starts.

Remaining 20 points:
– 5 points for appropriately updating the code for main() that we have provided
– 5 points for implementing the display_menu function
– 10 points for style and comments (see notes at end of write up)

Output & Testing:

It is your responsibility to test your program. Try all the “edge cases”. That is, try your best to
break your own code within the parameters (e.g. create infinite loops, try all the different types of
behaviours. Make sure your output exactly matches ours! Above you have an example of all
three games, of all options being selected, etc.

Development:

Your code should always be in a “runnable” state. Do not attempt to develop your entire
program without running it.

1) Start setting up the main function. You’ll need to write code to make this work. Run your
program. Without filling any of the functions out, main should immediately act as if the
user chose to exit.

2) Next, write the function that prints out the menu and asks for a user’s choice. Look at
how the function is being called in main, should the display_menu function be returning
anything or just printing? Test it.

3) Next, write the function for each game. As you work on a game, test it by running the
main function and choosing it from the menu. Ensure it is working as you expect and
looks as you want! Do test your game as you are implementing it. (For example, first
write the code to get the number from a user and flip a coin that many times. Test it.
Then work on figuring out if the user won or not.)

4) Finally, test your code with a variety of user input. Ensure that the menu keeps being
displayed after each game played until the user chooses to exit.

Example output:
Below is example output of the program. User input is shown in bold. Your program should
exactly reproduce the output shown below.

Example output (user input in bold, more example output files provided separately)
Welcome to the Game of Chance!
~=[Menu]=~
1 – St. Petersburg Lottery
2 – First to a Word
3 – The game of 21
4 – Exit the Game of Chance
Which menu option do you choose? 1
###### St. Petersburg Lottery ######

Instructions:

You will select any number greater than 0.
Up to the number of times you chose, we will randomly choose 0 or 1.
If a 1 is chosen before the last drawing, then you lose.
If a 1 does not appear at all, then you lose.
You win if 1 is chosen for the first time exactly on the last drawing.

Enter player’s name: Gabe
How many times do you think we will need to draw numbers before a 1
appears? 4

Womp womp! You lose!
Better luck next time!
~=[Menu]=~
1 – St. Petersburg Lottery
2 – First to a Word
3 – The game of 21
4 – Exit the Game of Chance
Which menu option do you choose? 1
###### St. Petersburg Lottery ######

Instructions:

You will select any number greater than 0.
Up to the number of times you chose, we will randomly choose 0 or 1.
If a 1 is chosen before the last drawing, then you lose.
If a 1 does not appear at all, then you lose.
You win if 1 is chosen for the first time exactly on the last drawing.

Enter player’s name: Gabe
How many times do you think we will need to draw numbers before a 1
appears? 30
Womp womp! You lose!

Better luck next time!
Welcome to the Game of Chance!
~=[Menu]=~
1 – St. Petersburg Lottery
2 – First to a Word
3 – The game of 21
4 – Exit the Game of Chance
Which menu option do you choose? 2
###### First to a Word ######

Instructions:

You will take turns choosing letters one at a time until a word is
formed.
After each letter is chosen you will have a chance to confirm whether or
not a word has been formed
When a word is formed, the player who played the last letter wins!

One of you has been chosen at random to initiate the game.
Note: Words must be longer than a single letter!
Enter player 1’s name: Theo (Gabe’s Cat)
Enter player 2’s name: Tommy (Gabe’s Fuzzier Cat)

Theo (Gabe’s Cat): Please enter a character. N
You currently have put together the letters: N
Tommy (Gabe’s Fuzzier Cat): Please enter a character. I
You currently have put together the letters: NI
Theo (Gabe’s Cat): Please enter a character. P
You currently have put together the letters: NIP
Is this a word? (yes or no) yes
Congratulations Theo (Gabe’s Cat), you win!
~=[Menu]=~

1 – St. Petersburg Lottery
2 – First to a Word
3 – The game of 21
4 – Exit the Game of Chance
Which menu option do you choose? 4
Welcome to the Game of Chance!

~=[Menu]=~
1 – St. Petersburg Lottery
2 – First to a Word
3 – The game of 21
4 – Exit the Game of Chance
Which menu option do you choose? 3
###### Game of 21 ######

Instructions:

Starting from 1, you will take turns saying a number that is 1, 2, or 3
greater than the previous number.
The first player always starts by saying 1.
The player who says 21 loses.
You may not say a number greater than 21.
One of you has been chosen at random to initiate the game.

Enter player 1’s name: Theo
Enter player 2’s name: Tommy
The current number is: 1

Tommy: Do you want to increase the current number by 1, 2, or 3? 3
The current number is: 4
Theo: Do you want to increase the current number by 1, 2, or 3? 3
The current number is: 7
Tommy: Do you want to increase the current number by 1, 2, or 3? 2
The current number is: 9
Theo: Do you want to increase the current number by 1, 2, or 3? 1
The current number is: 10
Tommy: Do you want to increase the current number by 1, 2, or 3? 2
The current number is: 12
Theo: Do you want to increase the current number by 1, 2, or 3? 3
The current number is: 15

Tommy: Do you want to increase the current number by 1, 2, or 3? 1
The current number is: 16
Theo: Do you want to increase the current number by 1, 2, or 3? 2
The current number is: 18
Tommy: Do you want to increase the current number by 1, 2, or 3? 2
The current number is: 20
Theo: Do you want to increase the current number by 1, 2, or 3? 1
Congratulations Tommy, you win!

~=[Menu]=~
1 – St. Petersburg Lottery
2 – First to a Word
3 – The game of 21
4 – Exit the Game of Chance
Which menu option do you choose? 4
We will now exit the Game of Chance.
Thank you for playing!

Comments (5 points)

Your code must be commented! You must include a file comment (your name, your section, the
homework name, and a brief description of what the program does), inline comments
(explaining any complex code), and function comments. An example function comment is
shown below:

def fahrenheit_to_celsius(fahrenheit):
“””
This function converts a temperature in fahrenheit to celsius
and prints the equivalence.
Parameters:
fahrenheit (int or float): degrees fahrenheit

Return:
celsius (float): equivalent degrees celsius
“””
celsius = (fahrenheit – 32) * (5/9)
return celsius

Style & Structure (5 points)

Naming (1 point)
● Your variable names should be meaningful and concise
● Your variable names should be formatted like: user_birthday (words start with lowercase
letters and are separated with underscores)

● Function names should be formatted the same way as your variables: get_today()(words
start with lowercase letters and are separated with underscores)

Proper Variable Usage (1 point)
● You should not have variables outside of functions.

Functions (3 points)
● You must have a main function. The only code in your program that is not inside a
function should be a single call to main().

● All non-main functions must be defined at the zero indentation level, as in the example
lecture code. Do not attempt to define a function inside another function.

● Do not define multiple main functions.