繁体   English   中英

我如何让我的数组进行比较,然后根据用户猜测显示某些输出

[英]How do i make my array compare and then display certain outputs based on users guess

编码较新,我卡住了,需要制作一个存储 5 个数字 (1-9) 的数组,然后我需要检查该数组是否有重复项,如果有重复项,我需要用一个全新的随机行替换该数字,没有重复项(似乎是更简单的选择)或者只是替换那个数字,然后让我让用户猜测 5 个数字将其存储在一个数组中,在底部显示该数组以及每个数字下的这些

// * = 表示数字在确切位置

// - = 表示数组不包含该数字

// + = 表示数组包含数字但不在正确的位置

所有 arrays 都希望一次输入一个数字,重复这些步骤,直到用户将所有数字都输入*,然后结束游戏并发送完成消息。

我真正的问题在于第 4 步和第 7 步; 以下是我今天一直在处理的代码,但我们将不胜感激任何帮助

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void game_instructions(int n);
bool search_array(int a[], int n, int t);
int main()
{

    //Step 1: Declare your variables and constants
    const int SIZE = 5; //this constant stores the length/size of the code

    int randcode[SIZE]; //this array stores the code generated randomly by the computer
    int guess[SIZE];    //this array stores the code inputted by the player/user
   
    //you may add more variables as needed
  
    //Step 2: Output game instructions by calling function game_instructions
    game_instructions(SIZE);
    //Step 3: Generate a random code and store it in the array randcode one digit at a time.
    //Each digit should be between 0 and 9 and should be stored as one array element
    //Recall that rand() % 10 can be used to generate a number between 0 and 9
    srand(time(0));
    for(int i=0;i<SIZE;i++)
       randcode[i]= (rand() % 10);  //Computers random 5 numbers generated
   
    cout<<"\nRandom C-numbers::"<<endl;
   
    for(int i=0;i<SIZE;i++)
       cout<<randcode[i] << " ";
    
    //Step 4: Repeat step 3 if the array randcode contains duplicates
    //You must use function contains_duplicates to implement this step
    
    
    
    //Step 5: Ask the user for his guess and store it in the array guess.
    //Read one digit at a time, validate it to make sure it is between 0 and 9, and store it as one array element
       
    for (int i=0; i<SIZE; i++) {
        cout<<"\nEnter Digit "<< i+1 << ": ";
        cin >> guess[i];}
            
    cout << endl;
    //Step 6: Output the array guess on a single line with a space after each element (see the sample output provided)
    for (int n=0; n < SIZE; ++n) {
        cout << guess[n] << "  ";
    }
    //Step 7: Compare the randomly generated code (randcode) with the user's guess (guess)
    //and display feedback for each digit as: *, + or –, as explained below:
    //For each digit in the user's guess do the following:
        // If it matches the digit from the random code (both value and position), output *
        // Otherwise, if it appears anywhere in the random code, output + (use function search_array here)
        // Otherwise, output -
        
    //Step 8: Repeat steps 5,6,7 until all digits have been guessed correctly
    
    //Step 9: Output congratulations message
    cout << endl << endl;
    cout << "Good job! You guessed the code!";
    return 0;
}

void game_instructions(int n)
//This function outputs the game instructions.
//Its parameter n represents the length of the code.
{
    cout << "A random " << n << " digit code has been generated. You have to guess it. \n";
    cout << "For every digit you will receive feedback in the form of *, + or -  \n";
    cout << "   * means the digit is in the code and it is in the correct position.\n";
    cout << "   + means the digit is in the code but it is not in the correct position.\n";
    cout << "   - means the digit is not in the code.\n";
}


bool search_array(int a[], int n, int t)
//This function searches the array a (of size n) for a target value t.
//If t is found in the array the function returns true; otherwise it returns false.
{
    for (int i = 0; i < n; i++)
    {
        if (a[i] == t) return true;
    }
    return false;
}

bool contains_duplicates(int a[], int n)
//This function searches the array a (of size n) and returns true if the array contains duplicates; otherwise, it returns false.
{
    for (int i = 0; i < n; i++)
    {
        //compare element a[i] with all the remaining elements from index i+1 to n
        for (int j = i+1; j < n; j++)
        {
            if (a[i] == a[j]) return true;
        }
    }
    return false;
}

我完成了大部分的辅助代码,但我只是对如何让这个数组匹配任何帮助感到困惑

以下是我将如何实现该程序以生成不重复的数字:

第 3 步看起来像这样:

int tempNumber;
int i = 0;
while (i < SIZE) {
    tempNumber = (rand() % 10);

    if (contains_duplicates(randcode, tempNumber, i) == false) {
        // every time there is no duplicate we push and add i by 1;
        randcode[i] = tempNumber;  // Computers random 5 numbers generated
        i++;
    }
}

contains_duplicates function 看起来像这样:

bool contains_duplicates(int arr[], int tempNum, int currentArrSize)
// This function searches the array a (of size n) and returns true if the 
//array
// contains duplicates; otherwise, it returns false.
{
    for (int j = 0; j <= currentArrSize; j++) {
        // if the array[index] not equal generated number the loop will 
        //iterate again without going to the line below
        if (arr[j] != tempNum) {
            continue;
        }
        // if the array[index] equal the function will return true and end 
        //the function

        // uncomment this to check how many time it duplicate (not 
        //necessary tho) 
        //cout << "if return true, this will appear"<< endl;
        return true;
    }
    // if the loop is done and no duplicate, function will return false
    return false;
}

使用这种方法不需要执行第 4 步,因为我们已经防止了重复数组!

这是您问题的最后一个问题,即第 7 步:

cout << endl;
for (int i = 0; i < SIZE; i++) {
    if (guess[i] == randcode[i]) {
        cout << "* ";
    } else if (search_array(randcode, SIZE, guess[i])) {
        cout << "+ ";
    } else {
        cout << "- ";
    }
}

看来问题已经解决了,现在尝试自己实现第8步,祝你好运:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM