1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/*
Simple Game Loader
-Free-
Warning: When executing this program from the command line,
argument 1 should not have a name of an important file
that exists and is not associated to this program.
Just think of the possibilities...
Load - Possibly read massive amounts of data.
Save - Possibly overwrite important file with loader data.
*/
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
using namespace std;
void register_game(int &number_of_games,vector<string> &game_name,vector<string> &game_path)
{
cout<<"\n===== Register Menu ====="<<endl;
string temp;
cout<<"Name: ";
getline(cin,temp);
game_name.push_back(temp);
cout<<"Path: ";
getline(cin,temp);
game_path.push_back(temp);
number_of_games++;
}
void load_from_file(vector<string> &game_name, vector<string> &game_path,int &number_of_games,string game_data)
{
cout<<"\n===== Auto Load " << game_data << " ====="<<endl;
fstream file(game_data.c_str(), ios::in);
if(file.bad())
cout<<game_data<<" does not exist - file will be created at exit"<<endl;
string s;
do
{
getline(file,s);
if(s == "")
break;
game_name.push_back(s);
getline(file,s);
if(s == "")
break;
game_path.push_back(s);
number_of_games++;
}while(true);
file.close();
}
void save_to_file(vector<string> game_name, vector<string> game_path,int number_of_games,string game_data)
{
cout<<"\n===== Auto Save " << game_data << " ====="<<endl;
fstream file(game_data.c_str(),ios::out|ios::trunc);
for(int i=0;i<number_of_games;i++)
{
file << game_name[i] << '\n';
file << game_path[i] << '\n';
}
file.close();
}
void game_option(int int_choice,vector<string> &game_name,vector<string> &game_path,int &number_of_games)
{
do
{
int i=0;
string choice;
cout<<"\n===== "<<game_name[int_choice]<<" "<<game_path[int_choice]<<" ====="<<endl;
cout<<endl<<i++<<". Play "<<game_name[int_choice]<<endl;
cout<<endl<<i++<<". Edit "<<game_name[int_choice]<<endl;
cout<<endl<<i++<<". Delete "<<game_name[int_choice]<<endl;
cout<<endl<<i++<<". Exit "<<game_name[int_choice]<<" option"<<endl;
getline(cin,choice);
if(choice == "0")
{
//for simplicity using system(); use CreateProcess instead for performance
system(game_path[i].c_str());
}
if(choice == "1")
{
register_game(number_of_games,game_name,game_path);
--number_of_games;
game_name[int_choice] = game_name[number_of_games];
game_name.pop_back();
game_path[int_choice] = game_name[number_of_games];
game_path.pop_back();
}
if(choice == "2")
{
game_name.erase(game_name.begin()+int_choice);
game_path.erase(game_path.begin()+int_choice);
--number_of_games;
break;
}
if(choice == "3")
{
break;
}
}while(true);
}
int main(int argc, char* argv[])
{
string game_data;
if(argc>1)
{
game_data = argv[1];
}
else
{
game_data = "my_game_list.dat";
}
vector<string> game_name;
vector<string> game_path;
int number_of_games=0;
load_from_file(game_name,game_path,number_of_games,game_data);
do
{
int i;
string choice;
cout<<"\n===== Main Menu ====="<<endl;
cout<<"Choose a game: "<<endl;
for(i=0;i<number_of_games;i++)
{
cout<<i<<". "<<game_name[i]<<endl;
}
cout<<endl<<i<<". Register new game"<<endl;
cout<<endl<<i+1<<". Exit"<<endl;
cout<<"\nChoice: ";
getline(cin,choice);
int int_choice = atoi(choice.c_str());
if(int_choice>=0 && int_choice<number_of_games)
{
game_option(int_choice,game_name,game_path,number_of_games);
}
else
{
if(int_choice==number_of_games)
{
register_game(number_of_games,game_name,game_path);
}
if(int_choice==number_of_games+1)
{
break;
}
}
}while(true);
save_to_file(game_name,game_path,number_of_games,game_data);
return 0;
}
|