LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2015, 10:42 PM   #1
lckwswws
LQ Newbie
 
Registered: Jan 2015
Posts: 16

Rep: Reputation: Disabled
Bulls and cows, again?


It's a bulls and cows games. Many of you may have heard of it, if not, here is an example:
A two-player number-guessing game called “Bulls and Cows”, to be
played between a the computer and a person.1 The computer thinks of a secret number sequence, consisting of four distinct numbers, each in the range {0, 1, . . . , 9}. The human opponent has to determine these four numbers, in their proper sequence, by making repeated guesses. A guess of a number in its proper slot is called a “bull”, whereas a guess in a number that’s not in its proper slot is called a “cow”. For example, if the secret number sequence is 3, 1, 4, 5 and the human opponent guesses 3, 2, 5, 4, then the computer would output
Bulls: 1, Cows: 2.

Here's my code:
#include <bjarne/std_lib_facilities.h>

void offer_help()
{
cout << "The program will generate a secret number sequence, consisting of four distinct numbers, each in the range 0 to 9. You have to determine these four numbers, in their proper sequences by making repeated guesses. A guess of a number in its proper slot is called a 'bull', whereas a guess in a number that is not in its proper slot is called a 'cow'. You can input negative numbers to give up.\n";
}

int main(){
cout << "Need help (0/1)? ";
int help;
cin >> help;
if (help==1){
offer_help();
}
if (help!=0 && help!=1) error("Please enter 0 for no or 1 for yes.\n");
for (;
{
vector<int> answer ={1, 2, 3, 4};
vector<int> guess;
int bulls=0;
int cows=0;
for (int times=0; times<4; times++){
int g;
cin>>g;
guess.push_back(g);}
if (guess[0]<0 || guess[1] <0 || guess[2] <0 || guess[3] <0) error("Negative number detected, byebye.\n");
if (guess[0]>9 || guess[1]>9 || guess[2]>9 || guess[3]>9 || (!cin)) error("Please enter intergers from 0 to 9.\n");
if (guess[0]==guess[1] || guess[0]==guess[2] || guess[0]==guess[3] || guess[1]==guess[2] || guess[1]==guess[3] || guess[2]==guess[3]) error("Please enter four different numbers.\n");
if (guess[0]==answer[0])
bulls++;
else if (guess[0] == answer[1] || answer[2] || answer[3])
cows++;
if (guess[1]==answer[1])
bulls++;
else if (guess[1] == answer[0] || answer[2] || answer[3])
cows++;
if (guess[2]==answer[2])
bulls++;
else if (guess[2] == answer[0] || answer[1] || answer[3])
cows++;
if (guess[3]==answer[3])
bulls++;
else if (guess[3] == answer[0] || answer[1] || answer[2])
cows++;
if (bulls<4)
cout << "There are " <<bulls<< " bulls, " <<cows<< " cows. Try again.\n";
else if (bulls==4)
cout << "Congratulations! Let's do it one more time! (Input negative numbers when you want to quit.\n";
}
}

Here's my 1st problem(I might have more after I finish this step):
How can I generate 4 random numbers? I'm now using four fixed numbers so that I can do the rest of the code(this exercise is based on Stroustrup's PPP chapter 1-5, which doesnt seem to have ran() or whatever.)
 
Old 03-19-2015, 02:51 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
rand?
http://www.cplusplus.com/reference/cstdlib/rand/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a Cows and Bulls algorithm cdog Programming 2 11-23-2011 09:21 PM
Things not to buy second hand: Mostly BULLS**T smeezekitty General 25 06-14-2010 12:48 PM
Three Bulls Jaqui General 2 03-03-2006 03:25 PM
Why don't we get insects the size of cows? dj_relentless General 15 08-09-2002 07:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration