Thread: Timer around code

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Timer around code

    Hey guys, I am making a hare and turtle simulation.

    It works perfectly well, but I want the game to run for 10 seconds, and after every second display one of the array positions of each animal.

    The theory I came up with did work and it calculates all the moves within 10 seconds and then prints out the final array showing all moves made during the game. I would like it to do this:

    #After first second passed - show array with only the first move
    #After second sec passed - update array with new move but keeping original

    The other theory I tried was placing a big loop around the whole code, including the final array, but all that did was print the final output 10 times.

    This is my full code:

    Code:
    // function prototypes
    void setUp ( char[][ 50 ] );
    void startingPoint ( char*, char*, char[][ 50 ] );
    void playGame ( char*, char*, char[][ 50 ] );
    const std::string displayTitleText();
    const std::string startRaceText();
    const std::string announceWinnerTurtle();
    const std::string announceWinnerHare();
    void showBoard ( char[][ 50 ] );
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
       srand((unsigned)time(0));
       
       const int MAX_ROWS = 50;
       const int MAX_COLS = 50;
       
       char playerBoard[ MAX_ROWS ][ MAX_COLS ] = {{ 0, 0 }};
       
       setUp ( playerBoard );
       
       std::cin.get(); // freeze console output window
       
       return 0; // return value from int main
    }
    
    void setUp ( char board[][ 50 ] )
    {
       char turtle[] = "T";
       char hare[] = "H";
       
       startingPoint ( turtle, hare, board );
    }
    
    void startingPoint ( char *tut, char *hre, char board[][ 50 ] )
    {
       board[ 0 ][ 0 ] = *tut;
       board[ 0 ][ 1 ] = *hre;
       
       playGame ( tut, hre, board );
    }
    
    void playGame ( char *tut, char *hre, char board[][ 50 ] )
    {
       std::cout << "\t" << displayTitleText();
       Sleep( 1500 );
       std::cout << std::endl << startRaceText();
       
       std::cout << "\n\n";
       
       for ( int i = 10; i > 0; )
       {
          int xCorrTurt = rand() % 50 + 1;
          int yCorrTurt = rand() % 50 + 1;
          int xCorrHare = rand() % 50 + 1;
          int yCorrHare = rand() % 50 + 1;
          
          board[ xCorrTurt ][ yCorrTurt ] = *tut;
          board[ xCorrHare ][ yCorrHare ] = *hre;
          
          if ( *tut == board[ 50 ][ 50 ] )
          {
             std::cout << "\n" << announceWinnerTurtle();
          }
       
          if ( *hre == board[ 50 ][ 50 ] )
          {
             std::cout << "\n" << announceWinnerHare();
          }
             
          Sleep( 1000 );
          i--;
       }
       
       showBoard ( board );
    }
    
    const std::string displayTitleText() 
    {
       std::string str = "Welcome to the Tortoise and the Hare!";
       
       return str;
    }
    
    const std::string startRaceText() 
    {
       std::string str = "And..... THEY ARE OFF!";
       
       return str;
    }
    
    const std::string announceWinnerTurtle() 
    {
       std::string str = "OMG! THE TURTLE WAS THE WINNER! WOOOOO!!";
       
       return str;
    }
    
    const std::string announceWinnerHare()
    {
       std::string str = "OH WELL, PREDICATABLE THAT THE HARE WOULD WIN...";
       
       return str;
    }
    
    void showBoard ( char board[][ 50 ] )
    {
       for ( int i = 0; i < 50; i++ )
       {
          for ( int j = 0; j < 50; j++ )
             std::cout << board[ i ][ j ];
             std::cout << std::endl;
       }
    }
    As it stands, it does work but I wanted to improve it so it looks better
    when it runs. I appreiciate any help guys.
    Double Helix STL

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You are overrunning your board. If the dimensions are [50][50], then board[50][50] should never have anything in it. However, since your rand() function modulo's 50, and then adds 1, it will. Bug.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM