Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************/
- /** 20150416 Exam 07 **/
- /** Maker : Yan **/
- /** E-Mail : [email protected] **/
- /** Date : 2015/04/16 pm.13:30 **/
- /***************************************/
- //C input / output library
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream> //C++ Input / Output library
- #include <iomanip> //To format output --> [setw()]
- using namespace std;
- //Use for Sleep() --> *mark on [Delay]
- #include <Windows.h>
- //Use for random the [random number]
- #include <time.h>
- class RunningGame{
- private:
- int NumOfRunner;
- int TripLenght;
- int *RunnerNumber; //To discern who is rabit, who is turtle
- int *RunnerAlreadyRun;
- int *Rank;
- int RankLenght;
- public:
- void FormatVariable(int NumOfRunnerFromMain, int TripLenghtFromMain){
- NumOfRunner = NumOfRunnerFromMain;
- TripLenght = TripLenghtFromMain;
- RunnerNumber = (int *)malloc(NumOfRunner * sizeof(int));
- for (int i = 0; i < NumOfRunner; i++)
- RunnerNumber[i] = i;
- for (int i = 0; i < NumOfRunner * NumOfRunner; i++){
- int a = rand() % NumOfRunner;
- int b = rand() % NumOfRunner;
- int tmp = RunnerNumber[a];
- RunnerNumber[a] = RunnerNumber[b]; RunnerNumber[b] = tmp;
- }
- RunnerAlreadyRun = (int *)malloc(NumOfRunner * sizeof(int));
- Rank = (int *)malloc(NumOfRunner * sizeof(int));
- for (int i = 0; i < NumOfRunner; i++){
- RunnerAlreadyRun[i] = 0;
- Rank[i] = EOF;
- }
- RankLenght = NumOfRunner;
- }
- void RunnerRun(){
- for (int i = 0; i < NumOfRunner; i++){
- if (RunnerAlreadyRun[i] < TripLenght)
- RunnerAlreadyRun[i] += rand() %5;
- }
- }
- void ShowOnBoard(){
- for (int i = 0; i < NumOfRunner; i++){
- if (RunnerNumber[i] < (NumOfRunner / 2))
- cout << "Rabit " << setw(2) << RunnerNumber[i] % (NumOfRunner / 2) << " ||";
- else
- cout << "Turtle " << setw(2) << RunnerNumber[i] % (NumOfRunner / 2) << " ||";
- for (int j = 0; j < ((TripLenght <= RunnerAlreadyRun[i]) ? TripLenght : RunnerAlreadyRun[i]); j++)
- cout << "#";
- for (int j = 0; j < ((TripLenght <= RunnerAlreadyRun[i]) ? -1 : (TripLenght - RunnerAlreadyRun[i])); j++)
- cout << " ";
- cout << "||" << endl;
- }
- }
- bool AddRank(){
- if (Rank[NumOfRunner - 1] != EOF){
- ShowRankList();
- return true; //Rank add over
- } else {
- for (int i = 0; i < NumOfRunner; i++){
- if (RunnerAlreadyRun[i] >= TripLenght){
- for (int j = 0; j < RankLenght; j++){
- if (Rank[j] == RunnerNumber[i]){
- break;
- } else {
- if (Rank[j] == EOF){
- Rank[j] = RunnerNumber[i];
- break;
- }
- }
- }
- }
- }
- }
- ShowRankList();
- return false;
- }
- void ShowRankList(){
- cout << endl << "-------- Rank List --------" << endl;
- for (int i = 0; i < RankLenght; i++){
- if (Rank[i] != EOF){
- cout << setw(2) << i + 1 << ". ";
- if (Rank[i] < (NumOfRunner / 2))
- cout << "Rabit " << Rank[i] % (NumOfRunner / 2) << endl;
- else
- cout << "Turtle " << Rank[i] % (NumOfRunner / 2) << endl;
- } else {
- for (int j = i; j < RankLenght; j++){
- cout << setw(2) << j << ". ";
- cout << "None" << endl;
- }
- return;
- }
- }
- }
- };
- int main(int argc, const char *argv[]){
- cout << "How many runner do you want: ";
- int NumOfRunner = 0;
- if (scanf_s("%d", &NumOfRunner) <= 0) {
- return 0;
- } else {
- cout << "Enter a number of trip lenght: ";
- int TripLenght = 0;
- scanf_s("%d", &TripLenght);
- RunningGame RunningGame;
- RunningGame.FormatVariable(NumOfRunner, TripLenght);
- while(1) {
- Sleep(0.5 * 1000);
- system("cls");
- RunningGame.RunnerRun();
- RunningGame.ShowOnBoard();
- if (RunningGame.AddRank() == true){
- system("pause"); return 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment