Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
保存游戏进度机制完成
Browse files Browse the repository at this point in the history
  • Loading branch information
Rratic committed Feb 20, 2021
1 parent dff3975 commit 1a1b739
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ test.*

# Program files
save/setting.txt
save/saving.txt
3 changes: 3 additions & 0 deletions cpp11/file-control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ bool tags::save(string filename){
}
return true;
}
bool tags::without(string x){
return tag.find(x)==tag.end();
}
tags lang,setting,savedgame;
1 change: 1 addition & 0 deletions cpp11/file-control.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class tags{
bool fill(string filename);
string search(string tagname);
bool save(string filename);
bool without(string x);
};
extern tags lang,setting,savedgame;
#endif
20 changes: 16 additions & 4 deletions cpp11/maze-game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ void init(){
if(!setting.fill("save/setting.txt")){
setting.tag["level-data"]="data/levels.data";
setting.tag["lang-data"]="data/lang/en.txt";
setting.tag["saving"]="save/saving.txt";
if(!setting.save("save/setting.txt")){
cout<<"Error saving setting!\nTarget file path:save/setting.txt\n";
cin>>input;
Expand All @@ -18,6 +19,7 @@ bool game::set(string filename,string find){
ifstream fin;
fin.open(filename,ios::in);
if(!fin.is_open())return false;
name=find;
string s;
find='$'+find;
unsigned short num,len;
Expand Down Expand Up @@ -59,15 +61,20 @@ unsigned short game::work(){
while(true){
a=chunks[focusing].work(b,help_words);
switch(a){
case 0:end_with(lang.tag["win"]+"\nYour score:"+tos(b));return 0;
case 0:{
end_with(lang.tag["win"]+'\n'+lang.tag["score"]+':'+tos(b));
savedgame.tag[name]=tos(b);
savedgame.save(setting.tag["saving"]);
return 0;
}
case 1:end_with(lang.tag["die-0"]);return 1;
case 4:return 4;
}
}
return 0;
}
void menu(){
cout<<"\033[92m"<<lang.tag["game-name"]<<" v"<<version<<'\n'\
cout<<"\033[92m"<<lang.tag["game-name"]<<'\n'\
<<"\033[93m===\n\033[m"\
<<"1="<<lang.tag["menu-first"]<<'\n'\
<<"2="<<lang.tag["menu-second"]<<'\n'\
Expand All @@ -87,7 +94,11 @@ void level_menu(){
game all;
while(true){
for(unsigned short i=1;i!=5;++i){
cout<<"1-"<<i<<' '<<lang.tag["name-1-"+tos(i)]<<'\n';
string te="1-"+tos(i);
cout<<"1-"<<i<<' '<<lang.tag["name-"+te]<<' ';
if(savedgame.without(te))cout<<lang.tag["level-uncompleted"];
else cout<<lang.tag["score"]<<':'<<savedgame.tag[te];
cout<<'\n';
}
cout<<"0 "<<lang.tag["exit"]<<"\n"\
<<lang.tag["menu-choice"]<<">>[ ]\033[4D";
Expand Down Expand Up @@ -117,6 +128,7 @@ void set_game(){
return;
}
setting.tag[input]=input2;
if(input=="lang-data")lang.fill(setting.tag["lang-data"]);
if(input=="lang-data")lang.fill(input2);
if(input=="saving")savedgame.fill(input2);
setting.save("save/setting.txt");
}
3 changes: 1 addition & 2 deletions cpp11/maze-game.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
#define rra_maze_game
#include "maze-chunk.h"
#include "file-control.h"
#define version "0.2.0"
extern string input;
class game{
private:
unsigned short focusing;
vector<chunk>chunks;
public:
string help_words;
string name,help_words;
bool set(string filename,string find);
unsigned short work();
void clear();
Expand Down
2 changes: 2 additions & 0 deletions data/lang/en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ exit:Exit
pause:Press any key to continue.
---
level-unfound:Error opening level :(
level-uncompleted:Uncompleted
score:Score
win:Success!
die-0:You fell into the void!
---
Expand Down
2 changes: 2 additions & 0 deletions data/lang/zh.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ exit:退出
pause:按任意键继续
---
level-unfound:打开关卡失败qaq
level-uncompleted:未完成
score:分数
win:成功了!
die-0:你掉进了虚空!
---
Expand Down

0 comments on commit 1a1b739

Please sign in to comment.