Code:
//Header Files
#include<stdio.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
//Function Prototypes - First declaration
void instructions();
void playGame();
void highRollers();
void quitGame();
//BETA FUNCTION PROTOTYPES - Testing Purposes!!!!
float sharePrices();
float buyShares(float oilPrice, float coalPrice, float gasPrice, float electricityPrice, float cottonPrice, float sugarPrice, int turnCount, int numberofUnits, float bankBalance);
int main()
{
//BETA VARIABLES
//----------------------------------------------------------------------------------\\
// Top Level Menu Construction - From here, all relevant top level choices are made \\
//----------------------------------------------------------------------------------\\
//Variable for storing mainMenu option selection
char mainMenu;
//Clear the screen before displaying options
system("cls");
//Display mainMenu options
printf(" ______________________\n");
printf("| |\n");
printf("| MAIN MENU |\n");
printf("|______________________|\n\n");
printf("\nPlease choose an option: \n\n");
printf(" 1. Instructions\n\n");
printf(" 2. Play Game\n\n");
printf(" 3. High Rollers\n\n");
printf(" 4. Quit Game\n");
printf("\n\n");
//Get user selection
scanf("%c", &mainMenu);
_flushall();
///Switch for deciding which option was selected - Compares the value held in the mainMenu variable
switch(mainMenu)
{
case '1':
instructions();
break;
case '2':
playGame();
break;
case '3':
highRollers();
break;
case '4':
quitGame();
break;
default:
printf("Invalid Entry");
break;
}
//--------------------\\
// End Top Level Menu \\
//--------------------\\
return 0;
}//End Main
//--------------------------------------------------------------------------------------------
// A simple menu function for showing the instructions and a means to return to the main menu
//--------------------------------------------------------------------------------------------
void instructions()
{
//Variable for storing instructionMenu option selection
char instructionMenu;
//Clear the screen before displaying options
system("cls");
//Display instructional information - To be completed!!!
printf("Instructional Stuff\n\n");
//Display instructionMenu options
printf("Press 1 followed by <ENTER> to return to the main menu\n\n");
//Get user selection
scanf("%c", &instructionMenu);
_flushall();
///Switch for deciding which option was selected by the user - Compares the value held in the instructionMenu variable
switch (instructionMenu)
{
case '1':
main();
break;
default:
system("cls");
printf("An Error Has Occoured\n\n");
system("pause");
main();
break;
}
}
//---------------------------------------------------------------------------------------------------------
// This is going to be the core of the "Game Engine..." All gameplay will end up running via this function
//---------------------------------------------------------------------------------------------------------
void playGame()
{
//Turn Counter - All Game play must go within the do loop.. Once a "turn" has been completed, +1 to turnCount
//This constitutes the overall counter and control of how many turns a player has to make their max profits
int turnCount = 0;
do
{
//Adds an incremental value of 1 to the turnCount variable at the start of each itteration of the do loop
turnCount++;
//Game play coding here - Will poss need to pass data between the function calls that will exist here
//BETA AREA!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!------------------------------------------------------
//Store Game Data Variables
//Stores buy or sell option selection
char buySell;
//Player Name - Store current players name - to be used with high score data later
char playerName[20];
//Bank Balance @ start will not hold updated balance - just used to hold initial starting figure..
float bankBalance = 500;
//Number of Individual Shares Owned
int oilSharesOwned = 0;
int coalSharesOwned = 0;
int gasSharesOwned = 0;
int electricitySharesOwned = 0;
int sugarSharesOwned = 0;
int cottonSharesOwned = 0;
//Total number of shares owned
int numberofUnits = 0;
//Get random share prices and assign them to each commodity as a float variable
float oilPrice = sharePrices();
float coalPrice = sharePrices();
float gasPrice = sharePrices();
float electricityPrice = sharePrices();
float sugarPrice = sharePrices();
float cottonPrice = sharePrices();
//Clear the screen before displaying Data
system("cls");
//Build Play Screen Layout Demo Here
//printf("Please type in Your Name followed by <ENTER>:\n\n");
//Obtain the input through the keyboard
//fgets(playerName, sizeof(playerName), stdin);
// _flushall();
system("cls");
printf("Your Current Balance is: %.2f\n\n", bankBalance);
//Find a way of setting start date and count days and then display day and date - add realism..?
printf("Todays Share Prices Are As Follows:\n\n");
printf("%i. Oil Price $%.2f\n", turnCount, oilPrice);
printf("%i. Coal Price: $%.2f\n", turnCount, coalPrice);
printf("%i. Gas Price: $%.2f\n", turnCount, gasPrice);
printf("%i. Electricity Price: $%.2f\n", turnCount, electricityPrice);
printf("%i. Sugar Price: $%.2f\n", turnCount, sugarPrice);
printf("%i. Cotton Price: $%.2f\n", turnCount, cottonPrice);
//Display Buy Sell Menu
printf("\n\nWould you like to (B)uy or (S)ell?\n\n");
scanf("%c", &buySell);
_flushall();
switch (buySell)
{
case 'b':
buyShares(oilPrice, coalPrice, gasPrice, electricityPrice, cottonPrice, sugarPrice, turnCount, numberofUnits, bankBalance);
break;
case 's':
printf("Sell some ........ function");
break;
default:
printf("Some ........ is broken waffle here");
break;
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!---------------------------------------------
//Temporary while testing "Game Engine" - Currently halts the program so you can see stuff
system("pause");
}while(turnCount < 100);//While condition used to specify the amount of maximum turns for the game
//Temporary End Game Message
system("cls");
printf("Thank you for playing, your score was ......... Don't give up your day job!\n\n");
//Temporary while testing "Game Engine" - Currently halts the program so you can see stuff
system("pause");
}
//-----------------------------------------------------------------------------------------------------
// A simple function for writing high scores to a file to be read back later when scores are displayed
//-----------------------------------------------------------------------------------------------------
void highRollers()
{
}
//----------------------------------------------------------------------------------------------------------------------
// A function devised to allow users to exit the game at any time using the press of a single designated key or similar
//----------------------------------------------------------------------------------------------------------------------
void quitGame()
{
}
//BETA FUNCTIONS - Functions created for testing purposes
//Generate random prices for shares
float sharePrices()
{
//Needs to be more random & accurate to within a given price range eg $1.00 - $200.00
float i = (float)rand() / (float)100;
return i;
}
//Buy function - Value of the generated coalPrice variable is passed into this function! Will need to add all comodities to this function
float buyShares(float oilPrice, float coalPrice, float gasPrice, float electricityPrice, float cottonPrice, float sugarPrice, int turnCount, int numberofUnits, float bankBalance)
{
system("cls");
char buySharesOption;
char unitsOption;
//Test Variable is passed
//printf("The price of coal is still: $%.2f\n\n", coalPrice);
//Not decided how best to develop this area... May need a switch if it allows me to pass data deep enough
//If not a bunch of ifs and else --
//First menu here on in will be to discover which comodity is being purchased.
//THe second menu will establish the quantity being purchased.
//Once the quantity and comodity is established, we can then check to see if the number of stocks attempting to be purchased
//will exceed the maximum stock level one play may hold at one time.. Currently 100 shares.
//We can use if statements for this purpose.
//Then we can move on to do the maths and worry about updating the bank balance & sharesowned variables
//Rememeber to store the number of purchased comodity within the apropriate variable for that comodity.
printf("Which comodity would you like to purchase today\n\n");
printf(" 1. Oil\n\n");
printf(" 2. Coal\n\n");
printf(" 3. Gas\n\n");
printf(" 4. Electricity\n\n");
printf(" 5. Cotton\n\n");
printf(" 6. Sugar\n\n");
printf(" 7. Back to Shares\n\n");
scanf("%c", &buySharesOption);
_flushall();
printf("\nNow please enter the number of shares you wish to buy:\n\n");
printf("You currently own %i shares\n\n", numberofUnits);
scanf("%c", &unitsOption);
_flushall();
if(buySharesOption == '1')
{
float bankBalance = bankBalance - (oilPrice * unitsOption);
//numberofUnits+unitsOption;
return bankBalance;
}//Etc,...
//Do some clever maths here after finding out which comodity is being purchased & in what quantity
// bankBalance= bankBalance - (sharePrice * noUnits);
}
The best example of what I want to do is shown just above with that little bit of mathematics.....