Python - Programming

Sigchi4life
Category:
Programming
Price: $40 USD

Question description

ASCII.pdf

There are two parts:
Part I: Steal or Deal?
You are required to write a Python program that allows a player to play a game of Steal or Deal against the computer. The program will allow a player to play at most 10 games of Steal or Deal but the player can stop before he/she has reached his/her 10 game limit. Once the player chooses to stop playing or the player has played 10 games, the program will report the game statistics to the screen.
Part II: Encryption
You are required to write a Python program that performs simple encryption and decryption on strings entered by the user. To do so, you are to use one of the simplest and most widely known encryption techniques known as the Caesar Cipher. The Caesar Cipher is named after Julius Caesar who used it for private correspondence.

STAGES (PART I)

It is recommended that you develop this part of the assignment in the suggested stages. Each stage is worth a portion of the marks. Many problems in later stages are due to errors in early stages. 

The following stages of development are recommended:

Stage 1

To begin, display the jackpot to the screen and prompt for and read the player’s choice. Display the player’s choice to the screen as seen below (the ‘C:’ stands for choice).

Sample output 1:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal

Sample output 2:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal

Make sure the program runs correctly. Once you have that working, back up your program. Note: When developing software, you should always have fixed points in your development where you know your software is bug free and runs correctly.

Stage 2

Add code to generate the computer’s choice. Use the random.choice("sd") or random.randint(1,2) function to simulate the computer’s choice. Display the computer’s choice to the screen as seen below. Don’t worry too much about formatting the output at this stage, just make sure that the values are correct.

Sample output 1:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Steal

Sample output 2:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Deal

Sample output 3:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Steal

Sample output 4:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Deal 8 of 36

Stage 3

Add code to determine whether the player wins, loses or draws with the computer. Display the score and appropriate message to the screen according to the following rules of play:

If both players (computer and human) choose to make a deal to share the jackpot, the jackpot is split equally between both players and they both receive half the money. i.e. $50 each. The following message is displayed to the screen: Draw! Split pot!

If the human player chooses steal the jackpot and the computer player chooses to make a deal to share the jackpot, the human player receives all the money and the computer player who chose to make a deal receives none. The following message is displayed to the screen: You win! Jackpot!

If the computer player chooses steal the jackpot and the human player chooses to make a deal to share the jackpot, the computer player receives all the money and the human player who chose to make a deal receives none. The following message is displayed to the screen: You lose! You get nothing!

If both players (computer and human) choose to steal the jackpot, they both receive nothing. The following message is displayed to the screen: Too greedy! You get nothing!

Display the score and appropriate message to the screen as seen below (the ‘S:’ stands for score):

Sample output 1:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Steal

S: 0 | 0

Too greedy!

You get nothing!

Sample output 2:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Deal

S: 100 | 0

You win!

Jackpot!

Sample output 3:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Deal

S: 50 | 50

Draw!

Split pot!

Sample output 4:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Steal

S: 0 | 100

You lose!

You get nothing!

Don’t worry too much about formatting the output at this stage, just make sure that the values are correct. 9 of 36

Stage 4

Now… it’s time to allow the player to play more than one game. Let’s add a loop which loops until the user either enters 'q' (to quit the game) or the 10 game limit is reached. Think about where this code should go – what needs to be repeated, etc. Don’t worry too much about formatting the output at this stage, just make sure that the values are correct.

Sample output 1:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Deal

S: 100 | 0

You win!

Jackpot!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Deal

S: 50 | 50

Draw!

Split pot!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Steal

S: 0 | 0

Too greedy!

You get nothing!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Deal

S: 50 | 50

Draw!

Split pot!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Steal

S: 0 | 0

Too greedy!

You get nothing!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Steal

S: 0 | 100

You lose!

You get nothing!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s 10 of 36

C: Steal | Steal

S: 0 | 0

Too greedy!

You get nothing!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Deal

S: 50 | 50

Draw!

Split pot!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Deal

S: 100 | 0

You win!

Jackpot!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Steal

S: 0 | 100

You lose!

You get nothing!

Game limit reached.

Sample output 2:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Deal

S: 100 | 0

You win!

Jackpot!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Deal

S: 50 | 50

Draw!

Split pot!

Jackpot: 100

Steal, Deal or Quit [s|d|q]? q

Stage 5

Modify your code to keep a cumulative total of the winnings of both the player and the computer player.

If both players (computer and human) choose to make a deal to share the jackpot, the jackpot is split equally between both players and they both receive half the money. The split jackpot amount (i.e. $50 each) is added to a cumulative total for both players.

11 of 36

If the human player chooses steal the jackpot and the computer player chooses to make a deal to share the jackpot, the human player receives all the money and the computer player who chose to make a deal receives none. The jackpot amount is added to the cumulative total for the human player.

If the computer player chooses steal the jackpot and the human player chooses to make a deal to share the jackpot, the computer player receives all the money and the human player who chose to make a deal receives none. The jackpot amount is added to the cumulative total for the computer player.

If both players (computer and human) choose to steal the jackpot, they both receive nothing. The cumulative total remains unchanged.

Display the results to the screen as seen below:

Sample output:

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Steal

S: 0 | 0

Too greedy!

You get nothing!

You: 0 | Computer: 0

Jackpot: 100

Steal, Deal or Quit [s|d|q]? s

C: Steal | Deal

S: 100 | 0

You win!

Jackpot!

You: 100 | Computer: 0

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Deal

S: 50 | 50

Draw!

Split pot!

You: 150 | Computer: 50

Jackpot: 100

Steal, Deal or Quit [s|d|q]? d

C: Deal | Steal

S: 0 | 100

You lose!

You get nothing!

You: 150 | Computer: 150

Jackpot: 100

Steal, Deal or Quit [s|d|q]? q 12 of 36

Stage 6

Add code to keep track of how many games were won, lost and drawn. Display this to the screen (as seen in the sample output).

Stage 7

Add code to validate all user input:

Would you like to play Steal or Deal [y|n]?

Sample output:

Would you like to play Steal or Deal [y|n]? t

Please enter either 'y' or 'n'.

Would you like to play Steal or Deal [y|n]? y

Steal, Deal or Quit [s|d|q]?

Sample output:

Steal, Deal or Quit [s|d|q]? e

Please enter either 's', 'd' or 'q'

Steal, Deal or Quit [s|d|q]? q

Which strategy for the computer [1,2,3]? (you can do this in stage 8). You are not required to check for the user entering a character/string instead of numbers in this case.

Sample output:

Which strategy for the computer [1,2,3]? 5

Please enter either 1, 2, or 3.

Which strategy for the computer [1,2,3]? 3

Stage 8

Add code to prompt for and read the strategy in which the computer player should play with. The computer player can be set to always steal, always deal or to randomly choose between the two (deal or steal).

Stage 9

Add code to ensure that the user is not choosing to steal the jackpot three times in a row. If the human player chooses to steal the jackpot twice in a row the following warning message is displayed to the screen: Careful… one more steal and you lose it all! If the human player chooses to steal the jackpot three times in a row, the following message is displayed to the screen: I told you so… you lose all of your winnings! In this case, the cumulative total of the human player is set back to zero. 13 of 36

Stage 10

Modify your code so that the players’ choices and scores are displayed to the screen in the format seen below (and as seen in the sample output). Make sure you read the section at the end of this assignment for help with this!

Sample Output 1 (result of game):

You | Computer

--------------------------

C: Deal | Steal

S: 0 | 0

Sample Output 2 (cumulative totals):

You | Computer

---------- Total ----------

0 | 0

Stage 11

Add code to keep track of game statistics. That is, how many times each player chose to steal or deal. Display this information to the screen (as seen in the sample output). You MUST use a list in order to store this information. You MUST also use any nested loops in order to display this information to the screen.

Define two lists in order to store choice stats and corresponding titles:

choice_titles = ['Steal','Deal', 'Comp steal', 'Comp deal']

choice_counts = [0, 0, 0, 0]

Algorithm to display the frequency to the screen:

index = 0

WHILE index < length of choice_counts list

display choice title to the screen (choice_titles[index])

FOR number in 0 to choice_counts[index]

display a star to the screen

increment index by 1

Stage 12

Finally, check the sample output (see section titled ‘Sample Output – Part I’ towards the end of this document) and if necessary, modify your code so that:

The output produced by your program EXACTLY adheres to the sample output provided.

Your program behaves as described in these specs and the sample output provided.

14 of 36

PART II SPECIFICATION - ENCRYPTION

A simple way to encrypt data is attributed to Julius Caesar, the Roman Emperor. (If you are interested, you may like to read the following… http://en.wikipedia.org/wiki/Caesar_cipher). This method takes each character in a message and replaces it with one which is a certain distance (offset) along the alphabet from it (moving right).

For example:

1 2 3 4 5 6 7 8 9 . . . . .

A B C D E F G H I J K L M . . . +3 à

A B C D E F G H I J . . .

If the offset is 3 then A becomes D, B becomes E, C becomes F etc. In this case the word DIG encrypts to GLJ. In order to decrypt the word/string, simply offset by the same amount moving in the opposite direction (i.e. moving left).

Instead of restricting the cipher to the alphabetic characters only, we will use all of the printable ASCII characters. That is, all the characters from ASCII 32 (Space) to ASCII 126 (~).

Write a menu driven program called yourEmailId_encryptor.py that will allow the user to enter commands and process these commands until the quit command is entered.

The following commands should be allowed:

1. Encrypt string:

Prompt for and read (from the keyboard) a string to be encrypted. The program should then ask for the offset value (limited to a range of 1 to 94 inclusive). Display the encrypted string to the screen.

2. Decrypt string:

Decrypt an encrypted message by prompting for and reading (from the keyboard) a string to be decrypted. The program should then ask for the offset value (limited to the range of 1 to 94 inclusive). Display the decrypted string to the screen.

3. Brute force decryption:

If the offset is not known, we can implement a brute force decryption algorithm that tries all of the 94 possible Caesar offsets in order to decrypt the encrypted text. Prompt for and read the string to be decrypted and display all 94 possible decrypted strings to the screen. (If you are interested, you may like to read the following… http://en.wikipedia.org/wiki/Brute-force_attack).

4. Quit:

Quits the program displaying a goodbye message to the screen.

Please note:

Your program must work with the printable ASCII character set. That is, all the characters from ASCII 32 (Space) to ASCII 126 (~). When the offset points to a character beyond 126 it should wrap around to the beginning of the set.

For Example:

If the offset is 4 and the character is ‘}’ (ASCII 125) then it will encrypt to ASCII 129. This is beyond 126 so wrap back to the beginning by subtracting the total number of characters (95). This gives character 34. Similarly when decrypting, if the subtracted offset results in a number less than 32 then add 95 to the result. 15 of 36

STAGES (PART II)

It is recommended that you develop this part of the assignment in the suggested stages. Each stage is worth a portion of the marks. Many problems in later stages are due to errors in early stages. Make sure you have finished and thoroughly tested each stage before continuing.

The following stages of development are recommended:

Stage 1

Implement the interactive mode (prompt for and read menu commands). Set up a loop to obtain and process commands. Test to ensure that this is working correctly before moving onto stage 2. You need not perform any encryption/decryption of text at this point, you may simply display an appropriate message to the screen.

Sample output:

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 1

In command 1 - encrypt string

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 2

In command 2 - decrypt string

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 3

In command 3 - brute force

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 4

Goodbye.

Make sure the program runs correctly. Once you have that working, back up your program. Note: When developing software, you should always have fixed points in your development where you know your software is bug free and runs correctly. 17 of 36

Stage 2

Add code to implement command 1. Encrypt string.

Prompt for and read a string to be encrypted. Prompt for and read the offset value (limited to a range of 1 to 94 inclusive). Display the encrypted string to the screen. Make sure you read the section titled ‘Useful Built-in Python Functions’ located at the end of this document.

Please note:

Your program must work with the printable ASCII character set. That is, all the characters from ASCII 32 (Space) to ASCII 126 (~). When the offset points to a character beyond 126 it should wrap around to the beginning of the set.

For Example:

If the offset is 4 and the character is ‘}’ (ASCII 125) then it will encrypt to ASCII 129. This is beyond 126 so wrap back to the beginning by subtracting the total number of characters (95). This gives character 34. Similarly when decrypting, if the subtracted offset results in a number less than 32 then add 95 to the result.

Make sure you read the section at the end of this assignment for help with this!

Sample output:

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 1

Please enter string to encrypt: secret squirrel

Please enter offset value (1 to 94): 6

Encrypted string:

ykixkz&yw{oxxkr

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 4

Goodbye.

Stage 3

Add code to implement command 2. Decrypt string.

Prompt for and read a string to be decrypted. Prompt for and read the offset value (limited to a range of 1 to 94 inclusive). Display the decrypted string to the screen.

Please note:

Your program must work with the printable ASCII character set. That is, all the characters from ASCII 32 (Space) to ASCII 126 (~). When the offset points to a character beyond 126 it should wrap around to the beginning of the set.

For Example:

If the offset is 4 and the character is ‘}’ (ASCII 125) then it will encrypt to ASCII 129. This is beyond 126 so wrap back to the beginning by subtracting the total number of characters (95). This gives character 34. Similarly when decrypting, if the subtracted offset results in a number less than 32 then add 95 to the result. 18 of 36

Sample output:

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 2

Please enter string to decrypt: ykixkz&yw{oxxkr

Please enter offset value (1 to 94): 6

Decrypted string:

secret squirrel

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 4

Goodbye.

Stage 4

Add code to implement command 3. Brute force decryption.

Prompt for and read a string to be decrypted. Display all 94 possible decrypted strings to the screen.

Please note:

Your program must work with the printable ASCII character set. That is, all the characters from ASCII 32 (Space) to ASCII 126 (~). When the offset points to a character beyond 126 it should wrap around to the beginning of the set.

For Example:

If the offset is 4 and the character is ‘}’ (ASCII 125) then it will encrypt to ASCII 129. This is beyond 126 so wrap back to the beginning by subtracting the total number of characters (95). This gives character 34. Similarly when decrypting, if the subtracted offset results in a number less than 32 then add 95 to the result.

Sample output:

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 3

Please enter string to decrypt: ykixkz&yw{oxxkr

Offset: 1 = Decrypted string: xjhwjy%xvznwwjq

Offset: 2 = Decrypted string: wigvix$wuymvvip

Offset: 3 = Decrypted string: vhfuhw#vtxluuho

Offset: 4 = Decrypted string: ugetgv"uswkttgn

Offset: 5 = Decrypted string: tfdsfu!trvjssfm

Offset: 6 = Decrypted string: secret squirrel

Offset: 7 = Decrypted string: rdbqds~rpthqqdk 19 of 36

Offset: 8 = Decrypted string: qcapcr}qosgppcj

Offset: 9 = Decrypted string: pb`obq|pnrfoobi

Offset: 10 = Decrypted string: oa_nap{omqennah

:

: you get the idea… :)

:

Offset: 87 = Decrypted string: "sq!s#." $w!!sz

Offset: 88 = Decrypted string: !rp r"-!~#v ry

Offset: 89 = Decrypted string: qo~q!, }"u~~qx

Offset: 90 = Decrypted string: ~pn}p +~|!t}}pw

Offset: 91 = Decrypted string: }om|o~*}{ s||ov

Offset: 92 = Decrypted string: |nl{n})|z~r{{nu

Offset: 93 = Decrypted string: {mkzm|({y}qzzmt

Offset: 94 = Decrypted string: zljyl{'zx|pyyls

*** Menu ***

1. Encrypt string

2. Decrypt string

3. Brute force decryption

4. Quit

What would you like to do [1,2,3,4]? 4

Goodbye.

Stage 5

Add code to validate all user input. Hint: use a while loop to validate input.

Menu input should also be validated with an appropriate message being displayed if an incorrect menu command is entered by the user.

Stage 6

Finally, check the sample output (see section titled ‘Sample Output – Part II’ towards the end of this document) and if necessary, modify your code so that:

The output produced by your program EXACTLY adheres to the sample output provided.

Your program behaves as described in these specs and the sample output provided.

20 of 36


Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1829 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors