Holey_yan

20150409_KUAS_VisualC++_RabitRunning(Class)

Apr 9th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. /***************************************/
  2. /** 20150409 Rabit Running (Class)    **/
  3. /** Maker  : Yan                      **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2015/04/09 pm.08:00      **/
  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 Rabit{
  22. public:
  23.     void RunRabit(int rabitNum, int *rabitAlreadyRun){
  24.         for (int i = 0; i < rabitNum; i++)
  25.             rabitAlreadyRun[i] += rand() % 3;
  26.     }
  27.  
  28.     void ShowOnForm(int rabitNum, int *rabitAlreadyRun, int tripLenght){
  29.         for (int i = 0; i < rabitNum; i++){
  30.             cout << "Rabit " << setw(2) << (i + 1) << " ||";
  31.             for (int j = 0; j < ((tripLenght <= rabitAlreadyRun[i]) ? tripLenght : rabitAlreadyRun[i]); j++)
  32.                 cout << "#";
  33.             for (int j = 0; j < ((tripLenght <= rabitAlreadyRun[i]) ? -1 : (tripLenght - rabitAlreadyRun[i])); j++)
  34.                 cout << " ";
  35.             cout << "||" << endl;
  36.         }
  37.     }
  38.  
  39.     bool CheckWinner(int rabitNum, int *rabitAlreadyRun, int tripLenght){
  40.         for (int i = 0; i < rabitNum; i++){
  41.             if (rabitAlreadyRun[i] >= tripLenght){
  42.                 cout << endl << "Rabit " ;
  43.                 for (int j = 0; j < rabitNum; j++){
  44.                     if (rabitAlreadyRun[j] >= tripLenght)
  45.                         cout << j + 1 << ", ";
  46.                 }
  47.                 cout << "\b\b" << " Win !" << endl;
  48.                 return true;;
  49.             }
  50.         }
  51.         return false;
  52.     }
  53.  
  54.     void Delay(double second){
  55.         Sleep(second * 1000);
  56.     }
  57. };
  58.  
  59. int main(int argc, char const *argv[]){
  60.     cout << "Enter a nuber of rabit: ";
  61.  
  62.     int rabitNum = 0;
  63.     if (scanf_s("%d", &rabitNum) <= 0)
  64.         return 0;
  65.     else {
  66.         int tripLenght = 0;
  67.         cout << "Enter a number of trop lenght: ";
  68.         if (scanf_s("%d", &tripLenght) == 0)
  69.             return 0;
  70.         else{
  71.             //Make a array to record now rabit aleady run lenght, and make refresh
  72.             int *rabitAlreadyRun = (int *)malloc(rabitNum * sizeof(int));
  73.             for (int i = 0; i < rabitNum; i++)
  74.                 rabitAlreadyRun[i] = 0;
  75.  
  76.             //Random the [random number]
  77.             srand(time(NULL));
  78.             Rabit rabit;
  79.             while (1){
  80.                 system("cls");
  81.  
  82.                 //Rabit run
  83.                 rabit.RunRabit(rabitNum, rabitAlreadyRun);
  84.  
  85.                 //Show on form
  86.                 rabit.ShowOnForm(rabitNum, rabitAlreadyRun, tripLenght);
  87.  
  88.                 //Check
  89.                 if (rabit.CheckWinner(rabitNum, rabitAlreadyRun, tripLenght) == true){
  90.                     free(rabitAlreadyRun);
  91.                     system("pause"); return 0;
  92.                 }
  93.  
  94.                 //Daley
  95.                 rabit.Delay(0.5);
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment