diff --git a/Running letter b/Running letter new file mode 100644 index 00000000..b6d03c14 --- /dev/null +++ b/Running letter @@ -0,0 +1,37 @@ +#include +#include +void running(int col); +int main(void) +{ + system("mode con cols=63"); + running(63); + + return 0; +} +void running(int col) +{ + int i,j; + while(1) + { + for(i=0;i<=col-1;i++) + { + Sleep(50); + system("cls"); + for(j=0;j'); + } + for(i=col-1;i>=0;i--) + { + Sleep(50); + system("cls"); + for(j=0;j +#include +#include + +void running(int col); + +int main(void){ + system("mode con cols=63"); + running(63); + + return 0; +} +void running(int col){ + int i=0,j; + bool flag=0; + + while(1){ + Sleep(50); + system("cls"); + for(j=0;j +#include + +int is_prime(int n); + +int main(void) +{ + int n; + + scanf("%d",&n); + if(is_prime(n)) + { + printf("%d is prime.\n",n); + } + else + { + printf("%d isn/'t prime.\n"); + } + + return 0; + +} +int is_prime(int n) +{ + int i; + + for(i=2;i<=pow(n,0.5);i++) + { + if(n%i==0) + { + return 0; + } + } + return 1; +} diff --git a/practices/c/level1/p03_Diophantus/Diophantus.c b/practices/c/level1/p03_Diophantus/Diophantus.c new file mode 100644 index 00000000..eb5a4ebc --- /dev/null +++ b/practices/c/level1/p03_Diophantus/Diophantus.c @@ -0,0 +1,26 @@ +#include +#define rate 1/6.0+1/12.0+1/7.0 + +void Diophantus(void); + +int main(void) +{ + Diophantus(); + system("pause"); + + return 0; +} +void Diophantus(void) +{ + int age_father=6; + + while(age_father<=200) + { + if(age_father==(int)age_father*(rate+0.5)+9) + { + printf("%d\n",age_father-4); + } + age_father+=2; + } + +} diff --git a/practices/c/level1/p04_ narcissus/narcissus.c b/practices/c/level1/p04_ narcissus/narcissus.c new file mode 100644 index 00000000..e46ab986 --- /dev/null +++ b/practices/c/level1/p04_ narcissus/narcissus.c @@ -0,0 +1,27 @@ +#include + +void narcissus(void); + +int main(void) +{ + narcissus(); + + return 0; +} +void narcissus(void) +{ + int i; + int a,b,c; + + for(i=100;i<1000;i++) + { + a=i/100; + b=(i-a*100)/10; + c=i-a*100-b*10; + if(i==a*a*a+b*b*b+c*c*c) + { + printf("%d\t",i); + } + } + +} diff --git a/practices/c/level1/p05_allPrimes/allprimes.c b/practices/c/level1/p05_allPrimes/allprimes.c new file mode 100644 index 00000000..f9c21269 --- /dev/null +++ b/practices/c/level1/p05_allPrimes/allprimes.c @@ -0,0 +1,40 @@ +#include +#include +#include + +int is_prime(int n); +void printprime(void); + +int main(void) +{ + printprime(); + + return 0; +} +void printprime(void) +{ + int max,i; + float delta; + + scanf("%d",&max); + delta=clock(); + for(i=2;i<=max;i++) + { + if(is_prime(i)) + { + printf("%d\t",i); + } + } + delta=clock()-delta; + printf("\nIt takes %.2f ms\n",delta); +} +int is_prime(int n) +{ + int j; + + for(j=2;j<=pow(n,0.5);j++) + { + if(n%j==0)return 0; + } + return 1; +} diff --git a/practices/c/level1/p05_allPrimes/prime.c b/practices/c/level1/p05_allPrimes/prime.c new file mode 100644 index 00000000..5a10f18e --- /dev/null +++ b/practices/c/level1/p05_allPrimes/prime.c @@ -0,0 +1,83 @@ +#include +#include +typedef struct link_list +{ + long long int num; + struct link_list * next; +}LINK; +LINK * create(long long int n); +void dele(LINK * head); +void screen(LINK * head); +void display(LINK * head); +int main(void) +{ + long long int max; + LINK * _head; + scanf("%lld",&max); + _head=create(max); + + //screen(_head); + display(_head); + + system("pause"); + return 0; +} +LINK * create(long long int n) +{ + LINK *head,*cp1,*cp2; + long long int i; + + head=NULL; + cp1=cp2=(LINK *)malloc(sizeof(LINK *)); + for(i=2;i<=n;i++) + { + if(2==i) + { + head=cp1; + } + else + { + cp1->next=cp2; + cp1=cp2; + } + cp1->num=i; + cp2=(LINK *)malloc(sizeof(LINK *)); + } + cp1->next=NULL; + free(cp2); + + return head; +} +void screen(LINK * head) +{ + LINK *cp1,*cp2,*cp3,*temp; + cp1=cp2=cp3=head; + while(cp3!=NULL) + { + cp2=cp3; + cp1=cp3; + while(cp2!=NULL&&cp1!=NULL) + { + cp2=cp2->next; + if(cp2!=NULL&&0==cp2->num%cp3->num) + { + temp=cp2; + cp1->next=cp1->next->next; + cp2=cp2->next; + } + cp1=cp1->next; + } + cp3=cp3->next; + } +} +void display(LINK * head) +{ + LINK *cp; + + cp=head; + while(cp!=NULL) + { + printf("%lld\t",cp->num); + cp=cp->next; + } +} diff --git a/practices/c/level1/p06_Goldbach/Goldbach.c b/practices/c/level1/p06_Goldbach/Goldbach.c new file mode 100644 index 00000000..0e39bf57 --- /dev/null +++ b/practices/c/level1/p06_Goldbach/Goldbach.c @@ -0,0 +1,75 @@ +#include +#include + +int is_prime(int n); +int _prime(int *P); +void Goldbach(int *P,int n); + +int main(void) +{ + int prime[100]; + int max; + + max=_prime(prime); + Goldbach(prime,max); + + return 0; + +} +void Goldbach(int *P,int n) +{ + int even=4; + int i,j; + int B; + + while(even<=100) + { + B=0; + for(i=0;i + +void encrypt(void); +void decrypt(void); + +int main(void) +{ + char judge; + + printf("1.encrypt 2.decrypt q.quit\n"); + while((judge=getch())!='q') + { + switch(judge) + { + case '1': + system("cls"); + printf("mode encrypt:\n"); + encrypt(); + break; + case '2': + system("cls"); + printf("mode decrypt:\n"); + decrypt(); + break; + case 'q': + return 0; + default: + printf("error\n"); + system("pause"); + } + system("cls"); + printf("1.encrypt 2.decrypt q.quit\n"); + } + + return 0; +} +void encrypt(void) +{ + char ch1; + + while((ch1=getchar())!=EOF) + { + if((ch1>=65&&ch1<=87)||(ch1>=97&&ch1<=119)) + { + putchar(ch1+3); + } + else if(ch1>87&&ch1<=90) + { + putchar(64+(ch1-87)); + } + else if(ch1>119&&ch1<=122) + { + putchar(96+(ch1-119)); + } + else if(ch1=='\n') + { + putchar('\n'); + } + else + { + putchar(' '); + } + } +} +void decrypt(void) +{ + char ch2; + + while((ch2=getchar())!=EOF) + { + if((ch2>=68&&ch2<=90)||(ch2>=100&&ch2<=122)) + { + putchar(ch2-3); + } + else if(ch2>64&&ch2<=67) + { + putchar(87+ch2-64); + } + else if(ch2>96&&ch2<=99) + { + putchar(119+ch2-96); + } + else if(ch2=='\n') + { + putchar(ch2); + } + else + { + putchar(' '); + } + } +} diff --git a/practices/c/level1/p08_hanoi/hanoi.c b/practices/c/level1/p08_hanoi/hanoi.c new file mode 100644 index 00000000..140e66ab --- /dev/null +++ b/practices/c/level1/p08_hanoi/hanoi.c @@ -0,0 +1,40 @@ +#include + +void Hanoi(int n,char a,char b,char c,unsigned long int * i); + +int main(void) +{ + char aP='A',bP='B',cP='C'; + unsigned long int sum=0; + int num; + + printf("Please enter the number of tower:\n"); + scanf("%d",&num); + if(num<=0) + { + printf("error\n"); + system("pause"); + + return 0; + } + Hanoi(num,aP,bP,cP,&sum); + printf("The sum is %u\n",sum); + + return 0; +} +void Hanoi(int n,char a,char b,char c,unsigned long int * i) +{ + if(n==1) + { + printf("%c to %c\n",a,c); + *i=*i+1; + return; + } + else + { + Hanoi(n-1,a,c,b,i); + Hanoi(1,a,b,c,i); + Hanoi(n-1,b,a,c,i); + } + +} diff --git a/practices/c/level1/p09_maze/maze.c b/practices/c/level1/p09_maze/maze.c new file mode 100644 index 00000000..3f5bba9b --- /dev/null +++ b/practices/c/level1/p09_maze/maze.c @@ -0,0 +1,591 @@ +//win10系统会出现排版问题,把四种方块改成127以内的字符方可正常使用 +//写迷宫地图的时候忘记可以直接从文档中读取了。。看到推箱子才想起来。。 +/*todolist +1.read map; +2.print map&you; +3.move it:1)input;2)canyoumove?; +4.judge if you win; +5.score;*/ +#include +#include +#define W 219 +#define P 232 +#define K 224 +#define E 215 + +unsigned char screen[22][22]; +void map(int level); +void print_map(void); +void judge(int level); +void gotoxy(int x, int y); + +int main(void) +{ + char input; + + system("chcp 437"); + system("mode con cols=23 lines=23"); + system("cls"); + + printf("Super Maze\n"); + printf("Info:Use \"w\" \"a\" \"s\" \"d\" to move,and you should"); + printf("get the %c to open the door(%c)\n",K,E); + printf("please choose a map:\n"); + printf("1 2 q.quit\n"); + while((input=getch())!='q') + { + switch(input) + { + case '1': + map(0); + judge(0); + break; + case '2': + map(1); + judge(1); + break; + case 'q': + return 0; + default: + ; + } + + map(-1); + system("cls"); + printf("Super Maze\n"); + printf("Info:Use \"w\" \"a\" \"s\" \"d\" to move,and you should"); + printf("get the %c to open the door(%c)\n",K,E); + printf("please choose a map:\n"); + printf("1 2 q.quit\n"); + + } + + + return 0; +} +void map(int level) +{ + int i,j; + + for(i=0;i<22;i++) + { + screen[i][0]=W; + screen[i][21]=W; + } + for(j=1;j<21;j++) + { + screen[0][j]=W; + screen[21][j]=W; + } + + if(level==0) + { + + for(i=8;i<11;i++) + { + screen[i][1]=W; + } + screen[4][1]=W; + screen[14][1]=W; + screen[16][1]=W; + + for(i=2;i<=14;i=i+2) + { + if(i!=8) + { + screen[i][2]=W; + } + } + for(i=16;i<20;i++) + { + screen[i][2]=W; + } + + for(i=1;i<15;i++) + { + if(i!=3&&i!=5&&i!=9&&i!=11 + &&i!=13) + { + screen[i][3]=W; + } + } + + screen[1][4]=W; + screen[4][4]=W; + screen[8][4]=W; + screen[12][4]=W; + screen[14][4]=W; + for(i=16;i<21;i++) + { + if(i!=18) + { + screen[i][4]=W; + } + } + + for(i=1;i<13;i++) + { + if(i!=2&&i!=7) + { + screen[i][5]=W; + } + } + screen[14][5]=W; + screen[16][5]=W; + screen[19][5]=W; + + screen[1][6]=W; + screen[3][6]=W; + screen[8][6]=W; + screen[16][6]=W; + screen[18][6]=W; + screen[19][6]=W; + + for(i=1;i<8;i=i+2) + { + screen[i][7]=W; + } + screen[8][7]=W; + for(i=10;i<19;i++) + { + if(i!=17) + { + screen[i][7]=W; + } + } + + for(i=3;i<8;i=i+2) + { + screen[i][8]=W; + } + screen[10][8]=W; + screen[16][8]=W; + screen[18][8]=W; + screen[19][8]=W; + + for(i=2;i<15;i++) + { + if(i!=4&&i!=6&&i!=8&&i!=11) + { + screen[i][9]=W; + } + } + + screen[5][10]=W; + screen[7][10]=W; + for(i=10;i<20;i++) + { + if(i!=13&&i!=15) + { + screen[i][10]=W; + } + } + + for(i=1;i<9;i++) + { + if(i!=4&&i!=6) + { + screen[i][11]=W; + } + } + screen[11][11]=W; + screen[14][11]=W; + screen[16][11]=W; + + screen[5][12]=W; + screen[8][12]=W; + screen[9][12]=W; + for(i=13;i<21;i++) + { + screen[i][12]=W; + } + + screen[1][13]=W; + for(i=3;i<12;i++) + { + screen[i][13]=W; + } + + screen[1][14]=W; + screen[3][14]=W; + screen[7][14]=W; + for(i=11;i<21;i++) + { + if(i!=16) + { + screen[i][14]=W; + } + } + + for(i=3;i<20;i=i+2) + { + if(i!=13) + { + screen[i][15]=W; + } + } + + for(i=3;i<20;i=i+2) + { + if(i!=15) + { + screen[i][16]=W; + } + } + screen[2][16]=W; + + for(i=5;i<20;i=i+2) + { + screen[i][17]=W; + } + for(i=14;i<18;i++) + { + screen[i][17]=W; + } + + screen[16][18]=W; + for(i=1;i<6;i++) + { + screen[i][18]=W; + } + for(i=7;i<12;i=i+2) + { + screen[i][18]=W; + } + screen[19][18]=W; + + screen[3][19]=W; + screen[11][19]=W; + for(i=7;i<10;i++) + { + screen[i][19]=W; + } + for(i=13;i<20;i++) + { + if(i!=17) + { + screen[i][19]=W; + } + } + + screen[1][20]=W; + screen[11][20]=W; + screen[5][20]=W; + + screen[0][2]=P; + screen[21][15]=E; + screen[8][18]=K; + } + else if(level==1) + { + for(i=5;i<8;i++) + { + screen[i][1]=W; + } + screen[11][1]=W; + screen[15][1]=W; + + for(i=1;i<14;i=i+2) + { + if(i!=5) + { + screen[i][2]=W; + } + } + for(i=15;i<19;i++) + { + screen[i][2]=W; + } + + for(i=3;i<6;i++) + { + screen[i][3]=W; + } + for(i=9;i<14;i=i+2) + { + screen[i][3]=W; + } + screen[18][3]=W; + screen[20][3]=W; + + screen[2][4]=W; + screen[3][4]=W; + for(i=5;i<10;i++) + { + screen[i][4]=W; + } + for(i=13;i<17;i++) + { + screen[i][4]=W; + } + screen[18][4]=W; + + screen[5][5]=W; + screen[12][5]=W; + screen[13][5]=W; + screen[16][5]=W; + screen[18][5]=W; + screen[19][5]=W; + + for(i=1;i<13;i++) + { + if(i!=4&&i!=6) + { + screen[i][6]=W; + } + } + screen[14][6]=W; + screen[16][6]=W; + + screen[5][7]=W; + screen[14][7]=W; + for(i=16;i<20;i++) + { + screen[i][7]=W; + } + + screen[1][8]=W; + screen[16][8]=W; + screen[19][8]=W; + for(i=3;i<15;i++) + { + if(i!=11) + { + screen[i][8]=W; + } + } + + screen[6][9]=W; + screen[10][9]=W; + screen[16][9]=W; + screen[17][9]=W; + + screen[1][10]=W; + screen[3][10]=W; + for(i=4;i<9;i=i+2) + { + screen[i][10]=W; + } + for(i=10;i<15;i++) + { + screen[i][10]=W; + } + screen[16][10]=W; + screen[19][10]=W; + + for(i=4;i<19;i=i+2) + { + if(i!=6&&i!=12) + { + screen[i][11]=W; + } + } + screen[19][11]=W; + + for(i=1;i<9;i++) + { + if(i!=3) + { + screen[i][12]=W; + } + } + for(i=10;i<17;i=i+2) + { + screen[i][12]=W; + } + screen[19][12]=W; + + screen[4][13]=W; + screen[10][13]=W; + screen[12][13]=W; + screen[16][13]=W; + screen[17][13]=W; + screen[19][13]=W; + + for(i=2;i<18;i++) + { + if(i!=5) + { + screen[i][14]=W; + } + } + + screen[2][15]=W; + screen[4][15]=W; + screen[14][15]=W; + screen[17][15]=W; + screen[19][15]=W; + screen[20][15]=W; + + for(i=1;i<13;i++) + { + if(i!=3) + { + screen[i][16]=W; + } + } + screen[16][16]=W; + screen[17][16]=W; + + screen[2][17]=W; + screen[6][17]=W; + screen[19][17]=W; + for(i=12;i<17;i++) + { + screen[i][17]=W; + } + + for(i=2;i<11;i=i+2) + { + screen[i][18]=W; + } + screen[9][18]=W; + screen[18][18]=W; + + for(i=2;i<9;i=i+2) + { + screen[i][19]=W; + } + screen[3][19]=W; + screen[20][19]=W; + for(i=10;i<19;i++) + { + screen[i][19]=W; + } + + screen[8][20]=W; + + screen[0][15]=P; + screen[0][17]=E; + screen[16][15]=K; + } + else if(level==-1) + { + for(i=0;i<22;i++) + { + for(j=0;j<22;j++) + { + screen[i][j]=' '; + } + } + } +} +void print_map(void) +{ + int i,j; + + system("cls"); + for(i=0;i<22;i++) + { + for(j=0;j<22;j++) + { + printf("%c",screen[i][j]); + } + printf("\n"); + } +} +void judge(int level) +{ + char move; + int i,j,k,l,m,n; + + if(level==0) + { + i=0; + j=2; + k=21; + l=15; + m=8; + n=18; + } + else if(level==1) + { + i=0; + j=15; + k=0; + l=17; + m=16; + n=15; + } + print_map(); + while(screen[k][l]!=P) + { + gotoxy(j,i); + move=getch(); + switch(move) + { + case 'w': + if(i!=0&&screen[i-1][j]!=W&&screen[i-1][j]!=E) + { + screen[i][j]=' '; + gotoxy(j,i); + printf("%c",screen[i][j]); + i--; + screen[i][j]=P; + gotoxy(j,i); + printf("%c",screen[i][j]); + } + break; + case 's': + if(i!=21&&screen[i+1][j]!=W&&screen[i+1][j]!=E) + { + screen[i][j]=' '; + gotoxy(j,i); + printf("%c",screen[i][j]); + i++; + screen[i][j]=P; + gotoxy(j,i); + printf("%c",screen[i][j]); + } + break; + case 'a': + if(j!=0&&screen[i][j-1]!=W&&screen[i][j-1]!=E) + { + screen[i][j]=' '; + gotoxy(j,i); + printf("%c",screen[i][j]); + j--; + screen[i][j]=P; + gotoxy(j,i); + printf("%c",screen[i][j]); + } + break; + case 'd': + if(j!=21&&screen[i][j+1]!=W&&screen[i][j+1]!=E) + { + + screen[i][j]=' '; + gotoxy(j,i); + printf("%c",screen[i][j]); + j++; + screen[i][j]=P; + gotoxy(j,i); + printf("%c",screen[i][j]); + } + break; + case 27: + return ; + default: + ; + } + if(screen[m][n]!=K&&screen[k][l]!=P) + { + screen[k][l]=' '; + gotoxy(l,k); + printf("%c",screen[k][l]); + } + + } + system("cls"); + printf("YOU WIN!\n"); + system("pause"); +} +void gotoxy(int x, int y)//该部分是借用某大神的 +{ + HANDLE hOutput; + COORD coo; + coo.X = x; + coo.Y = y; + hOutput = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleCursorPosition(hOutput, coo); +} diff --git a/practices/c/level1/p10_pushBoxes/map1.txt b/practices/c/level1/p10_pushBoxes/map1.txt new file mode 100644 index 00000000..a4fb6b16 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/map1.txt @@ -0,0 +1,20 @@ +0 0 0 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 5 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 5 5 5 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 2 3 0 3 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 +5 5 5 1 3 0 2 5 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 5 3 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 5 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/map2.txt b/practices/c/level1/p10_pushBoxes/map2.txt new file mode 100644 index 00000000..e34a393a --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/map2.txt @@ -0,0 +1,20 @@ +5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 1 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 0 3 3 5 0 5 5 5 0 0 0 0 0 0 0 0 0 0 0 +5 0 3 0 5 0 5 2 5 0 0 0 0 0 0 0 0 0 0 0 +5 5 5 0 5 5 5 2 5 0 0 0 0 0 0 0 0 0 0 0 +0 5 5 0 0 0 0 2 5 0 0 0 0 0 0 0 0 0 0 0 +0 5 0 0 0 5 0 0 5 0 0 0 0 0 0 0 0 0 0 0 +0 5 0 0 0 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 +0 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 diff --git a/practices/c/level1/p10_pushBoxes/map3.txt b/practices/c/level1/p10_pushBoxes/map3.txt new file mode 100644 index 00000000..1e797840 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/map3.txt @@ -0,0 +1,20 @@ +0 5 5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 +0 5 0 0 0 0 0 5 5 5 0 0 0 0 0 0 0 0 0 0 +5 5 3 5 5 5 0 0 0 5 0 0 0 0 0 0 0 0 0 0 +5 0 1 0 3 0 0 3 0 5 0 0 0 0 0 0 0 0 0 0 +5 0 2 2 5 0 3 0 5 5 0 0 0 0 0 0 0 0 0 0 +5 5 2 2 5 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 +0 5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 diff --git a/practices/c/level1/p10_pushBoxes/map4.txt b/practices/c/level1/p10_pushBoxes/map4.txt new file mode 100644 index 00000000..91976f39 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/map4.txt @@ -0,0 +1,20 @@ +0 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 5 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 1 3 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 5 3 0 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 5 0 3 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 2 3 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 2 2 4 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 diff --git a/practices/c/level1/p10_pushBoxes/map5.txt b/practices/c/level1/p10_pushBoxes/map5.txt new file mode 100644 index 00000000..6cbe4671 --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/map5.txt @@ -0,0 +1,20 @@ +0 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 5 1 0 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 5 0 3 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 +5 5 5 0 5 0 5 5 0 0 0 0 0 0 0 0 0 0 0 0 +5 2 5 0 5 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 +5 2 3 0 0 5 0 5 0 0 0 0 0 0 0 0 0 0 0 0 +5 2 0 0 0 3 0 5 0 0 0 0 0 0 0 0 0 0 0 0 +5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/map6.txt b/practices/c/level1/p10_pushBoxes/map6.txt new file mode 100644 index 00000000..3582b93a --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/map6.txt @@ -0,0 +1,20 @@ +0 0 0 5 5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 +0 0 5 5 0 0 5 0 1 5 0 0 0 0 0 0 0 0 0 0 +0 0 5 0 0 0 5 0 0 5 0 0 0 0 0 0 0 0 0 0 +0 0 5 3 0 3 0 3 0 5 0 0 0 0 0 0 0 0 0 0 +0 0 5 0 3 5 5 0 0 5 0 0 0 0 0 0 0 0 0 0 +5 5 5 0 3 0 5 0 5 5 0 0 0 0 0 0 0 0 0 0 +5 2 2 2 2 2 0 0 5 0 0 0 0 0 0 0 0 0 0 0 +5 5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/map7.txt b/practices/c/level1/p10_pushBoxes/map7.txt new file mode 100644 index 00000000..10fb7eab --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/map7.txt @@ -0,0 +1,20 @@ +0 0 0 5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 +0 5 5 5 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 +5 5 2 0 3 5 5 0 5 5 0 0 0 0 0 0 0 0 0 0 +5 2 2 3 0 3 0 0 1 5 0 0 0 0 0 0 0 0 0 0 +5 2 2 0 3 0 3 0 5 5 0 0 0 0 0 0 0 0 0 0 +5 5 5 5 5 5 0 0 5 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/practices/c/level1/p10_pushBoxes/pushbox.c b/practices/c/level1/p10_pushBoxes/pushbox.c new file mode 100644 index 00000000..4595b10f --- /dev/null +++ b/practices/c/level1/p10_pushBoxes/pushbox.c @@ -0,0 +1,512 @@ +#include +#include +#include +#define P 148 //player-1 +#define G 176 //goal-2 +#define B 177 //box-3 +#define BG 178 //box on goal-4 +#define W 219 //wall-5 +#define PG 232 //player on goal-6 +/*todolist +1.read map; +2.print map&you; +3.move it:1)input;2)canyoumove?; +4.judge if you win; +5.score;*/ +void gotoxy(int x,int y); +void print_map(void); +void print_block(int n); +void move(int level); +bool judge(int level); +int wasd(int * x,int * y,char way); +void load_map(int n); + +int block[20][20];//0:空位 1:人物 2:目标位置 3:箱子 4:放在目标位置的箱子 5:墙 6:站在目标位置的人物 +int score=0; + +int main(void) +{ + char jud; + + system("chcp 437"); + system("mode con cols=32 lines=23"); + system("cls"); + + printf("Push Box\n"); + printf("Please choose a map\n"); + printf("1 2 3 4 5 6 7 q.quit\n"); + while((jud=getch())!='q') + { + system("cls"); + score=0; + switch(jud) + { + case '1': + load_map(0); + print_map(); + move(0); + break; + case '2': + load_map(1); + print_map(); + move(1); + break; + case '3': + load_map(2); + print_map(); + move(2); + break; + case '4': + load_map(3); + print_map(); + move(3); + break; + case '5': + load_map(4); + print_map(); + move(4); + break; + case '6': + load_map(5); + print_map(); + move(5); + break; + case '7': + load_map(6); + print_map(); + move(6); + break; + case 'q': + return 0; + default: + ; + } + system("cls"); + printf("Push Box\n"); + printf("Please choose a map\n"); + printf("1 2 3 4 5 6 7 q.quit\n"); + } + + return 0; +} +void move(int level) +{ + int i,j; + char jud; + + if(level==0) + { + i=4; + j=3; + } + else if(level==1) + { + i=1; + j=1; + } + else if(level==2) + { + i=3; + j=2; + } + else if(level==3) + { + i=2; + j=1; + } + else if(level==4) + { + i=1; + j=2; + } + else if(level==5) + { + i=1; + j=8; + } + else if(level==6) + { + i=3; + j=8; + } + while(judge(level)) + { + jud=getch(); + if(!wasd(&i,&j,jud)) + { + return; + } + gotoxy(21,0); + printf("score:%4d",score); + } + system("cls"); + printf("You win!\n"); + printf("Your step is %4d.\n",score); + system("pause"); + +} +int wasd(int * x,int * y,char way) +{ + int di,dj; + + switch(way) + { + case 'w': + di=-1; + dj=0; + break; + case 's': + di=1; + dj=0; + break; + case 'a': + di=0; + dj=-1; + break; + case 'd': + di=0; + dj=1; + break; + case 27: + return 0; + default: + return; + } + if(block[*x+di][*y+dj]==0) + { + if(block[*x][*y]==6) + { + block[*x][*y]=2; + block[*x+di][*y+dj]=1; + } + else if(block[*x][*y]==1) + { + block[*x][*y]=0; + block[*x+di][*y+dj]=1; + } + + gotoxy(*y,*x); + print_block(block[*x][*y]); + *x=*x+di; + *y=*y+dj; + gotoxy(*y,*x); + print_block(block[*x][*y]); + score++; + } + else if(block[*x+di][*y+dj]==2) + { + if(block[*x][*y]==6) + { + block[*x][*y]=2; + block[*x+di][*y+dj]=6; + } + else if(block[*x][*y]==1) + { + block[*x][*y]=0; + block[*x+di][*y+dj]=6; + } + + gotoxy(*y,*x); + print_block(block[*x][*y]); + *x=*x+di; + *y=*y+dj; + gotoxy(*y,*x); + print_block(block[*x][*y]); + score++; + } + else if(block[*x+di][*y+dj]==3) + { + if(block[*x][*y]==6) + { + if(block[*x+2*di][*y+2*dj]==0) + { + block[*x+2*di][*y+2*dj]=3; + block[*x+di][*y+dj]=1; + block[*x][*y]=2; + score++; + } + else if(block[*x+2*di][*y+2*dj]==2) + { + block[*x+2*di][*y+2*dj]=4; + block[*x+di][*y+dj]=1; + block[*x][*y]=2; + score++; + } + else + { + return; + } + } + else + { + if(block[*x+2*di][*y+2*dj]==0) + { + block[*x+2*di][*y+2*dj]=3; + block[*x+di][*y+dj]=1; + block[*x][*y]=0; + score++; + } + else if(block[*x+2*di][*y+2*dj]==2) + { + block[*x+2*di][*y+2*dj]=4; + block[*x+di][*y+dj]=1; + block[*x][*y]=0; + score++; + } + else + { + return; + } + } + gotoxy(*y,*x); + print_block(block[*x][*y]); + *x=*x+di; + *y=*y+dj; + gotoxy(*y,*x); + print_block(block[*x][*y]); + gotoxy(*y+dj,*x+di); + print_block(block[*x+di][*y+dj]); + } + else if(block[*x+di][*y+dj]==4) + { + if(block[*x][*y]==6) + { + if(block[*x+2*di][*y+2*dj]==0) + { + block[*x+2*di][*y+2*dj]=3; + block[*x+di][*y+dj]=6; + block[*x][*y]=2; + score++; + } + else if(block[*x+2*di][*y+2*dj]==2) + { + block[*x+2*di][*y+2*dj]=4; + block[*x+di][*y+dj]=6; + block[*x][*y]=2; + score++; + } + else + { + return; + } + } + else + { + if(block[*x+2*di][*y+2*dj]==0) + { + block[*x+2*di][*y+2*dj]=3; + block[*x+di][*y+dj]=6; + block[*x][*y]=0; + score++; + } + else if(block[*x+2*di][*y+2*dj]==2) + { + block[*x+2*di][*y+2*dj]=4; + block[*x+di][*y+dj]=6; + block[*x][*y]=0; + score++; + } + else + { + return; + } + } + gotoxy(*y,*x); + print_block(block[*x][*y]); + *x=*x+di; + *y=*y+dj; + gotoxy(*y,*x); + print_block(block[*x][*y]); + gotoxy(*y+dj,*x+di); + print_block(block[*x+di][*y+dj]); + } + + return 1; +} +void load_map(int n) +{ + FILE *fp; + int i,j; + + if(n==0) + { + fp=fopen("map1.txt","r"); + } + else if(n==1) + { + fp=fopen("map2.txt","r"); + } + else if(n==2) + { + fp=fopen("map3.txt","r"); + } + else if(n==3) + { + fp=fopen("map4.txt","r"); + } + else if(n==4) + { + fp=fopen("map5.txt","r"); + } + else if(n==5) + { + fp=fopen("map6.txt","r"); + } + else if(n==6) + { + fp=fopen("map7.txt","r"); + } + for(i=0;i<20;i++) + { + for(j=0;j<20;j++) + { + fscanf(fp,"%d",&block[i][j]); + } + } + fclose(fp); + +} +void print_block(int n) +{ + if(n==0) + { + putchar(' '); + } + else if(n==1) + { + putchar(P); + } + else if(n==2) + { + putchar(G); + } + else if(n==3) + { + putchar(B); + } + else if(n==4) + { + putchar(BG); + } + else if(n==5) + { + putchar(W); + } + else if(n==6) + { + putchar(PG); + } + +} +bool judge(int level) +{ + if(level==0) + { + if(block[3][1]==4&&block[1][4]==4&& + block[4][6]==4&&block[6][3]==4) + { + return 0; + } + else + { + return 1; + } + } + else if(level==1) + { + if(block[3][7]==4&&block[4][7]==4&& + block[5][7]==4) + { + return 0; + } + else + { + return 1; + } + } + else if(level==2) + { + if(block[4][2]==4&&block[4][3]==4&& + block[5][2]==4&&block[5][3]==4) + { + return 0; + } + else + { + return 1; + } + } + else if(level==3) + { + if(block[5][1]==4&&block[6][3]==4&& + block[6][1]==4&&block[6][2]==4&& + block[6][4]==4) + { + return 0; + } + else + { + return 1; + } + } + else if(level==4) + { + if(block[4][1]==4&&block[5][1]==4&& + block[6][1]==4) + { + return 0; + } + else + { + return 1; + } + } + else if(level==5) + { + if(block[6][1]==4&&block[6][2]==4&& + block[6][3]==4&&block[6][4]==4&& + block[6][5]==4) + { + return 0; + } + else + { + return 1; + } + } + else if(level==6) + { + if(block[2][2]==4&&block[3][2]==4&& + block[4][2]==4&&block[3][1]==4&& + block[4][1]==4) + { + return 0; + } + else + { + return 1; + } + } +} +void print_map(void) +{ + int i,j; + + for(i=0;i<20;i++) + { + for(j=0;j<20;j++) + { + print_block(block[i][j]); + } + printf("\n"); + } +} +void gotoxy(int x, int y) +{ + HANDLE hOutput; + COORD coo; + coo.X = x; + coo.Y = y; + hOutput = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleCursorPosition(hOutput, coo); +} diff --git a/practices/c/level1/p11_linkedList/link_list.c b/practices/c/level1/p11_linkedList/link_list.c new file mode 100644 index 00000000..2b1f0bb8 --- /dev/null +++ b/practices/c/level1/p11_linkedList/link_list.c @@ -0,0 +1,116 @@ +#include +#include +#include + +struct list +{ + int num; + int value; + struct list *next; +}; +void _display(struct list *head); +void is_five(struct list *head); +struct list *exchange(struct list *head,int n); +struct list *move(struct list *head,int n); +int main(void) +{ + struct list *_link,*_head,*q; + int m,i=0,j; + + _link=q=(struct list*)malloc(sizeof(struct list)); + + printf("Now you can start to enter the value.(q to quit)\n"); + _head=NULL; + while(1==scanf("%d",&m)) + { + if(0==i) + { + _head=_link; + } + else + { + _link->next=q; + _link=q; + } + _link->num=i+1; + _link->value=m; + + q=(struct list*)malloc(sizeof(struct list)); + + + i++; + } + _link->next=NULL; + free(q); + _display(_head); + _head=exchange(_head,i); + is_five(_head); + + system("pause"); + + return 0; +} +void _display(struct list *head) +{ + struct list *cp; + + cp=head; + while(cp!=NULL) + { + printf("No.%d is %d\n",cp->num,cp->value); + cp=cp->next; + } + + return; +} +struct list *move(struct list *head,int n) +{ + struct list *cp; + int i; + + cp=head; + for(i=0;inext; + } + + return cp; +} +struct list *exchange(struct list *head,int n) +{ + struct list *ch1,*ch2; + int i; + + ch1=head; + ch2=move(ch1,n); + head=ch2; + for(i=n;i>1;i--) + { + ch2->next=move(ch1,i-1); + ch2=ch2->next; + } + ch2->next=NULL; + + return head; + +} +void is_five(struct list *head) +{ + struct list *ch; + bool flag=0; + ch=head; + while(ch!=NULL) + { + if(5==ch->value) + { + printf("%d\n",ch->num); + flag=1; + } + ch=ch->next; + } + + if(!flag) + { + printf("-1\n"); + } +} diff --git a/practices/c/level1/p12_warehouse/warehouse.c b/practices/c/level1/p12_warehouse/warehouse.c new file mode 100644 index 00000000..122923c9 --- /dev/null +++ b/practices/c/level1/p12_warehouse/warehouse.c @@ -0,0 +1,177 @@ +#include +#include +#include +/*todolist +1.read datas; +2.界面; +3.input&处理; +4.处理数据; +5.write to file;*/ +typedef struct goods +{ + char name[16],size[4]; + long long int amount; + +}GOODS; + +void menu(GOODS * Goods,int * n); +void write(GOODS * Goods,int n); +void display(GOODS * Goods,int n); +void input(GOODS** Goods,int * n); +void output(GOODS** Goods,int * n); +long long int is_num(void); + +int main(void){ + FILE *fp; + int max,i; + + fp=fopen("warehouse.txt","r"); + fscanf(fp,"%d",&max); + + GOODS list[100]; + for(i=0;i=0)return n; + else return 0; + +} diff --git a/practices/c/level1/p12_warehouse/warehouse.txt b/practices/c/level1/p12_warehouse/warehouse.txt new file mode 100644 index 00000000..1431c615 --- /dev/null +++ b/practices/c/level1/p12_warehouse/warehouse.txt @@ -0,0 +1,7 @@ +3 +God_WXZ +1 +xxl +list2 +1 +s diff --git a/practices/cpp/level1/p01_Queue/Queue.cpp b/practices/cpp/level1/p01_Queue/Queue.cpp new file mode 100644 index 00000000..822bc2e4 --- /dev/null +++ b/practices/cpp/level1/p01_Queue/Queue.cpp @@ -0,0 +1,63 @@ +#include "Queue.h" +#include +using std::cout; +using std::endl; +void Queue::disp(){ + if(head +#include +#include +#include"Queue.h" +/*todolist +1.append; +2.pop; +3.isFull?; +4.isNull?; */ +int main(){ + using std::cout; + using std::cin; + char ch; + double n; + Queue queue; + + do{ + system("cls"); + cout<<"The queue:\n"; + if(!queue.isNull()){ + queue.disp(); + } + else{ + cout<<"Null\n"; + } + cout<<"What do you want to do?\n"; + cout<<"1.append 2.pop q.quit\n"; + ch=getch(); + switch(ch){ + case '1': + if(!queue.isFull()){ + cout<<"Please enter a number:\n"; + cin>>n; + queue.append(n); + } + else{ + cout<<"The queue is full\n"; + system("pause"); + } + break; + case '2': + if(!queue.isNull()){ + queue.pop(); + } + else{ + cout<<"The queue is null\n"; + system("pause"); + } + break; + case 'q': + return 0; + default: + break; + } + }while(1); + return 0; +} diff --git a/practices/cpp/level1/p02_Stack/Stack.h b/practices/cpp/level1/p02_Stack/Stack.h new file mode 100644 index 00000000..514ec148 --- /dev/null +++ b/practices/cpp/level1/p02_Stack/Stack.h @@ -0,0 +1,71 @@ +#ifndef STACK_H +#define STACK_H +using std::cout; +using std::endl; +template +class Stack +{ + private: + T *data; + int top; + bool flag; + int len; + public: + Stack(int item=100); + bool isFull(); + bool isNull(); + void disp(); + void push(T item); + void pop(); + protected: +}; +template +Stack::Stack(int item){ + len=item; + top=0; + flag=0; + T *p=new T[item]; + data=p; +} +template +void Stack::disp(){ + cout<<"top\n"; + if(flag){ + cout<=0;i--){ + cout< +void Stack::push(T item){ + data[top]=item; + if(top +void Stack::pop(){ + if(len-1==top&&flag){ + flag=0; + data[top]=0; + return; + } + data[top]=0; + --top; +} +template +bool Stack::isFull(){ + if(flag)return true; + return false; +} +template +bool Stack::isNull(){ + if(0==top)return true; + return false; +} +#endif diff --git a/practices/cpp/level1/p02_Stack/main.cpp b/practices/cpp/level1/p02_Stack/main.cpp new file mode 100644 index 00000000..dec2e979 --- /dev/null +++ b/practices/cpp/level1/p02_Stack/main.cpp @@ -0,0 +1,58 @@ +#include +#include +#include"Stack.h" +/*todolist +1.append; +2.pop; +3.isFull?; +4.isNull?; */ +int main(){ + using std::cout; + using std::cin; + char ch; + int n; + cout<<"Please enter the size of you stack\n"; + cin>>n; + Stack stack(n); + + do{ + system("cls"); + cout<<"The stack:\n"; + if(!stack.isNull()){ + stack.disp(); + } + else{ + std::cout<<"Null\n"; + } + cout<<"What do you want to do?\n"; + cout<<"1.push 2.pop q.quit\n"; + ch=getch(); + switch(ch){ + case '1': + if(!stack.isFull()){ + cout<<"Please enter a number:\n"; + cin>>n; + stack.push(n); + } + else{ + cout<<"The stack is full\n"; + system("pause"); + } + break; + case '2': + if(!stack.isNull()){ + stack.pop(); + } + else{ + cout<<"The stack is null\n"; + system("pause"); + } + break; + case 'q': + return 0; + default: + break; + } + }while(1); + return 0; +} diff --git a/practices/cpp/level1/p03_SafeArray/Array.h b/practices/cpp/level1/p03_SafeArray/Array.h new file mode 100644 index 00000000..e9dbda71 --- /dev/null +++ b/practices/cpp/level1/p03_SafeArray/Array.h @@ -0,0 +1,45 @@ +#ifndef ARRAY_H +#define ARRAY_H +using std::cout; +template +class Array +{ + private: + int max; + T * data; + T def; + public: + Array(int item=100); + T & operator[](int n); + void disp(); + protected: +}; +template +Array::Array(int item){ + T *p=new T[item]; + + max=item; + def=0; + data=p; + for(int i=0;i +T & Array::operator[](int n){ + if(n>=max||n<=0){ + cout<<"Error\n"; + def=0; + return def; + } + + return data[n]; +} +template +void Array::disp(){ + for(int i=0;i +#include +#include"Array.h" +using namespace std; + +int main(){ + int n,i; + char ch; + + cout<<"Please enter the size of your array:\n"; + cin>>n; + Array array(n); + do{ + system("cls"); + cout<<"What do you want:\n"; + cout<<"1.change 2.display 3.quit\n"; + ch=getch(); + switch(ch){ + case '1': + cout<<"Which do you want to change:\n"; + cin>>i; + cout<<"Change to:\n"; + cin>>array[i]; + break; + case '2': + array.disp(); + system("pause"); + break; + case '3': + return 0; + default: + ; + } + }while(1); + + return 0; +} diff --git a/practices/cpp/level1/p04_cppScoreManagement/Score.cpp b/practices/cpp/level1/p04_cppScoreManagement/Score.cpp new file mode 100644 index 00000000..0cb42c66 --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/Score.cpp @@ -0,0 +1,66 @@ +#include "Score.h" +#include +#include +using std::string; +using std::cout; +using std::endl; +using std::cin; +Score::Score(string subject){ + Subject=subject; + for(int i=0;i<30;i++){ + student[i].name=""; + student[i].number=""; + student[i].score=0; + } +} +void Score::disp(){ + system("cls"); + cout<<"Subject:"<>input; + for(int i=0;i<30;i++){ + if(student[i].name==input){ + student[i].name=""; + student[i].number=""; + student[i].score=0; + break; + } + } +} +void Score::write(){ + + for(int i=0;i<30;i++){ + if(student[i].name!=""){ + cout<<"Enter the score of "<>student[i].score; + } + } +} diff --git a/practices/cpp/level1/p04_cppScoreManagement/Score.h b/practices/cpp/level1/p04_cppScoreManagement/Score.h new file mode 100644 index 00000000..ca52aef9 --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/Score.h @@ -0,0 +1,24 @@ +#ifndef SCORE_H +#define SCORE_H +#include +using std::string; +struct manage{ + string name; + string number; + int score; +}; +class Score +{ + private: + string Subject; + manage student[30]; + public: + Score(string subject); + void disp(); + void add(); + void del(); + void write(); + protected: +}; + +#endif diff --git a/practices/cpp/level1/p04_cppScoreManagement/main.cpp b/practices/cpp/level1/p04_cppScoreManagement/main.cpp new file mode 100644 index 00000000..0ce9f887 --- /dev/null +++ b/practices/cpp/level1/p04_cppScoreManagement/main.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include"Score.h" +using namespace std; +int main(){ + const string s1="c",s2="c++",s3="java"; + Score ccpp[3]={Score(s1),Score(s2),Score(s3),}; + char ch; + + do{ + system("cls"); + cout<<"What do you want to do?\n"; + cout<<"Watch the list(1.c 2.c++ 3.java)\n"; + cout<<"Add students(4.c 5.c++ 6.java)\n"; + cout<<"Delete students(7.c 8.c++ 9.java)\n"; + cout<<"Write the score(c.c p.c++ j.java)\n"; + cout<<"Quit(0)\n"; + ch=getch(); + switch(ch){ + case '1': + ccpp[0].disp(); + system("pause"); + break; + case '2': + ccpp[1].disp(); + system("pause"); + break; + case '3': + ccpp[2].disp(); + system("pause"); + break; + case '4': + ccpp[0].add(); + break; + case '5': + ccpp[1].add(); + break; + case '6': + ccpp[2].add(); + break; + case '7': + ccpp[0].del(); + break; + case '8': + ccpp[1].del(); + break; + case '9': + ccpp[2].del(); + break; + case 'c': + ccpp[0].write(); + break; + case 'p': + ccpp[1].write(); + break; + case 'j': + ccpp[2].write(); + break; + case '0': + return 0; + default: + ; + } + + }while(1); + + return 0; +} diff --git a/practices/cpp/level1/p05_Canvas/Canvas.cpp b/practices/cpp/level1/p05_Canvas/Canvas.cpp new file mode 100644 index 00000000..61cb1ac5 --- /dev/null +++ b/practices/cpp/level1/p05_Canvas/Canvas.cpp @@ -0,0 +1,20 @@ +#include "Canvas.h" +#include + +Canvas::Canvas(){ + i=0; +} +void Canvas::add(Shape & item){ + if(i<10){ + data[i]=&item; + i++; + } + else{ + std::cout<<"Error,this Canvas is full!\n"; + } +} +void Canvas::show(){ + for(int j=0;jshow(); + } +} diff --git a/practices/cpp/level1/p05_Canvas/Canvas.h b/practices/cpp/level1/p05_Canvas/Canvas.h new file mode 100644 index 00000000..1d46de34 --- /dev/null +++ b/practices/cpp/level1/p05_Canvas/Canvas.h @@ -0,0 +1,15 @@ +#ifndef CANVAS_H; +#define CANVAS_H +#include"Shape.h" +class Canvas +{ + int i; + Shape *data[10]; + public: + Canvas(); + void add(Shape & item); + void show(); + protected: +}; + +#endif diff --git a/practices/cpp/level1/p05_Canvas/Shape.cpp b/practices/cpp/level1/p05_Canvas/Shape.cpp new file mode 100644 index 00000000..27c85ca1 --- /dev/null +++ b/practices/cpp/level1/p05_Canvas/Shape.cpp @@ -0,0 +1,25 @@ +#include "Shape.h" +#include +using std::cout; +using std::endl; +Shape::Shape(int a,int b){ + x=a; + y=b; +} +void Shape::show(){ + ; +} +Circle::Circle(int a,int b,int c):Shape(a,b){ + r=c; +} +Rect::Rect(int a,int b,int c,int d):Shape(a,b){ + x_length=c; + y_length=d; +} +void Circle::show(){ + cout<<"Circle: Center: X = "< +using std::ostream; +Point::Point(int X,int Y){ + x=X; + y=Y; +} +void Point::move(int X,int Y){ + x+=X; + y+=Y; +} +void Point::move(Point p){ + x+=p.x; + y+=p.y; +} +ostream & operator<<(ostream & os,const Point & p){ + os<<"X="< +using std::cout; +Circuit::Circuit(){ + condition=0; + index=0; +} +void Circuit::add(Device * item){ + if(index<10){ + device[index]=item; + index++; + } + +} +void Circuit::on(){ + condition=1; + for(int i=0;ion(); + } + +} +void Circuit::off(){ + condition=0; + for(int i=0;ioff(); + } +} +void Circuit::disp(){ + if(condition){ + cout<<"The circuit is on\n"; + } + else{ + cout<<"The circuit is off\n"; + } + for(int i=0;ishow(); + } +} diff --git a/practices/cpp/level1/p07_Circuit/Circuit.h b/practices/cpp/level1/p07_Circuit/Circuit.h new file mode 100644 index 00000000..1d5692c2 --- /dev/null +++ b/practices/cpp/level1/p07_Circuit/Circuit.h @@ -0,0 +1,18 @@ +#ifndef CIRCUIT_H +#define CIRCUIT_H +#include "Device.h" +class Circuit +{ + Device *device[10]; + bool condition; + int index; + public: + Circuit(); + void add(Device * item); + void on(); + void off(); + void disp(); + protected: +}; + +#endif diff --git a/practices/cpp/level1/p07_Circuit/Device.cpp b/practices/cpp/level1/p07_Circuit/Device.cpp new file mode 100644 index 00000000..2457faee --- /dev/null +++ b/practices/cpp/level1/p07_Circuit/Device.cpp @@ -0,0 +1,39 @@ +#include "Device.h" +#include +using std::cout; +using std::endl; +Device::Device(){ + condition=0; +} +Fan::Fan(int speed):Device(){ + name="Fan"; + this->speed=speed; +} +Light::Light(int illumination):Device(){ + name="Light"; + this->illumination=illumination; +} +void Fan::show(){ + cout< +class Device +{ + public: + Device(); + virtual void on(){;}; + virtual void off(){;}; + virtual void show(){;}; + protected: + std::string name; + bool condition; +}; +class Fan:public Device +{ + int speed; + public: + Fan(int speed=10); + virtual void on(){condition=1;}; + virtual void off(){condition=0;}; + virtual void show(); + +}; +class Light:public Device +{ + int illumination; + public: + Light(int illumination=1); + virtual void on(){condition=1;}; + virtual void off(){condition=0;}; + virtual void show(); +}; + +#endif diff --git a/practices/cpp/level1/p07_Circuit/main.cpp b/practices/cpp/level1/p07_Circuit/main.cpp new file mode 100644 index 00000000..90d6559f --- /dev/null +++ b/practices/cpp/level1/p07_Circuit/main.cpp @@ -0,0 +1,16 @@ +#include +#include "Circuit.h" +#include "Device.h" +int main(){ + Circuit circuit1; + Fan fan1; + Light light1; + circuit1.add(&fan1); + circuit1.add(&light1); + circuit1.on(); + circuit1.disp(); + circuit1.off(); + circuit1.disp(); + + return 0; +} diff --git a/practices/cpp/level1/p08_EmployeeAndSales/Employee.cpp b/practices/cpp/level1/p08_EmployeeAndSales/Employee.cpp new file mode 100644 index 00000000..a716570d --- /dev/null +++ b/practices/cpp/level1/p08_EmployeeAndSales/Employee.cpp @@ -0,0 +1,41 @@ +#include "Employee.h" +#include +using std::cout; +using std::endl; +Employee::Employee(string name,int age,int rank){ + this->name=name; + this->age=age; + this->rank=rank; + salary=this->rank*1000; +} +void Employee::change(string name,int age,int rank){ + this->name=name; + this->age=age; + this->rank=rank; +} +void Employee::show(){ + cout<<"Name:"<sales=sales; + salary=salary+0.2*sales; +} +void Sale::change(string name,int age,int rank){ + this->name=name; + this->age=age; + this->rank=rank; +} +void Sale::setSales(int sales){ + this->sales=sales; + salary=rank*1000+this->sales*0.2; +} +void Sale::show(){ + cout<<"Name:"< +using std::string; +class Employee +{ + public: + Employee(string name="NULL",int age=0,int rank=1); + virtual void change(string name,int age,int rank); + virtual void show(); + protected: + string name; + int age; + int rank; + int salary; +}; +class Sale:public Employee +{ + int sales; + public: + Sale(string name="NULL",int age=0,int rank=1,int sales=0); + virtual void change(string name,int age,int rank); + void setSales(int sales); + virtual void show(); + +}; + +#endif diff --git a/practices/cpp/level1/p08_EmployeeAndSales/main.cpp b/practices/cpp/level1/p08_EmployeeAndSales/main.cpp new file mode 100644 index 00000000..48cbada1 --- /dev/null +++ b/practices/cpp/level1/p08_EmployeeAndSales/main.cpp @@ -0,0 +1,23 @@ +/*to do list +1.Class Employee + name,age,rank + salary + show +2.Class Sale(is-a-Employee) + set sales + salary + show +*/ +#include +#include "Employee.h" +int main(){ + Employee person1("WXZ",19,1); + Sale person2("God Du",18,10); + person2.setSales(10000); + person2.change("Du",18,20); + + person1.show(); + person2.show(); + + return 0; +} diff --git a/practices/cpp/level1/p11_Fighters/TodoList.md b/practices/cpp/level1/p11_Fighters/TodoList.md index 497725f5..33f5fd07 100755 --- a/practices/cpp/level1/p11_Fighters/TodoList.md +++ b/practices/cpp/level1/p11_Fighters/TodoList.md @@ -1,25 +1,25 @@ - | 浠诲姟锛堝姛鑳斤級 | Value | Effort | 鏄惁宸插畬鎴 ------|-------------------------------|-----------|-----------|------------| -1 | 瀹屾垚SFML閰嶇疆锛屾樉绀衡淪FML works鈥 | 0 | | | -2 | 鏄剧ず涓鏋堕潤姝㈢殑椋炴満浜庡睆骞曞簳閮 | 5 | | | -3 | 鑳屾櫙闊充箰 | 1 | | | -4 | 宸﹀彸閿紝鎺у埗绉诲姩椋炴満 | 10 | | | -5 | 闄愬埗宸﹀彸杈圭晫 | 1 | | | -6 | 绌烘牸閿紑鐐紝鏄剧ず杩愬姩鐨勭偖寮 | 5 | | | -7 | 鐐脊椋炲嚭杈圭晫澶勭悊 | 2 | | | -8 | 闅忔満浜х敓鏁屾満锛屽苟鍚戜笅杩愬姩 | 10 | | | -9 | 鏁屾満椋炲嚭杈圭晫澶勭悊 | 2 | | | -10 | 纰版挒澶勭悊锛堟晫鏈轰笌鐐脊纰版挒锛 | 10 | | | -11 | 鏄剧ず鏁屾満鐖嗙偢杩囩▼ | 10 | | | -12 | 鐖嗙偢澹伴煶 | 2 | | | -13 | 璁″垎鍙婃樉绀 | 5 | | | -14 | 鏁屾満鐐脊澶勭悊 | 10 | | | -15 | 琚晫鏈哄嚮涓鐞嗭紙鐐告瘉銆3鏉″懡锛 | 10 | | | -16 | 杩囧叧鎺у埗锛堣繃鍏抽渶瑕佽鍒嗐佹父鎴忛熷害鎺у埗锛墊 20 | | | -17 | | | | | -18 | | | | | -19 | | | | | -20 | | | | | -鍚堣 | | | | | +| | 浠诲姟锛堝姛鑳斤級 | Value | Effort | 鏄惁宸插畬鎴 | +|-----|---------------------------------|-----------|-----------|------------| +|1 | 瀹屾垚SFML閰嶇疆锛屾樉绀衡淪FML works鈥潀 0 |3 | down | +|2 | 鏄剧ず涓鏋堕潤姝㈢殑椋炴満浜庡睆骞曞簳閮 | 5 |4 | down | +|3 | 鑳屾櫙闊充箰 | 1 |2 | down | +|4 | 宸﹀彸閿紝鎺у埗绉诲姩椋炴満 | 10 |1 | down | +|5 | 闄愬埗宸﹀彸杈圭晫 | 1 |0.1 | down | +|6 | 绌烘牸閿紑鐐紝鏄剧ず杩愬姩鐨勭偖寮 | 5 |4 | down | +|7 | 鐐脊椋炲嚭杈圭晫澶勭悊 | 2 |2 | down | +|8 | 闅忔満浜х敓鏁屾満锛屽苟鍚戜笅杩愬姩 | 10 |2 | down | +|9 | 鏁屾満椋炲嚭杈圭晫澶勭悊 | 2 |0.1 | down | +|10 | 纰版挒澶勭悊锛堟晫鏈轰笌鐐脊锛岀帺瀹剁鎾烇級| 12 |3 | down | +|11 | 鏄剧ず鏁屾満鐖嗙偢杩囩▼ | 10 |3 | down | +|12 | 鐖嗙偢澹伴煶 | 2 |1 | down | +|13 | 璁″垎鍙婃樉绀 | 5 |2 | down | +|14 | 鏁屾満鐐脊澶勭悊 | 10 |4 | down | +|15 | 琚晫鏈哄嚮涓鐞嗭紙鐐告瘉銆3鏉″懡锛 | 8 |2 | down | +|16 | 杩囧叧鎺у埗锛堣繃鍏抽渶瑕佽鍒嗐佹父鎴忛熷害鎺у埗锛墊 20 |4 | | +|17 | | | | | +|18 | | | | | +|19 | | | | | +|20 | | | | | +|鍚堣 | | 103 | | | diff --git a/practices/cpp/level1/p11_Fighters/UFO.png b/practices/cpp/level1/p11_Fighters/UFO.png new file mode 100644 index 00000000..f63cebdd Binary files /dev/null and b/practices/cpp/level1/p11_Fighters/UFO.png differ diff --git a/practices/cpp/level1/p11_Fighters/bullet.cpp b/practices/cpp/level1/p11_Fighters/bullet.cpp new file mode 100644 index 00000000..230988b0 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/bullet.cpp @@ -0,0 +1,74 @@ +#include "stdafx.h" +#include +#include +#include "bullet.h" + +#define Xmax 800 +#define Ymax 600 +bullet::bullet(int max) { + info * tem = new info[max]; + bulletInfo = tem; + tail = 0; + this->max = max; + for (int i = 0; i Xmax || y<0 || y>Ymax) { + bulletInfo[index].ifExist = 0; + return false; + } + return true; +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/bullet.h b/practices/cpp/level1/p11_Fighters/bullet.h new file mode 100644 index 00000000..6c945d0b --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/bullet.h @@ -0,0 +1,28 @@ +#ifndef BULLET_H +#include "stdafx.h" +#include +#define BULLET_H + +struct info { + sf::Texture bullet_texture; + sf::Sprite bullet_sprite; + float x, y; + float speed; + bool ifExist; +}; +class bullet { + public: + info * bulletInfo; + bullet(int max=50); + void appendBullet(float x, float y, char type); + + void moveBullet(); + void showBullet(sf::RenderWindow &thisWindow); + bool isExist(int index); + protected: + + int max; + int tail; + +}; +#endif diff --git a/practices/cpp/level1/p11_Fighters/gameMusic.cpp b/practices/cpp/level1/p11_Fighters/gameMusic.cpp new file mode 100644 index 00000000..28f3727d --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/gameMusic.cpp @@ -0,0 +1,31 @@ +#include "stdafx.h" +#include"gameMusic.h" +gameMusic::gameMusic(int type) { + this->type = type; + if (type == 1) { + gameBuffer.loadFromFile("collision.wav"); + gameSound.setBuffer(gameBuffer); + + } + else if (type == 2) { + bigMusic.openFromFile("bgm1.flac"); + } + +} +void gameMusic::playMusic() { + if (type==2) { + bigMusic.setLoop(true); + } + switch (type) { + case 2: + bigMusic.play(); + break; + case 1: + gameSound.play(); + break; + default: + ; + } + + +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/gameMusic.h b/practices/cpp/level1/p11_Fighters/gameMusic.h new file mode 100644 index 00000000..7845f6a7 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/gameMusic.h @@ -0,0 +1,13 @@ +#include +#ifndef GAMEMUSIC_H +#define GAMEMUSIC_H +class gameMusic { + sf::SoundBuffer gameBuffer; + sf::Sound gameSound; + sf::Music bigMusic; + int type; + public: + gameMusic(int type=1); + void playMusic(); +}; +#endif diff --git a/practices/cpp/level1/p11_Fighters/gameText.cpp b/practices/cpp/level1/p11_Fighters/gameText.cpp new file mode 100644 index 00000000..e313ae55 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/gameText.cpp @@ -0,0 +1,74 @@ +#include"stdafx.h" +#include"gameText.h" +#include +gameText::gameText(char type, int data) { + switch (type) { + case 's': + font.loadFromFile("AdobeDevanagari-Regular.otf"); + text.setFont(font); + text.setCharacterSize(20); + text.setFillColor(sf::Color::White); + text.setPosition(700, 580); + text.setStyle(sf::Text::Bold); + text.setString("Score:" + std::to_string(data)); + this->data = data; + this->type = type; + break; + case 'l': + font.loadFromFile("AdobeDevanagari-Regular.otf"); + text.setFont(font); + text.setCharacterSize(20); + text.setFillColor(sf::Color::White); + text.setPosition(0, 580); + text.setStyle(sf::Text::Bold); + text.setString("Life:" + std::to_string(data)); + this->data = data; + this->type = type; + break; + case 't': + font.loadFromFile("AdobeDevanagari-Regular.otf"); + text.setFont(font); + text.setCharacterSize(20); + text.setFillColor(sf::Color::White); + text.setPosition(300, 580); + text.setStyle(sf::Text::Bold); + text.setString("Time:" + std::to_string(data)); + this->data = data; + this->type = type; + break; + default: + font.loadFromFile("AdobeDevanagari-Regular.otf"); + text.setFont(font); + text.setCharacterSize(20); + text.setFillColor(sf::Color::Blue); + text.setStyle(sf::Text::Bold); + this->data = 0; + this->type = 'h'; + break; + } +} +void gameText::setGameText(std::string s) { + text.setString(s); +} +void gameText::setData(int data) { + this->data = data; +} +void gameText::setScore(int data) { + this->setData(data); + this->setGameText("Score:" + std::to_string(data)); +} +void gameText::setPosition(int x, int y) { + text.setPosition(x, y); +} +void gameText::setScale(int size) { + text.setCharacterSize(size); +} +void gameText::showText(sf::RenderWindow &thisWindow) { + thisWindow.draw(this->text); +} + +void setInformation(gameText & information, std::string s, int x, int y, int size){ + information.setGameText(s); + information.setPosition(x, y); + information.setScale(size); +} diff --git a/practices/cpp/level1/p11_Fighters/gameText.h b/practices/cpp/level1/p11_Fighters/gameText.h new file mode 100644 index 00000000..84edec01 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/gameText.h @@ -0,0 +1,20 @@ +#ifndef GAMETEXT_H +#include +#define GAMETEXT_H +class gameText { + char type; + int data; + sf::Font font; + sf::Text text; + public: + gameText(char type='h',int data=0); + void setGameText(std::string s); + void setData(int data); + void setScore(int data); + void setPosition(int x, int y); + void setScale(int size); + void showText(sf::RenderWindow &thisWindow); + +}; +void setInformation(gameText & information, std::string s, int x, int y, int size); +#endif diff --git a/practices/cpp/level1/p11_Fighters/gameWindow.cpp b/practices/cpp/level1/p11_Fighters/gameWindow.cpp new file mode 100644 index 00000000..cae61893 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/gameWindow.cpp @@ -0,0 +1,204 @@ +#include "gameWindow.h" +#include "stdafx.h" +#include +#include "plane.h" +#include "gameMusic.h" +#include "gameText.h" +#include +#include + +void playerAction(plane & p) { + static float speedCtrl=1; + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl) || + sf::Keyboard::isKeyPressed(sf::Keyboard::K)) { + speedCtrl = 0.5; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)|| + sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { + p.movePlane(-speedCtrl, 0); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)|| + sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { + p.movePlane(speedCtrl, 0); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)|| + sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { + p.movePlane(0, -speedCtrl); + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)|| + sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { + p.movePlane(0, speedCtrl); + } + if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Space) || + sf::Keyboard::isKeyPressed(sf::Keyboard::J))) { + p.Fire('p'); + } + speedCtrl = 1; +} +void enemysAction(plane * p, bool & flag1,int max) { + static int num=0; + if (!flag1) { + appendEnemy(p, num); + num++; + flag1 = 1; + } + for (int i = 0; i < max; i++) { + if (!p[i].isExist())continue; + p[i].Fire('e'); + } + if (max == num) { + num = 0; + } +} + +bool gameloop(sf::RenderWindow &window, int max_enemy ,int &time_count,int &time) { + int fresh_count = 0, sound_count = 0, score = 0; + bool bulletCD = 0, enemyCD = 0; + int godfresh = 0; + bool enemyBulletCD = 0; + int flag_collision = 0; + plane *enemysPlane=new plane[max_enemy]; + + sf::Texture backgrand_texture; + sf::Sprite backgrand_sprite; + plane playersPlane(350, 500, 'p'); + + gameMusic gameBgm1(2); + gameMusic soundEffect; + gameText scoreText('s', score); + gameText lifeText('l', playersPlane.showLife()); + gameText timeText('t', time); + + backgrand_texture.loadFromFile("backgrand.psd"); + backgrand_sprite.setTexture(backgrand_texture); + backgrand_sprite.setTextureRect(sf::IntRect(0, 2048, 800, 600)); + window.setFramerateLimit(60); + gameBgm1.playMusic(); + window.draw(backgrand_sprite); + while (window.isOpen()) { + + sf::Event event; + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed || + sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { + window.close(); + return false; + } + } + enemysAction(enemysPlane, enemyCD, max_enemy); + playerAction(playersPlane); + backgrand_sprite.setTextureRect(sf::IntRect(0, 2048 - fresh_count * 2, 800, 600)); + window.clear(); + window.draw(backgrand_sprite); + playersPlane.moveBullet(); + moveEnemy(enemysPlane, playersPlane, max_enemy, flag_collision); + playersPlane.showPlane(window); + playersPlane.Count(); + playersPlane.freshBulletCD(); + if (flag_collision>0) { + soundEffect.playMusic(); + if (1 == flag_collision) { + score++; + } + } + if (2 == flag_collision) { + clearBullet(enemysPlane, max_enemy); + } + showEnemy(window, enemysPlane, max_enemy); + scoreText.setScore(score); + scoreText.showText(window); + timeText.setGameText("Time:" + std::to_string(time)); + timeText.showText(window); + lifeText.setGameText("Life:" + std::to_string(playersPlane.showLife() / 4)); + lifeText.showText(window); + window.display(); + ++fresh_count; + ++time_count; + if (0 == fresh_count % 60) { + enemyCD = 0; + } + if (1024 == fresh_count) { + fresh_count = 0; + } + if (time_count == 60) { + time++; + time_count = 0; + if (godfresh != 0) { + godfresh++; + if (godfresh == 3) { + playersPlane.godmode = 0; + godfresh = 0; + } + } + } + if (playersPlane.godmode&&godfresh == 0) { + godfresh++; + } + flag_collision = 0; + if (!playersPlane.isExist()) { + break; + } + if (score >= max_enemy*2) { + return true; + } + } + return false; +} +void gameProcess() { + sf::RenderWindow window(sf::VideoMode(800, 600), "Sky Legend"); + sf::Texture bg_texture; + sf::Sprite bg_sprite; + gameText help[5]; + setInformation(help[0], "SKY LEGEND", 250, 100, 50); + setInformation(help[1], "Use 'w','a','s','d'(or up,left,down,right)to move,use 'j' or space to fire.", 100, 200, 20); + setInformation(help[2], "press Space to start",275,300,20); + setInformation(help[3], "Next Level", 275, 300, 35); + setInformation(help[4], "YOU WIN!", 275, 300, 35); + bg_texture.loadFromFile("bg.jpg"); + bg_sprite.setTexture(bg_texture); + bg_sprite.setScale(sf::Vector2f(2.0f, 2.0f)); + int time_count = 0, time = 0; + int enemy_amount=20; + menu:while (window.isOpen()) { + sf::Event event; + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed || + sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { + window.close(); + } + } + window.clear(); + window.draw(bg_sprite); + for (int i = 0; i < 3; i++) { + help[i].showText(window); + } + window.display(); + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { + while (gameloop(window, enemy_amount, time_count, time)) { + enemy_amount = enemy_amount + 2; + if (enemy_amount>30) { + for (int i = 0; i < 30; i++) { + window.clear(); + help[4].showText(window); + window.display(); + } + goto menu; + } + for (int i = 0; i < 30; i++) { + window.clear(); + help[3].showText(window); + window.display(); + } + } + setInformation(help[4], "YOU LOSE!", 275, 300, 35); + for (int i = 0; i < 30; i++) { + window.clear(); + help[4].showText(window); + window.display(); + } + } + } + + +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/gameWindow.h b/practices/cpp/level1/p11_Fighters/gameWindow.h new file mode 100644 index 00000000..ec110953 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/gameWindow.h @@ -0,0 +1,9 @@ +#ifndef GAMEWINDOW_H +#define GAMEWINDOW_H +#include "plane.h" + +void gameProcess(); +void playerAction(plane & p); +void enemysAction(plane * p, bool & flag1,int max); +bool gameloop(sf::RenderWindow &window,int max_enemy, int &time_count,int &time); +#endif diff --git a/practices/cpp/level1/p11_Fighters/main.cpp b/practices/cpp/level1/p11_Fighters/main.cpp new file mode 100644 index 00000000..ec15ca47 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/main.cpp @@ -0,0 +1,16 @@ +// skyLegend.cpp : 定义控制台应用程序的入口点。 +// + +#include "stdafx.h" +#include +#include "plane.h" +#include "gameMusic.h" +#include +#include +#include "gameWindow.h" +int main() { + srand(time(0)); + gameProcess(); + + return 0; +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/plane.cpp b/practices/cpp/level1/p11_Fighters/plane.cpp new file mode 100644 index 00000000..9a0f4670 --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/plane.cpp @@ -0,0 +1,280 @@ +#include "stdafx.h" +#include +#include +#include "plane.h" +#include "bullet.h" +#include "gameMusic.h" +#define Xmax 800 +#define Ymax 600 +plane::plane(float x,float y,char type) { + this->type = type; + switch (type) { + case 'p': + plane_texture.loadFromFile("player.psd"); + plane_bullet=new bullet(25); + speed =8; + life = 12; + half_sizeX = 37.5; + half_sizeY = 15; + this->x = x; + this->y = y; + plane_sprite.setTextureRect(sf::IntRect(0, 0, 200, 101)); + plane_sprite.setScale(sf::Vector2f(0.375f, 0.3f)); + fireRate = 5; + godmode = 1; + break; + case 'e': + plane_texture.loadFromFile("enemy.psd"); + plane_bullet = new bullet(20); + speed = 1; + life = 0; + half_sizeX = 16; + half_sizeY = 12; + this->x = rand()%Xmax-half_sizeX; + this->y = y; + plane_sprite.setTextureRect(sf::IntRect(0, 0, 107, 80)); + plane_sprite.setScale(sf::Vector2f(0.3f, 0.3f)); + fireRate = 50; + godmode = 0; + break; + } + bulletCD = 0; + count = 0; + plane_sprite.setTexture(plane_texture); + + plane_sprite.setPosition(sf::Vector2f(this->x, this->y)); +} +void plane::setPlane(float x, float y) { + this->x = x; + this->y = y; + plane_sprite.setPosition(sf::Vector2f(this->x,this-> y)); +} +void plane::movePlane(float x, float y) { + float x0, y0; + x0 = this->x+x*speed; + y0 = this->y+y*speed; + if (x0 >= -half_sizeX && x0 <= Xmax- half_sizeX && y0 >= -half_sizeY && y0 <= Ymax-2*half_sizeY) { + this->x = x0; + this->y = y0; + }//边界判断 + plane_sprite.setPosition(sf::Vector2f(this->x, this->y)); +} +void plane::movePlane() { + if (life<=0)return; + this->y = this->y + speed; + if (this->x < -half_sizeX || this->x > Xmax - half_sizeX || + this->y < -half_sizeY || this->y > Ymax ) { + life = 0; + return; + } + plane_sprite.setPosition(sf::Vector2f(this->x, this->y)); +} +void plane::showPlane(sf::RenderWindow &thisWindow) { + switch (this->life) { + case 11: + case 7: + case 3: + plane_sprite.setTextureRect(sf::IntRect(0, 101, 200, 101)); + thisWindow.draw(plane_sprite); + plane_bullet->showBullet(thisWindow); + this->life = this->life - 1; + break; + case 10: + case 6: + plane_sprite.setTextureRect(sf::IntRect(0, 202, 200, 101)); + thisWindow.draw(plane_sprite); + plane_bullet->showBullet(thisWindow); + this->life = this->life - 1; + break; + case 2: + plane_sprite.setTextureRect(sf::IntRect(0, 202, 200, 101)); + thisWindow.draw(plane_sprite); + plane_bullet->showBullet(thisWindow); + this->life = 0; + break; + case 9: + case 5: + plane_sprite.setTextureRect(sf::IntRect(0, 0, 200,101)); + thisWindow.draw(plane_sprite); + plane_bullet->showBullet(thisWindow); + plane_sprite.setPosition(sf::Vector2f(350, 500)); + this->x = 350, this->y = 500; + this->godmode = 1; + this->life = this->life - 1; + break; + case 12: + case 8: + case 4: + case 1: + if (this->godmode) { + plane_sprite.setTextureRect(sf::IntRect(0, 303, 200, 101)); + } + else if('p'==this->type){ + plane_sprite.setTextureRect(sf::IntRect(0, 0, 200, 101)); + } + thisWindow.draw(plane_sprite); + plane_bullet->showBullet(thisWindow); + break; + case -1: + case -2: + case -3: + plane_sprite.setTextureRect(sf::IntRect(0, 80, 107, 80)); + thisWindow.draw(plane_sprite); + plane_bullet->showBullet(thisWindow); + this->life = this->life - 1; + break; + case -4: + case -5: + case -6: + plane_sprite.setTextureRect(sf::IntRect(0, 160, 107,100)); + thisWindow.draw(plane_sprite); + plane_bullet->showBullet(thisWindow); + this->life = this->life - 1; + break; + case -7: + + this->life = 0; + break; + case 0: + plane_bullet->showBullet(thisWindow); + default: + break; + + } + +} +void plane::Fire(char type) { + if (this->life <= 0)return; + if (this->life % 4 != 0 && this->life != 1)return; + if (!bulletCD) { + switch (this->type) { + case 'p': + plane_bullet->appendBullet(this->x + this->half_sizeX , this->y, type); + break; + case 'e': + plane_bullet->appendBullet(this->x + this->half_sizeX, this->y + 2 * this->half_sizeY, type); + break; + default: + ; + } + + bulletCD = 1; + } + +} +void plane::moveBullet() { + plane_bullet->moveBullet(); +} +void plane::lifeChange(int delta) { + life += delta; +} +void plane::freshBulletCD() { + if (bulletCD&&(0==count%fireRate)) { + bulletCD =0; + } +} +void plane::Count() { + this->count=this->count+1; + if (100 == count) { + count = 0; + } +} +bool plane::isExist() { + if (life != 0)return true; + return false; +} +bool plane::isCollision(float x, float y,float Xsize,float Ysize) { + if ((this->x + 2 * this->half_sizeX > x&&this->x < x + 2 * Xsize) && + (this->y + 2*this->half_sizeY > y&&this->y< y+2*Ysize ))return true; + return false; +} +bool plane::collision(bullet & bp) { + if ( this->life<=0)return false; + for (int i = 0; i < 25; i++) { + if (bp.bulletInfo[i].ifExist == 0)continue; + if ((bp.bulletInfo[i].x > this->x && + bp.bulletInfo[i].x < this->x + 2*this->half_sizeX) && + (bp.bulletInfo[i].y > this->y && + bp.bulletInfo[i].y < this->y + 2*this->half_sizeY)) { + bp.bulletInfo[i].ifExist = 0; + switch (this->showType()) { + case 'e': + this->lifeChange(-2); + break; + case 'p': + if (this->godmode)return false; + this->lifeChange(-1); + break; + default: + ; + } + + + return true; + } + } + return false; +} +bool plane::collision(plane & p) { + if (0 >= this->life)return false; + if (!p.isExist())return false; + if (p.isCollision(this->x, this->y,this->half_sizeX,this->half_sizeY)) { + this->lifeChange(-2); + if (p.godmode)return true; + p.lifeChange(-1); + return true; + } + return false; +} +char plane::showType() { + return this->type; +} +int plane::showLife(){ + return this->life; +} +void appendEnemy(plane * enemy,int index) { + enemy[index] = *(new plane()); + enemy[index].lifeChange(1); +} + +void moveEnemy(plane * enemy,plane & player,int max,int & ifCollision) { + for (int i = 0; i < max; i++) { + enemy[i].movePlane(); + enemy[i].moveBullet(); + if (enemy[i].collision(*player.plane_bullet)|| + enemy[i].collision(player) ){ + ifCollision = 1; + } + if (player.collision(*enemy[i].plane_bullet)) { + ifCollision = 2; + } + } +} +void showEnemy(sf::RenderWindow &thisWindow,plane * enemy,int max) { + for (int i = 0; i < max; i++) { + enemy[i].showPlane(thisWindow); + enemy[i].Count(); + enemy[i].freshBulletCD(); + } +} +void plane::deleteBullet() { + int max; + switch (this->type) { + case 'p': + max = 25; + break; + case 'e': + max = 20; + break; + default: + ; + } + for (int i = 0; i < max; i++) { + this->plane_bullet->bulletInfo[i].ifExist = 0; + } +} +void clearBullet(plane * enemy,int max) { + for (int i = 0; i < max; i++) { + enemy[i].deleteBullet(); + } +} \ No newline at end of file diff --git a/practices/cpp/level1/p11_Fighters/plane.h b/practices/cpp/level1/p11_Fighters/plane.h new file mode 100644 index 00000000..1c1053bb --- /dev/null +++ b/practices/cpp/level1/p11_Fighters/plane.h @@ -0,0 +1,46 @@ +#ifndef PLANE_H +#include "stdafx.h" +#include "bullet.h" +#include +#define PLANE_H +class plane { + + public: + bool godmode; + bullet * plane_bullet; + plane(float x=0, float y=0,char type='e'); + void setPlane(float x,float y); + void movePlane(float x, float y); + void movePlane(); + void showPlane(sf::RenderWindow &thisWindow); + void Fire(char type); + void moveBullet(); + void lifeChange(int delta); + void freshBulletCD(); + void Count(); + void deleteBullet(); + char showType(); + int showLife(); + bool isExist(); + bool isCollision(float x, float y, float Xsize, float Ysize); + bool collision(bullet & bp); + bool collision(plane & p); + protected: + bool bulletCD; + + char type; + int count; + int fireRate; + sf::Texture plane_texture; + sf::Sprite plane_sprite; + short life; + float x, y; + float speed; + float half_sizeX, half_sizeY; + +}; +void appendEnemy(plane * enemy,int index); +void moveEnemy(plane * enemy, plane & player,int max, int & ifCollision); +void showEnemy(sf::RenderWindow &thisWindow,plane * enemy,int max); +void clearBullet(plane * emeny,int max); +#endif