RSM Code Listing File: cell.h Date: Fri Jun 22 19:31:58 2001 ________________________________________________________________________________ 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| ________________________________________________________________________________ RSM Code Listing File: game.h Date: Fri Jun 22 19:31:58 2001 ________________________________________________________________________________ 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| ________________________________________________________________________________ RSM Code Listing File: ocean.h Date: Fri Jun 22 19:31:58 2001 ________________________________________________________________________________ 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| ________________________________________________________________________________ RSM Code Listing File: player.h Date: Fri Jun 22 19:31:58 2001 ________________________________________________________________________________ 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 ________________________________________________________________________________ RSM Code Listing File: sub.h Date: Fri Jun 22 19:31:58 2001 ________________________________________________________________________________ 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 ________________________________________________________________________________ RSM Code Listing File: target.h Date: Fri Jun 22 19:31:58 2001 ________________________________________________________________________________ 1| // target.h 2| // specification for the target base class 3| // 4| // R. Mudge 5| // 09/10/1998 6| 7| // Notes: Turn on symbol TARGET_UNIT_TEST to test the component 8| 9| #ifndef TARGET_H 10| #define TARGET_H 11| 12| #include <iostream> 13| #include <string> 14| 15| namespace Seahunt 16| { 17| 18| enum TargetStatus 19| { 20| UNKNOWN, 21| NO_DAMAGE, 22| DAMAGE, 23| DEAD, 24| STATUS_MAX 25| }; 26| 27| // base class 28| class Target 29| { 30| protected: 31| enum CONSTANTS { MAX_ARMOR=5, MAX_DEPTH=1000 }; 32| 33| public: 34| Target( std::string name="Uninitialized" , 35| int armor = MAX_ARMOR, 36| int depth = MAX_DEPTH ); 37| virtual ~Target(); 38| 39| // Method interface for derived objects 40| virtual bool Hit( void ); 41| virtual void Show( void ) const; 42| 43| // return the status of the targer 44| TargetStatus Get_status( void ) const; 45| std::string Get_name ( void ) const; 46| int Get_depth_limit ( void ); 47| int Get_armor ( void ) { return( armor ); } 48| void Reset( void ); 49| 50| // pure virtual method creates abstract class for 51| // deployed application, cannot create an instance 52| // of this class outside the test driver. 53| #ifndef TARGET_TEST 54| virtual void Abstract ( void ) = 0; 55| #endif 56| 57| protected: 58| TargetStatus status; 59| std::string name; 60| int armor; 61| int hits; 62| int depth_limit; 63| 64| private: 65| Target( const Target & t ) 66| { 67| // cannot pass target by value from a private 68| // copy constructor 69| } 70| 71| }; 72| 73| } // namespace Seahunt 74| 75| #endif // TARGET_H 76| 77| // eof target.h 78| ________________________________________________________________________________ RSM Code Listing File: utility.h Date: Fri Jun 22 19:31:58 2001 ________________________________________________________________________________ 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| ________________________________________________________________________________ RSM Code Listing File: cell.cpp Date: Fri Jun 22 19:31:58 2001 ________________________________________________________________________________ 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| ________________________________________________________________________________ RSM Code Listing File: game.cpp Date: Fri Jun 22 19:31:58 2001 ________________________________________________________________________________ 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 ________________________________________________________________________________ RSM Code Listing File: ocean.cpp Date: Fri Jun 22 19:31:58 2001 ________________________________________________________________________________ 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| ________________________________________________________________________________ Shareware evaluation licenses process only files. Paid licenses enable wild cards and file/project totals.