C 高级骰子游戏
在我发布代码之前,有两件事我应该提到,1有一个文本文件与此代码链接,我将在文章中发布一些细节。我的这个项目快完成了,所以我只关注topFive()函数和topWin()函数。但我的问题是,当我试图在这个函数中获得前五名时,它只打印出一个名字五次。我真的很感激任何帮助,非常难堪 下面是players.txt文件的内容和代码C 高级骰子游戏,c,dice,C,Dice,在我发布代码之前,有两件事我应该提到,1有一个文本文件与此代码链接,我将在文章中发布一些细节。我的这个项目快完成了,所以我只关注topFive()函数和topWin()函数。但我的问题是,当我试图在这个函数中获得前五名时,它只打印出一个名字五次。我真的很感激任何帮助,非常难堪 下面是players.txt文件的内容和代码 Peter 100 90 Emma 150 0 Richard 50 10 Abigail 138 128 Jacob 210 100 Ant
Peter 100 90
Emma 150 0
Richard 50 10
Abigail 138 128
Jacob 210 100
Anthony 800 -10
Joseph 328 62
Ashley 89 16
Hannah 197 7
Ethan 11 -20
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef struct {
char pl_name[10];
int balance;
int wincount;
} plyr;
plyr pl[10];
plyr top[5];
int psearch(){ // finds the index of the name in the file
int i;
char name[20];//This creates the initial array that searches throughout the array for the users name variable identity.
printf("Enter the user's name. ");
scanf("%s", name); // This will ask the user for the name whos balance they are changing.
for(i=0;i<10;){
if (strcmp(&name, pl[i].pl_name) == 0 ) break;
i++; //This will continue the search.
}
return i;
}
void topBalance(){ // finds the top balance for 5 players
int i;
printf("What is the player name? ");
scanf("%s", pl[i].pl_name); // This basically takes a users name and is ready to edit it.
printf("What's the balance? ");
scanf("%d", &pl[i].balance); // This changes whatever name is enters balance to whatever it wants to change it to.
}
void playGame(){ //Needs correction.
srand(time(0));
char enter = 'i';
int r1, r2, sum, point;
int playerno = psearch();
char again = 'y';
while (playerno == 10) { // This ensures that there are only 10 players that are going to be searched throughout.
printf("Players not found.\n");
playerno = psearch();
}
while(again == 'y'){
for(;;) {
printf("Press enter to roll the dice. ");
getchar(); // This will continue the dice to be rolled past the original point of just one or two rolls
while (enter != '\n')
enter = getchar();
r1 = (rand() % 6) + 1; // This acts as the math of the function and it takes a random integer and divides it by 6 and takes the remainder by 1.
r2 = (rand() % 6) + 1;
sum = r1 + r2;
printf("You got a %d and %d from the roll, the sum is %d. \n", r1, r2, sum);
if(sum == 7 || sum == 11) {
pl[playerno].balance += 10;
pl[playerno].wincount += 10;
printf("Your new balance is %d. \n", pl[playerno].balance);
break;
}
else if(sum == 2 || sum == 3 || sum == 12){
pl[playerno].balance -= 1; // This is anoyher simple way to lose if the sum is only equal to 2 or 3.
pl[playerno].wincount -= 1;
printf("You lose with a balance of %d", pl[playerno].balance);
break; // The basic loss variable in the play game function.
}
else {
point = sum;
printf("Your point is %d. Roll %d without rolling a 7 to win. \n", point, point);
for(;;){
enter = 'i';
printf("Roll the dice.\n");
while (enter != '\n') // This is a while loop that basically makes sure that if the dice is equal to 0 then it can not be submitted.
enter = getchar();
r1 = (rand() % 6) + 1;
r2 = (rand() % 6) + 1;
sum = r1 + r2; // Adds together the first and second random calculation above.
printf("You rolled %d and %d to get %d. \n", r1, r2, sum);
if(sum == point){
pl[playerno].balance += 10;
pl[playerno].wincount += 10;
printf("You win. New Balance is %d. \n", pl[playerno].balance);
break;
}
else if(sum == 7){
pl[playerno].balance -= 1;
pl[playerno].wincount -= 1;
printf("You lose. New Bal is %d. \n", pl[playerno].balance);
break;
}
else
printf("Retry. \n");
}
break;
}
}
printf("Want to play again?");
scanf("%s", &again);
}
}
void topWin(){
int maxM, list, max1;
for(int p = 0; p < 5; p++) {
maxM = 999999; //makes sure that the top maximum variable is a really high number in this loop, basically ensures that this loop will run as long as it doesn't go over 999999
for(int a = 0; a < 10; a++){
list = 0;
for(int t = 0; t < 5; t++){
if(strcmp(top[t].pl_name, pl[a].pl_name) == 0) list = 1;
}
if (pl[a].balance > maxM && !list) {
maxM = pl[a].wincount;
max1 = a;
}
}
top[p] = pl[max1];
}
printf("\n");
for(int a = 0; a < 5; a++) {
printf("%s\t%d\n", top[a].pl_name, top[a].wincount);
}
}
void topFive(){
int maxM, list, max1;
for(int p = 0; p < 5; p++) {
maxM = 999999; //makes sure that the top maximum variable is a really high number in this loop, basically ensures that this loop will run as long as it doesn't go over 999999
for(int a = 0; a < 10; a++){
list = 0;
for(int t = 0; t < 5; t++){
if(strcmp(top[t].pl_name, pl[a].pl_name) == 0) list = 1;
}
if (pl[a].balance > maxM && !list) {
maxM = pl[a].wincount;
max1 = a;
}
}
top[p] = pl[max1];
}
printf("\n");
for(int a = 0; a < 5; a++) {
printf("%s\t%d\n", top[a].pl_name, top[a].wincount);
}
}
int main(){
int i = 0, ch;
FILE *rp;
FILE *wp;
rp = fopen("players.txt", "r");
while(!feof(rp)){
fscanf(rp, "%s\t%d\t%d", pl[i].pl_name, &pl[i].balance, &pl[i].wincount);
i++;
}
char name[10];
srand (time(NULL));
while (ch != 4) {
printf("\n0. Top up your balance ");
printf("\n1. Play Game!!! ");
printf("\n2. Top 5 Players by Balance ");
printf("\n3. Top 5 Winners! ");
printf("\n4. Exit ");
printf("\nWhat do you pick? ");
scanf("%d", &ch);
// From here on, I need to create seperate functions for each of these then tie them into the menu!!
switch(ch) {
case 0:
topBalance();
wp = fopen("players.txt", "w");
for(i = 0; i < 10; i++)
fprintf(wp, "%s\t%d\t%d\n", pl[i].pl_name, pl[i].balance, pl[i].wincount);
fclose(wp);
break;
case 1: // Need to finish this and correct it!
playGame();
wp = fopen("players.txt", "w");
for(i = 0; i < 10; i++)
fprintf(wp, "%s\t%d\t%d\n", pl[i].pl_name, pl[i].balance, pl[i].wincount);
fclose(wp);
break;
case 2: // Segmentation Error
topFive();
break;
case 3:
topWin();
break;
case 4:
break;
break;
}
}
}
彼得100 90
艾玛1500
理查德50 10
阿比盖尔138 128
雅各布210 100
安东尼800-10
约瑟夫328 62
阿什利89 16
汉娜197
伊桑11-20
#包括
#包括
#包括
#包括
类型定义结构{
字符名称[10];
国际收支平衡;
温克蒙特国际酒店;
}plyr;
plyr pl[10];
plyr top[5];
int psearch(){//查找文件中名称的索引
int i;
char name[20];//这将创建一个初始数组,在整个数组中搜索用户名变量标识。
printf(“输入用户名”);
scanf(“%s”,name);//这将询问用户要更改的余额的名称。
对于(i=0;i最大值&&!列表){
maxM=pl[a].wincount;
max1=a;
}
}
top[p]=pl[max1];
}
printf(“\n”);
对于(int a=0;a<5;a++){
printf(“%s\t%d\n”,顶部[a].pl\u名称,顶部[a].wincount);
}
}
int main(){
int i=0,ch;
文件*rp;
文件*wp;
rp=fopen(“players.txt”,“r”);
而(!feof(rp)){
fscanf(rp,“%s\t%d\t%d”,pl[i].pl\u name,&pl[i].balance,&pl[i].wincount);
i++;
}
字符名[10];
srand(时间(空));
while(ch!=4){
printf(“\n0.加满余额”);
printf(“\n1.玩游戏!!!”;
printf(“\n2.余额前5名玩家”);
printf(“\n3.前5名获奖者!”);
printf(“\n4.退出”);
printf(“\n您选择什么?”);
scanf(“%d”和“ch”);
//从这里开始,我需要为每一个创建单独的函数,然后将它们绑定到菜单中!!
开关(ch){
案例0:
上平衡();
wp=fopen(“players.txt”,“w”);
对于(i=0;i<10;i++)
fprintf(wp,“%s\t%d\t%d\n”,pl[i]。pl\u名称,pl[i]。余额,pl[i]。wincount);
fclose(wp);
打破
案例1://需要完成并纠正它!
游戏();
wp=fopen(“players.txt”,“w”);
对于(i=0;i<10;i++)
fprintf(wp,“%s\t%d\t%d\n”,pl[i]。pl\u名称,pl[i]。余额,pl[i]。wincount);
fclose(wp);
打破
案例2://分段错误
前五名();
打破
案例3:
topWin();
打破
案例4:
打破
打破
}
}
}
我可以看出您的代码存在一个问题-
for(int t = 0; t < 5; t++){
if(strcmp(top[t].pl_name, pl[a].pl_name) == 0)
list = 1;
}
此外,您的
topBalance
功能也有严重问题。您正在使用pl[i]
。但是i
从未设置为任何值。对于top5,只需对其进行排序并获得前5条记录。我已经考虑过使用qsort函数来获得正确的输出,但是,我还想知道如何使用这些For loop&if语句来获得我正在寻找的输出。请您的代码将其减少到您的问题的一部分。您当前的代码包含了许多与您的问题无关的内容-最小的样本通常看起来类似于良好的单元测试:只执行一个任务,输入值指定为可再现性。谢谢!这正是我所需要的
for(int t = 0; t < p; t++){
....
}