主题:【求助!急】关于经典的猜数字游戏。
其实很easy 小弟新手。。。要求用c语言编译(不是c++)!
电脑随机出1-6中任意一个数字,然后猜4个数字。你必须告诉玩家对了几个数字(但位置不对)以及数字和位置都对的个数。 有20次机会。 最后必须告诉玩家正确答案(无论对错)
比较烦人的是:要求你必须写入3个函数(function)。
以下是我大致写的:
我的问题在于。一个是对正确数字的把握不准确;第2,在赢得之后,要求询问玩家是否继续玩,用了函数之后,我发现很难完成。如果有好的想法,谢谢大神指点了! qq:375014437.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 4
int roll_numbers(int puzzle[SIZE]);
int check_array(int puzAry[SIZE],int gusAry[SIZE]);
int print_array(int ary[SIZE]);
int main()
{
int choice;
int puz[SIZE]={0};
int guess[SIZE]={0};
int iteration;
int guesses;
printf("Welcome to the game of DaGesser. A game of number guessing.\n");
printf("Would you like to play? 1-yes and 2-no\n");
scanf("%d",&choice);
while (choice == 1)
{
guesses = 20;
srand(time(NULL));
roll_numbers(puz); //call function
while (guesses > 0) {
for (iteration=1;iteration<=SIZE;iteration++) {
printf("Guess a value between 1 and 6 for #%d\n",iteration);
scanf("%d",&guess[iteration-1]);
}
check_array(puz,guess); //call function
guesses--;
printf("You have %d guesses left\n",guesses);
} //end nested while-guesses
if (guesses <= 0)
{
printf("Sorry,you have run out of your chances.\n");
print_array(puz);
printf("Do you want to play again? 1-yes,2-no\n");
scanf("%d",&choice);
} //end if
} // end while
if (choice == 2)
{
printf("Thank you for playing!\n");
}
return 0; // program ends successfully
}
int roll_numbers(int puzzle[SIZE])
{
int i;
for (i=0;i<SIZE;i++)
{
puzzle[i] = 1 + (rand()%6);
printf("%2d",puzzle[i]);
}
} //end function roll_numbers
int check_array(int puzAry[SIZE],int gusAry[SIZE])
{
int j,k;
int count1,count2;
for (j=0;j<SIZE;j++)
{
if (puzAry[j]==gusAry[0] || puzAry[j]==gusAry[1] ||
puzAry[j]==gusAry[2] || puzAry[j]==gusAry[3]) {
count1++;
} // end if
} // end for loop
for(k=0;k<SIZE;k++)
{
if (puzAry[k] == gusAry[k]) {
count2++;
} //end if
} // end for loop
if (count1 != 4 && count2 != 4) {
printf("Congrt!you win.\n");
}
else {
printf("You have %d numbers correct and %d in the correct location\n",count1,count2);
}
} // end function check_array
int print_array(int ary[SIZE])
{
int p;
printf("The correct numbers are :");
for (p=0;p<SIZE;p++)
{
printf("%2d",ary[p]);
}
printf("\n");
} // end function print_array
电脑随机出1-6中任意一个数字,然后猜4个数字。你必须告诉玩家对了几个数字(但位置不对)以及数字和位置都对的个数。 有20次机会。 最后必须告诉玩家正确答案(无论对错)
比较烦人的是:要求你必须写入3个函数(function)。
以下是我大致写的:
我的问题在于。一个是对正确数字的把握不准确;第2,在赢得之后,要求询问玩家是否继续玩,用了函数之后,我发现很难完成。如果有好的想法,谢谢大神指点了! qq:375014437.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 4
int roll_numbers(int puzzle[SIZE]);
int check_array(int puzAry[SIZE],int gusAry[SIZE]);
int print_array(int ary[SIZE]);
int main()
{
int choice;
int puz[SIZE]={0};
int guess[SIZE]={0};
int iteration;
int guesses;
printf("Welcome to the game of DaGesser. A game of number guessing.\n");
printf("Would you like to play? 1-yes and 2-no\n");
scanf("%d",&choice);
while (choice == 1)
{
guesses = 20;
srand(time(NULL));
roll_numbers(puz); //call function
while (guesses > 0) {
for (iteration=1;iteration<=SIZE;iteration++) {
printf("Guess a value between 1 and 6 for #%d\n",iteration);
scanf("%d",&guess[iteration-1]);
}
check_array(puz,guess); //call function
guesses--;
printf("You have %d guesses left\n",guesses);
} //end nested while-guesses
if (guesses <= 0)
{
printf("Sorry,you have run out of your chances.\n");
print_array(puz);
printf("Do you want to play again? 1-yes,2-no\n");
scanf("%d",&choice);
} //end if
} // end while
if (choice == 2)
{
printf("Thank you for playing!\n");
}
return 0; // program ends successfully
}
int roll_numbers(int puzzle[SIZE])
{
int i;
for (i=0;i<SIZE;i++)
{
puzzle[i] = 1 + (rand()%6);
printf("%2d",puzzle[i]);
}
} //end function roll_numbers
int check_array(int puzAry[SIZE],int gusAry[SIZE])
{
int j,k;
int count1,count2;
for (j=0;j<SIZE;j++)
{
if (puzAry[j]==gusAry[0] || puzAry[j]==gusAry[1] ||
puzAry[j]==gusAry[2] || puzAry[j]==gusAry[3]) {
count1++;
} // end if
} // end for loop
for(k=0;k<SIZE;k++)
{
if (puzAry[k] == gusAry[k]) {
count2++;
} //end if
} // end for loop
if (count1 != 4 && count2 != 4) {
printf("Congrt!you win.\n");
}
else {
printf("You have %d numbers correct and %d in the correct location\n",count1,count2);
}
} // end function check_array
int print_array(int ary[SIZE])
{
int p;
printf("The correct numbers are :");
for (p=0;p<SIZE;p++)
{
printf("%2d",ary[p]);
}
printf("\n");
} // end function print_array