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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/*
int random(int max)
Calculates a random number from 1 to max
Parameters:
max, value parameter for highest possible value
Return Type: int for random number from 1 to max
*/
int random(int max)
{
int r;
//Feed random number generator
srand(time(NULL));
//generate number from 1 to max
r = rand() % max + 1;
cout << r;
return r;
}
/*
void CalculateScores(gameChoice p1Choice, gameChoice p2Choice,
PlayerScores& scores)
Determines winner based on choice of weapon
Parameters:
p1Choice: Value Parameter for player 1 choice
p2Choice: Value Parameter for player 2 choice
scores: Reference Parameter for where score is updated
Return type: void
*/
void CalculateScores(gameChoice p1Choice, gameChoice p2Choice,
PlayerScores& scores)
{
//Table of outcomes
PlayerScores pscores[3][3]=
{
{//P1 picks Rock
{0,0},{0,1},{1,0}
},
{//P1 picks Paper
{1,0},{0,0},{0,1}
},
{//P1 picks Scissors
{0,1},{1,0},{0,0}
},
};
//find the winner
scores.p1Score += pscores[p1Choice][p2Choice].p1Score;
scores.p2Score += pscores[p1Choice][p2Choice].p2Score;
}
/*
void UpdateScores(HighScore Old[], HighScore New, int size)
Insert current score into Highscores if high enough.
Parameters:
Old: Old Highscores read from text file
New: New Highscores to compare to old
size: Number of Highscore entries
Return Type: void
*/
void UpdateScores(HighScore Old[], HighScore New, int size)
{
//Check scores
for(int i = 0; i<size; i++)
{
if(New.score>Old[i].score)
{
for(int ii=size; ii>i; i++)
{
Old[ii].score=Old[ii-1].score;
Old[ii].name=Old[ii-1].name;
}
Old[i].score=New.score;
i=size;
}
}
}
/*
ofstream GetHighscores(HighScore Score[], int size)
Read Highscores from file, if file does not exist, create it.
Return file for later output
Parameters:
Score: array of scores to store new scores
Size: size of array
Return Type: void
*/
void GetHighscores(HighScore Score[], int size)
{
ofstream outFile;
ifstream inFile;
//Read the file
inFile.open("highscore.txt");
//Check if file doesn't exist
if(!inFile)
{
inFile.close();
//Create file
outFile.open("highscore.txt");
outFile << "Bob" << endl;
outFile << 3;
outFile << "Joe" << endl;
outFile << 2;
outFile << "Smith" << endl;
outFile << 1;
outFile.close();
inFile.open("highscore.txt");
}
//read contents of file
for (int i=0;i<size;i++){
getline(inFile, Score[i].name);
inFile >> Score[i].score;
}
//Close infile and open outfile
inFile.close();
}
/*
gameChoice getChoice()
Prompts user for selection.
Parameters:
None
Return Type: gameChoice enum type for Players choice
*/
gameChoice getChoice()
{
gameChoice pChoice;
int gameMenu=-4;
//Menu
while(gameMenu>3 || gameMenu<1)//Prompt user for input
{
cout << "1. Rock"
<< endl << "2. Paper"
<< endl << "3. Scissors"
<< endl; //Display menu
cout << "Enter your choice: ";
cin >> gameMenu;//User input
if(gameMenu>3 || gameMenu<1)
cout << "Error: Invalid input."
<< endl;
}
cout << endl;
//Set player selection
if(gameMenu==1)
pChoice=ROCK;
if(gameMenu==2)
pChoice=PAPER;
if(gameMenu==3)
pChoice=SCISSORS;
return pChoice;
}
/*
gameChoice cpuChoice()
Gets cpu selection.
Parameters:
None.
Return Type: gameChoice enum type
*/
gameChoice cpuChoice()
{
gameChoice pChoice;
int gameMenu;
gameMenu=random(3);
if(gameMenu==1)
pChoice=ROCK;
if(gameMenu==2)
pChoice=PAPER;
if(gameMenu==3)
pChoice=SCISSORS;
return pChoice;
}
|