# Maximum PC

 It is currently Thu Aug 28, 2014 3:49 am

 All times are UTC - 8 hours

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Bulls and Cows GamePosted: Tue Nov 23, 2010 5:58 pm
 Bitchin' Fast 3D Z8000*

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
I was browsing through some of the problems at Rosetta Code earlier today and came across a fun game called Cows and Bulls. The premise of the game is straight-forward: 1) You make a guess using a 4 digit number with unique digits; 2) You're told the number of bulls (correct digits in the correct position) and the number of cows (correct digits in the wrong position); 3) Rinse and repeat until you guess the number (ie 4 bulls). I have the funny feeling this is similar to the Master Mind game that we were discussing in another thread, but I haven't verified it. The game sounded like fun and someone had already implemented it in Common Lisp, so I decided to give it a try... I've included some output below.

One of the best things about Rosetta Code is that you're bound to learn something new and interesting if you examine the some of the solutions. This includes languages in which you're already proficient. For example, the Java implementation of this game uses an Exception called InputMismatchException in the utils package. I've been programming in Java for ~8 years now and this may be the first time that I've seen someone use that Exception. Of course, knowledge of a programming language is only half the battle in computation -- memorizing a English->Spanish language dictionary doesn't make you fluent in Spanish, right?! The Java solution, for example, is quite a kludge compared to the CL implementation -- darn street programmers! =)

Anyways, I would encourage everyone to take a look at one or more of the implementations and POST something here if you see anything clever or kludgie or whatever. If the solution in your favorite language is missing (or badly written), I would also encourage you to implement it or fix it (but you may want to post your code here first to get some feedback). If you come across another one of the problems that you find interesting and would like to discuss, then by all means start a new thread.

For the more adventurous, there is actually a related task on Rosetta Code where you implement the Bulls and Cows player. I was thinking about implementing it in Common Lisp and Prolog (probably not this week though... or maybe even this month).

Code:
CL-USER> (play-game)
Guess a 4-digit number: 1234

Score: 1 cows, 1 bulls.
Guess a 4-digit number: 1243

Score: 0 cows, 2 bulls.
Guess a 4-digit number: 5643

Score: 0 cows, 1 bulls.
Guess a 4-digit number: 3241

Score: 1 cows, 1 bulls.
Guess a 4-digit number: 1342

Score: 2 cows, 0 bulls.
Guess a 4-digit number: 5246

Score: 0 cows, 1 bulls.
Guess a 4-digit number: 5236

Score: 1 cows, 1 bulls.
Guess a 4-digit number: 5263

Score: 0 cows, 2 bulls.
Guess a 4-digit number: 7283

Score: 0 cows, 3 bulls.
Guess a 4-digit number: 7293

Score: 1 cows, 2 bulls.
Guess a 4-digit number: 9283

Correct, you win!
NIL
CL-USER>

Yeah, I could have probably done a better job at 'guessing' the correct number.

Top

 Post subject: Why I don't like the Java implementation of Bulls and CowsPosted: Tue Nov 23, 2010 9:14 pm
 Bitchin' Fast 3D Z8000*

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
[Note: notice the subject!]

Here are a couple of the kludges that I mentioned with the Java code:
Code:
while (hasDupes (target = (gen.nextInt(9000) + 1000)));

It might not seem like a big deal until you consider that 2/3rds of the numbers from 1000 to 9999 contain duplicates!
(valid 3024 vs invalid 5976)

The Java implementation is sort of odd. Whoever wrote the code knew about the random InputMismatchException (or stumbled upon it), yet instead of separating the number into digits and using a set, he/she wrote a very C like function to check for duplicates instead.

Does anyone see a problem with this code?
Code:
try {
guess = input.nextInt();
if (hasDupes(guess) || guess < 1000)
continue;
} catch (InputMismatchException e) {
continue;
}

Besides not being a nice programmer and informing the user when they've entered an invalid number, he/she doesn't check to see if the input number is greater than 10000 -- for example, 1234567890 would get by this code.

I consider the code below to be another kludge. Why caste to a string?! If you're not going to use a set (and consequently write a hasDups method), at least use an int[4] to hold the digits. You might be thinking "Well, then you'd have to write some iteration code to check for cows", which is true, but you've already had to write the hasDups method -- what's the problem with writing a six line method for this?! It's a much cleaner solution compared to pulling the number apart in hasDups then casting to a string to count the bulls and cows.

Code:
guesses++;
String guessStr = guess + "";
for (int i = 0; i < 4; i++) {
if (guessStr.charAt(i) == targetStr.charAt(i)) {
bulls++;
} else if (targetStr.contains(guessStr.charAt(i)+"")) {
cows++;
}
}

Yeah, I'm going to rewrite this code and update Rosetta Code. I can't let Java look this bad! =)

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 2 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets