だから私は今コーディングを学んでいます。これは、Linux のターミナルで実行されるプログラムです。
ゲームは 2 ~ 14 (カード デッキ) の数字を生成し、次の数字が高いか低いかをユーザーに推測させます。
私が遭遇している問題は、ゲームが正常に動作することですが、ゲームでは前のターンの数字が評価されます。最初のターンに 6、2 番目のターンに 7、3 番目のターンに 3 が出た場合、3 番目のターンの数字の方が高いと推測した場合、7 は 6 よりも高いため、正しい推測として評価されます。 3を考慮に入れる必要があります。
また、間違った入力があると新しい乱数を作成するコードに問題があります。ユーザーが有効な選択肢を入力するまで、同じ番号を保持する必要があります。
出力例:
現在のカードは 9 です。次の数字は高い (1) または低い (2) ですか? 2
あなたは間違って推測しました。あなたの現在のスコアは-1です!現在の数字は 9 です。次の数字は高い (1) または低い (2) ですか? 2
カードは同じです。あなたの現在のスコアは-1です!現在の数値は 3 次の数値は高い (1) または低い (2) ですか?
ここに私のコード全体があります
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
/*declare variables*/
int pastCard;
int currentCard;
int score;
int userChoice;
int playGame = 1;
/*set up random*/
int range;
srand(time(NULL));
range = (13 - 1) + 1;
pastCard = rand() % range + 2;
currentCard = rand() % range + 2;
while (playGame == 1)
{
/*change the current card to the past card before creating new current card*/
pastCard = currentCard;
/*generate a random int for card*/
currentCard = rand() % range + 2;
if (currentCard < 11)
{
printf("The current card is a %d.\n", currentCard);
}
else if (currentCard == 11)
{
printf("The current card is a jack.\n", currentCard);
}
else if (currentCard == 12)
{
printf("The current card is a queen.\n", currentCard);
}
else if (currentCard == 13)
{
printf("The current card is a king.\n", currentCard);
}
else if (currentCard == 14)
{
printf("The current card is an ace.\n", currentCard);
}
printf("Will the next card be higher(1) or lower(2)? (press 0 to quit)\n");
scanf("%d", &userChoice);
printf("\n");
if (userChoice == 1)
{
if (currentCard > pastCard)
{
score++;
printf("You have guessed correctly.\n");
printf("Your current score is %d!\n", score);
}
else if (currentCard < pastCard)
{
score--;
printf("You have guessed incorrectly.\n");
printf("Your current score is %d!\n", score);
}
else if (currentCard == pastCard)
{
printf("The cards are the same.\n");
printf("Your current score is %d!\n", score);
}
}
else if (userChoice == 2)
{
if (currentCard < pastCard)
{
score++;
printf("You have guessed correctly.\n");
printf("Your current score is %d!\n", score);
}
else if (currentCard > pastCard)
{
score--;
printf("You have guessed incorrectly.\n");
printf("Your current score is %d!\n", score);
}
else if (currentCard == pastCard)
{
printf("The cards are the same.\n");
printf("Your current score is %d!\n", score);
}
}
else if (userChoice == 0)
{
playGame = 0;
printf("Final score: %d\n", score);
score = 0;
printf("Play again? (press 1 for yes, 0 for no)\n");
scanf("%d", &playGame);
printf("\n");
}
else
{
printf("Please enter a valid choice.\n");
}
}
return 0;
}
助けてください!これは私を非常に苛立たせてきました!