diff --git a/level1/p09_maze/maze.cpp b/level1/p09_maze/maze.cpp new file mode 100644 index 00000000..f5411e6d --- /dev/null +++ b/level1/p09_maze/maze.cpp @@ -0,0 +1,122 @@ +// I don't know if I set the target in my own account successfully + +#include +#include +#include +#include +#include +int map[100][100]; // 0 is road ,1 is block, 3 is person, 2 is terminal +int n=11,m=20; +#define alpha (1) +#define beta 60 +#define sleep_time 5000 +void sleep(){ + for(int i=1;i=(rt-lt)*alpha ) d=1; + if ( (rt-lt)>=(dn-up)*alpha ) d=0; + if (up==dn) d=0; + if (lt==rt) d=1; + if ( d ){ + int d1=random(up,dn-1); + int d2=random(lt,rt); + map[d1*2][d2*2-1]=0; + build(up,d1,lt,rt); + build(d1+1,dn,lt,rt); + } + if ( !d ){ + int d1=random(lt,rt-1); + int d2=random(up,dn); + map[d2*2-1][d1*2]=0; + build(up,dn,lt,d1); + build(up,dn,d1+1,rt); + } +// if (dn-up>=2&&rt!=m&&rt-lt>=2&&dn!=n) // make the map be a forest but not a single tree , and the bigger beta is ,the more trees is would be +// if (random(1,100)=0&&nx<=2*n) + if (ny>=0&&ny<=2*m) + if (map[nx][ny]!=1){ + if (map[nx][ny]==2) return 1; // success + map[player_x][player_y]=0; + player_x=nx; player_y=ny; + map[player_x][player_y]=3; + } + return 0; +} +void game_start(){ + init_maze(); + print(); + int success=0; + while(!success){ + success=move( get_d() ); + system("cls"); + print(); + sleep(); + } + sleep();sleep();sleep(); + system("cls"); + printf("YOU WIN !!!"); + return; +} +int main(){ + game_start(); + return 0; +} diff --git a/level1/p10_pushBoxes/map1.txt b/level1/p10_pushBoxes/map1.txt new file mode 100644 index 00000000..c7297da6 --- /dev/null +++ b/level1/p10_pushBoxes/map1.txt @@ -0,0 +1,8 @@ +7 7 +5 5 5 5 5 5 5 +5 0 0 2 0 0 5 +5 0 0 0 0 0 5 +5 0 0 1 0 0 5 +5 0 0 0 0 0 5 +5 0 3 0 0 0 5 +5 5 5 5 5 5 5 diff --git a/level1/p10_pushBoxes/map2.txt b/level1/p10_pushBoxes/map2.txt new file mode 100644 index 00000000..4b789ad3 --- /dev/null +++ b/level1/p10_pushBoxes/map2.txt @@ -0,0 +1,11 @@ +10 10 +5 5 5 5 5 5 5 5 5 5 +5 2 0 0 0 0 0 0 0 5 +5 0 1 0 0 0 0 3 0 5 +5 0 0 0 0 0 0 0 0 5 +5 0 0 0 5 5 0 0 0 5 +5 0 0 0 5 5 0 0 0 5 +5 0 0 0 0 0 0 0 0 5 +5 0 3 0 0 0 0 1 0 5 +5 0 0 0 0 0 0 0 0 5 +5 5 5 5 5 5 5 5 5 5 diff --git a/level1/p10_pushBoxes/map3.txt b/level1/p10_pushBoxes/map3.txt new file mode 100644 index 00000000..530d7467 --- /dev/null +++ b/level1/p10_pushBoxes/map3.txt @@ -0,0 +1,13 @@ +12 12 +5 5 5 5 5 5 5 5 5 5 5 5 +5 2 0 0 0 0 0 0 0 0 0 5 +5 0 3 0 0 3 0 1 0 0 0 5 +5 0 0 0 5 0 0 0 5 0 0 5 +5 0 0 0 5 0 0 0 5 0 0 5 +5 0 0 0 5 0 1 0 5 0 0 5 +5 0 0 0 5 0 0 0 5 0 0 5 +5 0 0 0 5 5 5 5 5 0 0 5 +5 0 1 0 0 0 0 0 0 0 0 5 +5 5 5 0 0 0 0 0 0 3 0 5 +5 5 5 0 0 0 0 0 0 0 0 5 +5 5 5 5 5 5 5 5 5 5 5 5 diff --git a/level1/p10_pushBoxes/pushboxes.cpp b/level1/p10_pushBoxes/pushboxes.cpp new file mode 100644 index 00000000..7371c6a0 --- /dev/null +++ b/level1/p10_pushBoxes/pushboxes.cpp @@ -0,0 +1,105 @@ +// □ stand for box v==1 +// ■ stand for player v==2 +// ○ stand for the place the box should be put in v===3 +// ● stand for the place with a box in it v==4 +// █ stand for block v==5 +#include +#include +#include +#include +const int maxn=20; +int get_d(){ + char ch=getch(); + if (ch==-32){ + ch=getch(); + if (ch==72) return 2; + if (ch==80) return 1; + if (ch==75) return 3; + if (ch==77) return 4; + return 0; + } + return 0; +} +int mov[5][2]={{0,0},{1,0},{-1,0},{0,-1},{0,1}}; +struct pushbox{ + int map[maxn][maxn]; + int width,height; + int length,sumlength,score; + int remain; + int player_x,player_y; + void init(){ + remain=0; + for(int i=1;i<=height;i++) + for(int j=1;j<=width;j++) { + if(map[i][j]==1) remain++; + if(map[i][j]==2) {player_x=i,player_y=j;} + } + } + void print(){ + for(int i=1;i<=height;i++){ + for(int j=1;j<=width;j++) + if (!map[i][j]) printf(" "); + else if (map[i][j]==1) printf("□"); + else if (map[i][j]==2) printf("■"); + else if (map[i][j]==3) printf("○"); + else if (map[i][j]==4) printf("●"); + else if (map[i][j]==5) printf("█"); + printf("\n"); + } + } + int one_step(int d){ + int nx=player_x+mov[d][0]; + int ny=player_y+mov[d][1]; + if (nx>=1&&nx<=height) + if (ny>=1&&ny<=width) + if (map[nx][ny]!=5&&map[nx][ny]!=4&&map[nx][ny]!=3) + if (map[nx][ny]==0) {map[nx][ny]=2;map[player_x][player_y]=0;player_x=nx;player_y=ny;} + if (map[nx][ny]==1){ + int nnx=nx+mov[d][0],nny=ny+mov[d][1]; + if (nnx>=1&&nnx<=height) + if (nny>=1&&nny<=width) + if (map[nnx][nny]==0) { map[nx][ny]=2;map[nnx][nny]=1;map[player_x][player_y]=0; player_x=nx;player_y=ny;} + if (map[nnx][nny]==3) { map[nx][ny]=2;map[nnx][nny]=4;map[player_x][player_y]=0; remain--;player_x=nx;player_y=ny;} + } + if (remain) return 0; + return 1; + } + + void load(int level){ + FILE *fp; + if (level==1) fp = fopen("map1.txt","r"); + if (level==2) fp = fopen("map2.txt","r"); + if (level==3) fp = fopen("map3.txt","r"); + + fscanf(fp,"%d%d",&width,&height); + for(int i=1;i<=height;i++) + for(int j=1;j<=width;j++) fscanf(fp,"%d",&map[i][j]); + fclose(fp); + } + + + +}game; + +void game_start(){ + int level=1; + int success; + while (level<=3){ + game.load(level); + game.init(); game.print(); + success=0; + while (!success){ + success=game.one_step(get_d()); + system("cls"); + game.print(); + } + system("cls"); + printf("YOU WIN !!! Level Up!!!"); + level++; + } +} +int main(){ + game_start(); + return 0; + +} diff --git a/level1/p12_warehouse/warehouse.cpp b/level1/p12_warehouse/warehouse.cpp new file mode 100644 index 00000000..59a4dd37 --- /dev/null +++ b/level1/p12_warehouse/warehouse.cpp @@ -0,0 +1,136 @@ +// just put save.txt and load.txt in the same direction of warehouse.cpp is ok . +// it doesn't matter if you don't create the two txt , my program take it in to consideration + +#include +#include +#include + +using namespace std; + +struct Item{ + int amount,id; + char name[10]; + Item *next; + Item(int i,int n,char *s): id(i),amount(n),next(NULL) { + strcpy(name,s); + } + void print(){ + printf("the name of the %d th item : %s , and the amount is %d\n",id,name,amount); + if (next) next->print(); + else printf("we have these %d items above in total\n",id); + } + +}*head=NULL; + +void modify(Item* &o,char* s,int amount,int sigma){ // sigma=1 is in_warehouse and sigma=-1 is out_warehouse + if (!o && sigma==1){ // the first item so the id=1 + o=new Item(1,amount,s); + printf("Add successfully !\n"); + return ; + } + if (!o && sigma==-1){ + printf("ERROR! we don't have such item named '%s'\n",s); + return; + } + if ( strcmp(s,o->name) ==0 ){ + if (sigma==1){ + o->amount+=amount; + printf("Add successfully !\n"); + return ; + } + if (sigma==-1 && amount > o->amount ){ + printf("ERROR! we have this item but we don't have enough, we only have %d\n",o->amount); + return ; + } + if (sigma==-1 && amount <= o->amount ){ + o->amount-=amount; + printf("Delete successfully !\n"); + } + if (o->amount==0){ // exactly out this item with no remain so attach the next to the ahead and delete this pointer + Item* p=o; + o=o->next; + delete(p); // to save memory + return ; + } + return ; + } + if ( !o->next ) { + if (sigma==-1) { + printf("ERROR! we don't have such item named '%s'\n",s); + } + else { // is not the first item so the id is o->id + 1 + o->next=new Item(o->id+1,amount,s); + printf("Add successfully !\n"); + } + return ; + } + modify(o->next,s,amount,sigma); + return ; +} + +void print_instruction(){ + printf("please choose your requirment ,just input the number \n"); + printf("1: Show the inventory\n"); + printf("2: put something in storage\n"); + printf("3:take out something from storage\n"); + printf("4: save and end\n"); +} +void print(){ // op==1 + if( !head ) printf("We have no storage at present \n"); + else head->print(); + system("pause"); +} + +char read_name[10]; +int read_amount; +void add(){ // op==2 + printf("please imput the name , the amount\n"); + scanf("%s%d",read_name,&read_amount); + modify(head,read_name,read_amount,1); + system("pause"); +} +void delet(){ // op==3 + printf("please imput the name , the amount\n"); + scanf("%s%d",read_name,&read_amount); + modify(head,read_name,read_amount,-1); + system("pause"); +} +void save(){ // op==4 + FILE *fp = fopen("save.txt","w"); + Item *o=head; + while(o){ + fprintf(fp,"%s %d\n",o->name,o->amount); + o=o->next; + } + fclose(fp); + printf("save successfully !\n press any key to END THE PROGRAM "); +} +void load(){ + FILE *fp = fopen("load.txt","r"); + while(fp && fscanf(fp,"%s%d",read_name,&read_amount)==2){ + modify(head,read_name,read_amount,1); + } + system("cls"); + printf("Program Start !\n"); + if (fp) printf(" Load successfully !") ; // different output defends on whether we have load.txt + fclose(fp); + system("pause"); +} +int main(){ + load(); + system("cls"); + print_instruction(); + int op; // op is short for "operation" + while ( scanf("%d",&op) ){ + system("cls"); + if (op==1) print(); + else if (op==2) add(); + else if (op==3) delet(); + else if (op==4) { + save(); break; + } + system("cls"); + print_instruction(); + } + return 0; +}