Holey_yan

20150416_KUAS_VisualC++_Exam07

Apr 16th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. /***************************************/
  2. /** 20150416 Exam 07                  **/
  3. /** Maker  : Yan                      **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2015/04/16 pm.13:30      **/
  6. /***************************************/
  7.  
  8. //C input / output library
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #include <iostream> //C++ Input / Output library
  13. #include <iomanip> //To format output --> [setw()]
  14. using namespace std;
  15.  
  16. //Use for Sleep() --> *mark on [Delay]
  17. #include <Windows.h>
  18. //Use for random the [random number]
  19. #include <time.h>
  20.  
  21. class RunningGame{
  22. private:
  23.     int NumOfRunner;
  24.     int TripLenght;
  25.     int *RunnerNumber; //To discern who is rabit, who is turtle
  26.     int *RunnerAlreadyRun;
  27.     int *Rank;
  28.     int RankLenght;
  29.  
  30. public:
  31.     void FormatVariable(int NumOfRunnerFromMain, int TripLenghtFromMain){
  32.         NumOfRunner = NumOfRunnerFromMain;
  33.  
  34.         TripLenght = TripLenghtFromMain;
  35.  
  36.         RunnerNumber = (int *)malloc(NumOfRunner * sizeof(int));
  37.         for (int i = 0; i < NumOfRunner; i++)
  38.             RunnerNumber[i] = i;
  39.         for (int i = 0; i < NumOfRunner * NumOfRunner; i++){
  40.             int a = rand() % NumOfRunner;
  41.             int b = rand() % NumOfRunner;
  42.             int tmp = RunnerNumber[a];
  43.             RunnerNumber[a] = RunnerNumber[b]; RunnerNumber[b] = tmp;
  44.         }
  45.  
  46.         RunnerAlreadyRun = (int *)malloc(NumOfRunner * sizeof(int));
  47.         Rank = (int *)malloc(NumOfRunner * sizeof(int));
  48.         for (int i = 0; i < NumOfRunner; i++){
  49.             RunnerAlreadyRun[i] = 0;
  50.             Rank[i] = EOF;
  51.         }
  52.  
  53.         RankLenght = NumOfRunner;
  54.     }
  55.  
  56.     void RunnerRun(){
  57.         for (int i = 0; i < NumOfRunner; i++){
  58.             if (RunnerAlreadyRun[i] < TripLenght)
  59.                 RunnerAlreadyRun[i] += rand() %5;
  60.         }
  61.     }
  62.  
  63.     void ShowOnBoard(){
  64.         for (int i = 0; i < NumOfRunner; i++){
  65.             if (RunnerNumber[i] < (NumOfRunner / 2))
  66.                 cout << "Rabit  " << setw(2) << RunnerNumber[i] % (NumOfRunner / 2) << " ||";
  67.             else
  68.                 cout << "Turtle " << setw(2) << RunnerNumber[i] % (NumOfRunner / 2) << " ||";
  69.  
  70.             for (int j = 0; j < ((TripLenght <= RunnerAlreadyRun[i]) ? TripLenght : RunnerAlreadyRun[i]); j++)
  71.                 cout << "#";
  72.             for (int j = 0; j < ((TripLenght <= RunnerAlreadyRun[i]) ? -1 : (TripLenght - RunnerAlreadyRun[i])); j++)
  73.                 cout << " ";
  74.             cout << "||" << endl;
  75.         }
  76.     }
  77.  
  78.     bool AddRank(){
  79.         if (Rank[NumOfRunner - 1] != EOF){
  80.             ShowRankList();
  81.             return true; //Rank add over
  82.         } else {
  83.             for (int i = 0; i < NumOfRunner; i++){
  84.                 if (RunnerAlreadyRun[i] >= TripLenght){
  85.                     for (int j = 0; j < RankLenght; j++){
  86.                         if (Rank[j] == RunnerNumber[i]){
  87.                             break;
  88.                         } else {
  89.                             if (Rank[j] == EOF){
  90.                                 Rank[j] = RunnerNumber[i];
  91.                                 break;
  92.                             }
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.         ShowRankList();
  99.         return false;
  100.     }
  101.  
  102.     void ShowRankList(){
  103.         cout << endl << "-------- Rank List --------" << endl;
  104.         for (int i = 0; i < RankLenght; i++){
  105.             if (Rank[i] != EOF){
  106.                 cout << setw(2) << i + 1 << ". ";
  107.                 if (Rank[i] < (NumOfRunner / 2))
  108.                     cout << "Rabit  " << Rank[i] % (NumOfRunner / 2) << endl;
  109.                 else
  110.                     cout << "Turtle " << Rank[i] % (NumOfRunner / 2) << endl;
  111.             } else {
  112.                 for (int j = i; j < RankLenght; j++){
  113.                     cout << setw(2) << j << ". ";
  114.                     cout << "None" << endl;
  115.                 }
  116.                 return;
  117.             }
  118.         }
  119.     }
  120. };
  121.  
  122. int main(int argc, const char *argv[]){
  123.     cout << "How many runner do you want: ";
  124.     int NumOfRunner = 0;
  125.     if (scanf_s("%d", &NumOfRunner) <= 0) {
  126.         return 0;
  127.     } else {
  128.         cout << "Enter a number of trip lenght: ";
  129.         int TripLenght = 0;
  130.         scanf_s("%d", &TripLenght);
  131.  
  132.         RunningGame RunningGame;
  133.         RunningGame.FormatVariable(NumOfRunner, TripLenght);
  134.         while(1) {
  135.             Sleep(0.5 * 1000);
  136.             system("cls");
  137.  
  138.             RunningGame.RunnerRun();
  139.             RunningGame.ShowOnBoard();
  140.             if (RunningGame.AddRank() == true){
  141.                 system("pause"); return 0;
  142.             }
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment