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
|
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <cctype>
using namespace std;
//declare functions
void Opening (void);
void GetPLayerInfo (struct AllPlayersT * Player);
void PlayGame (string & winner, int & winnerAge, string & winnerThing, struct AllPlayersT * Player);
void PrepareGameBoard (string & winner, int newCherries, int & winnerAge, string & winnerThing, struct AllPlayersT * Player);
......
//declare constants
const int MAX_CHERRIES = 10;
const int MAX_NUMBER_OF_PLAYERS = 4;
const string COMP_1 = "Alphie";
const string COMP_2 = "Bettie";
const string COMP_3 = "Deltie";
const string COMP_4 = "Gammie";
//declare player struct
struct AllPlayersT
{ string name, thing;
int age, playerCherry;
};
int main (){
//declare variables
int rounds = 0, winnerAge =0;
string winner, winnerThing, enter;
AllPlayersT Player [MAX_NUMBER_OF_PLAYERS];
Opening();
winner = "NONE";
//Player and computer info
GetPLayerInfo(Player);
cout << "Now let's play the game! You go first " << Player[1].name << ". Hit ENTER to spin!" << endl;
getline(cin, enter);
while (winner.find("NONE") != string::npos)
{ rounds ++;
PlayGame(winner, winnerAge, winnerThing, Player);
}
//make sure correct ending
CorrectEnding(winner, winnerThing, winnerAge, rounds);
return 0;
}
/* This routine welcomes the player to the game.
no input or output
*/
void Opening (void)
{ cout << "Hello, and welcome to a fun game of Hi Ho! Cherry-O!";
cout << endl << endl;
return;
}
/* This routine gets the players information in the PLayerT struct.
no input
Its output is the stuct human.Player.
*/
void GetPLayerInfo(struct AllPlayersT * Player)
{ int i=0, times = 1;
char answer;
while (i != MAX_NUMBER_OF_PLAYERS)
{ i ++;
cout << "Are you a player or commputer (p for player or c for computer)=>";
cin >> answer;
if (answer == 'p')
{ cin.ignore(80,'\n');
cout << "What is your name? => ";
getline(cin, Player[i].name);
cout << endl;
cout << "How old are you? => ";
cin >> Player[i].age;
Player[i].thing = "human";
}
if (answer == 'c')
{ if (times == 1)
{Player[i].name = COMP_1;
}
if (times == 2)
{Player[i].name = COMP_2;
}
if (times == 3)
{Player[i].name = COMP_3;
}
if (times == 4)
{Player[i].name = COMP_4;
}
Player[i].thing = "computer";
cout << "How old is this computer player? => ";
cin >> Player[i].age;
times ++;
}
cout << endl << endl;
}
return;
}
/* The routine contain how the game is played.
the input is the humanplayer's info and all the number of cherries in each basket.
the output is the string which will contain the winner's name
*/
void PlayGame(string & winner, int & winnerAge, string & winnerThing, struct AllPlayersT * Player)
{ int number = 0, newCherries = 0, i = 0;
srand (time(NULL));
cout << "here" << endl;
while (i != MAX_NUMBER_OF_PLAYERS)
{ i ++;
cout << "in here" << endl;
if (Player[i].thing == "human")
{ newCherries = 0;
cout << Player[i].name << ", it is your turn," << endl;
cout << "Press Enter to spin." << endl;
cin.ignore();
cout << endl << endl;
number = rand()%10 - 1;
Spinner (number, newCherries);
}
if (Player[i].thing == "computer")
{ newCherries = 0;
cout << "It is now " << Player[i].name << "'s turn." << endl;
cout << Player[i].name << " spun the spinner." << endl;
cout << endl << endl;
number = rand()%10 - 1;
Spinner (number, newCherries);
}
}
PrepareGameBoard(winner, newCherries, winnerAge, winnerThing, Player);
if (winner.find("NONE") == string::npos)
{
return;
}
GameBoard(Player);
cout << "Press ENTER to contine:" << endl;
cin.ignore();
cout << endl << endl;
return;
}
......
|