Holey_yan

20150409_KUAS_VisualC++_RabitRunning(Not Class)

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