[英]Using Functions One by One or both in int main()
所以,我得到了 2 個函數,它們基本上都在玩一個名為“豬游戲”的游戲,下面的代碼是我的代碼中的int main() ,它只是打印主要內容,然后一一啟動函數。 此外,還有另一個名為roll_a_dice() 的函數,您可以忽略它,它基本上是擲骰子。
例如,在第一個 while 循環中play_computer首先啟動,然后play_user啟動。 循環重復 6 次。 我需要的是在每一輪之后,我需要獲取函數的結果(或輸出或返回)並將其放入另一個名為scoresheets() 的函數中。 我不知道該怎么做。 請幫幫我。
int main(void){
int roll, comp, me, round=1;
srand(time(NULL));
printf("Welcome to Big Pig game.");
printf("\nLets get started!");
comp = roll_a_dice();
printf("\nI have rolled the dice and got %d!",comp);
printf("\nShall i roll the dice for you (Y/N)? ");
scanf("%c",&roll);
if (roll=='Y'){
me=roll_a_dice();
printf("I have rolled the dice for you and you got %d!",me);
if (comp>me){
while (round<=6){
printf("\nRound %d--My Turn: ",round);
printf("\n===================================================================================");
printf("%d",play_computer());
printf("\nRound %d--Your Turn: ",round);
printf("\n===================================================================================");
printf("%d",play_user());
round++;
}
}
else{
while (round<=6){
printf("\nRound %d--Your Turn: ",round);
printf("\n===================================================================================");
printf("%d",play_user());
printf("\nRound %d--My Turn: ",round);
printf("\n===================================================================================");
printf("%d",play_computer());
round++;
}
}
}
printf("%d",scoresheet());
return 0;
}
另外,我稍微修改了您的代碼
//Included the header files which I assume you included in your program so that it compiles
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
//Declaring the functions
int scoresheet(int getOrPut,int score, int player)
{
//0 is put and 1 is get
static int playerScore=0;
static int computerScore=0;
if(player==0 && getOrPut==0)
{
playerScore+=score;
return 0;
}
if(player==1 && getOrPut==0)
{
computerScore+=score;
return 0;
}
if(player==0 && getOrPut==1)
return playerScore;
if(player==1 && getOrPut==1)
return computerScore;
return -1;
}
int roll_a_dice()
{
return (rand()%6) +1; //I assume the code looks something like this
}
int play_user(int round) //Using parameters
{
int totalScoreOfUser=(rand()%36) + 1; //Enter code to calculate actual score here
scoresheet(0,totalScoreOfUser,0);//Add score to scoresheet
return totalScoreOfUser;
}
int play_computer() //Without passing parameters
{
static int round=0; //Declares a variable static to the function
round++;//Increases value of variable each time function is called
int totalScoreOfComputer=(rand()%36) + 1;
scoresheet(0,totalScoreOfComputer,1);//Add score to scoresheet
return totalScoreOfComputer;
}
int main(void)
{
int comp, me, round=1;
char roll; //changed type of roll from int to char as you were scanning input as %c
srand(time(NULL));
printf("Welcome to Big Pig game.");
printf("\nLets get started!");
comp = roll_a_dice();
printf("\nI have rolled the dice and got %d!",comp);
printf("\nShall I roll the dice for you (Y/N)? "); //Fixed grammar changing i to I in string
scanf(" %c",&roll); //Added a blank space before %c to remove whitespace errors during run time
while(roll!='Y' && roll!='y' && roll!='N' && roll!='n') //Added some extra code so that user will always input either y or n
{
printf("\nInvalid Input. Please enter either (Y/N)\n");
scanf(" %c",&roll);
}
if (roll=='Y' || roll=='y'){ //Most people don't usually press shift while typing so included the lower case y in your code
me=roll_a_dice();
printf("I have rolled the dice for you and you got %d!",me);
if (comp>me){ //When computer has rolled a higher number, it goes first
while (round<=6){
//I reordered and combined a few print statements so the code looks shorter
printf("\nRound %d--My Turn: ",round);
printf("%d",play_computer());
printf("\n===================================================================================");
printf("\nRound %d--Your Turn: ",round);
printf("%d",play_user(round));
printf("\n===================================================================================");
round++;
}
}
else{ //When user has rolled higher or both rolled equal, user goes first
while (round<=6){
//I reordered and combined a few print statements so the code looks shorter
printf("\nRound %d--Your Turn: %d",round,play_user(round));
printf("\n===================================================================================");
printf("\nRound %d--My Turn: %d",round,play_computer());
printf("\n===================================================================================");
round++;
}
}
printf("\nTotal Scores are Computer: %d and User: %d\n",scoresheet(1,0,1),scoresheet(1,0,0));
}
else
{
printf("You chose not to roll the die and hence the game did not begin\n");
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.