gpt4 book ai didi

C++ Mastermind - 一种情况不起作用

转载 作者:行者123 更新时间:2023-11-28 02:57:59 25 4
gpt4 key购买 nike

<分区>

有人可以帮我弄清楚为什么这对以下情况不起作用吗?

服务器代码:1441

我猜:1114

输出:你的猜测:11141 个黑色命中和 3 个白色命中。

这应该是 1 黑色和 2 白色,对吧?


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <string>
#include <sstream>
#include <map>

typedef enum {RED,GREEN,BLUE,YELLOW,ORANGE} color;

std::vector<color> GetInputAsColorMap(std::string input)
{
std::vector<color> theCode;

std::istringstream iss;
iss.str (input);

int theNumber;
iss >> theNumber;

for (int i = 0 ; i < 4 ; i++)
{
theCode.push_back(static_cast<color>(theNumber % 10));
theNumber /= 10;
}
std::reverse(theCode.begin(), theCode.end());
return theCode;
}

int StartMasterMind(std::istream& userInput)
{
std::cout << "Welcome to ... M A S T E R M I N D.\n";

// there are 6 colors
std::vector<color> the_colors(5);
for (int i = 0; i < 5; ++i)
the_colors[i] = color(i);

std::vector<color> ServerCodeColorVector;
std::string strValue;
std::getline(userInput, strValue);

if (strValue == "random" || strValue == "Random")
{
// initialize random seed and shuffle all_colors
srand(time(NULL));
std::random_shuffle(the_colors.begin(), the_colors.end());

// fill real_code with the four first colors
std::vector<color>::iterator it = the_colors.begin();
// ServerCodeColorVector.insert(it,4);
}
else
{
ServerCodeColorVector = GetInputAsColorMap(strValue);
}



for (int i = 0; i < 15; ++i) {
std::vector<color> current_try(4);
int black_hits = 0, white_hits = 0;
std::vector<int> correctColorIndex;
std::vector<int> correctColor;

bool exclude[4] = {false};

std::cout << "Your Guess: " << std::flush;
std::cin >> strValue;
current_try = GetInputAsColorMap(strValue);

for (int j = 0; j < 4; j++) {
if (color(current_try[j]) == ServerCodeColorVector[j]) {
black_hits++;
exclude[j] = true;
correctColorIndex.push_back(j);
}
}

for (int j = 0; j < 4; j++)
{
for (int k = 0; k < 4; k++)
{
if (std::find(correctColorIndex.begin(), correctColorIndex.end(), j) != correctColorIndex.end())
break;

if (color(current_try[j]) == ServerCodeColorVector[k] &&
!exclude[j])
{
correctColor.push_back(j);
++white_hits;
break;
}
}
}

std::cout << black_hits << " black hits & " << white_hits
<< " white hits.\n";

if (black_hits == 4) {
std::cout << "you won!\n";
return 0;
}
}

std::cout << "The real code was ";

for (int i = 0; i < 4; ++i)
std::cout << ServerCodeColorVector[i] << ' ';

std::cout << std::endl;
return 0;
}

int main()
{
std::cout << "Gimme: " << std::flush;
StartMasterMind(std::cin);
return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com