#include // All *_X/Y_COUNT constants must be 1 or greater! #define QUADRANT_X_COUNT 2 #define QUADRANT_Y_COUNT 2 #define SECTOR_X_COUNT 8 #define SECTOR_Y_COUNT 8 #define SECTOR_POINT_LENGTH 3 struct sector { char points[ SECTOR_X_COUNT ][ SECTOR_Y_COUNT ]; }; struct quadrant { struct sector sectors[ QUADRANT_X_COUNT ][ QUADRANT_Y_COUNT ]; }; struct ship_coord { int QuadrantX; // Can go all the way up to galaxy, if we want. int QuadrantY; // Currently this example only has 1 quadrant. int SectorX; // Current sector in the quadrant int SectorY; int PointX; // Current point in the sector int PointY; }; struct ship { char* name; struct ship_coord location; int hit_points; }; const char* sector_point( char ); void print_quadrant( struct quadrant* ); int main() { struct quadrant QuadA; memset( &QuadA, 0, sizeof( QuadA ) ); struct ship Enterprise; memset( &Enterprise, 0, sizeof( Enterprise ) ); // Beginnigs of a way to track locations of // a ship without having to A) scour the whole // grid, and B) change the underlying sector array. Enterprise.location.SectorX = 0; Enterprise.location.SectorY = 0; Enterprise.location.PointX = 3; Enterprise.location.PointY = 5; // Presumably, the ship would have to be "moved" into a given // point, checking its boundaries and what might currently exist // there. If the map is modified directly, as in this case, 2 // other things need to be tracked: A) where the ship was previously, // so as to be able to remove the character indicating its position, // and B) what was there before (in case it was obscuring a star, or // it's actually possible to cohabit a point with another object) so // that it can be replaced and redrawn correctly. QuadA.sectors [ Enterprise.location.SectorX ] [ Enterprise.location.SectorY ].points [ Enterprise.location.PointX ] [ Enterprise.location.PointY ] = 'E'; print_quadrant( &QuadA ); return 0; } const char* sector_point( char in ) { // Note that each of these strings must be EXACTLY // SECTOR_POINT_LENGTH characters long. switch( in ){ case 'E': return "<*>"; break; case 'K': return "+K+"; break; case '!': return ">!<"; break; case '*': return " * "; break; default: return " "; break; } return " "; // Should never reach this. } void print_quadrant( struct quadrant* Quad ) { int i, XX, YY, x, y; // Key: // XX = X axis coordinate of the current sector, // and used in calculating the offset within the // current line. // x = X axis coordinate within that sector, and also // used in calculating current line position offset. // YY = Y axis coordinate of the current sector, // also used as the current line buffer number. // y = Y axis coordinate within that sector, as well // as assisting in calculating current line number. // // Simplified illustration, not to scale: // // _XX_ // / x \ // / XYXxXY YYYYYY YYYYYY // YY| YXYxYX YYYYYY YYYYYY // |y yyy!yy yyyyyy yyyyyy // \ YXYxYX YYYYYY YYYYYY // // XXXxXX 000000 000000 // XXXxXX 000000 000000 // XXXxXX 000000 000000 // XXXxXX 000000 000000 // Print the top of the frame printf( "/" ); for( i = 0; i < ( QUADRANT_X_COUNT * SECTOR_X_COUNT * SECTOR_POINT_LENGTH ); i++ ){ printf( "-" ); } printf( "\\\n" ); // Print the individual lines across all sectors in this quadrant for( YY = 0; YY < QUADRANT_Y_COUNT; YY++ ){ for( y = 0; y < SECTOR_Y_COUNT; y++ ){ // Left edge of the frame printf( "|" ); for( XX = 0; XX < QUADRANT_X_COUNT; XX++ ){ for( x = 0; x < SECTOR_X_COUNT; x++ ){ // Interpret the point character into it's asciigraph printf( "%s", sector_point( Quad->sectors[XX][YY].points[x][y] ) ); } } // Right edge of the frame printf( "|\n" ); } } // Print bottom frame. printf( "\\" ); for( i = 0; i < ( QUADRANT_X_COUNT * SECTOR_X_COUNT * SECTOR_POINT_LENGTH ); i++ ){ printf( "-" ); } printf( "/\n" ); }