seahunt-rsm-p-htm

Print Format Report File: cell.h Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // cell.h 2| // 3| // Specification for type cell 4| // A cell is a piece of an ocean 5| // 6| 7| #ifndef CELL_H 8| #define CELL_H 9| 10| #include "target.h" 11| 12| namespace Seahunt 13| { 14| 15| class Cell 16| { 17| public: 18| Cell (); // constructor 19| ~Cell(); // destructor 20| void Set_xyz ( int x=0, int y=0, int z=0 ); 21| int Get_x ( void ) const; 22| int Get_y ( void ) const; 23| int Get_z ( void ) const; 24| bool Set_target ( Target * t ); 25| Target * Get_target ( void ) const; 26| bool Hit ( void ); 27| 28| private: 29| Cell ( const Cell & c ); // copy constructor 30| bool hit_status; // true if the cell is hit 31| Target * target; // assigned to the cell 32| int x; 33| int y; 34| int z; // depth of the ocean 35| }; 36| 37| } // namepsace Seahunt 38| 39| #endif // CELL_H 40| 41| ________________________________________________________________________________ Print Format Report File: game.h Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // game.h 2| 3| #ifndef GAME_H 4| #define GAME_H 5| 6| #include <iostream> 7| 8| #include "ocean.h" 9| #include "player.h" 10| 11| namespace Seahunt 12| { 13| 14| class Game 15| { 16| public: 17| Game(); 18| ~Game(); 19| void Play( void ); 20| bool Get_status ( void ); 21| private: 22| Player * player; 23| Ocean * ocean; 24| bool status; 25| enum LIMITS { MAXSHIPS=5, MAXPLAYERS=1, MAXTRIES=100, 26| MAXTIME=10 }; 27| }; 28| 29| } 30| #endif 31| ________________________________________________________________________________ Print Format Report File: ocean.h Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // ocean.h 2| // 3| // Specification for type ocean 4| // 5| 6| #ifndef OCEAN_H 7| #define OCEAN_H 8| 9| #include <string> 10| 11| #include "utility.h" 12| #include "cell.h" 13| #include "target.h" 14| 15| namespace Seahunt 16| { 17| 18| class Ocean 19| { 20| public: 21| Ocean(); 22| ~Ocean(); 23| void PlaceTarget( void ); 24| bool Hit( void ); 25| void Show( void ); 26| void ShowTargets( void ); 27| int Get_target_count( void ) const; 28| int Get_active_targets( void ) const; 29| int Get_destroyed_targets( void ) const; 30| enum Size { MAX_TARGET=5 }; 31| private: 32| enum DIM { ROW=10, COL=10, DEPTH=10, DEPTHFACTOR=100 }; 33| std::string name; 34| int target_count; 35| int active_targets; 36| int destroyed_targets; 37| Cell ocean[ROW][COL][DEPTH]; 38| Target * targets[MAX_TARGET]; 39| void TargetPlacement ( Target * t ); 40| void Init ( void ); 41| }; 42| 43| } 44| 45| #endif // OCEAN_H 46| 47| ________________________________________________________________________________ Print Format Report File: player.h Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // player.h 2| 3| #ifndef PLAYER_H 4| #define PLAYER_H 5| 6| #include <iostream> 7| #include <iomanip> 8| #include <string> 9| #include <ctime> 10| 11| #include "ocean.h" 12| 13| namespace Seahunt 14| { 15| 16| class Player 17| { 18| public: 19| Player(); 20| Player( std::string name ); 21| ~Player(); 22| bool Hit( Ocean * ocean ); 23| int Score( Ocean * ocean ); 24| void Show( void ) const; 25| int Get_score ( void ) const; 26| int Get_number_hits ( void ) const; 27| int Get_number_tries ( void ) const; 28| std::string Get_name ( void ) const; 29| void Set_name ( std::string n ); 30| 31| private: 32| std::string name; 33| int score; 34| int number_tries; 35| int number_hits; 36| time_t begintime; 37| }; 38| 39| } 40| 41| #endif ________________________________________________________________________________ Print Format Report File: sub.h Date: Sat Mar 07 22:11:52 2009 ________________________________________________________________________________ 1| // sub.h 2| 3| #ifndef SUB_H 4| #define SUB_H 5| 6| #include <string> 7| 8| #include "utility.h" 9| #include "target.h" 10| 11| namespace Seahunt 12| { 13| 14| class Sub : public Target 15| { 16| public: 17| static Sub * Create ( void ); 18| static Sub * Create ( std::string name, int armor, int maxdepth ); 19| ~Sub(); 20| void Show( void ) const; 21| bool Hit( void ); 22| void Abstract ( void ){} 23| private: 24| Sub ( std::string name, int armor, int maxdepth ); 25| }; 26| 27| } // namespace Seahunt 28| 29| #endif ________________________________________________________________________________ Print Format Report File: target.h Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // target.h 2| // specification for the target base class 3| // 4| 5| 6| // Notes: Turn on symbol TARGET_UNIT_TEST to test the component 7| 8| #ifndef TARGET_H 9| #define TARGET_H 10| 11| #include <iostream> 12| #include <string> 13| 14| namespace Seahunt 15| { 16| 17| enum TargetStatus 18| { 19| UNKNOWN, 20| NO_DAMAGE, 21| DAMAGE, 22| DEAD, 23| STATUS_MAX 24| }; 25| 26| // base class 27| class Target 28| { 29| protected: 30| enum CONSTANTS { MAX_ARMOR=5, MAX_DEPTH=1000 }; 31| 32| public: 33| Target( std::string name="Uninitialized" , 34| int armor = MAX_ARMOR, 35| int depth = MAX_DEPTH ); 36| virtual ~Target(); 37| 38| // Method interface for derived objects 39| virtual bool Hit( void ); 40| virtual void Show( void ) const; 41| 42| // return the status of the targer 43| TargetStatus Get_status( void ) const; 44| std::string Get_name ( void ) const; 45| int Get_depth_limit ( void ); 46| int Get_armor ( void ) { return( armor ); } 47| void Reset( void ); 48| 49| // pure virtual method creates abstract class for 50| // deployed application, cannot create an instance 51| // of this class outside the test driver. 52| #ifndef TARGET_TEST 53| virtual void Abstract ( void ) = 0; 54| #endif 55| 56| protected: 57| TargetStatus status; 58| std::string name; 59| int armor; 60| int hits; 61| int depth_limit; 62| 63| private: 64| Target( const Target & t ) 65| { 66| // cannot pass target by value from a private 67| // copy constructor 68| } 69| 70| }; 71| 72| } // namespace Seahunt 73| 74| #endif // TARGET_H 75| 76| // eof target.h 77| ________________________________________________________________________________ Print Format Report File: timer.h Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| #ifndef TIMER_H 2| #define TIMER_H 3| // timer.h 4| // 5| // Process timer 6| 7| 8| #include <ctime> 9| #include <string> 10| 11| #ifndef WIN32 12| #include <sys/times.h> 13| #include <unistd.h> 14| #endif 15| 16| class Timer 17| { 18| public: 19| // ------------------------------------------------------------------ | ------ 20| Timer(); 21| 22| // ------------------------------------------------------------------ | ------ 23| Timer(const std::string &label); 24| 25| // ------------------------------------------------------------------ | ------ 26| ~Timer(); 27| 28| // ------------------------------------------------------------------ | ------ 29| void start(void); 30| 31| // ------------------------------------------------------------------ | ------ 32| void stop(void); 33| 34| // ------------------------------------------------------------------ | ------ 35| void show(void); 36| 37| // ------------------------------------------------------------------ | ------ 38| double get_user_time(void); 39| double get_system_time(void); 40| double get_wait_time(void); 41| double get_elapsed_time(void); 42| double get_non_wait_time(void); 43| 44| private: 45| 46| // ------------------------------------------------------------------ | ------ 47| void reset(void); 48| 49| std::string 50| label; 51| 52| long 53| tps; 54| 55| #ifdef WIN32 56| clock_t 57| start_time, 58| end_time; 59| #else 60| long 61| start_time, 62| end_time; 63| #endif 64| 65| double 66| usertime, 67| systemtime, 68| elapsedtime, 69| waittime; 70| 71| #ifndef WIN32 72| struct tms 73| start_cpu_time, 74| end_cpu_time; 75| #endif 76| }; 77| #endif 78| // eof timer.h ________________________________________________________________________________ Print Format Report File: utility.h Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // Utility Class 2| 3| #ifndef UTILITY 4| #define UTILITY 5| 6| #include <iostream> 7| #include <string> 8| 9| namespace Seahunt 10| { 11| 12| class Utility 13| { 14| public: 15| static void UserEntry( std::string label, std::string & entry, int le | ngth ); 16| static void UserEntry( std::string label, int & entry, int min, int m | ax ); 17| // convert int to ascii 18| static std::string itos( int i ); 19| // convert float to ascii 20| static std::string dtos( double d ); 21| static void ClearScreen( void ); 22| static void WaitKey( void ); 23| private: 24| enum LIMITS { MAXBUFF = 1024 }; 25| }; 26| 27| } 28| 29| #endif 30| ________________________________________________________________________________ Print Format Report File: cell.cpp Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // cell.cpp 2| // 3| // Implementation for type cell where cell is a piece of an ocean. 4| // Cell contains a pointer to the base class Target. Through 5| // the based class pointer Cell polymorphically calls Hit of the 6| // derived target object/ 7| 8| #include "cell.h" 9| 10| namespace Seahunt 11| { 12| 13| Cell::Cell() 14| { 15| hit_status = false; 16| x = 0; 17| y = 0; 18| z = 0; 19| target = 0; 20| } 21| 22| Cell::Cell ( const Cell & c ) 23| { 24| hit_status = false; 25| x = c.Get_x(); 26| y = c.Get_y(); 27| z = c.Get_z(); 28| target = c.Get_target(); 29| } 30| 31| Cell::~Cell() 32| { 33| // do not cleanup the target 34| } 35| 36| void 37| Cell::Set_xyz( int _x, int _y, int _z ) 38| { 39| x = _x; y = _y; z = _z; 40| } 41| 42| inline 43| int 44| Cell::Get_x ( void ) const 45| { 46| return(x); 47| } 48| 49| inline 50| int 51| Cell::Get_y ( void ) const 52| { 53| return(y); 54| } 55| 56| inline 57| int 58| Cell::Get_z ( void ) const 59| { 60| return(z); 61| } 62| 63| bool 64| Cell::Set_target ( Target * t ) 65| { 66| bool set_status = false; 67| if ( target == 0 ) 68| { 69| target = t; 70| set_status = true; 71| } 72| return(set_status); 73| } 74| 75| Target * 76| Cell::Get_target ( void ) const 77| { 78| return ( target ); 79| } 80| 81| bool 82| Cell::Hit ( void ) 83| { 84| std::cout << "\n[" << x+1 << "," 85| << y+1 << "," << z+1 << "]" << std::endl; 86| bool local_hit = false; 87| if ( target ) 88| { 89| if ( hit_status == false ) 90| { 91| if ( target->Hit() ) 92| { 93| std::cout << "*** Hit ***" << std::endl; 94| hit_status = true; 95| local_hit = true; 96| } 97| } 98| else 99| { 100| std::cout << "*** Already Hit ***" << std::endl; 101| local_hit = false; 102| } 103| } 104| else 105| { 106| std::cout << "*** Miss ***" << std::endl; 107| } 108| return(local_hit); 109| } 110| 111| } // namespace Seahunt 112| 113| #ifdef CELL_TEST 114| 115| #include "sub.h" 116| 117| int 118| main (void) 119| { 120| std::cout << "Cell Unit Test" << std::endl; 121| 122| Seahunt::Sub * s = Seahunt::Sub::Create(); 123| 124| Seahunt::Cell layer[10][10][10]; 125| 126| for ( int i = 0; i < 10; i++ ) 127| { 128| for ( int j = 0; j < 10; j++ ) 129| { 130| for ( int k = 0; k < 10; k++ ) 131| { 132| layer[i][j][k].Set_xyz( i, j, k ); 133| } 134| } 135| } 136| 137| layer[3][1][2].Set_target( s ); 138| layer[3][2][2].Set_target( s ); 139| layer[3][3][2].Set_target( s ); 140| 141| // fails for already popluated 142| bool place_stat = layer[3][3][2].Set_target( s ); 143| if ( !place_stat ) 144| { 145| std::cout << "\nFailed to place target: already popluated\n" 146| << std::endl; 147| s->Show(); 148| } 149| 150| // miss 151| layer[2][1][5].Hit(); 152| 153| // hits 154| layer[3][1][2].Hit(); 155| layer[3][2][2].Hit(); 156| layer[3][3][2].Hit(); 157| 158| // multiple common hit 159| layer[3][3][2].Hit(); 160| 161| return(0); 162| } 163| #endif 164| 165| 166| // eof cell.cpp 167| ________________________________________________________________________________ Print Format Report File: game.cpp Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // game.cpp 2| 3| #include "game.h" 4| 5| namespace Seahunt 6| { 7| 8| Game::Game() 9| { 10| status = true; 11| player = new Player(); 12| if ( player == 0 ) 13| { 14| std::cerr << "Memory Allocation Error!" << std::endl; 15| status = false; 16| } 17| ocean = new Ocean(); 18| if ( ocean == 0 ) 19| { 20| std::cerr << "Memory Allocation Error!" << std::endl; 21| status = false; 22| } 23| } 24| 25| Game::~Game() 26| { 27| if ( ocean ) 28| { 29| delete ocean; 30| ocean = 0; 31| } 32| if ( player ) 33| { 34| delete player; 35| player = 0; 36| } 37| } 38| 39| void 40| Game::Play() 41| { 42| bool loop_exit = false; 43| int more = 0; 44| 45| /* Place the sub in the ocean */ 46| for ( int i=0; i<ocean->Get_target_count(); i++ ) 47| { 48| ocean->PlaceTarget(); 49| } 50| 51| while ( !loop_exit ) 52| { 53| 54| if ( player->Hit(ocean) ) 55| { 56| player->Show(); 57| } 58| 59| if ( ocean->Get_active_targets() == 0 ) 60| { 61| std::cout << "You Win! and Get an A Grade!" << std::endl; 62| loop_exit = true; 63| continue; 64| } 65| else if ( player->Get_number_tries() > MAXTRIES ) 66| { 67| std::cout << "You are a LOSER! and Get an D Grade!" << std::endl; 68| loop_exit = false; 69| continue; 70| } 71| 72| Utility::UserEntry( "Hit Again 0-No 1-Yes 2-Ocean 3-Targets", more, 0 | , 4 ); 73| 74| switch ( more ) 75| { 76| case 0: 77| { 78| loop_exit = true; 79| continue; 80| } 81| break; 82| 83| case 2: 84| { 85| ocean->Show(); 86| } 87| break; 88| 89| case 3: 90| { 91| ocean->ShowTargets(); 92| } 93| 94| case 1: // fall through 95| default: 96| { 97| } 98| break; 99| } 100| } 101| 102| player->Show(); 103| Utility::WaitKey(); 104| 105| } 106| 107| } // namespace ________________________________________________________________________________ Print Format Report File: ocean.cpp Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // ocean.h 2| // 3| // implementation for ocean 4| // 5| 6| #include <cstdlib> 7| #include <ctime> 8| 9| #include "ocean.h" 10| #include "Sub.h" 11| 12| namespace 13| { 14| std::string OceanTargetsString[Seahunt::Ocean::MAX_TARGET] = { 15| "Stealth Spy Submarine", 16| "Rescue Submarine", 17| "Supply Submarine", 18| "Attack Submarine", 19| "Boomer Submarine" }; 20| int OceanTargetDepths[Seahunt::Ocean::MAX_TARGET] = { 100, 200, 400, 700, | 1000 }; 21| int OceanTargetArmor[Seahunt::Ocean::MAX_TARGET] = { 1, 2, 3, 4, 5 }; 22| } 23| 24| namespace Seahunt 25| { 26| 27| Ocean::Ocean() 28| { 29| Utility::UserEntry( "Ocean Name", name, 50 ); 30| Utility::UserEntry( "Number of Targets", target_count, 1, MAX_TARGET ); | 31| Init(); 32| #ifdef OCEAN_TEST 33| std::cout << "Constructed Ocean: " << name << std::endl; 34| #endif 35| } 36| 37| void 38| Ocean::Init( void ) 39| { 40| active_targets = 0; 41| destroyed_targets = 0; 42| 43| // Go through the target list and delete the objects 44| for ( int c=0; c < MAX_TARGET; ++c ) 45| { 46| targets[c] = 0; 47| } 48| for ( int i = 0; i < 10; i++ ) 49| { 50| for ( int j = 0; j < 10; j++ ) 51| { 52| for ( int k = 0; k < 10; k++ ) 53| { 54| ocean[i][j][k].Set_xyz( i, j, k ); 55| } 56| } 57| } 58| } 59| 60| Ocean::~Ocean() 61| { 62| // Go through the target list and delete the objects 63| for ( int i=0; i < MAX_TARGET; ++i ) 64| { 65| if ( targets[i] ) 66| { 67| delete targets[i]; 68| targets[i] = 0; 69| } 70| } 71| #ifdef OCEAN_TEST 72| std::cout << "Destructed Ocean: " << name << std::endl; 73| #endif 74| } 75| 76| void 77| Ocean::PlaceTarget( void ) 78| { 79| for ( int i=0; i<target_count; i++ ) 80| { 81| targets[i] = Sub::Create( OceanTargetsString[i], 82| OceanTargetArmor[i], 83| OceanTargetDepths[i] ); 84| if ( targets[i] ) 85| { 86| TargetPlacement ( targets[i] ); 87| } 88| } 89| active_targets = i; 90| } 91| 92| void 93| Ocean::TargetPlacement ( Target * t ) 94| { 95| int i = 0; 96| if (t) 97| { 98| while ( i < t->Get_armor() ) 99| { 100| int r = 0; 101| int c = 0; 102| int d = 0; 103| std::cout << "\nTarget: " << t->Get_name() << std::endl; 104| std::cout << "Position " << i+1 << " of " << t->Get_armor() << std: | :endl; 105| Utility::UserEntry ( "Row Position", r, 1, ROW ); 106| Utility::UserEntry ( "Column Position", c, 1, COL ); 107| Utility::UserEntry ( "Depth Position", d, 100, t->Get_depth_limit() | ); 108| d = d/DEPTHFACTOR; 109| if ( ocean[r-1][c-1][d-1].Get_target() == 0 ) 110| { 111| ocean[r-1][c-1][d-1].Set_target(t); 112| i++; 113| } 114| else 115| { 116| std::cout << "\nOcean [" << r << "][" << c << "][" << d << "]" 117| << " is already populated\n" << std::endl; 118| } 119| } 120| } 121| } 122| 123| bool 124| Ocean::Hit ( void ) 125| { 126| int r = 0; 127| int c = 0; 128| int d = 0; 129| bool status = false; 130| std::cout << "\nHit a spot in the " << name << std::endl; 131| Utility::UserEntry ( "Row Position", r, 1, ROW ); 132| Utility::UserEntry ( "Column Position", c, 1, COL ); 133| Utility::UserEntry ( "Depth Position", d, 100, DEPTH*DEPTHFACTOR ); 134| d = d/DEPTHFACTOR; 135| status = ocean[r-1][c-1][d-1].Hit(); 136| if ( status == true ) 137| { 138| if ( ocean[r-1][c-1][d-1].Get_target()->Get_status() == DEAD ) 139| { 140| destroyed_targets++; 141| active_targets--; 142| } 143| } 144| return ( status ); 145| } 146| 147| void 148| Ocean::Show ( void ) 149| { 150| int i = 0; 151| int j = 0; 152| int d = 0; 153| for ( d=0; d<DEPTH; ++d ) 154| { 155| std::cout << "Ocean Depth: " << (d+1)*DEPTHFACTOR << std::endl; 156| std::cout << "------------------------------------------" << std::end | l; 157| std::cout << " 1 2 3 4 5 6 7 8 9 10" << std::end | l; 158| for ( i=0; i<ROW; ++i ) 159| { 160| std::cout << std::endl << i+1; 161| if ( i < 9 ) 162| { 163| std::cout << " "; 164| } 165| else 166| { 167| std::cout << " "; 168| } 169| for ( j=0; j<COL; ++j ) 170| { 171| if ( ocean[i][j][d].Get_target() ) 172| { 173| 174| std::cout << ocean[i][j][d].Get_target()->Get_armor() << " "; | 175| } 176| else 177| { 178| std::cout << "- "; 179| } 180| } 181| std::cout << std::endl; 182| } 183| Utility::WaitKey(); 184| } 185| } 186| 187| void 188| Ocean::ShowTargets( void ) 189| { 190| std::cout << 191| "\nOcean Target Status\n" << 192| "----------------------------------------\n" << 193| "Target Count : " << target_count << "\n" << 194| "Active Targets : " << active_targets << "\n" << 195| "Destroyed Targets: " << destroyed_targets << std::endl; 196| for ( int i=0; i<MAX_TARGET; ++i ) 197| { 198| std::cout << "\nOcean Target " << i+1 << std::endl; 199| if ( targets[i] ) 200| { 201| targets[i]->Show(); 202| } 203| } 204| std::cout << "----------------------------------------" << std::endl; 205| } 206| 207| int 208| Ocean::Get_target_count( void ) const 209| { 210| return( target_count ); 211| } 212| 213| int 214| Ocean::Get_active_targets( void ) const 215| { 216| return( active_targets ); 217| } 218| 219| int 220| Ocean::Get_destroyed_targets( void ) const 221| { 222| return( destroyed_targets ); 223| } 224| 225| } // namespace Seahunt 226| 227| #ifdef OCEAN_TEST 228| 229| int 230| main ( void ) 231| { 232| std::cout << "Ocean Unit Test\n" << std::endl; 233| 234| Seahunt::Ocean atlantic; 235| 236| atlantic.PlaceTarget(); 237| 238| atlantic.Hit(); 239| atlantic.Hit(); 240| 241| Seahunt::Utility::WaitKey(); 242| 243| atlantic.Show(); 244| 245| atlantic.ShowTargets(); 246| 247| Seahunt::Utility::WaitKey(); 248| 249| return(0); 250| } 251| #endif 252| ________________________________________________________________________________ Print Format Report File: player.cpp Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // player.cpp 2| 3| #include "player.h" 4| 5| namespace Seahunt 6| { 7| 8| Player::Player() 9| { 10| score = 0; 11| begintime = time(0); 12| number_hits = 0; 13| number_tries = 0; 14| Utility::UserEntry( "Player Name", name, 50 ); 15| } 16| 17| Player::Player( std::string n ) 18| { 19| name = n; 20| begintime = time(0); 21| score = 0; 22| number_hits = 0; 23| number_tries = 0; 24| } 25| 26| Player::~Player() 27| { 28| // no dynamic memory allocated by the object 29| } 30| 31| bool 32| Player::Hit( Ocean * ocean ) 33| { 34| bool status = ocean->Hit(); 35| number_tries++; 36| if ( status == true ) 37| { 38| number_hits++; 39| } 40| Score(ocean); 41| return( status ); 42| } 43| 44| int 45| Player::Score( Ocean * ocean ) 46| { 47| score = (ocean->Get_destroyed_targets() * 100) 48| + (2*number_hits) - 49| (number_tries-number_hits); 50| return(score); 51| } 52| 53| void 54| Player::Show( void ) const 55| { 56| double hmr = 0.0; 57| if ( number_tries > 0 ) 58| { 59| hmr = (static_cast<double>(number_hits)/ 60| static_cast<double>(number_tries)); 61| } 62| std::cout << std::endl; 63| std::cout.setf( std::ios::fixed ); 64| std::cout << std::setprecision(2); 65| std::cout << "Player : " << name << std::endl; 66| std::cout << "Elapsed : " << 67| (static_cast<double>((time(0)-begintime))/ 68| 60.0 ) << " Minutes" << std::endl; 69| std::cout << "Score : " << score << std::endl; 70| std::cout << "Hit/Miss: " << hmr << std::endl << std::endl; 71| std::cout.unsetf(std::ios::fixed); 72| } 73| 74| int 75| Player::Get_score ( void ) const 76| { 77| return(score); 78| } 79| 80| int 81| Player::Get_number_hits ( void ) const 82| { 83| return(number_hits); 84| } 85| 86| int 87| Player::Get_number_tries ( void ) const 88| { 89| return(number_tries); 90| } 91| 92| std::string 93| Player::Get_name ( void ) const 94| { 95| return(name); 96| } 97| 98| void 99| Player::Set_name ( std::string n ) 100| { 101| name = n; 102| } 103| 104| } // namespace 105| 106| #ifdef PLAYER_TEST 107| int 108| main ( void ) 109| { 110| Seahunt::Ocean pacific("Pacific"); 111| Seahunt::Player p( "John Smith" ); 112| 113| pacific.PlaceTarget(); 114| p.Hit(&pacific); 115| p.Show(); 116| 117| return(0); 118| } 119| #endif 120| 121| 122| 123| 124| 125| 126| ________________________________________________________________________________ Print Format Report File: player_solution.cpp Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // player.cpp 2| 3| #include "player.h" 4| 5| namespace Seahunt 6| { 7| 8| //--------------------------------------------------------------------- | ------ 9| Player::Player() 10| { 11| score_value = 0; 12| number_tries = 0; 13| number_hits = 0; 14| set_name(); 15| begin(); 16| #ifdef PLAYER_TEST 17| std::cout << "Constructed Player: " << name << std::endl; 18| #endif 19| } 20| 21| //--------------------------------------------------------------------- | ------ 22| Player::Player(const std::string& label ) 23| { 24| std::string prompt = label + " Player"s Name"; 25| reset(); 26| Utility::entry(prompt, name); 27| begin(); 28| #ifdef PLAYER_TEST 29| std::cout << "Constructed Player: " << name << std::endl; 30| #endif 31| } 32| 33| //--------------------------------------------------------------------- | ------ 34| Player::~Player() 35| { 36| // no dynamic memory allocated by the object 37| #ifdef PLAYER_TEST 38| std::cout << "Destructed Player: " << name << std::endl; 39| #endif 40| } 41| 42| //--------------------------------------------------------------------- | ------ 43| void Player::reset(void) 44| { 45| score_value = 0; 46| number_tries = 0; 47| number_hits = 0; 48| } 49| 50| //--------------------------------------------------------------------- | -- 51| int Player::begin(void) 52| { 53| begin_time = time(0); 54| return static_cast<int>(begin_time); 55| } 56| 57| //--------------------------------------------------------------------- | -- 58| int Player::elapsed(void) const 59| { 60| time_t elapsed_value = time(0) - begin_time; 61| return static_cast<int>(elapsed_value); 62| } 63| 64| //--------------------------------------------------------------------- | ------ 65| bool 66| Player::hit(Ocean* ocean) 67| { 68| bool status = ocean->hit(); 69| 70| number_tries++; 71| 72| if (status) 73| { 74| number_hits++; 75| } 76| 77| number_kills = ocean->get_destroyed_targets(); 78| 79| score(ocean); 80| 81| return status; 82| } 83| 84| //--------------------------------------------------------------------- | ------ 85| void Player::place_targets(Ocean* ocean) const 86| { 87| ocean->place_target(); 88| } 89| 90| //--------------------------------------------------------------------- | ------ 91| int 92| Player::score(Ocean* ocean) 93| { 94| score_value = (number_kills * 100) + 95| (number_hits * 10) - number_tries - elapsed(); 96| return score_value; 97| } 98| 99| //--------------------------------------------------------------------- | ------ 100| void 101| Player::show(void) const 102| { 103| std::cout << "\nPlayer : " << name << std::endl; 104| std::cout << "Elapsed Time : " << elapsed() << std::endl; 105| std::cout << "Number of Attempts: " << number_tries << std::endl; 106| std::cout << "Number of Hits : " << number_hits << std::endl; 107| std::cout << "Score : " << score_value << "\n" << std::en | dl; 108| Utility::wait(); 109| } 110| 111| //--------------------------------------------------------------------- | ------ 112| int 113| Player::get_score(void) const 114| { 115| return score_value; 116| } 117| 118| //--------------------------------------------------------------------- | ------ 119| int 120| Player::get_number_hits(void) const 121| { 122| return number_hits; 123| } 124| 125| //--------------------------------------------------------------------- | ------ 126| int 127| Player::get_number_tries(void) const 128| { 129| return number_tries; 130| } 131| 132| //--------------------------------------------------------------------- | ------ 133| const std::string& 134| Player::get_name(void) const 135| { 136| return name; 137| } 138| 139| //--------------------------------------------------------------------- | ------ 140| void 141| Player::set_name(const std::string& n ) 142| { 143| name = n; 144| } 145| 146| //--------------------------------------------------------------------- | ------ 147| void 148| Player::set_name( void ) 149| { 150| Utility::entry("Player Name", name); 151| } 152| 153| } // namespace 154| 155| //----------------------------------------------------------------------- | ------ 156| #ifdef PLAYER_TEST 157| int 158| main ( void ) 159| { 160| Seahunt::Ocean ocean; 161| 162| Seahunt::Player placer("Placing"); 163| Seahunt::Player chooser( "Choosing" ); 164| 165| placer.place_targets(&ocean); 166| 167| while (chooser.elapsed() < 600 && 168| ocean.get_active_targets() > 0) 169| { 170| if (chooser.hit(&ocean)) 171| { 172| chooser.show(); 173| } 174| } 175| 176| chooser.show(); 177| ocean.show(); 178| 179| return(0); 180| } 181| #endif 182| 183| 184| 185| 186| 187| 188| ________________________________________________________________________________ Print Format Report File: seahunt.cpp Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // seahunt.cpp 2| 3| #include <iostream> 4| 5| #include "game.h" 6| 7| #ifdef RELEASE 8| 9| int 10| main ( void ) 11| { 12| std::cout << "\nSeahunt\n" << std::endl; 13| Seahunt::Game seahunt; 14| seahunt.Play(); 15| return(0); 16| } 17| 18| #endif ________________________________________________________________________________ Print Format Report File: sub.cpp Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // sub.cpp 2| 3| #include <iostream> 4| 5| #include "sub.h" 6| 7| namespace Seahunt 8| { 9| 10| Sub * 11| Sub::Create( void ) 12| { 13| std::string newname; 14| int d = 0; 15| int a = 0; 16| std::cout << std::endl; 17| Utility::UserEntry( "Submarine Name", newname, 60 ); 18| Utility::UserEntry( "Submarine Maximum Depth", d, 0, MAX_DEPTH ); 19| Utility::UserEntry( "Submarine Armor", a, 1, MAX_ARMOR ); 20| Sub * newsub = new Sub ( newname, a, d ); 21| return( newsub ); 22| } 23| 24| Sub * 25| Sub::Create( std::string name, int a, int d ) 26| { 27| Sub * newsub = new Sub ( name, a, d ); 28| return( newsub ); 29| } 30| 31| Sub::Sub( std::string name, int a, int d ) 32| : Target( name, a, d ) 33| { 34| #ifdef SUB_TEST 35| std::cout << "Constructed Sub" << name << std::endl; 36| #endif 37| } 38| 39| Sub::~Sub() 40| { 41| #ifdef SUB_TEST 42| std::cout << "Destructed Sub" << name << std::endl; 43| #endif 44| } 45| 46| void 47| Sub::Show ( void ) const 48| { 49| std::cout << "Sub : "; 50| Target::Show(); 51| std::cout << "Max Depth: " << depth_limit << std::endl; 52| } 53| 54| bool 55| Sub::Hit ( void ) 56| { 57| bool hit_status = Target::Hit(); 58| if ( hit_status == true ) 59| { 60| if ( Get_status() == DEAD ) 61| { 62| Show(); 63| } 64| } 65| return(hit_status); 66| } 67| 68| } // namespace Seahunt 69| 70| #ifdef SUB_TEST 71| int 72| main ( void ) 73| { 74| Seahunt::Sub * s = Seahunt::Sub::Create(); 75| 76| s->Hit(); 77| s->Hit(); 78| s->Hit(); 79| s->Hit(); 80| 81| std::cout << "\nCheckDepth(5): " << s->CheckDepth(5) << "\n" << std::en | dl; 82| 83| s->Reset(); 84| s->Show(); 85| 86| return (0); 87| } 88| #endif // SUB_TEST 89| 90| 91| ________________________________________________________________________________ Print Format Report File: target.cpp Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // target.cpp 2| 3| #include "target.h" 4| 5| namespace Seahunt 6| { 7| 8| static const std::string StatusString[] = 9| { 10| "Unknown", 11| "No Damage", 12| "Damaged", 13| "Dead" 14| }; 15| 16| Target::Target( std::string n, int a, int d ) 17| { 18| name = n; 19| armor = a; 20| hits = 0; 21| depth_limit = d; 22| status = NO_DAMAGE; 23| #ifdef TARGET_TEST 24| std::cout << "Constructed Target: " << name << std::endl; 25| Show(); 26| #endif 27| } 28| 29| Target::~Target() 30| { 31| #ifdef TARGET_TEST 32| std::cout << "Destructed Target: " << name << std::endl; 33| Show(); 34| #endif 35| } 36| 37| TargetStatus 38| Target::Get_status ( void ) const 39| { 40| return ( status ); 41| } 42| 43| int 44| Target::Get_depth_limit ( void ) 45| { 46| return( depth_limit ); 47| } 48| 49| std::string 50| Target::Get_name ( void ) const 51| { 52| return(name); 53| } 54| 55| bool 56| Target::Hit ( void ) 57| { 58| bool hit_state = false; 59| if ( hits < armor ) 60| { 61| hits++; 62| if ( hits == armor ) 63| { 64| status = DEAD; 65| } 66| else 67| { 68| status = DAMAGE; 69| } 70| hit_state = true; 71| } 72| else 73| { 74| status = DEAD; 75| } 76| return(hit_state); 77| } 78| 79| void 80| Target::Reset ( void ) 81| { 82| hits = 0; 83| status = NO_DAMAGE; 84| } 85| 86| void 87| Target::Show ( void ) const 88| { 89| std::cout << name << std::endl 90| << "Armor : " << armor << std::endl 91| << "Hits : " << hits << std::endl 92| << "Status : " << StatusString[static_cast<int>(status)] << | std::endl; 93| } 94| 95| } // namespace Seahunt 96| 97| #ifdef TARGET_TEST 98| int 99| main ( void ) 100| { 101| 102| Seahunt::Target t( "Generic Target", 3 ); 103| 104| t.Hit(); 105| t.Show(); 106| t.Hit(); 107| t.Show(); 108| t.Hit(); 109| t.Show(); 110| t.Hit(); 111| t.Show(); 112| 113| return (0); 114| 115| } 116| #endif // UNIT_TEST 117| 118| // eof target.cpp 119| ________________________________________________________________________________ Print Format Report File: timer.cpp Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // timer.cpp 2| 3| #include "stdafx.h" 4| 5| #include <iostream> 6| #include "timer.h" 7| 8| Timer::Timer () 9| { 10| label = "Process Timer"; 11| reset(); 12| } 13| 14| Timer::Timer (const std::string &label) 15| { 16| Timer::label = label; 17| reset(); 18| } 19| 20| Timer::~Timer() 21| { 22| } 23| 24| void 25| Timer::reset(void) 26| { 27| #ifdef WIN32 28| tps = CLOCKS_PER_SEC; 29| #else 30| tps = sysconf ( _SC_CLK_TCK ); 31| #endif 32| end_time = 0; 33| usertime = 0; 34| systemtime = 0; 35| elapsedtime = 0; 36| waittime = 0; 37| } 38| 39| void 40| Timer::start(void) 41| { 42| #ifdef WIN32 43| start_time = clock(); 44| #else 45| start_time = times(&(start_cpu_time)); 46| #endif 47| } 48| 49| void 50| Timer::show(void) 51| { 52| #ifdef WIN32 53| std::cout 54| << " " 55| << label << "\n" 56| << " -------------------------------\n" 57| << " Elapsed Time : " 58| << elapsedtime 59| << "s" << std::endl; 60| #else 61| std::cout 62| << " " 63| << label << "\n" 64| << " -------------------------------\n" 65| << " User CPU Time : " 66| << usertime << " s\n" 67| << " System CPU Time: " 68| << systemtime << " s\n" 69| << " Wait Time : " 70| << waittime << " s\n" 71| << " -------------------------------\n" 72| << " Elapsed Time : " 73| << elapsedtime << " s\n" << std::endl; 74| #endif 75| } 76| 77| void 78| Timer::stop(void) 79| { 80| #ifdef WIN32 81| end_time = clock(); 82| #else 83| end_time = times(&end_cpu_time); 84| #endif 85| 86| elapsedtime = ((double)(end_time - 87| start_time )/(double)tps ); 88| if (elapsedtime < 0.001) 89| { 90| elapsedtime = 0.001; 91| } 92| 93| #ifndef WIN32 94| usertime = ((double)(end_cpu_time.tms_utime - 95| start_cpu_time.tms_utime)/(double)tps); 96| systemtime = ((double)(end_cpu_time.tms_stime - 97| start_cpu_time.tms_stime)/(double)tps); 98| waittime = (elapsedtime - (usertime + systemtime)); 99| #endif 100| 101| if ( waittime < 0.00 ) 102| { 103| waittime = 0.00; 104| } 105| } 106| 107| double Timer::get_user_time(void) 108| { 109| return usertime; 110| } 111| 112| double Timer::get_system_time(void) 113| { 114| return systemtime; 115| } 116| 117| double Timer::get_wait_time(void) 118| { 119| return waittime; 120| } 121| 122| double Timer::get_non_wait_time(void) 123| { 124| return usertime + systemtime; 125| } 126| 127| double Timer::get_elapsed_time(void) 128| { 129| return elapsedtime; 130| } 131| 132| #ifdef TIMER_TEST 133| 134| int 135| main(void) 136| { 137| Timer 138| t1("Example Process Timing"); 139| 140| t1.begin(); 141| 142| // begin some processing ... 143| for (int i=0; i<1000; i++) 144| { 145| std::string 146| name = getenv("PATH"); 147| std::cout << name << std::endl; 148| } 149| 150| // end some processing ... 151| 152| t1.end(); 153| 154| t1.show(); 155| 156| return 0; 157| } 158| #endif 159| // eof timer.cpp ________________________________________________________________________________ Print Format Report File: utility.cpp Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| // utility.cpp 2| 3| #include <strstream> 4| #include <cstdlib> 5| #include <cstdio> 6| 7| #include "utility.h" 8| 9| namespace Seahunt 10| { 11| 12| // convert int to ascii 13| std::string 14| Utility::itos( int i ) 15| { 16| std::strstream stream; 17| std::string str; 18| stream << i; 19| stream >> str; 20| return str; 21| } 22| 23| // convert float to ascii 24| std::string 25| Utility::dtos( double d ) 26| { 27| std::strstream stream; 28| std::string str; 29| stream << d; 30| stream >> str; 31| return str; 32| } 33| 34| void 35| Utility::UserEntry ( std::string label, int & entry, int min, int max ) 36| { 37| char buff[MAXBUFF] = {0}; 38| bool valid = true; 39| entry = -1; 40| std::string range = " <" + itos(min) + " to " + itos(max) + ">"; 41| do 42| { 43| valid = false; 44| std::cout << "Enter " << label << range << ": "; 45| std::cin.getline(buff, 21); 46| for ( int i=0; i<strlen(buff); ++i ) 47| { 48| if ( isdigit(buff[i]) ) 49| { 50| valid = true; 51| } 52| else 53| { 54| valid = false; 55| break; 56| } 57| } 58| if ( valid == true ) 59| { 60| entry = atoi(buff); 61| if ( entry < min || entry > max ) 62| { 63| std::cout << "\nInvalid Entry: " << entry << range << std::endl; 64| } 65| } 66| else 67| { 68| std::cout << "\nInvalid Entry: " << buff << range << std::endl; 69| } 70| } 71| while ( entry < min || entry > max ); 72| } 73| 74| void 75| Utility::UserEntry ( std::string label, std::string & entry, int length ) | 76| { 77| std::cout << "Enter " << label << ": "; 78| char buff[MAXBUFF] = {0}; 79| std::cin.getline(buff, length); // bug in VCPP 5.0 does not work corre | ctly 80| entry = buff; 81| } 82| 83| void 84| Utility::WaitKey( void ) 85| { 86| char buff[1] = {0}; 87| std::cout << "\nPress Any Key to Continue "; 88| gets(buff); 89| } 90| 91| void 92| Utility::ClearScreen( void ) 93| { 94| std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << std: | :endl; 95| } 96| 97| } 98| ________________________________________________________________________________ Print Format Report File: Fibonacci.cs Date: Sat Mar 07 22:11:52 2013 ________________________________________________________________________________ 1| using System; 2| 3| namespace test 4| { 5| public class Fibonacci 6| { 7| public static void Main() 8| { 9| long v1, v2, v3; 10| 11| v1 = 0; //1st number 12| v2 = 1; //2nd number 13| Console.WriteLine("The Fibonacci series are: "); 14| Console.Write("{0}, {1}", v1, v2); 15| 16| for(int i= 3; i <= 50; i++) 17| { 18| v3 = v1 + v2; //Each number is the sum of the two previous number | s 19| Console.Write(", {0}", v3); 20| 21| //Update the variables for the two previous numbers 22| v1 = v2; 23| v2 = v3; 24| } 25| Console.WriteLine("\n\n"); 26| } 27| 28| } 29| } ________________________________________________________________________________           Report Banner - Edit rsm.cfg File