我在重置变量以重新玩游戏时遇到问题 [英] I am having a problem resetting my variables to play game again
本文介绍了我在重置变量以重新玩游戏时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// This is our standard input/output library
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//Defining
typedef struct {
char cSuit;
int nVal;
} Card;
//Function Prototypes
void printCard(int cardNumber, char cSuit); //Prints card number and suit
void dealCard(Card deck[], int *index, Card *card);//Access to function in MAIN
void welcomeMenu(void);//Prints welcomeMenu
void hitMenu(void);//Prints hitMenu
void showMenu(void);//Prints showMenu
void exitMessage(void);//Prints exitMessage
void hitMessage(void);//Prints hitMessage
//Global Variables
int main(void)
{
welcomeMenu(); //Function that prints menu for game
int menuChoice = -1;
int nCardValue = 2; //Prints card value
int playerCardsCount = 0; //Inital count set to zero
int compCardsCount = 0; //Inital count set to zero
int index = 0;
int hitMenu; //Print hit menu
int nComputerTotal = 0;
int nPlayerTotal = 0;
char cSuit = 3; //Prints suit symbol
Card Deck[52]; //Assign an array for Card Deck
Card playerCards[5]; //Assign an array of 5 cards to player
Card computerCards[5]; //Assign an array of 5 cards to computer
srand(time(NULL)); // seed random number generator from time
////////////////////////////////////////////////////////////////////////////////////////////////////////
for (int i = 0; i < 52; i++)
{
Deck[i].nVal = nCardValue;
Deck[i].cSuit = cSuit;
nCardValue++;
if (nCardValue > 14)
{
nCardValue = 2;
cSuit++;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//for loop to randomize the selection of cards a maxium of 299 times
//for loop first shuffles up to 199 and adds another 100 more
for (int i = 0; i < ((rand() % 200) + 100); i++)
{
int nFirst = (rand() % 52);
int nSecond = nFirst;
while (nSecond == nFirst)
{
nSecond = (rand() % 52);
}
Card Temp;
//assigning temp cards
Temp.nVal = Deck[nFirst].nVal;
Temp.cSuit = Deck[nFirst].cSuit;
//assign second card to first
Deck[nFirst].nVal = Deck[nSecond].nVal;
Deck[nFirst].cSuit = Deck[nSecond].cSuit;
//assign second card to temp position
Deck[nSecond].nVal = Temp.nVal;
Deck[nSecond].cSuit = Temp.cSuit;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
while (menuChoice != 0)
{
showMenu();
scanf(" %d", &menuChoice);
if (menuChoice == 1) //Input from player
{
//Calling the function dealCard and feeding in the Deck into the function
//Then passing in the address of index from the pointer of function
//Then call in the address of card a from the pointer
//Player Card 1
dealCard(Deck, &index, &playerCards[playerCardsCount]); //Sub 0
playerCardsCount++; //update playersCard count
printf("\n");
//Computer Card 1
dealCard(Deck, &index, &computerCards[compCardsCount]); //Sub 0
compCardsCount++; //update computerCard count
printf("\n");
/*//Player Card 2
dealCard(Deck, &index, &playerCards[playerCardsCount]); //Sub 1
playerCardsCount++; //update playersCard count
printf("\n");*/
////////////////////////////////////////////////////////////////////////////////////////////////////////
// print cards for how many "playerCardsCount" is active in playerCards[] array
for (int i = 0; i < playerCardsCount; i++)
{
//print card playerCards[0]
printCard(playerCards[i].nVal, playerCards[i].cSuit);
// accumulate playerCards total
if (playerCards[i].nVal > 10 && playerCards[i].nVal < 14)
{
nPlayerTotal += 10;
}
else if (playerCards[i].nVal == 14)
{
if ((nPlayerTotal + 11) > 21)
nPlayerTotal += 1;
else
nPlayerTotal += 11;
}
nPlayerTotal += playerCards[i].nVal;
printf("Player Card's total is %d\n", nPlayerTotal);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// print cards for how many "compterCardsCount" is active in computerCards[] array
for (int i = 0; i < compCardsCount; i++)
{
//print card playerCards[0]
printCard(computerCards[i].nVal, computerCards[i].cSuit);
// accumulate playerCards total
if (computerCards[i].nVal > 10 && computerCards[i].nVal < 14)
{
nComputerTotal += 10;
}
else if (computerCards[i].nVal == 14)
{
if ((nComputerTotal + 11) > 21)
nComputerTotal += 1;
else
nComputerTotal += 11;
}
nComputerTotal += computerCards[i].nVal;
printf("Computer Card's total is %d\n", nComputerTotal);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
printf("\n");
while (playerCardsCount < 5)
{
hitMessage();
scanf("%d", &hitMenu);
if (hitMenu == 1)
{
dealCard(Deck, &index, &playerCards[playerCardsCount]);
playerCardsCount++;
nPlayerTotal = 0;
for (int i = 0; i < playerCardsCount; i++)
{
//print card playerCards[i]
printCard(playerCards[i].nVal, playerCards[i].cSuit);
//printf("Player Card #%d is a %d%c\n", (i + 1), playerCards[i].nVal, playerCards[i].cSuit);
// accumulate playerCards total, not considering 11 | 1 aces yet
if (playerCards[i].nVal > 10 && playerCards[i].nVal < 14)
{
nPlayerTotal += 10;
}
else if (playerCards[i].nVal == 14)
{
if ((nPlayerTotal + 11) > 21)
nPlayerTotal += 1;
else
nPlayerTotal += 11;
}
nPlayerTotal += playerCards[i].nVal;
printf("Player Card's total is %d\n", nPlayerTotal);
}
}
else if (hitMenu == 2)
break;
} // end of while, "hit loop"
////////////////////////////////////////////////////////////////////////////////////////////////////////
// "Hit" loop is over and print results
// print player cards
// print total for player
// print computer cards
// print total for computer
// Game over?
//////////////////////////////////////////////////////////////////////////////
//Menu for player
showMenu();
scanf(" %d", &menuChoice);
//*
if (menuChoice == 1)
{
// reset all variables for game
nPlayerTotal = 0;
playerCardsCount = 0;
nComputerTotal = 0;
compCardsCount = 0;
for (int i = 0; i < 5; i++)
{
playerCards[0].nVal = 2;
playerCards[1].nVal = 2;
playerCards[2].nVal = 2;
playerCards[3].nVal = 2;
playerCards[4].nVal = 2;
computerCards[0].nVal = 2;
computerCards[1].nVal = 2;
computerCards[2].nVal = 2;
computerCards[3].nVal = 2;
computerCards[4].nVal = 2;
}
} //*/
else // don't want to play again
{
exitMessage();
break; // original menu break
}
///////////////////////////////////////////////////////////////////////////////
}
else
exitMessage();
} // end of menu
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////Functions//////////////////////////////////////////
//Function welcomeMenu
void welcomeMenu(void)
{
printf(" ************BLACKJACK************\n");
printf(" **********PLEASE CHOOSE**********\n\n");
}
//Function for dealCard
void dealCard(Card deck[], int *index, Card *card)
{
int spot = *index;
(*card).nVal = deck[spot].nVal; //temp value of card = the spot in the deck
(*card).cSuit = deck[spot].cSuit; //temp suit = the spot in deck
(*index)++;
} //End of Function
//Function for int cardNumber & char suit
void printCard(int cardNumber, char cSuit)
{
if (cardNumber <= 10)
{
printf(" ------------\n");
printf(" |%c |\n", cSuit);
printf(" | |\n");
printf(" | |\n");
printf(" | %d |\n", cardNumber);
printf(" | |\n");
printf(" | |\n");
printf(" | %c|\n", cSuit);
printf(" ------------\n");
//printf("Player card#: %d is a %c\n", cardNumber, cSuit); // Can not store 2 digit in a char variable
}
else
{
if (cardNumber == 11) // If nCardValue is equal to 11 change to letter
printf(" Player card#: J of %c\n", cSuit); // Changes cSuit to Jack
if (cardNumber == 12)
printf(" Player card#: Q of %c\n", cSuit); // Changes cSuit to Queen
if (cardNumber == 13)
printf(" Player card#: K of %c\n", cSuit); // Changes cSuit to King
if (cardNumber == 14)
printf(" Player card#: A of %c\n", cSuit); // Changes cSuit to Ace
}
} //End of function
//Showmenu
void showMenu(void)
{
printf("\n");
printf(" ---------------------------------\n");
printf(" | 1- Play BlackJack |\n"); //Choice 1
printf(" | 0- Exit Program |\n"); //Choice 2
printf(" ---------------------------------\n");
printf(" Choice:");
} //End of function
//Hit message
void hitMessage(void)
{
printf(" ---------------------------------\n");
printf(" | Would you like to HIT!?! |\n");
printf(" | Enter 1 for yes and 2 for no.|\n");
printf(" ---------------------------------\n");
printf(" Choice:");
}
//Exit Message
void exitMessage(void)
{
printf(" Thank you for playing Blackjack!!!\n\n");
} //End of function
//Reset all Varaiables
void resetVariables(void)
{
} //End of function
/////////////////////////////////////////////////////////////////////////
////////////////Working on/////////////////////////////////////
我的尝试:
我将所有重置都移到了底部,希望能清除变量,但它仍然只能运行。
What I have tried:
I moved all my resets to bottom in hope that would clear variables but it still only sometimes works.
推荐答案
你还没有理解for循环并忘记了ot她的结构成员
You havent understood the for loop and forgot the other member of your struct
//looping all members
for (int i = 0; i < 5; i++)
{
playerCards[i].nVal = 2;
playerCards[i].cSuit = 0;//reset
computerCards[i].nVal = 2;
computerCards[i].cSuit = 0;//reset
}
更清晰的是课程或功能
Clearer would be classes or a function
void resetCard(Card &card)
{
card.nval = 2;
card.cSuit = 0;
}
//used like that
for (int i = 0; i < 5; i++)
{
resetCard( playerCards[i] );
resetCard( computerCards[i] );
}
您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。
调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。
在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
这篇关于我在重置变量以重新玩游戏时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文