1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
#include <string>
using namespace std;
typedef int int32;
int main()
{
string MyHiddenWord = "job";
string Guess = "bot";
struct {
int Bulls;
int Cows;
} BullCowCount = {
0, 0};
//loop through all letters in the guess
int32 HiddenWordLenght = MyHiddenWord.length();
for (int32 i = 0; i < HiddenWordLenght; i++) {
//compare letters against the hidden word
for (int32 j = 0; j < HiddenWordLenght; j++) {
//if they match then
cout << "DEBUG: i=" << i
<< ", j=" << j
<< ", Guess[i]=" << Guess[i]
<< ", HW[i]=" << MyHiddenWord[i] << endl;
if (Guess[i] == MyHiddenWord[i]) {
//if theyre in the same place
if (i == j) {
BullCowCount.Bulls++; //increment bu lls
} else {
BullCowCount.Cows++; //incriment cows
}
}
}
}
cout << "Bulls=" << BullCowCount.Bulls
<< ", Cows=" << BullCowCount.Cows << endl;
return 0;
}
$ g++ -std=c++11 main.cpp
$ ./a.out
DEBUG: i=0, j=0, Guess[i]=b, HW[i]=j
DEBUG: i=0, j=1, Guess[i]=b, HW[i]=j
DEBUG: i=0, j=2, Guess[i]=b, HW[i]=j
DEBUG: i=1, j=0, Guess[i]=o, HW[i]=o
DEBUG: i=1, j=1, Guess[i]=o, HW[i]=o
DEBUG: i=1, j=2, Guess[i]=o, HW[i]=o
DEBUG: i=2, j=0, Guess[i]=t, HW[i]=b
DEBUG: i=2, j=1, Guess[i]=t, HW[i]=b
DEBUG: i=2, j=2, Guess[i]=t, HW[i]=b
Bulls=1, Cows=2
|