Merge pull request #1 from Splatink/police
This update adds the police, a fun feature where by selling illegal items the police can get suspicious and attempt to arrest the player. It also splits a lot of things into functions, and adds definitions for certain values so they can easily be changed without looking through every function. Also fixed a couple of bugs, and adjusted some printed text.
This commit was merged in pull request #1.
This commit is contained in:
223
main.c
223
main.c
@@ -3,6 +3,24 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
//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
|
struct gameItem
|
||||||
{
|
{
|
||||||
@@ -23,9 +41,14 @@ struct gameCustomer
|
|||||||
struct gamePlayer
|
struct gamePlayer
|
||||||
{
|
{
|
||||||
char playerName[10];
|
char playerName[10];
|
||||||
unsigned int playerScore;
|
int playerScore;
|
||||||
unsigned int playerMoney;
|
int playerMoney;
|
||||||
unsigned totalItemsSold;
|
int totalItemsSold;
|
||||||
|
int illegalItemsSold;
|
||||||
|
bool warrantForArrest;
|
||||||
|
bool gameForceEnd;
|
||||||
|
bool playerNotified;
|
||||||
|
bool gameKilled;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct gameItem item[10] =
|
struct gameItem item[10] =
|
||||||
@@ -38,7 +61,7 @@ struct gameItem item[10] =
|
|||||||
{"TV", 400, 400, 20, 0},
|
{"TV", 400, 400, 20, 0},
|
||||||
{"Smartphone", 0, 1200, 0, 0},
|
{"Smartphone", 0, 1200, 0, 0},
|
||||||
//special items
|
//special items
|
||||||
{"Snake Oil", 65, 65, 150, 0},
|
{"Ink", 65, 65, 150, 0},
|
||||||
{"Weed", 12, 12, 300, 0},
|
{"Weed", 12, 12, 300, 0},
|
||||||
{"Crypto", 150, 150, 500, 0}
|
{"Crypto", 150, 150, 500, 0}
|
||||||
};
|
};
|
||||||
@@ -92,7 +115,7 @@ void gameIntro()
|
|||||||
printf("The game ends after your chosen amount of turns and you get a score!\n");
|
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");
|
printf("Please enter your player name (Max 10 characters)\n");
|
||||||
scanf("%s", &player.playerName);
|
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);
|
printf("You start with %d gold and 1 %s\n", player.playerMoney, item->itemName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,7 +281,12 @@ void buyItems()
|
|||||||
|
|
||||||
bool checkForItem(int itemPicked)
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -268,6 +296,124 @@ bool checkForItem(int itemPicked)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void policeAccept()
|
||||||
|
{
|
||||||
|
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 > POLICE_DECLINE_SUCCESS)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
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 > POLICE_SHOOT_SUCCESS)
|
||||||
|
{
|
||||||
|
printf("You were shot and killed by 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 == 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 >= POLICE_ARREST)
|
||||||
|
{
|
||||||
|
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)
|
int gameUI(int itemPicked)
|
||||||
{
|
{
|
||||||
printf("A: Accept\nN: Negotiate\nD: Decline\nS: Print Stats\nI: Print Inventory\nB: Buy Items\n");
|
printf("A: Accept\nN: Negotiate\nD: Decline\nS: Print Stats\nI: Print Inventory\nB: Buy Items\n");
|
||||||
@@ -325,24 +471,18 @@ bool checkCustomerWiggle(int potNewItemPrice, int customerPicked, int itemPicked
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void gameTurn()
|
void sellItem(int itemPicked)
|
||||||
{
|
{
|
||||||
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);
|
|
||||||
int selection = gameUI(itemPicked);
|
|
||||||
switch (selection)
|
|
||||||
{
|
|
||||||
case 1:
|
|
||||||
player.playerMoney += item[itemPicked].itemCurrentItemPrice;
|
player.playerMoney += item[itemPicked].itemCurrentItemPrice;
|
||||||
player.playerScore++;
|
player.playerScore++;
|
||||||
item[itemPicked].itemInInventory = false;
|
item[itemPicked].itemInInventory = false;
|
||||||
player.totalItemsSold++;
|
player.totalItemsSold++;
|
||||||
printf("Sold!\n");
|
printf("Sold!\n\n");
|
||||||
break;
|
}
|
||||||
case 2:
|
|
||||||
|
void negotiateItem(int itemPicked, int customerPicked)
|
||||||
|
{
|
||||||
|
int potNewItemPrice;
|
||||||
printf("%s: Aight, what are we talking?\n", customer[customerPicked].customerName);
|
printf("%s: Aight, what are we talking?\n", customer[customerPicked].customerName);
|
||||||
printf("Enter desired item price:\n");
|
printf("Enter desired item price:\n");
|
||||||
scanf("%d", &potNewItemPrice);
|
scanf("%d", &potNewItemPrice);
|
||||||
@@ -352,20 +492,41 @@ void gameTurn()
|
|||||||
player.playerScore += 2;
|
player.playerScore += 2;
|
||||||
item[itemPicked].itemInInventory = false;
|
item[itemPicked].itemInInventory = false;
|
||||||
player.totalItemsSold++;
|
player.totalItemsSold++;
|
||||||
printf("Sold!\n");
|
printf("Sold!\n\n");
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("%s: Nah, think I'll pass\n", customer[customerPicked].customerName);
|
printf("%s: Nah, think I'll pass\n\n", customer[customerPicked].customerName);
|
||||||
if(player.playerScore != 0)
|
if(player.playerScore != 0)
|
||||||
{
|
{
|
||||||
player.playerScore -= 1;
|
player.playerScore -= 1;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void gameTurn()
|
||||||
|
{
|
||||||
|
checkPolice();
|
||||||
|
if (player.gameKilled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
checkMoney();
|
||||||
|
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:
|
||||||
|
sellItem(itemPicked);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
negotiateItem(itemPicked, customerPicked);
|
||||||
|
break;
|
||||||
case 3:
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -387,7 +548,7 @@ void gameEnd(int turns)
|
|||||||
{
|
{
|
||||||
printf("Game End!\n");
|
printf("Game End!\n");
|
||||||
printf("Total turns played: %d\n", turns);
|
printf("Total turns played: %d\n", turns);
|
||||||
printf("Player stats:\n");
|
printf("Player Stats:\n");
|
||||||
printExtStats();
|
printExtStats();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -395,11 +556,19 @@ int main()
|
|||||||
{
|
{
|
||||||
initCustomers();
|
initCustomers();
|
||||||
gameIntro();
|
gameIntro();
|
||||||
|
int turns;
|
||||||
int numOfTurns = turnSelector();
|
int numOfTurns = turnSelector();
|
||||||
for (int i = 0; i < numOfTurns; i++)
|
for (turns = 0; turns < numOfTurns; turns++)
|
||||||
|
{
|
||||||
|
if (player.gameForceEnd)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
gameTurn();
|
gameTurn();
|
||||||
}
|
}
|
||||||
gameEnd(numOfTurns);
|
}
|
||||||
|
gameEnd(turns);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user