|
@ -9,141 +9,192 @@ |
|
|
|
|
|
|
|
|
int *openShop(Item *availableItems, Player player) |
|
|
int *openShop(Item *availableItems, Player player) |
|
|
{ |
|
|
{ |
|
|
int mode = 0; |
|
|
|
|
|
|
|
|
int mode = -1; |
|
|
int *x = malloc(sizeof(int) * 2); |
|
|
int *x = malloc(sizeof(int) * 2); |
|
|
|
|
|
|
|
|
int userModeInput; |
|
|
|
|
|
fflush(stdout); |
|
|
|
|
|
bool selectMode = 1; |
|
|
|
|
|
while (selectMode == 1) |
|
|
|
|
|
|
|
|
char *userModeInput = malloc(sizeof(char) * 1); |
|
|
|
|
|
do |
|
|
{ |
|
|
{ |
|
|
printf("Please type '0' for buy or '1' to sell items!\n"); |
|
|
|
|
|
while (scanf(" %d", &userModeInput) != 1) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("Invalid [index]. Please try again: "); |
|
|
|
|
|
fflush(stdout); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
printf("Please type '0' for buy or '1' to sell items:\n"); |
|
|
|
|
|
scanf(" %s", userModeInput); |
|
|
|
|
|
|
|
|
if (userModeInput > 1 || userModeInput < 0) |
|
|
|
|
|
|
|
|
if (strlen(userModeInput) == 1) |
|
|
{ |
|
|
{ |
|
|
printf("Invalid [index]. Please try again: "); |
|
|
|
|
|
} |
|
|
|
|
|
else if (userModeInput == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
mode = 0; |
|
|
|
|
|
selectMode = 0; |
|
|
|
|
|
|
|
|
if (isdigit(userModeInput[0])) |
|
|
|
|
|
{ |
|
|
|
|
|
int check = userModeInput[0] - '0'; |
|
|
|
|
|
if (check > 1 || check < 0) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("\nInvalid Index.\n"); |
|
|
|
|
|
} |
|
|
|
|
|
else if (check == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
mode = 0; |
|
|
|
|
|
} |
|
|
|
|
|
else if (check == 1) |
|
|
|
|
|
{ |
|
|
|
|
|
mode = 1; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
printf("\nInvalid input. Input isn't a number.\n"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
else if (userModeInput == 1) |
|
|
|
|
|
|
|
|
else |
|
|
{ |
|
|
{ |
|
|
mode = 1; |
|
|
|
|
|
selectMode = 0; |
|
|
|
|
|
|
|
|
printf("\nOnly ONE char is needed!\n"); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} while (mode != 0 && mode != 1); |
|
|
|
|
|
|
|
|
int userInput; |
|
|
|
|
|
fflush(stdout); |
|
|
fflush(stdout); |
|
|
bool shopIsOpen = 1; |
|
|
|
|
|
|
|
|
bool shopIsOpen = true; |
|
|
|
|
|
|
|
|
if (mode == 0) |
|
|
if (mode == 0) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
while (shopIsOpen == 1) |
|
|
|
|
|
|
|
|
char *userInput = malloc(sizeof(char) * 1); |
|
|
|
|
|
do |
|
|
{ |
|
|
{ |
|
|
|
|
|
// header |
|
|
char *header = "SHOP-Items"; |
|
|
char *header = "SHOP-Items"; |
|
|
printHeader(header); |
|
|
printHeader(header); |
|
|
|
|
|
|
|
|
// printf("%-5s %-30s %5s\n", "Index", "Name", "Price"); |
|
|
|
|
|
|
|
|
// items |
|
|
|
|
|
int maxItemsShop = 0; |
|
|
for (int i = 0; i < 6; i++) |
|
|
for (int i = 0; i < 6; i++) |
|
|
{ |
|
|
{ |
|
|
//printf("%-5d %-40s %5d$\n", availableItems[i].id, availableItems[i].itemName, availableItems[i].price); |
|
|
|
|
|
|
|
|
// printf("%-5d %-40s %5d$\n", availableItems[i].id, availableItems[i].itemName, availableItems[i].price); |
|
|
printItem(header, availableItems[i].itemName, availableItems[i].id, availableItems[i].price); |
|
|
printItem(header, availableItems[i].itemName, availableItems[i].id, availableItems[i].price); |
|
|
|
|
|
maxItemsShop += 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// instruction |
|
|
printf("\n-> to buy items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); |
|
|
printf("\n-> to buy items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); |
|
|
|
|
|
|
|
|
while (scanf(" %d", &userInput) != 1) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("Invalid [index]. Please try again: "); |
|
|
|
|
|
fflush(stdout); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// input |
|
|
|
|
|
scanf(" %s", userInput); |
|
|
|
|
|
|
|
|
if (userInput > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
x[0] = mode; |
|
|
|
|
|
x[1] = userInput; |
|
|
|
|
|
return x; |
|
|
|
|
|
// BUY ITEM added later -> addItemToInventory(userInput); |
|
|
|
|
|
} |
|
|
|
|
|
else if (userInput == 0) |
|
|
|
|
|
|
|
|
bool finished = false; |
|
|
|
|
|
// check input |
|
|
|
|
|
if (strlen(userInput) == 1) |
|
|
{ |
|
|
{ |
|
|
shopIsOpen = 0; |
|
|
|
|
|
printf("Enjoy your items, have a great day!\n"); |
|
|
|
|
|
x[0] = -1; |
|
|
|
|
|
x[1] = -1; |
|
|
|
|
|
return x; |
|
|
|
|
|
|
|
|
if (isdigit(userInput[0])) |
|
|
|
|
|
{ |
|
|
|
|
|
int check = userInput[0] - '0'; |
|
|
|
|
|
|
|
|
|
|
|
if (check > maxItemsShop) // about max items index -> invalid |
|
|
|
|
|
{ |
|
|
|
|
|
printf("\nInvalid [index]. Please try again: \n"); |
|
|
|
|
|
} |
|
|
|
|
|
else if (check > 0) // all available Indizes |
|
|
|
|
|
{ |
|
|
|
|
|
shopIsOpen = false; |
|
|
|
|
|
x[0] = mode; |
|
|
|
|
|
x[1] = check; |
|
|
|
|
|
return x; |
|
|
|
|
|
} |
|
|
|
|
|
else if (check == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
shopIsOpen = false; |
|
|
|
|
|
printf("Enjoy your items, have a great day!\n"); |
|
|
|
|
|
x[0] = -1; |
|
|
|
|
|
x[1] = -1; |
|
|
|
|
|
return x; |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
printf("Invalid [index]. Please try again. \n"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
printf("\nInvalid input. Input isn't a number.\n"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
printf("Invalid [index]. Please try again: "); |
|
|
|
|
|
|
|
|
printf("\nToo many chars entered. Only ONE char is needed. Try again!\n"); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} while (shopIsOpen == true); |
|
|
|
|
|
|
|
|
|
|
|
} //end mode = 0 |
|
|
else if (mode == 1) |
|
|
else if (mode == 1) |
|
|
{ |
|
|
{ |
|
|
while (shopIsOpen == 1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *userInput = malloc(sizeof(char) * 1); |
|
|
|
|
|
if (player.itemCounter > 0)//check if inventory of player is empty |
|
|
{ |
|
|
{ |
|
|
if (player.itemCounter > 0) |
|
|
|
|
|
|
|
|
do |
|
|
{ |
|
|
{ |
|
|
|
|
|
// header |
|
|
char *header = "Your inventory"; |
|
|
char *header = "Your inventory"; |
|
|
|
|
|
|
|
|
printHeader(header); |
|
|
printHeader(header); |
|
|
|
|
|
|
|
|
// printf("%-5s %-30s %5s\n", "Index", "Name", "Price"); |
|
|
|
|
|
|
|
|
// print inventory |
|
|
|
|
|
int maxItemsInv = 0; |
|
|
for (int i = 0; i < player.itemCounter; i++) |
|
|
for (int i = 0; i < player.itemCounter; i++) |
|
|
{ |
|
|
{ |
|
|
printItem(header, player.itemInventory[i].itemName, i+1, player.itemInventory[i].price); |
|
|
|
|
|
|
|
|
printItem(header, player.itemInventory[i].itemName, i + 1, player.itemInventory[i].price); |
|
|
|
|
|
maxItemsInv += 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// instructions |
|
|
printf("\n-> to sell items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); |
|
|
printf("\n-> to sell items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); |
|
|
|
|
|
|
|
|
while (scanf(" %d", &userInput) != 1) |
|
|
|
|
|
{ |
|
|
|
|
|
printf("Invalid [index]. Please try again: "); |
|
|
|
|
|
fflush(stdout); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// input |
|
|
|
|
|
scanf(" %s", userInput); |
|
|
|
|
|
|
|
|
if (userInput > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
x[0] = mode; |
|
|
|
|
|
x[1] = userInput-1; //index begin on 1 because we will cancel with 0 |
|
|
|
|
|
return x; |
|
|
|
|
|
// BUY ITEM added later -> addItemToInventory(userInput); |
|
|
|
|
|
} |
|
|
|
|
|
else if (userInput == 0) |
|
|
|
|
|
|
|
|
bool finished = false; |
|
|
|
|
|
// check input |
|
|
|
|
|
if (strlen(userInput) == 1) |
|
|
{ |
|
|
{ |
|
|
shopIsOpen = 0; |
|
|
|
|
|
printf("Enjoy your money, have a great day!\n"); |
|
|
|
|
|
x[0] = -1; |
|
|
|
|
|
x[1] = -1; |
|
|
|
|
|
return x; |
|
|
|
|
|
|
|
|
if (isdigit(userInput[0])) |
|
|
|
|
|
{ |
|
|
|
|
|
int check = userInput[0] - '0'; |
|
|
|
|
|
if (check > maxItemsInv) // about max items index -> invalid |
|
|
|
|
|
{ |
|
|
|
|
|
printf("\nInvalid [index]. Please try again: \n"); |
|
|
|
|
|
} |
|
|
|
|
|
else if (check > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
shopIsOpen = false; |
|
|
|
|
|
x[0] = mode; |
|
|
|
|
|
x[1] = check - 1; // index begin on 1 because we will cancel with 0 |
|
|
|
|
|
return x; |
|
|
|
|
|
} |
|
|
|
|
|
else if (check == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
shopIsOpen = false; |
|
|
|
|
|
printf("Enjoy your money, have a great day!\n"); |
|
|
|
|
|
x[0] = -1; |
|
|
|
|
|
x[1] = -1; |
|
|
|
|
|
return x; |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
printf("Invalid [index]. Please try again: "); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
printf("\nInvalid input. Input isn't a number.\n"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
printf("Invalid [index]. Please try again: "); |
|
|
|
|
|
|
|
|
printf("\nToo many chars entered. Only ONE char is needed. Try again!\n"); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
shopIsOpen = 0; |
|
|
|
|
|
printf("*** Your inventory is empty *** \n\n"); |
|
|
|
|
|
x[0] = -1; |
|
|
|
|
|
x[1] = -1; |
|
|
|
|
|
return x; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} while (shopIsOpen == true); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
else //when inventory of player is empty |
|
|
|
|
|
{ |
|
|
|
|
|
printf("*** Your inventory is empty *** \n\n"); |
|
|
|
|
|
x[0] = -1; |
|
|
|
|
|
x[1] = -1; |
|
|
|
|
|
return x; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}//end mode = 1 |
|
|
|
|
|
|
|
|
x[0] = -1; |
|
|
|
|
|
return x; |
|
|
|
|
|
} |
|
|
} |