From 766924668e80afea2ae423021ce17bbaf10d0bfd Mon Sep 17 00:00:00 2001 From: Splatink Date: Wed, 16 Apr 2025 10:49:13 +0300 Subject: [PATCH 1/2] added police and fixed bugs --- main.c | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 151 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index 2160c34..6097e02 100644 --- a/main.c +++ b/main.c @@ -23,9 +23,14 @@ struct gameCustomer struct gamePlayer { char playerName[10]; - unsigned int playerScore; - unsigned int playerMoney; - unsigned totalItemsSold; + int playerScore; + int playerMoney; + int totalItemsSold; + int illegalItemsSold; + bool warrantForArrest; + bool gameForceEnd; + bool playerNotified; + bool gameKilled; }; struct gameItem item[10] = @@ -38,7 +43,7 @@ struct gameItem item[10] = {"TV", 400, 400, 20, 0}, {"Smartphone", 0, 1200, 0, 0}, //special items - {"Snake Oil", 65, 65, 150, 0}, + {"Ink", 65, 65, 150, 0}, {"Weed", 12, 12, 300, 0}, {"Crypto", 150, 150, 500, 0} }; @@ -258,7 +263,12 @@ void buyItems() bool checkForItem(int itemPicked) { - if (item[itemPicked].itemInInventory == true) + if ((item[itemPicked].itemInInventory == true) && (itemPicked >= 7)) + { + player.illegalItemsSold++; + return true; + } + else if (item[itemPicked].itemInInventory == true) { return true; } @@ -268,6 +278,124 @@ bool checkForItem(int itemPicked) } } +void policeAccept() +{ + printf("You were arrested by the police and issued a 400$ fine\n"); + player.playerMoney -= 400; + player.illegalItemsSold = 0; +} + +void policeDecline() +{ + int randomness = rand() % 100 + 1; + if (randomness < 50) + { + printf("You have been arrested by the police and have been fined 400$, and an additional 500$ for resisting arrest\n"); + player.playerMoney -= 500; + player.illegalItemsSold = 0; + return; + } + else + { + printf("Alright then, since we have no evidence we will get a warrant\n"); + player.warrantForArrest = true; + return; + } +} + +void policeShoot() +{ + int randomness = rand() % 100 + 1; + if (randomness < 80) + { + printf("You were shot and killed buy the police\n"); + player.gameForceEnd = true; + player.gameKilled = true; + return; + } + else + { + printf("You shot and killed the police, you escaped this time\n"); + player.illegalItemsSold = 0; + return; + } + +} + +void policeUI() +{ + printf("A: Accept\nD: Decline\nS: Shoot Police\n"); + while (true) + { + int selection = getchar(); + switch (selection) + { + case 'A': + policeAccept(); + return; + case 'D': + policeDecline(); + return; + case 'S': + policeShoot(); + return; + } + } + +} + +void warrantUI() +{ + printf("A: Accept\nS: Shoot Police\n"); + while (true) + { + int selection = getchar(); + switch (selection) + { + case 'A': + policeAccept(); + return; + case 'S': + policeShoot(); + return; + } + } +} + +void checkPolice() +{ + if (player.warrantForArrest) + { + printf("Police: %s, WE HAVE A WARRANT FOR YOUR ARREST. PUT YOUR HANDS BEHIND YOUR BACK AND INTERLOCK YOUR FINGERS\n", player.playerName); + warrantUI(); + return; + } + if (player.illegalItemsSold == 1 && player.playerNotified == false) + { + printf("You have sold an illegal item, if you keep this up the police might start asking questions\n"); + player.playerNotified = true; + } + else if (player.illegalItemsSold > 2) + { + printf("Police: You seem to be selling contraband, you are under arrest!\n"); + policeUI(); + } +} + +void checkMoney() +{ + if (player.playerMoney <= 0) + { + printf("You have no money, you lose unless you can sell your inventory\n"); + player.playerMoney = 0; + player.gameForceEnd = true; + } + else + { + player.gameForceEnd = false; + } +} + int gameUI(int itemPicked) { printf("A: Accept\nN: Negotiate\nD: Decline\nS: Print Stats\nI: Print Inventory\nB: Buy Items\n"); @@ -327,11 +455,17 @@ bool checkCustomerWiggle(int potNewItemPrice, int customerPicked, int itemPicked void gameTurn() { + checkPolice(); + if (player.gameKilled) + { + return; + } + checkMoney(); int customerPicked = rand() % 10; int itemPicked = rand() % 10; int potNewItemPrice; item[itemPicked].itemCurrentItemPrice = item[itemPicked].itemStartingPrice; //reset item prices for negotiation; stupid solution - printf("%s: I need a %s, willing to pay %d gold for it.\n", customer[customerPicked].customerName, item[itemPicked].itemName, item[itemPicked].itemCurrentItemPrice); + printf("%s: I need %s, willing to pay %d gold for it.\n", customer[customerPicked].customerName, item[itemPicked].itemName, item[itemPicked].itemCurrentItemPrice); int selection = gameUI(itemPicked); switch (selection) { @@ -395,11 +529,19 @@ int main() { initCustomers(); gameIntro(); + int turns; int numOfTurns = turnSelector(); - for (int i = 0; i < numOfTurns; i++) + for (turns = 0; turns < numOfTurns; turns++) { - gameTurn(); + if (player.gameForceEnd) + { + break; + } + else + { + gameTurn(); + } } - gameEnd(numOfTurns); + gameEnd(turns); return 0; } \ No newline at end of file -- 2.49.1 From a5ec8b09eb4e7690632967f0ebaa86ef45189f74 Mon Sep 17 00:00:00 2001 From: Splatink Date: Wed, 16 Apr 2025 11:30:52 +0300 Subject: [PATCH 2/2] added definitions for easier setting of variables --- main.c | 109 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 68 insertions(+), 41 deletions(-) diff --git a/main.c b/main.c index 6097e02..0b54659 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,24 @@ #include #include +//values below control random item/customer selections, change for debugging (illegal items only 7-9, default 0-9) +#define ITEM_LOWER_BOUND 7 +#define ITEM_UPPER_BOUND 9 +#define CUSTOMER_LOWER_BOUND 0 +#define CUSTOMER_UPPER_BOUND 9 + +//values below control the police random chances, out of a 100 +#define POLICE_SHOOT_SUCCESS 20 +#define POLICE_DECLINE_SUCCESS 50 + +//values below control the police fines, and when they will get suspicous or attempt to arrest the player +#define POLICE_FINE_NORMAL 400 +#define POLICE_FINE_INCREASE 100 +#define POLICE_WARNING 1 //warn at X amount of illegal items sold +#define POLICE_ARREST 3 //attempt arrest at X amount of illegal items sold + +//values below control how the player starts the game, default is 100 gold and 1 banana +#define PLAYER_STARTING_MONEY 100 struct gameItem { @@ -97,7 +115,7 @@ void gameIntro() printf("The game ends after your chosen amount of turns and you get a score!\n"); printf("Please enter your player name (Max 10 characters)\n"); scanf("%s", &player.playerName); - player.playerMoney = 100; + player.playerMoney = PLAYER_STARTING_MONEY; printf("You start with %d gold and 1 %s\n", player.playerMoney, item->itemName); } @@ -280,18 +298,18 @@ bool checkForItem(int itemPicked) void policeAccept() { - printf("You were arrested by the police and issued a 400$ fine\n"); - player.playerMoney -= 400; + printf("You were arrested by the police and issued a %d gold fine\n"); + player.playerMoney -= POLICE_FINE_NORMAL; player.illegalItemsSold = 0; } void policeDecline() { int randomness = rand() % 100 + 1; - if (randomness < 50) + if (randomness > POLICE_DECLINE_SUCCESS) { - printf("You have been arrested by the police and have been fined 400$, and an additional 500$ for resisting arrest\n"); - player.playerMoney -= 500; + printf("You have been arrested by the police and have been fined %d gold, and an additional %d gold for resisting arrest\n", POLICE_FINE_NORMAL, POLICE_FINE_INCREASE); + player.playerMoney -= (POLICE_FINE_NORMAL + POLICE_FINE_INCREASE); player.illegalItemsSold = 0; return; } @@ -306,9 +324,9 @@ void policeDecline() void policeShoot() { int randomness = rand() % 100 + 1; - if (randomness < 80) + if (randomness > POLICE_SHOOT_SUCCESS) { - printf("You were shot and killed buy the police\n"); + printf("You were shot and killed by the police\n"); player.gameForceEnd = true; player.gameKilled = true; return; @@ -370,12 +388,12 @@ void checkPolice() warrantUI(); return; } - if (player.illegalItemsSold == 1 && player.playerNotified == false) + if (player.illegalItemsSold == POLICE_WARNING && player.playerNotified == false) { printf("You have sold an illegal item, if you keep this up the police might start asking questions\n"); player.playerNotified = true; } - else if (player.illegalItemsSold > 2) + else if (player.illegalItemsSold >= POLICE_ARREST) { printf("Police: You seem to be selling contraband, you are under arrest!\n"); policeUI(); @@ -453,6 +471,39 @@ bool checkCustomerWiggle(int potNewItemPrice, int customerPicked, int itemPicked } } +void sellItem(int itemPicked) +{ + player.playerMoney += item[itemPicked].itemCurrentItemPrice; + player.playerScore++; + item[itemPicked].itemInInventory = false; + player.totalItemsSold++; + printf("Sold!\n\n"); +} + +void negotiateItem(int itemPicked, int customerPicked) +{ + int potNewItemPrice; + printf("%s: Aight, what are we talking?\n", customer[customerPicked].customerName); + printf("Enter desired item price:\n"); + scanf("%d", &potNewItemPrice); + if (checkCustomerWiggle(potNewItemPrice, customerPicked, itemPicked)) + { + player.playerMoney += item[itemPicked].itemCurrentItemPrice; + player.playerScore += 2; + item[itemPicked].itemInInventory = false; + player.totalItemsSold++; + printf("Sold!\n\n"); + } + else + { + printf("%s: Nah, think I'll pass\n\n", customer[customerPicked].customerName); + if(player.playerScore != 0) + { + player.playerScore -= 1; + } + } +} + void gameTurn() { checkPolice(); @@ -461,45 +512,21 @@ void gameTurn() return; } checkMoney(); - int customerPicked = rand() % 10; - int itemPicked = rand() % 10; - int potNewItemPrice; + int itemPicked = rand() % (ITEM_UPPER_BOUND - ITEM_LOWER_BOUND + 1) + ITEM_LOWER_BOUND; + int customerPicked = rand() % (CUSTOMER_UPPER_BOUND - CUSTOMER_LOWER_BOUND + 1) + CUSTOMER_LOWER_BOUND; item[itemPicked].itemCurrentItemPrice = item[itemPicked].itemStartingPrice; //reset item prices for negotiation; stupid solution printf("%s: I need %s, willing to pay %d gold for it.\n", customer[customerPicked].customerName, item[itemPicked].itemName, item[itemPicked].itemCurrentItemPrice); int selection = gameUI(itemPicked); switch (selection) { case 1: - player.playerMoney += item[itemPicked].itemCurrentItemPrice; - player.playerScore++; - item[itemPicked].itemInInventory = false; - player.totalItemsSold++; - printf("Sold!\n"); + sellItem(itemPicked); break; case 2: - printf("%s: Aight, what are we talking?\n", customer[customerPicked].customerName); - printf("Enter desired item price:\n"); - scanf("%d", &potNewItemPrice); - if (checkCustomerWiggle(potNewItemPrice, customerPicked, itemPicked)) - { - player.playerMoney += item[itemPicked].itemCurrentItemPrice; - player.playerScore += 2; - item[itemPicked].itemInInventory = false; - player.totalItemsSold++; - printf("Sold!\n"); - break; - } - else - { - printf("%s: Nah, think I'll pass\n", customer[customerPicked].customerName); - if(player.playerScore != 0) - { - player.playerScore -= 1; - } - break; - } + negotiateItem(itemPicked, customerPicked); + break; case 3: - printf("%s: Alright then, maybe some other time\n", customer[customerPicked].customerName); + printf("%s: Alright then, maybe some other time\n\n", customer[customerPicked].customerName); break; } } @@ -521,7 +548,7 @@ void gameEnd(int turns) { printf("Game End!\n"); printf("Total turns played: %d\n", turns); - printf("Player stats:\n"); + printf("Player Stats:\n"); printExtStats(); } -- 2.49.1