⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 choices.cpp

📁 用java编的游戏.rar
💻 CPP
字号:
/*
LuckY
A demonstration of how to make your own
choice-based text-reading game. For example,
"A big ogre is coming. What do you do?
 choices: 1.Fight 2.Run...etc"
I've seen a few people actually write an
entire program with a BUNCH of couts and the
entire manuscript written into the code and 
thought 'hey, that should be in a separate
text file' and so I did it. It is very simple
and does not show you how to move around in a
file based on the choices the user has made. It
simply lets you continue if u choose the right
one or ends the game when you've made a bad 
choice. There is lots of room for more 
interesting changes, but I'll leave that to 
you. Hope you learn from the code!

If you want to be really adventurous, 
ps. Look in the included 'choices.scr' file
    for the proper syntax of the 'script' file

12.26.00

lucky760@yahoo.com
*/

#include <fstream>
#include <cstring>

const int LEN = 60;
const int MAXCHOICES = 5;

struct menu_s {
  char caption[256];
  char choices[5][LEN];
  int correct;
  char msg[LEN];
};

int ReadFile(ifstream&);
ifstream& GetLine(ifstream&,char*,int);

int main() {
  int choice;

  ifstream fin("choices.scr");
  
  if (!fin.is_open()) {
    cerr << "ERROR opening file!\n";
    return 0;
  }

  while ((choice = ReadFile(fin)) > 0)
    ;

  cout << (choice == -1 ? "\nYou won!\n" : "\nYou lost...\n");
  fin.close();

  return 0;
}

int ReadFile(ifstream &fin) {
  char line[256];
  char temp;
  int totalchoices=0;
  menu_s menu;
  
  *menu.caption = 0;

  while (GetLine(fin,line,256) && strcmp(line,"[END]")) {
    if (!strlen(line))
      continue;

    if (!strcmp(line,"[caption]")) 
      while (GetLine(fin,line,256) && strcmp(line,"[choices]") && strlen(line))
          strcat(menu.caption,line);
             
    if (!strcmp(line,"[choices]")) {
      int i=0;
      for (;i<MAXCHOICES;++i) {
        GetLine(fin,line,LEN);
        if (!strlen(line) || !strcmp(line,"[correct]")) 
          break;
        strcpy(menu.choices[i],line);
      }
      totalchoices = i;
    }

    if (!strcmp(line,"[correct]"))
      (fin >> menu.correct).get();
    else if (!strcmp(line,"[msg]"))
      GetLine(fin,menu.msg,LEN);
  }

  cout << "-------------------\n";
  cout << menu.caption << "\n\n";
  cout << "What do you do?\n\n";
  for (int i=0;i<totalchoices;++i)
    cout << i+1 << ". " << menu.choices[i] << "\n";
  cout << "\nEnter choice: ";

  int choice;
  (cin >> choice).get();

  if (choice != menu.correct)
    return 0;
  else
    cout << endl <<  menu.msg << endl;

  if (fin.eof())
    return -1;

  return choice;
}  

//this gets a line and removes comments before processing
ifstream& GetLine(ifstream &fin,char *str,int max) {
  fin.getline(str,max);
  char *commentloc = strstr(str,"//");
  if (commentloc)
    *commentloc = '\0';
  return fin;
}
    
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -