Friday, June 08, 2007

Cows and Bulls .... !!

You never know what would make you nostalgic. During the week, I experienced one instance of NOSTALGIA when I drove my friend's scooter for a while. Memories of Wadias came storming back when I used to have immense pride in going to college on my Dad's Bajaj Super every Saturday. The pride originated from the fact that on other days I had to be content in driving my sister's Sunny which isnt the most "pridey" bike you can ever ride. But after reminiscing about this piece of History, I thought I wont feel nostalgic for quite some time but I was pleasantly mistaken. After a hectic schedule at office for over 5 months, I found myself idle. Coinciding with my leisure was another interesting occurence. My wife found a way to be online on Yahoo which hadn't happened for the last 3 years. Since she was also idle, we decided to play a game. The game goes by the weird name of COWS AND BULLS.

I remember playing this game in all the periods (since I am talking about School, I am calling it periods instead of lectures) in School that were either boring or I was occupying one of the last 2 benches. The primary pre requisite of the game is that you should have an equally enthusiastic opponent. Here are the rules.

Its predominantly a 2 player game though nothing stops you from making it 1 vs n. Lets assume that its a 1 vs 1 game. Lets call them Player 1 and Player 2. Now Player 1 thinks of a 4 digit number. There are some constraints on this number. Firstly, it should not start with a zero. Secondly, no digit in the number should repeat.

Now, Player 2 starts guessing the number. Suppose Player 1 thought of 2758 and Player 2 guessed 1786 in his first attempt. Player 1 would compare the guessed number (1786) with his own number (2758). Player 1 will find that 7 and 8 are the digits that are present in his number (2758) whereas 1 and 6 aren't. Now he will compare the positions of 7 and 8 in his number. 7 is the second digit in the guessed number as well as his own number. All such digits that are present and also in the correct position are called BULLS. But 8 is not at the correct position since it is the 3rd digit in the guessed number as 4th in the actual number thought of by Player 1. All such digits that are present but at the wrong position are called COWS.

So Player 1's response to the guess of 1786 by Player 2 would be 1 BULL and 1 COW. Player 2 will then know that out of 1786, two digits are correct and 2 are wrong. Out of the 2 correct, 1 is at the correct position and 1 is at the wrong position. Of course, Player 1 doesnt mention which 2 digits are BULLS AND COWS. Player 2 takes note of this and proceeds with his second guess and so on until Player 2 guesses the number correctly. Both players keep track of the number of attempts to guess the number.

Now, Player 2 thinks of a number and Player 1 guesses. If Player 1 guesses the number in lesser attempts as compared to Player 2, Player 1 wins and vice-versa.

I find it tough to imagine this as a game since it can be an extremely challenging exercise to hone your Logical Reasoning skills.

Its interesting thinking that I spent 3 years (Grade 8 to Grade 10) playing this game in school but only hit upon it 9 years later.

Happy Playing .... !!

Cheers .. !!

3 comments:

Prameela said...

Interesting game......wonder howcome i have never herad of it :)

Pappul said...

It aint famous ... i dnt think anyone outside Bishop's knows of this game ro atleast I haven't come across any such person. This doesnt mean I am boasting about my school ... ;)

Unknown said...

This JAVA program written by Pratik himself can help you avoid mistakes while calculating Bulls and Cows.
Unfortunately I used to do a lot of mistakes and hence Pratik used to loose interest in the game. That was the very reason that he wrote this code for me :D

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class CowsAndBulls
{

/**
* @param args
*/
public static void main(String[] args) throws IOException
{
String number = args[0];
do
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the guess (Enter -1 to Exit): ");
String guess = reader.readLine();
if (String.Equals(guess, "-1"))
{
System.out.println("Thanks!");
System.exit(0);
}
int bulls = 0, cows = 0;
for(int i=0;i<4;i++)
{
int index = number.indexOf(guess.charAt(i));
if(index > -1)
{
if(index == i)
{
bulls++;
} else
{
cows++;
}
}
}
System.out.print(bulls + "B " + cows + "C\n");
} while (true);
}
}