Anda di halaman 1dari 8

http://rosettacode.org/wiki/Bulls_and_cows aqui se encuentra el cdigo del algoritmo en varios lenguajes ejemplo del juego http://www.mathsisfun.com/games/bulls-and-cows.html https://secure.wikimedia.

org/wikipedia/en/wiki/Bulls_and_cows

The algorithm that I wrote is as follows: 1. Player 'A' thinks of a number. 2. Player 'B' starts of with number 1234 as the first try and player 'A' responds with the number of bulls and cows in the number. (I haven't tried other numbers, but I guess it will give similar results) 3. Player 'B' tries to find out the set of solutions that can match each of the previous steps. 4. 'B' then finds out how many times each digit occurs in the list of possible solutions. These form the weights of the digits. 5. Using this, 'B' computes a weight for each possible solution as folows: If 'abcd' is a possible solution and the digits 'a','b','c','d' have weights 'p','q','r','s' respectively, then weight of 'abcd' is 'p'+'q'+'r'+'s', such that 'a','b','c','d' are distinct. If they are not distinct, then each digit is considered only once to compute the weight -> (This is the tweak I had to apply). 6. 'B' then finds the number with the highest weight and uses this as the next step. 7. Repeat Steps 3 - 6 until you get the solution. I ran this simulation for all the numbers from 1 - 9999 and got the following results: Algorithm 1 result sheet. It took me about 20 minutes to run the simulation in my system (an average of about 10 games per second). I am not sure if this is the best solution, but there are learnings from this: * It is possible for a computer to find out a solution in an average of 6 steps or less. * The maximum number of steps required to arrive at a solution is 10 or less. Please note that the algorithm was not tested (i.e. I am not sure if it works exactly as I have mentioned it here), but the answers it provides and the steps have been authenticated. Is there any better solution?

http://codereview.stackexchange.com/questions/8656/how-can-i-improve-this-implementationof-bulls-and-cows-algorithm aqui se encuuentra algunas ayportes para lograr el algoritmo

http://sourceforge.net/projects/fourdigits/files/4digits-0.3/ no se que es pero se ve prometedor ^^

#include<iostream> #include<vector> using namespace std; vector<int>number(4); vector<int>user_number(4); void initialize_vector() { number[0]=4; number[1]=8; number[2]=3; number[3]=1; } void get_number() { cout<<"Please enter a four digit number " <<"separated by a space\n"; int user_num=0; for(int i=0;i<user_number.size();++i){ cin>>user_num; user_number[i]=user_num; //Store user values in a vector } } int main() { initialize_vector(); int bull=0; int cow=0; int const reset=0; while(number!=user_number){ //Better way to express when to end loop get_number(); //Gets the user input bull=reset; //Avoids growing bull and cow indefinately cow=reset; for(int i=0;i<user_number.size();++i) //Go through the user number for(int j=0;j<number.size();++j) //Go through the program stored number if(user_number[i]==number[j]) if(i==j)bull+=1; //add one to "bull" else cow+=1; //add one to "cow"

cout<<"There were "<<bull<<" bull(s) and "<<cow<<" cow(s)\n"; } cout<<"Congratulations, you got it!\n"; return 0; }

http://www.enrico-franchi.org/2010/08/mastermindbulls-and-cows-breaker.html http://delphiforfun.org/programs/cows_bulls.htm ejecutable http://www.physicsforums.com/showthread.php?t=301897 pautas extra


www.jfwaf.com/Bulls%20and%20Cows.pdf

http://www.programmingforums.org/thread24199.html

// This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { unsigned seed = time(0); srand(seed); int guess[4]; int secretCode[] = {rand()%10,rand()%10,rand()%10,rand()%10}; int bull = 0; int cow = 0; cout << "\t\t\tHow to play Bulls and Cows:\n"; cout << " The game involves two players, a coder and a cracker."; cout << "The coder (a computer in\n this case)"; cout << "chooses the four-digit number with all different digits and keeps it\n secret. The cracker tries to"; cout << "guess the secret number. After a guess, the coder\n gives you information about number of digits"; cout << "which were correctly guessed but\n in the wrong place (they are called cows) and how many are"; cout << "both the right digit\n and in the correct place (they are called bulls)."; cout << "For example, If the coder\n chooses the secret number 0419 and the cracker guesses 6039, then the "; cout << "coder\n scores This as 1 cow, since 0 is a digit in the secret number thats in the\n wrong place,"; cout << "and 1 bull 9 is in the right place." << endl;

do { secretCode[4]; break; } while (secretCode[0] == secretCode[1] || secretCode[0] == secretCode[2] || secretCode[0] == secretCode[3] || secretCode[0] == secretCode[0] || secretCode[1] == secretCode[2] || secretCode[1] == secretCode[3] || secretCode[1] == secretCode[0] || secretCode[1] == secretCode[1] ||

secretCode[2] == secretCode[3] || secretCode[2] == secretCode[0] || secretCode[2] == secretCode[1] || secretCode[2] == secretCode[2] || secretCode[3] == secretCode[0] || secretCode[3] == secretCode[1] || secretCode[3] == secretCode[2] || secretCode[3] == secretCode[3] ); for(int i = 0; i < 4; ++i) { cout << secretCode[i]; }

cout << "\n\n\tGuess 4 digets seperated by one space between each diget." << endl; do { bull =0; cow = 0; cin >> guess[0]; cin >> guess[1]; cin >> guess[2]; cin >> guess[3]; if (guess[0] == secretCode[0]) ++bull; if (guess[0] == secretCode[1] || guess[0] == secretCode[2] secretCode[3]) ++cow; if (guess[1] == secretCode[1]) ++bull; if (guess[1] == secretCode[2] || guess[1] == secretCode[3] secretCode[0]) ++cow; if (guess[2] == secretCode[2]) ++bull; if (guess[2] == secretCode[3] || guess[2] == secretCode[0] secretCode[1]) ++cow; if (guess[3] == secretCode[3]) ++bull; if (guess[3] == secretCode[0] || guess[3] == secretCode[1] secretCode[2]) ++cow; if (bull == 4) break; else { cout << bull << " is your # of bulls.\n" << cow << " is cows.\n\n"; cout << "\nNOW TRY AGAIN\n"; } } while (bull!=4); cout << "\n\n\n\nYOU'RE A WINNER!!!\n\n\n"; return 0; }

|| guess[0] ==

|| guess[1] ==

|| guess[2] ==

|| guess[3] ==

your # of

Codigo en c++ http://www.cpp-home.com/forum/viewtopic.php?f=6&t=17475 Otro cdigo en c++ http://www.cpp-home.com/forum/viewtopic.php?f=6&t=17470

http://fourdigits.sourceforge.net/
#include "stdafx.h" #include <iostream> #include <cstdlib> #include <ctime> using namespace std;

int _tmain(int argc, _TCHAR* argv[]) { int bull=0,cow=0, guess,x ; srand((unsigned)time(0)); int random_number = rand(); for (int rnum=0;rnum<4; rnum++) { random_number= (rand() % 10+ 0); } cout << random_number << endl; cout << "Enter your number" << endl; cin >> guess >> endl; if ( guess[0]==random_number[0];guess >= 0; guess++); do (x = 1; x<5; x++); bull++; else if cow++; system("PAUSE"); return 0; }

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1720&lngWId=7 codigo completo en java ^

http://www.cplusplus.com/forum/beginner/15811/ complete en c++ *w*


/* Name: cow_bull.cpp Purpose: to create a working example of a console cow & bull game. */ #include<iostream> #include<stdexcept> #include<sstream>

#include<ctime> #include<cstdlib> using namespace std; //======================================================================= ====== struct Cow_bull{ Cow_bull(string g,string a) :guess(g),answer(a),cow(0),bull(0){ calculate_bull(); calculate_cows(); } void set_guess(string g){ guess=g; bull=0; //reset to zero for each guess cow=0; //reset to zero for each guess g2=""; //resetting string for each guess a2=""; //resetting string for each guess calculate_bull(); calculate_cows(); } int get_cow() const{return cow;} int get_bull() const{return bull;} private: string guess; string answer; string g2; string a2; int cow; int bull; void calculate_bull(){ for(unsigned i =0; i<guess.size();++i){ if(guess[i]==answer[i]){++bull;} else{ g2=g2+guess[i]; //saving this wrong guess to check for cows a2=a2+answer[i]; //saving this answer to check for cows } } } void calculate_cows(){ for(unsigned i =0; i<g2.size();++i){ for(unsigned ii =0; ii<g2.size();++ii){ if(g2[i]==a2[ii]){ ++cow; a2[ii]=0; /*ensuring that I do not check this answer break; } } } } };

right twice.

//======================================================================= ====== ostream& operator<<(ostream& os, Cow_bull& cb){ return os<<"you had "<<cb.get_bull()<<" bulls and " <<cb.get_cow()<<" cows."; } //======================================================================= ====== string create_random_answer(){ srand(time(0)); stringstream reply; for(int i=0; i<4;++i){ reply<<int(rand() % 10); } return reply.str(); } //======================================================================= ====== bool just_number(string s){ for(unsigned i=0; i<s.size();++i){ if(!isdigit(s[i]))return false; } return true; } //======================================================================= ====== int main()try{ cout<<"\n\nTry to guess a four number sequence. If you guess the right number\n"; cout<<"in the right location you will be awarded a bull. If you guess\n"; cout<<"the right number in the wrong location you receive a cow. The goal is\n"; cout<<"to get four bulls.\n\n"; string guess; string answer=create_random_answer(); Cow_bull cb(guess,answer); while(cb.get_bull()!=4){ do{ cout<<"\nEnter a four number string e.g. 1234 :"; cin>>guess; }while(guess.size()!=4 || !just_number(guess)); cb.set_guess(guess); cout<<cb<<'\n'; } return 0; } catch(exception& e){ cerr<<e.what()<<endl; return 1;

} catch(...){ return 2; }

Anda mungkin juga menyukai