Wrote the check_num_args() function; still need to integrate it into the program. Also wrote a help text that is printed with -h flag.

master
Aadhavan Srinivasan 9 months ago
parent 77a147e08f
commit c2bedb0601

@ -49,6 +49,24 @@ const float BASE_BOUNCE_RAD = (BASE_BOUNCE_DEG / 180.0) * M_PI;
const float BASE_SPEED_COMPONENTS = 15; const float BASE_SPEED_COMPONENTS = 15;
const float BASE_SPEED = sqrt(powf(BASE_SPEED_COMPONENTS, 2) * 2); const float BASE_SPEED = sqrt(powf(BASE_SPEED_COMPONENTS, 2) * 2);
std::string HELP_TEXT = "\nnetpong - A networked pong game for the internet era.\n"
"\n"
"Usage: \n"
"netpong [MODE] [ADDRESS|CODE]\n"
"\n"
"MODE: \n"
"-S : Server mode. Starts a server to allow the other player to connect.\n"
"IP address must be specified. Port is optional, 6500 is used as default.\n"
"\n"
"-C: Client mode. Connects to a server, using the provided connection code.\n"
"\n"
"If no mode is specified, single player mode is used as default.\n"
"\n"
"CONTROLS:"
"\'W\' and \'S\' control left paddle (AKA client paddle)\n"
"Up and Down arrow keys control right paddle (AKA server paddle)\n";
/* Simple function to return 1 if a value is positive, and -1 if it is negative */ /* Simple function to return 1 if a value is positive, and -1 if it is negative */
int signum(int num) { int signum(int num) {
int retval = 0; int retval = 0;
@ -77,11 +95,23 @@ raylib::Vector2 changeVelocityAfterCollision(Paddle paddle, Ball ball) {
} }
/* Checks the number and type of the command-line arguments. Throws an exception /* Checks the number and type of the command-line arguments. Throws an exception
if the args are invalid. */ if the args are invalid. DOES NOT PROCESS VALID ARGUMENTS. */
void check_num_args(int argc, char** argv) { void check_num_args(int argc, char** argv) {
if (argc > 3) {
throw std::invalid_argument("ARGUMENT ERROR: Too many arguments. To view syntax, use -h or --help.");
}
if (argc > 1) { // Either server or client mode
if (std::string(argv[1]) == "-S" && argc < 3) { // Server mode but no address
throw std::invalid_argument("ARGUMENT ERROR: Server mode specified without any address.");
}
if (std::string(argv[1]) == "-C" && argc < 3) { // Client mode but no code
throw std::invalid_argument("ARGUMENT ERRROR: Client mode specified without any code.");
}
if (std::string(argv[1]) == "-h" || std::string(argv[1]) == "--help") {
throw new std::invalid_argument(HELP_TEXT); // I am abusing the exception mechanism here, so that I can ensure that the caller quits the program.
}
}
return;
} }
/* This function checks the command-line arguments passed to the program. /* This function checks the command-line arguments passed to the program.
@ -188,6 +218,19 @@ GameType check_server_client(int argc, char** argv) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
/* Check the number of command-line arguments */
try {
check_num_args(argc, argv);
} catch (std::invalid_argument& inv) {
std::cout << inv.what() << std::endl;
return -1;
}
/* From here on, we assume that:
a. The program was started in single player mode, OR
b. The program was started in server mode, and an address was given, OR
c. The program was started in client mode, and a code was given. */
/* Check if game was started in server or client mode, and set appropriate variables */ /* Check if game was started in server or client mode, and set appropriate variables */
/* GameType struct, to define whether the game is in single or multi-player mode, and /* GameType struct, to define whether the game is in single or multi-player mode, and

Loading…
Cancel
Save