/*Title: Cracker
*File name: "cracker.exe"
*Description: Cracker is a text-based game
* that I wrote while learning C++. It is not
* meant to have a a snazzy GUI or great gameplay;
* it's mostly for my personal learning.
*Date: Jan 4, 2003
*Copyright: Absolute freeware, chnage the code,
* redistribute, sell it if you can find someone
* who would actually pay for it, etc.
*/
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
enum GAME_STATE { NEW_GAME = 1, TUTORIAL, ABOUT, CREDITS, QUIT };
void GameLoop();
void Tutorial();
void About();
void Credits();
void Quit();
int main()
{
int choice;
while(choice != 5)
{
//Clear screen making sur to get rid of old text
//from each iteration
system("CLS");
//start creating menu
cout << "\t____Cracker\\\\Menu____\n";
cout << "\t1)New Game\n";
cout << "\t2)Tutorial(HIGHLY RECOMMENDED)\n";
cout << "\t3)About/Disclaimer\n";
cout << "\t4)Credits\n";
cout << "\t5)Quit\n\n";
//get user input
cout << ">";
cin >> choice;
//Game state machine
//Check for wich state the game is currently in
switch(choice)
{
case NEW_GAME:
GameLoop();
break;
case TUTORIAL:
Tutorial();
break;
case ABOUT:
About();
break;
case CREDITS:
Credits();
break;
case QUIT:
Quit();
break;
}
}
return 0;
}
void GameLoop()
{
// Clear the screen
system("CLS");
string prompt;
string ip;
// Main game loop
while(prompt != "exit")
{
cout << ">";
cin >> prompt;
// Connect stuff
if(prompt == "c")
{
cout << endl << "\tCONNECT TO: ";
cin >> ip;
// Crack0rz.org
//Here is where I need it
}
}
}
void Tutorial()
{
// Clear the screen
system("CLS");
cout << "\n\nWelcome to Cracker tutorial. This will teach you how to\n";
cout << "\t1)Log on to a hacker community server\n";
cout << "\t2)Buy software\n";
cout << "\t3)Upload, download, and delete files from servers(viruses, etc.)\n";
cout << "\t4)";
}
void About()
{
int null;
// Clear the screen
system("CLS");
cout << "\t____Cracker\\\\About/Disclaimer____\n";
cout << "\tCrack is a hacking *simulator that\n";
cout << "\tI programmed in my spare time to help me learn C++.\n";
cout << "\tIt is in NO WAY a hacking tutorial and should not be\n";
cout << "\ttaken that way. I, myself, am not a hacker/cracker, and\n";
cout << "\tI DO NOT encourage it.\n\n";
cout << "\t----crack0rz.org----\n";
cout << "\tAt the time of this writing a website did not\n";
cout << "\texist at this domain. If one does now, I am not affiliated\n";
cout << "\twith it, and I am not promoting it.\n\n\t";
// Wait for user input
system("PAUSE");
}
void Credits()
{
// Clear screen
system("CLS");
cout << "\t____Cracker\\\\Credits____\n";
cout << "\tProgramming: Valkyr\n";
cout << "\tSpecial Thanks To:\n\tAll the peeps at GameDev.net and that\n";
cout << "\tpeeps at the GameDev forums for all their help.\n\n\t";
// Wait fort user input
system("PAUSE");
}
void Quit()
{
// cls
system("CLS");
cout << "Thanks for playing!" << endl;
}
// Server functions
void Crack()
{
// Clear screen
system("CLS");
cout << "WORKS!";
system("PAUSE");
}
🔒 Locked
Switch/Case Method
Advertisement
Original Post
Hey all,
I want to use the switch/case method in one part of my text game, but I don't want to have to use numbers to select the case. Ok, that sounded real funky. Basically I don't want the user to be able to type a name(e.g. "main") and go to "main" with out having a menu that says something like:
1)main
2)exit
I want there to be a blank prompt, and(in my case) the user to type the name of a fictional "server" and go to the "server."
But, I don't know how to do this because the switch/case method requires using the enum thing, right? Is there another method where I can use strings to do this? Sorry if I confused you. I'll go ahead and just post all of the source below. This is my first game, so don't laugh because my code is really messy(PS, thanks to ph33r for re-writing it).
[edited by - Valkyr on January 7, 2003 3:51:23 PM]
Have an array containing all the valid strings that can be entered. When the user types a string, search the array for that string, and use the index of the found string as the input to the switch statement.
There are many other ways of achieving the same thing, and most of them would be much better, but this is probably the simplest.
There are many other ways of achieving the same thing, and most of them would be much better, but this is probably the simplest.
What he''s saying is something like this I think:
const int TOTAL_SERVERS = 10;
char* valid_servers[TOTAL_SERVERS] = {"server1", "server2", ...};
cin >> server_StrChoice;
for (int i = 0; i < TOTAL_SERVERS; i++)
{
if (strcmp(server_StrChoice, valid_servers) == 1)
{
server_IntChoice = i;
break;
}
}
switch(server_IntChoice)
{
case 0:
break;
case 1:
.
.
.
}
That''s the basic gist of it, but I''m pretty sure there are much better ways of doing it. For instance, in a case like this, you''re really making it more complicated for yourself. Why not just use an if()else if() else construct?
Also, I noticed the system("cls"); commands. I''m not sure, but I think invoking the system in a text based game can cause to some nasty debugging later if your project gets big. If you include the "conio.h" library, you have access to the command clrscr() which does the same thing. Conio also has other useful functions like getch().
(The code I wrote above may not work exactly because I didn''t declare all the variables and the strcmp may be wrong -- that requires string.h btw -- but you get the general gist. Correct me if I''m wrong.)
const int TOTAL_SERVERS = 10;
char* valid_servers[TOTAL_SERVERS] = {"server1", "server2", ...};
cin >> server_StrChoice;
for (int i = 0; i < TOTAL_SERVERS; i++)
{
if (strcmp(server_StrChoice, valid_servers) == 1)
{
server_IntChoice = i;
break;
}
}
switch(server_IntChoice)
{
case 0:
break;
case 1:
.
.
.
}
That''s the basic gist of it, but I''m pretty sure there are much better ways of doing it. For instance, in a case like this, you''re really making it more complicated for yourself. Why not just use an if()else if() else construct?
Also, I noticed the system("cls"); commands. I''m not sure, but I think invoking the system in a text based game can cause to some nasty debugging later if your project gets big. If you include the "conio.h" library, you have access to the command clrscr() which does the same thing. Conio also has other useful functions like getch().
(The code I wrote above may not work exactly because I didn''t declare all the variables and the strcmp may be wrong -- that requires string.h btw -- but you get the general gist. Correct me if I''m wrong.)
Well, here's one approach. Say you declare an array of strings,
where each string is an IP address to some server:
Now then, after the user has enter the IP of some server to
connect to, you can search for the entered IP in the array
like this:
Alrighty then, now you can use the index we found in a switch
statement like this:
Was this what your were looking for ? I hope you can read it
Not the best way to handle it, but its a start.
Good luck!
[edited by - JackRabbit on January 7, 2003 5:27:39 PM]
where each string is an IP address to some server:
#define NUM_SERVERS 5 // Or whatever you need
string strServers[ NUM_SERVERS ] =
{
"bla.bla.bla.bla",// Server 1
"bla.bla.bla.bla",// Server 2
"bla.bla.bla.bla",// Server 3
"bla.bla.bla.bla",// Server 4
"bla.bla.bla.bla",// etc....
};
Now then, after the user has enter the IP of some server to
connect to, you can search for the entered IP in the array
like this:
// This will hold the index in the array of the server
// we found. -1 meaing No server found.
int iServerIndex = -1;
// Now we loop through the server array, looking for a match
for( int i=0;i < NUM_SERVERS; i++)
{
if( IP == strServers[ i ] )
{
// Found a server !!!! YAY!!!! :)
// Alright then, all we do now is to save the index:
iServerIndex = i;
break; // Bust out of the loop
}
}
Alrighty then, now you can use the index we found in a switch
statement like this:
switch( iServerIndex )
{
case -1:
{
// No server was found.
// ... do whatever should be done here.
}break;
case 0:
{
// .....
}break;
case 1:
{
// .....
}break;
case 2:
{
// .....
}break;
} // End switch
Was this what your were looking for ? I hope you can read it
Not the best way to handle it, but its a start.
Good luck!
[edited by - JackRabbit on January 7, 2003 5:27:39 PM]
I''m not too lazy to code, but too lazy to read. Unless I miss what you''re asking, this code could give you a hint on how to do it. The comments should explain what''s going on fairly well:
#include <string>
#include <iostream>
// List of all command identifiers.
enum Command {
ComError = 0, // Must remain the first
ComNewGame,
ComTutorial,
ComAbout,
ComCredits,
ComQuit,
ComLastIndex // Must remain the last
};
// Array with a struct that ties an identifier with a string
struct { Command com_id; char *com_str; } com_table[] = {
{ ComNewGame, "new" },
{ ComTutorial, "tutorial" },
{ ComAbout, "about" },
{ ComCredits, "credits" },
{ ComQuit, "quit" },
};
// Provided a string, returns the correct identifier if found,
// or the error identifier (== 0) if not found.
Command MatchCommand(std::string &com)
{
for(int i = 1; i < (int)ComLastIndex; i++)
if(com.compare(com_table[i].com_str) == 0)
return com_table[i].com_id;
return ComError;
}
int main(void)
{
// Get some input
std::string com;
std::cin >> com;
// Get the integer identifier and switch it
switch(MatchCommand(com)) {
case ComNewGame:
break;
case ComTutorial:
break;
case ComAbout:
break;
case ComCredits:
break;
case ComQuit:
break;
case ComError:
std::cout << "Unknown command";
break;
}
return 0;
}
quote:
Original post by xuwen
Also, I noticed the system("cls"); commands. I''m not sure, but I think invoking the system in a text based game can cause to some nasty debugging later if your project gets big.
Don''t worry, believe me, this project won''t get very big. It''s just for learning purposes. But I guess I shouldn''t get into the habit of using it, so I''ll go ahead and change all the cls''s. Thanks for the code!
JackRabit:
I tried you code, and everything seems to work fine, except for this one compile error I get. it's about the strServers string variable thingy. It says this:
`strServers' undeclared(first use this function)
It's the "if( ip == strServers[ i ] )" line. How do I fix this?
[edited by - Valkyr on January 7, 2003 9:52:29 PM]
I tried you code, and everything seems to work fine, except for this one compile error I get. it's about the strServers string variable thingy. It says this:
`strServers' undeclared(first use this function)
It's the "if( ip == strServers[ i ] )" line. How do I fix this?
[edited by - Valkyr on January 7, 2003 9:52:29 PM]
Well, i assumed for my example that the strServers array would
be declared as a global. Which i should have mentioned :D
Make sure you put the declaration at the top of your file.
But after your #include's and the "using namespace std;"
If it still wont work, or you simply dont understand what i mean, just post your code again.
Hope this helps :D
PS:
And yes, i did actually compile this code myself without problems.
[edited by - JackRabbit on January 8, 2003 3:53:58 AM]
be declared as a global. Which i should have mentioned :D
Make sure you put the declaration at the top of your file.
But after your #include's and the "using namespace std;"
If it still wont work, or you simply dont understand what i mean, just post your code again.
Hope this helps :D
PS:
And yes, i did actually compile this code myself without problems.
[edited by - JackRabbit on January 8, 2003 3:53:58 AM]
Ok, I'm pretty sure it's a global variable. It's in the list of variables right after "int main(){." Anyways, here's my code:
[edited by - Valkyr on January 8, 2003 7:41:25 PM]
/*Title: Cracker
*File name: "cracker.exe"
*Description: Cracker is a text-based game
* that I wrote while learning C++. It is not
* meant to have a a snazzy GUI or great gameplay;
* it's mostly for my personal learning.
*Date: Jan 4, 2003
*Copyright: Absolute freeware, chnage the code,
* redistribute, sell it if you can find someone
* who would actually pay for it, etc.
*/
#include <iostream>
#include <stdlib.h>
#include <string>
#include <conio.h>
#define NUM_SERVERS 2
using namespace std;
enum GAME_STATE { NEW_GAME = 1, TUTORIAL, ABOUT, CREDITS, QUIT };
void GameLoop();
void Tutorial();
void About();
void Credits();
void Quit();
// Start the main portion
int main()
{
int choice;
// Create server list
string strServers[ NUM_SERVERS ] =
{
"crack0rz.org", // Server 1
"65.746.388.41", // Server 2
};
while(choice != 5)
{
//Clear screen making sur to get rid of old text
//from each iteration
system("CLS");
//start creating menu
cout << "\t____Cracker\\\\Menu____\n";
cout << "\t1)New Game\n";
cout << "\t2)Tutorial(HIGHLY RECOMMENDED)\n";
cout << "\t3)About/Disclaimer\n";
cout << "\t4)Credits\n";
cout << "\t5)Quit\n\n";
//get user input
cout << ">";
cin >> choice;
//Game state machine
//Check for wich state the game is currently in
switch(choice)
{
case NEW_GAME:
GameLoop();
break;
case TUTORIAL:
Tutorial();
break;
case ABOUT:
About();
break;
case CREDITS:
Credits();
break;
case QUIT:
Quit();
break;
}
}
return 0;
}
void GameLoop()
{
// Clear the screen
system("CLS");
string prompt;
string ip;
// Main game loop
while(prompt != "exit")
{
cout << ">";
cin >> prompt;
// Connect stuff
if(prompt == "c")
{
cout << endl << "\tCONNECT TO: ";
cin >> ip;
// Search the array for a match. (-1 meaning no server found).
int iServerIndex = -1;
// Loop through the server array and look for a match
for( int i=0; i < NUM_SERVERS; i++)
{
if( ip == strServers[ i ] )
{
// Server found
// Save the index
iServerIndex = i;
break; // Escape da 100p
}
}
// Begin the switch/case thingy
switch( iServerIndex )
{
case -1:
{
cout << "ERROR: No server at this ip/address could be found!";
}
break;
case 0:
{
system("CLS");
cout << "Crack0rz.org" << endl;
}
break;
case 1:
{
system("CLS");
cout << "\tServer 2" << endl;
}
break;
} // End switch
}
}
}
void Tutorial()
{
// Clear the screen
system("CLS");
cout << "\n\nWelcome to Cracker tutorial. This will teach you how to\n";
cout << "\t1)Log on to a hacker community server\n";
cout << "\t2)Buy software\n";
cout << "\t3)Upload, download, and delete files from servers(viruses, etc.)\n";
cout << "\t4)";
}
void About()
{
int null;
// Clear the screen
system("CLS");
cout << "\t____Cracker\\\\About/Disclaimer____\n";
cout << "\tCrack is a hacking *simulator that\n";
cout << "\tI programmed in my spare time to help me learn C++.\n";
cout << "\tIt is in NO WAY a hacking tutorial and should not be\n";
cout << "\ttaken that way. I, myself, am not a hacker/cracker, and\n";
cout << "\tI DO NOT encourage it.\n\n";
cout << "\t----crack0rz.org and other sites----\n";
cout << "\tAt the time of this writing a website did not\n";
cout << "\texist at this domain. If one does now, I am not affiliated\n";
cout << "\twith it, and I am not promoting it. Same goes for all the\n";
cout << "\tother sites, IPs, and whatnots.\n\n\t";
// Wait for user input
cout << "Press any key to return to the main menu";
getch();
}
void Credits()
{
// Clear screen
system("CLS");
cout << "\t____Cracker\\\\Credits____\n";
cout << "\tProgramming: Valkyr\n";
cout << "\t And a lotta members of the GD forums(THANKS!)\n";
cout << "\tSpecial Thanks To:\n\tAll the peeps at GameDev.net and that\n";
cout << "\tpeeps at the GameDev forums for all their help.\n\n\t";
// Wait fort user input
cout << "Press any key to return to the main menu";
getch();
}
void Quit()
{
// cls
system("CLS");
cout << "Thanks for playing!" << endl;
}
// Server functions
void Crack0rz()
{
// Clear screen
system("CLS");
cout << "WORKS!";
cout << "Press any key to return to the main menu";
getch();
}
[edited by - Valkyr on January 8, 2003 7:41:25 PM]
This won''t solve the current problem, but a couple of points:
1) Never test a variable without first initializing it. In your main() for example, you have this:
int choice;
while(choice!=5) {}
It will usually work, but there is no guarantee that choice does not contain a 5 when it is first created. If you are going to test a variable straight away like this, you should explicitly give it a value.
2) Just a suggestion to help you debug, but you should consider changing your prompts to something more informative. Instead of displaying a ">", make it something like "main menu >" and "game >". That way you can easily see from the prompt what part of the program is running. If you don''t like it, you can always change it back to ">" after the program is debugged.
1) Never test a variable without first initializing it. In your main() for example, you have this:
int choice;
while(choice!=5) {}
It will usually work, but there is no guarantee that choice does not contain a 5 when it is first created. If you are going to test a variable straight away like this, you should explicitly give it a value.
2) Just a suggestion to help you debug, but you should consider changing your prompts to something more informative. Instead of displaying a ">", make it something like "main menu >" and "game >". That way you can easily see from the prompt what part of the program is running. If you don''t like it, you can always change it back to ">" after the program is debugged.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement