5位猜码游戏中search_array函数t参数取值及do-while循环问题
猜数字游戏代码问题求助
这是一个5位不重复数字的猜码游戏,每位数字范围0-9,玩家逐位输入猜测后会收到三种反馈:
- *:数字在密码中且位置正确
- +:数字在密码中但位置错误
- -:数字不在密码中
我现在遇到两个问题:
- 调用
search_array函数时,参数t应该传入什么值? - 第二个
do-while循环无法正常工作,推测两者有关联。
作业要求不可修改给定的函数,但可以新增变量或函数,希望尽量遵循这个规则。我试过传入位数5、密码数组(但t是int类型无效),现在用i作为占位符。以下是我的代码:
#include <iostream> using namespace std; void game_instructions(int n); bool contains_duplicates(int a[], 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]; const int SIZE2 = 5; int target[SIZE2]{ 1, 7, 4, 0, 9 }; //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 do { for (int i = 0; i < SIZE; i++) { randcode[i] = rand() % 10; cout << randcode[i]; } //Step 4: Repeat step 3 if the array randcode contains duplicates //You must use function contains_duplicates to implement this step } while (contains_duplicates(randcode, SIZE)); do { //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 cout << "\nEnter your guess for the 5 digit code(one digit at a time) :\n"; for (int g = 0; g < SIZE; g++) { cout << "Enter digit " << (g + 1) << ":"; cin >> guess[g]; while (guess[g] < 0 || guess[g] > 9) { cout << "Your guess must be between 0 and 9.\n"; cout << "Please re-enter: "; cin >> guess[g]; } } //Step 6: Output the array guess on a single line with a space after each element (see the sample output provided) for (int g = 0; g < SIZE; g++) { cout << guess[g] << " "; } cout << "\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 - for (int i = 0; i < SIZE; i++) { if (guess[i] == randcode[i]) cout << "* "; else if (search_array(randcode, 5, i)) { cout << "+ "; } else cout << "- "; } } while (guess!= randcode); //Step 8: Repeat steps 5,6,7 until all digits have been guessed correctly //Step 9: Output congratulations message cout << "\nGood job! You guessed the code!"; } 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; }
问题修复方案
1. 修正search_array的t参数传入值
search_array的作用是检查某个数字值是否存在于密码数组中,根据游戏规则,当当前猜测的数字位置不对时,需要验证这个数字本身是否在密码里。所以应该传入当前猜测的数字guess[i],而不是循环下标i。修改后的判断代码:
else if (search_array(randcode, SIZE, guess[i]))
2. 修复do-while循环的终止条件
直接用guess != randcode是错误的——数组名代表的是数组首地址,这里比较的是两个数组的内存地址,永远不会相等。可以通过新增函数判断数组是否完全匹配来解决:
// 在main函数外新增这个函数(符合作业允许新增函数的规则) bool arrays_equal(int a[], int b[], int n) { for (int i = 0; i < n; i++) { if (a[i] != b[i]) return false; } return true; }
然后把循环条件修改为:
} while (!arrays_equal(guess, randcode, SIZE));
额外优化建议
生成随机数时,在main函数开头添加srand(time(0));,确保每次运行程序生成的密码都是随机的,否则每次启动生成的密码会完全一致。
内容的提问来源于stack exchange,提问作者bv1300
相关产品推荐
相关产品推荐

