Most of us would have had the experience of playing wordle, a web-based game where we need to guess a 5 — letter word under six tries and the computer would tell us if a particular letter is correct and if it is in the right position or not.
The game is derived from the old pen-paper based game called cows and bulls where a person would think of a 4-digit number and the others have to guess the number. If a digit of the number is correct and is in the same position as the original number it is termed as a bull and the right digit in a different position is termed as a cow. A modified version of this game, is guessing the right word following the same set of rules. In addition to this, each guess must be a meaningful word and the letters should not occur more than once.
There are many web and mobile-based applications where you can play the game in different forms but, if there’s something more fun than playing the game, it is actually building it from scratch. Attempting to build this game is also a nice exercise for people who are new to programming and attempting to learn a programming language. In this post, we’ll be building cows and bulls in python. Let’s get started with coding it then.
The word that needs to be guessed is going to be generated randomly from a text file called words.txt, which contains English words of different lengths. So, we would need to use the random module to choose a word from the list. Then, fetch the words from the file and store it as a list and pick a random word from the list using random.choice().
import random
wordlist = open("words.txt", "r")
words = wordlist.read().splitlines()
final_word=random.choice(words)
Note: The wordlist does not contain words which have repeating characters and hence does not need to be checked for repetition.
After picking a word, we need to inform the player about the number of characters in the word and then start getting their input. Once the user enters a word, we need to check if it has the same length as the original word and if it has any repeating characters. For this, let us define two functions checkLength() and checkRepetition() which would return Boolean values, true or false.
print('The word contains ',len(final_word),'letters')
def checkLength(word):
if len(word)==len(final_word):
return True
else:
return False
def checkRepetition(word):
flag = False
for i in word:
count = word.count(i)
if count>1:
flag=True
break
return flag
The checkLength() function is fairly straightforward as it compares the user input to the original word and returns true if the lengths are the same and false if the lengths are different. The flag variable is used to determine if a repetition of characters exist in the user input or not. Hence, we iterate through the characters and check the count of each character. If the count exceeds one, then the flag would be set to True and it would break from the loop to continue to the return statement.
Now that we have the two functions ready, we can proceed to make use of them to continuously prompt the user for the input until they guess the word. We aren’t going to place a limit on the number attempts in this game but, you can try it out if you want.
next=True
while next:
guess=input('Enter your guess: ')
if checkLength(guess):
if not(checkRepetition(guess)):
cows=0
bulls=0
for j in range(len(guess)):
if guess[j]==final_word[j]:
bulls+=1
elif guess[j] in final_word:
cows+=1
print('Cows=',cows,'Bulls=',bulls,'\n')
if guess==final_word:
print('Good job!!\n')
next=False
else:
print("Repeating Letters!!\n")
else:
print("Incorrect Length!!\n")
Let’s break down the above code now. The next variable is used to control the while loop and is set to false only when the exact word is found terminating the while loop. Then, we get the user input, store it in the variable guess and call the two functions checkLength() and checkRepetition(). If the conditions are met, we proceed with counting the number of cows and bulls for that particular iteration. Once every letter in the input string is checked, display the count of cows and bulls. In the situation that any one of the condition fails, print the reason and prompt for the input again.
This is just a basic version of the game and you could modify it as you want to involve numbers, checking for a valid English word, adding a GUI and much more. If you are looking to start building coding projects and are in the initial stages of your programming journey, then this is the right project to start with as it involves basic concepts in programming like loops, decision statements etc. and will help build your understanding in fundamental concepts of programming.