Я новичок и у меня проблемы с проектом для школы. У меня наброски программы работают нормально. У меня проблемы с сохранением новых данных в мой файл Players.txt.
Если я играю в игру с игроком, и он выигрывает 10 очков, я хочу сохранить его в файле .txt и показать, когда я смотрю на верхние 5 балансов или закрываю программу и открываю ее и снова поиграйте с этим игроком.
В настоящее время он только сохраняет данные о выигрышах / проигрышах, пока я остаюсь в функции play (). Затем он возвращается к исходным номерам в файле Players.txt.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char Name[20];
int Balance;
int Gain;
} Person;
Person player[10];
//Function Declaration to get data for ingame
Person GetPlayerData(const char* name, Person *p);
//Function Declaration to exit to main menu when finished with game
void mainMenu(void);
//Opens file player.txt and puts it into array of persons: Person player[10]
void loadPlayer()
{
//initialize an array for players with room for 10 player's information
//opens file with pointer fptr
FILE *fptr;
fptr = fopen("players.txt", "r");
//prints error if file doesn't exist
if(fptr == NULL)
{
printf("Error loading player.txt\n");
}
//Runs through each line of the file, assigning name, balance and gain to the array player[]
int i=0;
while (fscanf(fptr, "%s%d%d", player[i].Name, &player[i].Balance, &player[i].Gain) != EOF)
{
i++;
}
for(i=0;i<10;i++)
{
printf("%s\t%d\t%d\n", player[i].Name, player[i].Balance, player[i].Gain);
}
fclose(fptr);
}
//GetPlayerData function
Person GetPlayerData(const char* name, Person *p) {
int j = 0;
for (;;) {
if (strcmp(name, p[j].Name) == 0) {
return p[j];
}
j++;
}
}
//Play function
void play(void) {
char name[20];
int bal = 0;
int dice1, dice2, sumofDice;
printf("Enter your name: ");
scanf("%s", &name);
Person playerDetails = GetPlayerData(name, player);
getchar();
//Begin Game
while (1) {
printf("Press Enter to Roll the dice.\n");
getchar();
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
sumofDice = dice1 + dice2;
printf("The values of the dices are %d and %d whose sum is %d.\n", dice1, dice2, sumofDice);
if (sumofDice == 7 || sumofDice == 11) {
playerDetails.Balance = playerDetails.Balance + 10;
printf("You win the game. Your current balance is %d.\n", playerDetails.Balance);
char userChoice;
printf("Play another game? y/n ");
scanf("%s", &userChoice);
if (userChoice == 'y') {
getchar();
continue;
}
else {
break;
}
}
else if (sumofDice == 2 || sumofDice == 3 || sumofDice == 12) {
playerDetails.Balance = playerDetails.Balance - 1;
printf("You lose. Your current balance is %d.\n", playerDetails.Balance);
char userChoice;
printf("Play another game? y/n ");
scanf("%s", &userChoice);
if (userChoice == 'y') {
getchar();
continue;
}
else {
getchar();
break;
}
}
else {
int point = sumofDice;
//Continuing the game after your point is declared
while (1) {
printf("Press Enter to Roll the dice.\n");
getchar();
int dice1 = rand() % 6 + 1;
int dice2 = rand() % 6 + 1;
int sumofDice = dice1 + dice2;
printf("The values of the dice are %d and %d whose sum is %d.\n", dice1, dice2,
sumofDice);
if (sumofDice == point) {
playerDetails.Balance = playerDetails.Balance + 10;
printf("You win the game. Your current balance is %d.\n", playerDetails.Balance);
char userChoice;
printf("Play another game? y/n ");
scanf("%s", &userChoice);
if (userChoice == 'y') {
getchar();
continue;
}
else {
break;
}
}
else if (sumofDice == 7) {
playerDetails.Balance = playerDetails.Balance - 1;
printf("You lose. Your current balance is %d.\n", playerDetails.Balance);
char userChoice;
printf("Play another game? y/n ");
scanf("%s", &userChoice);
if (userChoice == 'y') {
getchar();
continue;
}
else {
break;
}
}
else {
continue;
}
}
break;
}
}
getchar();
mainMenu();
}
void topBalance()
{
int firstLoc, i;
int firstVal = 0;
for(i=0; i<10; i++)
{
if ((player[i].Balance) > firstVal)
{
firstVal = player[i].Balance;
firstLoc = i;
}
}
int secondLoc;
int secondVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Balance) > secondVal) && ((player[i].Balance) < firstVal))
{
secondVal = player[i].Balance;
secondLoc = i;
}
}
int thirdLoc;
int thirdVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Balance) > thirdVal) && ((player[i].Balance) < secondVal))
{
thirdVal = player[i].Balance;
thirdLoc = i;
}
}
int forthLoc;
int forthVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Balance) > forthVal) && ((player[i].Balance) < thirdVal))
{
forthVal = player[i].Balance;
forthLoc = i;
}
}
int fifthLoc;
int fifthVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Balance) > fifthVal) && ((player[i].Balance) < forthVal))
{
fifthVal = player[i].Balance;
fifthLoc = i;
}
}
printf("1st Place %s \t%d\n", player[firstLoc].Name, player[firstLoc].Balance);
printf("2nd Place %s \t%d\n", player[secondLoc].Name, player[secondLoc].Balance);
printf("3rd Place %s \t%d\n", player[thirdLoc].Name, player[thirdLoc].Balance);
printf("4th Place %s \t%d\n", player[forthLoc].Name, player[forthLoc].Balance);
printf("5th Place %s \t%d\n", player[fifthLoc].Name, player[fifthLoc].Balance);
}
void topGain()
{
int firstLoc, i;
int firstVal = 0;
for(i=0; i<10; i++)
{
if ((player[i].Gain) > firstVal)
{
firstVal = player[i].Gain;
firstLoc = i;
}
}
int secondLoc;
int secondVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Gain) > secondVal) && ((player[i].Gain) < firstVal))
{
secondVal = player[i].Gain;
secondLoc = i;
}
}
int thirdLoc;
int thirdVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Gain) > thirdVal) && ((player[i].Gain) < secondVal))
{
thirdVal = player[i].Gain;
thirdLoc = i;
}
}
int forthLoc;
int forthVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Gain) > forthVal) && ((player[i].Gain) < thirdVal))
{
forthVal = player[i].Gain;
forthLoc = i;
}
}
int fifthLoc;
int fifthVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Gain) > fifthVal) && ((player[i].Gain) < forthVal))
{
fifthVal = player[i].Gain;
fifthLoc = i;
}
}
printf("1st Place %s \t%d\n", player[firstLoc].Name, player[firstLoc].Gain);
printf("2nd Place %s \t%d\n", player[secondLoc].Name, player[secondLoc].Gain);
printf("3rd Place %s \t%d\n", player[thirdLoc].Name, player[thirdLoc].Gain);
printf("4th Place %s \t%d\n", player[forthLoc].Name, player[forthLoc].Gain);
printf("5th Place %s \t%d\n", player[fifthLoc].Name, player[fifthLoc].Gain);
}
void mainMenu()
{
int menuSellect;
printf("\n--------Main Menu--------");
printf("\n0) Top Up Account");
printf("\n1) Play");
printf("\n2) Top 5 Players by Balance");
printf("\n3) Top 5 Players by Winnings");
printf("\n4) Exit Game");
printf("\n-------------------------\n");
//prompt user to pick a menu item 0 to 3
printf("Please Enter a Number for a Valid Menu Item: ");
scanf("%d", &menuSellect);
printf("\n\n");
//switch statement controlls what menu item is going to be sellected
switch (menuSellect)
{
case 0:
printf("Top Up Account\n");
mainMenu();
break;
case 1:
printf("Play\n");
play();
mainMenu();
break;
case 2:
printf("Top 5 Players by Balance\n");
topBalance();
mainMenu();
break;
case 3:
printf("Top 5 Players by Winnings\n");
topGain();
mainMenu();
break;
case 4:
printf("Exitting Game\n");
exit(0);
//deffault catches any errors and prints the main menu a second time
deffault:
printf("Invalid Menu Option\n");
mainMenu();
break;
}
}
void main()
{
loadPlayer();
mainMenu();
}