1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace std;
// declare prototypes
void intializeArrays(string param1[], int param2[], float param3[]);
int actionOnSpace();
int main()
{
int numPlayers = 20; // 20 players (this would be prompted to the user)
const int maxSpace = 25;
// for names, with some dummy test data, these would obviously be entered
// during runtime by the players - here for testing - please see the
// intializeArrays as i have commented out the code that clears this so
// that this test data isnt cleared just to show you what can be done.
string names[20] = {
"Michael",
"Jill",
"Denny",
"Mark",
"Dylan",
"Debbie",
"Ann",
"Jack",
"Amy",
"Danny",
"George",
"Amanda",
"Alison",
"Kevin",
"Tim",
"Adrian",
"Anthony",
"Bjorn",
"Tony",
"Daniel"
};
float money[20]; // players money
int boardSpace[25]; // player position
int move, wins; // temp for rand
intializeArrays(names, boardSpace, money); // initialise the variables
// set the random seed.
srand(time(NULL));
for (int plrCount = 0; plrCount < numPlayers; plrCount++)
{
cout << endl << names[plrCount] << ", press Enter to roll the dice...";
cin.get();
cin.sync(); // you need this to clear out the buffer and to avoid skipping
// loop through each player updating their positions
move = rand() % 6 + 1; // roll the dice
// tell them what they rolled...
cout << endl << "Well done " << names[plrCount] << ", you rolled " << move << " on the dice!" << endl;
if ((move + boardSpace[plrCount]) > maxSpace) {
// we have generated a number which exceeds the maze end. technically the game
// would be over here now that a player has reached the end before the others??
boardSpace[plrCount] = maxSpace;
// do we want to add some funds?
if (actionOnSpace() == 1) {
// we got a 1, do a random number between 1 - 100000
// and add it to players funds.
wins = rand() % 100000 + 1;
money[plrCount] += wins;
}
}
else
{
// lets make a move, we are still going.
boardSpace[plrCount] += move;
// we got a 1, do a random number between 1 - 100000
// and add it to players funds.
if (actionOnSpace() == 1) {
// we got a 1, do a random number
wins = rand() % 100000 + 1;
money[plrCount] += wins;
}
}
cout << "Player " << plrCount + 1 << " ("<<names[plrCount]<<") is at position " << boardSpace[plrCount] << " and has $" << money[plrCount] << " funds." << endl;
}
return 0;
}
// Dummy function to simulate a 1 to allow
// testing of code - see your pdf
int actionOnSpace()
{
return 1;
// from what i read you do a rand here to either get 0 or 1
// if 1 you open up the good.txt file or 0 the bad.txt file
// those txt files have a list of 20 messages in each
// you then rand a number between 1 to 20 and create a loop
// using that number to pick out the message. i.e. if you
// rand 10 it would loop and display the 10th message in
// that text file.
// it then returns the first rand (1 or 0) back to the caller
// which in your case is main (ive simulated a 1 here)
}
// clear out the arrays
void intializeArrays(string param1[], int param2[], float param3[])
{
// i could have used memset here to clear out the memory
// but it wants you to use loops etc so here goes.
for (int i = 0; i < 20; i++) {
// param1[i].clear(); // names (disabled as i populated some test data)
param3[i] = 0; // money
}
for (int i = 0; i < 25; i++)
param2[i] = 0; // positions
}
|