Skip to main content

Bulls and Cows in Python.

Project description

Bulls and Cows

Bulls and Cows - the classic paper-and-pencil logic game recreated in Python.

Want to improve your critical thinking skills? Play this game!

What is Bulls and Cows?

Bulls and Cows, popularised as the board game Mastermind, is a code-breaking logic-based game where you try to guess a number that has been randomly generated by the program. the randomly generated number contains exactly four digits between 0 and 9 with no repeats.

Read more about Bulls and Cows here: https://en.wikipedia.org/wiki/Bulls_and_Cows

Using the Module

First, you need to install the package.

pip install bullsandcows

Now in your project, import the package and create a new instance. We will be using game for this example, but you may use whatever name you want.

from bullsandcows import bullsandcows

game = bullsandcows()

This module has a feature that allows you to disable the anti-cheat (prevents the user from getting the random number until the game stops), which is automatically enabled. You can send a boolean variable to the bullsandcows instance which determines whether the anti-cheat should be enabled or not. The first method below disables it upon instantiation.

game = bullsandcows(anticheat=False)

The second method below disables it post-instantiation, for whatever reason.

game.anticheat = False

game.anticheat itself is a boolean value which you can use for other things such as displaying on the console. Once the anti-cheat is disabled, you may retrieve the random number as string using get_number() whenever you wish to.

Using get_number() without disabling the anti-cheat will raise an exception if the game is still in progress.

guess(value):

Gets and verifies a user's guess. Requires the user to parse a value, which must either be a four-digit number with no repeated digits such as 1234, or the following exit commands which are for forcibly stopping the game: 'exit', 'stop', 'halt', 'quit' (case-insensitive).

Function returns two values in order: number of bulls (integer), number of cows (integer). Sample usage:

value = input("Guess a number: ")
bulls, cows = game.guess(value)
print("Bulls:",bulls)
print("Cows:",cows)

get_time():

Gets the total time spent by the player and converts it to minutes and seconds.

Function returns two values in order: minutes (integer), seconds (integer; is reset to 0 every 60 seconds). Sample usage:

mins, secs = game.get_time()
print(f"Total time spent: {mins} min & {secs} sec.")

Other Getters:

get_attempts() - Returns the total number of attempts used; integer value.

get_stopped_bool() - Returns the game stopping indicator value; boolean value.

get_won_bool() - Returns the game winning indicator value; boolean value.

Gameplay

In the game, the player is asked to enter a four-digit number which is then compared to the randomly generated four-digit number; each individual digit entered by the player is compared to each digit within the randomly generated number. If a digit in the player's guess is in the randomly generated number and is in the same position in it as it was in their number, then it is scored as a 'bull'. If that same digit is in a different position, then it is marked as a 'cow'.

The objective of the game is to obviously find all four bulls in the shortest amount of time and using as few attempts as possible.

Example of a valid guess: 1234

Example of an invalid guess: 1244 (the digit 4 has been used twice when it can only be used once)

Another example of an invalid guess: 12345 (five digits instead of four)

Example Guess:

Randomly generated number: 1234

Your guess: 1324

Result: 2 bulls and 2 cows. (bulls: 1 and 4; cows: 2 and 3)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bullsandcows-1.4.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bullsandcows-1.4-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file bullsandcows-1.4.tar.gz.

File metadata

  • Download URL: bullsandcows-1.4.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.7

File hashes

Hashes for bullsandcows-1.4.tar.gz
Algorithm Hash digest
SHA256 62b9dc5970ad993791cf7193b7375f6926cd7fdefc1901c52f6260a9b66b60b3
MD5 ae69810927c196a6ce41a6184364f939
BLAKE2b-256 d7b205947b696bdec8f19eefd17dbf59beff26df48963a46c45358545ddb1544

See more details on using hashes here.

File details

Details for the file bullsandcows-1.4-py3-none-any.whl.

File metadata

  • Download URL: bullsandcows-1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.7

File hashes

Hashes for bullsandcows-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a62a20ee5fc5efddb3fec9cf423c411e239984355db1a2103e675ca71a902671
MD5 370f53bb0dd356df059ac2b100a5549e
BLAKE2b-256 61a5a7602a85be2f42db1e23398a26128603baaa71121746488a9b673e1f7d6f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page