CSc 110 - PA 4 - Bulls and Cows

In this programming assignment, you will be implementing a 2-player code-breaking console game called Bulls and Cows. This game is related to the code breaking board game Mastermind. Mastermind was inspired by Bulls and Cows. You can take a look at the Bulls and Cows Wikipedia page to get an idea of how the standard game works. The version that you should implement in this PA will have a some differences from the standard Bulls and Cows game.

The game will be played on the console, and players will interact with the game by entering console input. You should call your program bulls_and_cows.py.

Both Mastermind and Bulls and Cows have a similar core gameplay concept. One or both players chooses a particular sequence of numbers, or letters, or colors, which is referred to as the code. Then, the other player provides a sequence of guesses of the code. After each guess, the player who created the code provides a form of feedback to help the other player determine how close they are to guessing the correct code.

Bulls and Cows Rules and Gameplay

Initially, each player will select a nickname to be used during the game. Programmatically, you can request the nicknames with the input() function. Once the nickname has been selected, each player is asked to select a code. The code must be:

You must check that the codes that the players enter meet these requirements. If any of them are not met, then your program must print out a message and then exit. If you forgot how to exit a program, check the slides. See the examples later to determine what the message should be.

After all of these values have been entered, the turns of the game can begin. Each player will take turns guessing the other player’s code. The guesses will be entered on the command line. After each guess is made, the program will report two pieces of information related to the guess.

For example, let’s say that player A provided the code abc. Then, player R gave the guess acr. In this guess, there is 1 letter in the correct position. There is also one letter that is in the code, but not in the correct position. Thus, player R should be told that their guess resulted in 1 bull and 1 cow. This information can be used to inform future guesses, to eventually arrive at the correct answer.

The first player to guess the other player’s code is the winner. Once a player has won, this will be reported and the game will exit.

Even / Odd Turns

One player (player 1) has a guessing turn first. This could give player 2 a disadvantage. If player 1 guess player 2’s code, player 1 had one more guess than player 2 did. In this version of the game, you don’t need to do anything special to make this “fair”. If player 1 guesses the answer first, then player 1 wins.

Implementation

You should only use features that have been covered in class and in the readings. Particularly, you may not use lists or for-loops to implement this. We have not yet reached these topics, but we will later in the semester.

You will probably need to use a number of if statements for checking the user inputs. You should use a while loop to handle the repetition of turns. You may also use a while loop to could the bulls and cows.

Example Runs

Below are the outputs of several program runs. Player 1 selected the nickname “Art” and player 2 “Red”. Each player entered their code, and then one guessed two’s code correct on the very first turn, causing him to win and the game to end.

-----------------------------------------
------- WELCOME TO BULLS AND COWS -------
-----------------------------------------
Player 1, enter your username:
Art
Player 2, enter your username:
Red
Art, enter your code:
abc
Red, enter your code:
was
Art, enter guess:
was
Art wins!

Let’s look at a longer game run.

-----------------------------------------
------- WELCOME TO BULLS AND COWS -------
-----------------------------------------
Player 1, enter your username:
Art
Player 2, enter your username:
Red
Art, enter your code:
and
Red, enter your code:
the
Art, enter guess:
tri
  * bulls: 1
  * cows:  0
Red, enter guess:
nda
  * bulls: 0
  * cows:  3
Art, enter guess:
thi
  * bulls: 2
  * cows:  0
Red, enter guess:
and
Red wins!

In this example, each player got Red guesses before the game ended. Notice how after each incorrect guess, the game replied with the bulls and cows counts for the guess.

After the first turn (Art), he could know that one of the three letters that he guessed was in the exact correct position. Thus, one bull and zero cows. On Art’s second guess, two out of three of the letters in the guess were correct and in the correct position. Thus, two bulls and zero cows.

Red’s first guess was had the three letters in the code, but in the wrong positions. Thus, zero bulls and three cows. Red’s second guess was the correct code. After announcing that Red won, the program ended.

Below are a few examples of the game rejecting a code entered by art of the players.

-----------------------------------------
------- WELCOME TO BULLS AND COWS -------
-----------------------------------------
Player 1, enter your username:
Art
Player 2, enter your username:
Red
Art, enter your code:
wonderful
Art, that code is not valid. Exiting.
-----------------------------------------
------- WELCOME TO BULLS AND COWS -------
-----------------------------------------
Player 1, enter your username:
Art
Player 2, enter your username:
Red
Art, enter your code:
abc
Red, enter your code:
tr3
Red, that code is not valid. Exiting.

There will be more test cases on Gradescope.

Due Date

This PA is due on Friday, September 14th at 5pm. Turn it in on Gradescope. You should make sure your program passes as many test cases as possible. You can submit it without all of the cases passing, but that is not preferable. Also, remember to follow the code style guidelines.