diff --git a/level1/p01_runningLetter/runningLetter.c b/level1/p01_runningLetter/runningLetter.c new file mode 100644 index 00000000..5820bdda --- /dev/null +++ b/level1/p01_runningLetter/runningLetter.c @@ -0,0 +1,34 @@ +#include +#include + + +void main() { + int n = 0; + const int RIGHT_BORDER = 105, LEFT_BORDER = 0; + char value = 1; + while (1) { + if (value) { + for (int i = 0; i < n; i++) { + printf(" "); + } + printf("Running Word"); + n++; + system("cls"); + if (n == RIGHT_BORDER) { + value = 0; + } + } + else { + for (int i = 0; i < n; i++) { + printf(" "); + } + printf("Running Word"); + n--; + system("cls"); + if (n == LEFT_BORDER) { + value = 1; + } + } + } +} + diff --git a/level1/p02_isPrime/isprime.c b/level1/p02_isPrime/isprime.c new file mode 100644 index 00000000..7423b3af --- /dev/null +++ b/level1/p02_isPrime/isprime.c @@ -0,0 +1,23 @@ +#include +#include + +int is_prime(int n){ + int i; + for(i=2;i<=sqrt(n);i++){ + if(n%i==0){ + return 0; + } + } + return 1; +} + +int main(){ + int n; + printf("Please enter a number: "); + scanf("%d",&n); + if(is_prime(n)&&n>1){ + printf("It is a prime number."); + }else{ + printf("It isn't a prime number.'"); + } +} diff --git a/level1/p03_Diophantus/Diophantus.c b/level1/p03_Diophantus/Diophantus.c new file mode 100644 index 00000000..5dd48f6a --- /dev/null +++ b/level1/p03_Diophantus/Diophantus.c @@ -0,0 +1,12 @@ +#include +//丢番图问题 +int main(){ + float x; + for(x=1;x<200;x++){ + if((x/6+x/12+x/7+5+x/2+4)==x){ + printf("%.0f",x); + break; + } + } + return 0; +} diff --git a/level1/p04_ narcissus/narcissus.c b/level1/p04_ narcissus/narcissus.c new file mode 100644 index 00000000..30a39470 --- /dev/null +++ b/level1/p04_ narcissus/narcissus.c @@ -0,0 +1,14 @@ +#include + +int main(){ + int n; + int a,b,c; + for(n=100;n<1000;n++){ + c = n%10;//¸öλÊý + b = n/10%10;//ʮλÊý + a = n/100;//°ÙλÊý + if((a*a*a+b*b*b+c*c*c)==n){ + printf("%d\n",n); + } + } +} diff --git a/level1/p05_allPrimes/allPrimes.c b/level1/p05_allPrimes/allPrimes.c new file mode 100644 index 00000000..b2a70a7e --- /dev/null +++ b/level1/p05_allPrimes/allPrimes.c @@ -0,0 +1,64 @@ +#include +#include +#include + +//³£¹æ×ö·¨ +int is_prime1(){ + int i,n,m,t = 0; + for(n = 2;n < 1000;n ++){ + m = 1; + for(i = 2;i <= sqrt(n);i ++){ + if(n % i == 0){ + m = 0; + break; + } + } + if(m == 1){ + printf("%d\t",n); + t ++; + } + if(t == 15){ + printf("\n"); + t = 0; + } + } + return 0; +} + +//°£ÊÏɸ +int is_prime2(){ + char a[1000] = {1}; + int i,x,m = 0; + for(i = 2;i < 1000;i ++){ + if(a[i] == 0){ + for(x = i*i;x < 1000;x = x + i)a[x] = 1; + printf("%d\t",i); + m ++; + if(m == 15){ + printf("\n"); + m = 0; + } + } + } +} + +int main(){ + int n,m = 0; + clock_t start_t,end_t; + double total_t; + + //³£¹æ×ö·¨ + start_t = clock(); + is_prime1(); + end_t=clock(); + total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; + printf("\n³ÌÐòÔËÐеÄ×Üʱ¼äΪ: %fs\n",total_t); + + //°£ÊÏɸ + start_t = clock(); + is_prime2(); + end_t = clock(); + total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; + printf("\n³ÌÐòÔËÐеÄ×Üʱ¼äΪ: %fs",total_t); + return 0; +} diff --git a/level1/p06_Goldbach/Goldbach.c b/level1/p06_Goldbach/Goldbach.c new file mode 100644 index 00000000..8defbe72 --- /dev/null +++ b/level1/p06_Goldbach/Goldbach.c @@ -0,0 +1,26 @@ +#include +#include +#include + +bool is_prime(int n){ + int i; + for(i=2;i<=sqrt(n);i++){ + if(n%i==0)return false; + } + return true; +} + +int main(){ + int n,m,x=0; + for(n = 4;n <= 100;n += 2){ + for(m = 2;m <= n;m ++){ + if(is_prime(m) && is_prime(n - m)){ + printf("%3d = %2d + %2d\n",n,m,n - m); + x++; + break; + } + } + if(x==49)printf("The Goldbach is true"); + } + return 0; +} diff --git a/level1/p07_encrypt_decrypt/encrypt.c b/level1/p07_encrypt_decrypt/encrypt.c new file mode 100644 index 00000000..6004d416 --- /dev/null +++ b/level1/p07_encrypt_decrypt/encrypt.c @@ -0,0 +1,99 @@ +/*³¢ÊÔ¿­Èö¼ÓÃÜÓë½âÃÜ*/ +#include +#include +#include + +//¼ÓÃÜ +void encode(char str[], int n) { + int i; + char c; + for (i = 0; i < strlen(str); ++i) { + c = str[i]; + if (str[i] >= 'A' && str[i] <= 'Z') { + if (c + n % 26 <= 'Z') { + str[i] = (char)(c + n % 26); + } + else { + str[i] = (char)(c + n % 26 - 26); + } + } + else if (str[i] >= 'a' && str[i] <= 'z') { + if (c + n % 26 <= 'z') { + str[i] = (char)c + n % 26; + } + else { + str[i] = (char)c + n % 26 - 26; + } + } + else { + str[i] = c;//²»ÊÇ×Öĸ£¬²»¼ÓÃÜ + } + } + printf("\nAfter encode: \n"); + puts(str); +} + +//½âÃÜ +void decode(char str[], int n) { + int i; + char c; + for (i = 0; i <= strlen(str); ++i) { + c = str[i]; + if (str[i] <= 'z' && str[i] >= 'a') { + if (c - n % 26 > 'a') { + str[i] = (char)(c - n % 26); + } + else { + str[i] = (char)(c - n % 26 + 26); + } + } + else if (str[i] <= 'Z' && str[i] >= 'A') { + if (c - n % 26 > 'A') { + str[i] = (char)(c - n % 26); + } + else { + str[i] = (char)(c - n % 26 + 26); + } + } + else { + str[i] = c; + } + } + printf("\nAfter decode: \n"); + puts(str); +} + + +int main() { + char str[50]; + int k, q = 1; + int n,c; + while (q) { + system("cls"); + printf("Please enter your word:\n"); + scanf_s("%s", str,50); + printf("Please set the encode function: \n"); + scanf_s("%d", &n); + printf("-----------------\n"); + printf("1: Encryption\n"); + printf("2: Decryption\n"); + printf("-----------------\n"); + printf("\nPlease choose: "); + scanf_s("%d", &k); + if (k == 1) { + encode(str, n); + } + else if (k == 2) { + decode(str, n); + } + + printf("Do you want to quit (1/0): \n"); + scanf_s("%d", &c); + if (c == 1) { + q = 0; + } + } + return 0; + +} + diff --git a/level1/p08_hanoi/haoni.c b/level1/p08_hanoi/haoni.c new file mode 100644 index 00000000..4695a81a --- /dev/null +++ b/level1/p08_hanoi/haoni.c @@ -0,0 +1,28 @@ +#include +int m = 0; +void move(int disk, char A, char C) { + m++; + printf("µÚ %d ´ÎÒƶ¯£º ½« %d ºÅÔ²ÅÌ´Ó %c Òƶ¯µ½ %c\n", m, disk, A, C); +} +void hanoi(int n, char A, char B, char C) { + if (n == 1) { + move(n, A, C); + } + else { + hanoi(n - 1, A, C, B); + move(n, A, C); + hanoi(n - 1, B, A, C); + } +} + +int main() { + char A = 'A'; + char B = 'B'; + char C = 'C'; + int disks; + printf("ÇëÊäÈëÔ²Å̵ĸöÊý\n"); + scanf("%d", &disks); + hanoi(disks, A, B, C); + return 0; +} + diff --git a/level1/p09_maze/maze.c b/level1/p09_maze/maze.c new file mode 100644 index 00000000..3daaf9cb --- /dev/null +++ b/level1/p09_maze/maze.c @@ -0,0 +1,238 @@ +#include +#include +#include +#include +#include + +int rank = 6; +//µØͼ±ß³¤ L£¬°üÀ¨ÃÔ¹¬Ö÷Ìå 20£¬Íâ²àµÄ°üΧµÄǽÌå 2£¬×îÍâ²à°üΧ·¾¶ 2 +#define L 24 +#define WALL 0 //ǽ +#define ROUTE 1 //·¾¶ +#define PLAYER 2 //Íæ¼Ò + +void menu(); +void start(); +void degree(); +int init(int** maze); +void print(int** maze); +void creatMaze(int** maze, int x, int y); +void move(int** maze, char t, int& x, int& y); + + +int main() { + menu(); + return 0; +} + +//ÏÔʾÖ÷²Ëµ¥ +void menu() { + char c; + while (1) { + system("cls"); + printf("---------------------\n"); + printf("---»¶Ó­À´µ½ÃÔ¹¬ÓÎÏ·---\n"); + printf("----1. ¿ªÊ¼ÓÎÏ· -----\n"); + printf("----2. ÓÎÏ·ÄÑ¶È -----\n"); + printf("----0. Í˳öÓÎÏ· -----\n"); + printf("--------------------\n"); + c = _getch(); + switch (c) { + case '1': + start(); + break; + case '2': + degree(); + break; + case '0': + printf("»¶Ó­Ï´μÌÐøÓÎÍæ........\n"); + Sleep(1500); + exit(0); + default: + break; + } + } +} + +//¿ªÊ¼ÓÎÏ· +void start() { + char t; + //x£¬yÊÇÍæ¼ÒµÄ×ø±ê£¬outÊdzö¿ÚµÄ×Ý×ø±ê(ÐкÅ) + int i, x = 2, y = 1, out = 0; + //³õʼ»¯Ëæ»úÊýÖÖ×Ó£¬ÀûÓÃϵͳʱ¼äÉú³ÉËæ»úÊý,Ö®ºó¾Í¿ÉÒÔʹÓÃrandº¯ÊýÉú³ÉËæ»úÊý + srand((unsigned)time(NULL)); + //³õʼ»¯ÃÔ¹¬ + int** maze = (int**)malloc(L * sizeof(int*)); + for (i = 0; i < L; i++) { + maze[i] = (int*)calloc(L, sizeof(int)); + } + //µÃµ½³ö¿Ú×Ý×ø±ê + out = init(maze); + system("cls"); + print(maze); + while (t = _getch()) { + system("cls"); + move(maze, t, x, y); + print(maze); + if (x == out && y == L - 2) { + system("cls"); + printf("¹§Ï²Ä㣬µ½´ïÁËÖÕµã!\n"); + printf("¼´½«·µ»ØÖ÷²Ëµ¥\n"); + Sleep(1500); + break; + } + } + for (i = 0; i < L; i++) + free(maze[i]); + free(maze); +} + +void degree() { + char t; + system("cls"); + printf("---------------------\n"); + printf("---ÇëÊäÈëÓÎÏ·ÄѶÈ---\n"); + printf("----- 1. ¼òµ¥ -----\n"); + printf("----- 2. ÖÐµÈ -----\n"); + printf("----- 3. À§ÄÑ -----\n"); + printf("--------------------\n"); + t = _getch(); + switch (t) { + case '1': + printf("ÒÑÐÞ¸ÄÓÎÏ·ÄѶÈΪ¼òµ¥\n"); + rank = 6; + break; + case '2': + printf("ÒÑÐÞ¸ÄÓÎÏ·ÄѶÈΪÖеÈ\n"); + rank = 3; + break; + case '3': + printf("ÒÑÐÞ¸ÄÓÎÏ·ÄѶÈΪÀ§ÄÑ\n"); + rank = 0; + break; + default: + break; + } + printf("°´ÈÎÒâ¼ü·µ»Ø²Ëµ¥\n"); + system("pause"); + +} +//³õʼ»¯ÃÔ¹¬ +int init(int** maze) { + int i; + //½«×îÍâ²ã¶¼ÉèÖÃΪ¿ÕµÄ·¾¶£¬·ÀÖ¹ÍÚ´© + for (i = 0; i < L; i++) { + maze[i][0] = ROUTE; + maze[0][i] = ROUTE; + maze[i][L - 1] = ROUTE; + maze[L - 1][i] = ROUTE; + } + creatMaze(maze, 2, 2);//Éú³ÉÃÔ¹¬Èë¿ÚºÍ³ö¿Ú + maze[2][1] = PLAYER; //¸øÍæ¼Ò³õʼλÖà + //Ñ°ÕÒ³ö¿Ú£¬³ö¿Ú¿ÉÄܲ»ÔÚ[L-3][L-2]£¬Òò´ËÐèÒª²éÕÒ + for (i = L - 3; i >= 0; i--) { + if (maze[i][L - 3] == ROUTE) { + maze[i][L - 2] = ROUTE; + return i; //³ö¿Ú·µ»Ø×Ý×ø±ê + } + } +} + +//´òÓ¡ÃÔ¹¬ +void print(int** maze) { + int i, j; + for (i = 0; i < L; i++) { + for (j = 0; j < L; j++) { + if (maze[i][j] == ROUTE) + printf(" "); + else if (maze[i][j] == WALL) + printf("ǽ"); + else if (maze[i][j] == PLAYER) + printf("Ê®"); + } + printf("\n"); + } +} + +//¹¹½¨ÃÔ¹¬ +void creatMaze(int** maze, int x, int y) { + maze[x][y] = ROUTE; + //È·±£Ëĸö·½ÏòËæ»ú£¬¶ø²»ÔÙÊǹ̶¨µÄÉÏÏÂ×óÓÒÕâÖÖÅÅÁÐ + int direction[4][2] = { { 1,0 },{ -1,0 },{ 0,-1 },{ 0,1 } }; + for (int i = 0; i < 4; i++) { + int r = rand() % 4; + int temp = direction[0][0]; + direction[0][0] = direction[r][0]; + direction[r][0] = temp; + temp = direction[0][1]; + direction[0][1] = direction[r][1]; + direction[r][1] = temp; + } + //ÏòËĸö·½Ïò¿ªÍÚ + for (int i = 0; i < 4; i++) { + int dx = x; + int dy = y; + //¿ØÖÆÍڵľàÀ룬ÓÉRankÀ´µ÷Õû´óС + int range = 1 + (rank == 0 ? 0 : rand() % rank); + while (range > 0) { + //¼ÆËã³ö½«Òª·ÃÎʵ½µÄ×ø±ê + dx += direction[i][0]; + dy += direction[i][1]; + //Åųýµô»Øͷ· + if (maze[dx][dy] == ROUTE) { + break; + } + //ÅжÏÊÇ·ñÍÚ´©Â·¾¶ + int count = 0; + for (int j = dx - 1; j < dx + 2; j++) { + for (int k = dy - 1; k < dy + 2; k++) { + //abs(j - dx) + abs(k - dy) == 1 È·±£Ö»ÅжϾŹ¬¸ñµÄËĸöÌض¨Î»Öà + if (abs(j - dx) + abs(k - dy) == 1 && maze[j][k] == ROUTE) { + count++; + } + } + } + //count´óÓÚ1±íÃ÷ǽÌå»á±»ÍÚ´©£¬Í£Ö¹ + if (count > 1) + break; + //È·±£²»»áÍÚ´©Ê±£¬Ç°½ø + range -= 1; + maze[dx][dy] = ROUTE; + } + //ûÓÐÍÚ´©Î£ÏÕ£¬ÒÔ´ËΪ½ÚµãµÝ¹é + if (range <= 0) { + creatMaze(maze, dx, dy); + } + } +} + +//Òƶ¯ +void move(int** maze, char t, int& x, int& y) { + int i, j; + i = x; + j = y; + switch (t) { + case 'w': + x--; + break; + case 's': + x++; + break; + case 'a': + y--; + break; + case 'd': + y++; + break; + default: + break; + } + if (x >= 0 && x < L-1 && y >= 0 && y < L-1 && maze[x][y] != WALL) { + maze[i][j] = ROUTE; + maze[x][y] = PLAYER; + } + else { + x = i; + y = j; + } +} diff --git a/level1/p10_pushBoxes/function.c b/level1/p10_pushBoxes/function.c new file mode 100644 index 00000000..8121c3ca --- /dev/null +++ b/level1/p10_pushBoxes/function.c @@ -0,0 +1,250 @@ +#include +#include +#include +#include "function.h" + +//¿ªÊ¼ÓÎÏ· +void start(int map[][L],int nmap) { + int num,goal = 0; + int score = 0; + int bestscore = map[L][0]; + for(int i=0;i0;num--) { + for (int i = 0; i < L; i++) { + fscanf(fp, "%d %d %d %d %d %d %d %d", &map[i][0], &map[i][1], &map[i][2], &map[i][3], &map[i][4], + &map[i][5], &map[i][6], &map[i][7]); + } + fscanf(fp,"%d",&map[L][0]); + } + fclose(fp); + return map; +} + +//ÓÎϷ˵Ã÷ +void explain(){ + system("cls"); + printf("»¶Ó­À´µ½ÍÆÏä×ÓÓÎÏ·......\n"); + printf("±¾ÓÎÏ·¹²ÓÐÈý¹Ø\n"); + printf("ʹÓÃwÉÏÒÆ£¬ÊÊÓÃsÏÂÒÆ\n"); + printf("ʹÓÃa×óÒÆ£¬Ê¹ÓÃdÓÒÒÆ\n"); + printf("ÓÎÏ·Ä¿µÄÊǽ«ËùÓеÄÏä×ÓÍƵ½¶ÔÓ¦µÄÄ¿µÄµØÉÏ\n"); + printf("ÓÎÏ·¹ý³ÌÖлáËæʱ¼Ç¼ÄãµÄ²½Êý£¬Ï£ÍûÄãÄܹ»´òÆƼǼ£¡\n"); + printf("×¢Ò⣺ÔÚÓÎÏ·ÖÐËæʱ¿ÉÒÔ°´qÍ˳öÓÎÏ·£¬»Øµ½Ö÷½çÃæ\n"); + system("pause"); +} + +void write(int bestscore,int num){ + int (*map)[L]; + map=(int(*)[L])malloc((3*L+3)*sizeof(int[L])); + FILE* fp; + //ÏÖ¶ÁÈ¡Ô­ÓÐÎļþ£¬Îª¸üÐÂ×î¼Ñ³É¼¨µÄÖµ×öºÃ×¼±¸ + if ((fp = fopen("map.txt", "r")) == NULL) { + printf("File failed to open......\n"); + system("pause"); + exit(0); + } + for (int i = 0; i < 3*L+3; i++) { + if(i==L||i==2*L+1||i==3*L+2) + fscanf(fp,"%d",&map[i][0]); + else + fscanf(fp, "%d %d %d %d %d %d %d %d", &map[i][0], &map[i][1], &map[i][2], &map[i][3], &map[i][4], + &map[i][5], &map[i][6], &map[i][7]); + } + fclose(fp); + + map[num*L+num-1][0] = bestscore; //¸üÐÂ×î¼Ñ³É¼¨ + //ÖØÐÂдÈëÎļþÒÔ¸üÐÂ×î¼Ñ³É¼¨ + if ((fp = fopen("map.txt", "w")) == NULL) { + printf("File failed to open......\n"); + system("pause"); + exit(0); + } + for(int i = 0;i< 3*L+3;i++){ + if(i==L||i==2*L+1||i==3*L+2) + fprintf(fp,"%d\n",map[i][0]); + else + fprintf(fp, "%d %d %d %d %d %d %d %d\n", map[i][0], map[i][1], map[i][2], map[i][3], map[i][4], + map[i][5], map[i][6], map[i][7]); + } + fclose(fp); +} + + diff --git a/level1/p10_pushBoxes/function.h b/level1/p10_pushBoxes/function.h new file mode 100644 index 00000000..ad753919 --- /dev/null +++ b/level1/p10_pushBoxes/function.h @@ -0,0 +1,22 @@ +#ifndef __FUNCTION_H__ +#define __FUNCTION_H__ + +#define ROUTE 0 //·¾¶ +#define WALL 1 //ǽ +#define PLAYER 2 //Íæ¼Ò +#define BOX 3 //Ïä×Ó +#define DESTINATION 4 //ÖÕµã +#define FINSHED 7// ±íʾÒÑÍê³ÉµÄÏä×Ó +#define PFINSHED 6 // È˼ÓÖÕµã +#define L 8 //¹æ¶¨µÄµØͼ´óС£¨ÎļþÖеĵØͼҲ½«°´Õâ¸ö³ß´ç±àд£© + +void start(int map[][L],int nmap); +void print(int map[][L],int score,int bestscore); +int (*tuidong(int map[][L]))[L]; +int (*fileReader(int num))[L]; +int check(int map[][L]); +void explain(); +void write(int bestscore,int num); + +#endif + diff --git a/level1/p10_pushBoxes/main.c b/level1/p10_pushBoxes/main.c new file mode 100644 index 00000000..9ffe50da --- /dev/null +++ b/level1/p10_pushBoxes/main.c @@ -0,0 +1,52 @@ +/*ÍÆÏä×ÓСÓÎÏ·£¬¾­²âÊÔ¿ÉÒÔÕý³£ÔËÐУ¬ +1. ½«p09ÃÔ¹¬ÓÎÏ·¸ÄÔìΪ¡°ÍÆÏä×Ó¡±ÓÎÏ·£» +1. ÔÚµØͼÖÐÔö¼ÓÏä×Ó¡¢Ïä×ÓÄ¿±êλÖõÈͼÐΣ» +1. µ±Íæ¼Ò½«ËùÓÐÏä×Ó¹é룬ÔòÏÔʾÍæ¼ÒÓ®µÃÁËÓÎÏ·£» +1. °´Íæ¼Ò×߶¯²½Êý¼Ç·Ö£» +1. Éè¼Æ¶à¸ö¹Ø¿¨£¬Ã¿Ò»¹ØµÄµØͼ´ÓÎļþÖжÁÈ¡£¬Íæ¼Òÿ¹ØµÄ·ÖÊý¼Ç¼µ½ÎļþÖУ» +×¢Ò⣺ÔËÐÐÇ°Ç뽫¸÷¸öÎļþ·Åµ½Í¬Ò»¹¤³ÌĿ¼Ï£¬ +Èô²»ÄÜÕý³£ÔËÐУ¬Ç뽫¶ÁдÎļþµÄ·¾¶¸ÄΪ¾ø¶Ô·¾¶*/ + +#include +#include +#include +#include "function.h" + +int main() { + int(*map)[L]; + char c; + while (1) { + system("cls"); + printf("---------------------\n"); + printf("---»¶Ó­À´µ½ÍÆÏä×ÓСÓÎÏ·---\n"); + printf("----1. ¹Ø¿¨1 -----\n"); + printf("----2. ¹Ø¿¨2 -----\n"); + printf("----3. ¹Ø¿¨3 -----\n"); + printf("----4. ÓÎϷ˵Ã÷---\n"); + printf("----0. Í˳öÓÎÏ· -----\n"); + printf("--------------------\n"); + c = _getch(); + switch (c) { + case '1': + map= fileReader(1); + start(map,1); + break; + case '2': + map = fileReader(2); + start(map,2); + break; + case '3': + map = fileReader(3); + start(map,3); + break; + case '4': + explain(); + break; + case '0': + printf("»¶Ó­Ï´μÌÐøÓÎÍæ........\n"); + Sleep(1500); + exit(0); + } + } +} + diff --git a/level1/p10_pushBoxes/map.txt b/level1/p10_pushBoxes/map.txt new file mode 100644 index 00000000..7abdc5ba --- /dev/null +++ b/level1/p10_pushBoxes/map.txt @@ -0,0 +1,27 @@ +0 0 1 1 1 0 0 0 +0 0 1 4 1 0 0 0 +0 0 1 0 1 1 1 1 +1 1 1 3 0 3 4 1 +1 4 0 3 2 1 1 1 +1 1 1 1 3 1 0 0 +0 0 0 1 4 1 0 0 +0 0 0 1 1 1 0 0 +100 +1 1 1 1 1 1 1 0 +1 4 4 3 4 4 1 0 +1 4 4 1 4 4 1 0 +1 0 3 3 3 0 1 0 +1 0 0 3 0 0 1 0 +1 0 3 3 3 0 1 0 +1 0 0 1 2 0 1 0 +1 1 1 1 1 1 1 0 +100 +1 1 1 1 1 1 1 0 +1 4 0 4 0 4 1 0 +1 0 3 3 3 0 1 0 +1 4 3 2 3 4 1 0 +1 0 3 3 3 0 1 0 +1 4 0 4 0 4 1 0 +1 1 1 1 1 1 1 0 +0 0 0 0 0 0 0 0 +100 \ No newline at end of file diff --git a/level1/p11_linkedList/linkedList.c b/level1/p11_linkedList/linkedList.c new file mode 100644 index 00000000..07aac8ff --- /dev/null +++ b/level1/p11_linkedList/linkedList.c @@ -0,0 +1,108 @@ +/* ÔÚ main º¯ÊýÖд´½¨Ò»¸öµ¥ÏòÁ´±í£» +1. ±éÀú¸ÃÁ´±í£¬ÒÀ´ÎÏÖʵ¸÷½ÚµãµÄ value£» +1. ½«¸ÃÁ´±íËùÓнڵ㷴Ðò£» +1. ÔÚ¸ÃÁ´±íÖвéÕÒµÚÒ»¸öֵΪ 5 µÄ½Úµã£¬Èç¹ûÕÒµ½Ôò·µ»Ø¸Ã½ÚµãµÄÐòºÅ£¬·ñÔò·µ»Ø£­1£» +1. ²éÕÒÏÂÒ»¸öֵΪ 5 µÄ½Úµã£¬·µ»ØֵͬÉÏ£»*/ + +#include +#include +typedef struct linkedList{ + int elem; + struct linkedList *next; +}link; + +link* initLink(int n); +link* reverse(link* p,int n); +link* insertLink(link* p,int elem,int add); +int find(link* p,int n,int ele,int num); + +int main(){ + int n; + printf("ÇëÊäÈëÒª´´½¨µÄÁ´±í´óС£º\n"); + scanf("%d",&n); + link* p = initLink(n+1); + link* temp = p->next; + for(int i = 1;i <= n;i ++){ + printf("ÇëÊäÈëµÚ%dºÅ½ÚµãµÄÔªËØÖµ\n",i); + scanf("%d",&temp->elem); + temp = temp->next; + } + + //ÄæÐò + p = reverse(p,n); + temp = p->next; + printf("ÄæÐòÊä³ö³É¹¦£º\n"); + for(int i = 1;i<=n;i++){ + printf("%d ",temp->elem); + temp = temp->next; + } + printf("\n"); + + //²éÕÒ 5 + for(int num=1;num<=2;num++){ + int position = find(p,n,5,num); + if(position!=-1) + printf("µÚ%d¸öֵΪ5µÄ½ÚµãÐòºÅ(ÄæÐòºó)ÊÇ£º%d\n",num,position); + else + printf("ûÓÐÕÒµ½ÖµÎª5µÄ½Úµã\n"); + } + return 0; +} + +//init linklist +link* initLink(int n){ + link* p = (link*)malloc(sizeof(link)); //´´½¨Ò»¸öÍ·½Úµã + link* temp = p; //´´½¨Ò»¸öÖмä±äÁ¿ÓÃÓÚ±éÀúÁ´±í + for(int i = 1 ; i < n; i ++){ + link* a = (link*)malloc(sizeof(link)); + a->elem = i; + a->next = NULL; + temp->next = a; + temp = temp->next; + } + return p; +} + +//add new element +link* insertLink(link* p,int elem,int add){ + link* temp = p; //´´½¨ÁÙʱ½Úµã£¬ÓÃÓÚ±éÀú + link* c = (link*)malloc(sizeof(link)); + for(int i = 1;i < add;i ++){ + if(temp==NULL){ + printf("²åÈëµÄλÖÃÎÞЧ\n"); + return p; + } + temp = temp->next; + } + c->elem = elem; + c->next = temp->next; + temp->next = c; + return p; +} + +//reverse the linklist +link* reverse(link*p,int n){ + link* temp = p->next; + link* newp = (link*)malloc(sizeof(link)); + newp->next = NULL; + for(int i =1;i<=n;i++){ + insertLink(newp,temp->elem,1); + temp = temp->next; + } + return newp; +} + +//find the element +int find(link* p,int n,int ele,int num){ + link* temp = p->next; + int x = 0; //¼Ç¼ÕÒµ½µÄ´ÎÊý + for(int i=1;i<=n;i++){ + if(temp->elem==ele){ + x++; + if(x==num) + return i; + } + temp = temp->next; + } + return -1; +} diff --git a/level1/p12_warehouse/warehouse.cpp b/level1/p12_warehouse/warehouse.cpp new file mode 100644 index 00000000..5076b35c --- /dev/null +++ b/level1/p12_warehouse/warehouse.cpp @@ -0,0 +1,226 @@ +#include +#include +#include + +typedef struct Node { + char name[200]; + int num; + struct Node* next; +}nodeList; + +void menu(); +void showList(nodeList* p); +void fileWrite(nodeList* p); +int find(nodeList* p, char* name); +nodeList* fileRead(); +nodeList* getin(nodeList* p); +nodeList* getout(nodeList* p); +nodeList* remove(nodeList* p, char* name, int num, int del); + +int main() { + menu(); +} + +// ´òÓ¡²Ëµ¥ +void menu() { + int t, q = 1; + nodeList* p = (nodeList*)malloc(sizeof(nodeList)); //³õʼ»¯²Ö¿â + p->next = NULL; + while (q) { + system("cls"); + printf("--------------\n"); + printf("»¶Ó­À´µ½²Ö¿â£¡\n"); + printf("1.ÏÔʾ´æ»õÁбí\n"); + printf("2.½ø¿â\n"); + printf("3.³ö¿â\n"); + printf("4.±£´æ\n"); + printf("5.¶ÁÈ¡\n"); + printf("0.Í˳ö\n"); + printf("--------------\n"); + scanf("%d", &t); + switch (t) { + case 1: + showList(p); //´òÓ¡²Ö¿âÇåµ¥ + break; + case 2: + p = getin(p); //Èë¿â + break; + case 3: + p = getout(p); //³ö¿â + break; + case 4: + fileWrite(p); //±£´æÎļþ + break; + case 5: + p = fileRead(); //¶ÁÈ¡Îļþ + break; + case 0: //Í˳ö + q = 0; + printf("ÕýÔÚÍ˳ö£¬»¶Ó­Ï´ÎÔÙÀ´......\n"); + Sleep(1500); + break; + default: + printf("error\n"); + system("pause"); + } + } +} + +//Èë¿â +nodeList* getin(nodeList* p) { + char name[100]; + int num; + nodeList* temp = p; //ÁÙʱ±äÁ¿ + nodeList* value = (nodeList*)malloc(sizeof(nodeList)); + system("cls"); + printf("ÇëÊäÈëÒª´æÈë²Ö¿âµÄ»õÎïÃû³Æ:\n"); + scanf("%s", name); + printf("ÇëÊäÈëÒª´æÈë²Ö¿âµÄ»õÎïÊýÁ¿:\n"); + scanf("%d", &num); + int position = find(p, name); //Ñ°ÕÒ»õÎïÊÇ·ñÒѾ­ÔÚ²Ö¿âÖУ¬ÈôÔÚÔò·µ»ØλÖÃ,·ñÔò·µ»Ø0 + if (position == 0) { //²åÈëµ±Á´Î² + for (int i = 0; temp->next != NULL; i++) { + temp = temp->next; + } + value->next = NULL; + strcpy(value->name, name); + value->num = num; + temp->next = value; + } + else { + for (int i = 0; i < position; i++) { + temp = temp->next; + } + temp->num += num; + } + printf("ÒÑ´æÈë²Ö¿â......\n"); + system("pause"); + return p; +} + +//³ö¿â +nodeList* getout(nodeList* p) { + char name[100]; + int num, del; + nodeList* temp = p; + system("cls"); + printf("ÇëÊäÈëÒª³ö¿âµÄ»õÆ·Ãû×Ö:\n"); + scanf("%s", name); + del = find(p, name); //Ñ°ÕÒ»õÎïÔÚ²Ö¿âÖеÄλÖà + if (del > 0) { + printf("ÇëÊäÈëҪȡ³öµÄ»õÎïÊýÁ¿:\n"); + scanf("%d", &num); + p = remove(p, name, num, del); + system("pause"); + } + else { + printf("δÕÒµ½»õÎï......\n"); + } + return p; +} + +//´òÓ¡²Ö¿â +void showList(nodeList* p) { + system("cls"); + nodeList* temp = p->next; + printf("---------------------------------------------------\n"); + printf("||»õÆ·Ãû³Æ\t\t\t\t»õÆ·ÊýÁ¿ ||\n"); + printf("---------------------------------------------------\n"); + for (int i = 0; temp != NULL; i++) { + printf("||%-10s\t\t\t\t%-10d||\n", temp->name, temp->num); + printf("---------------------------------------------------\n"); + temp = temp->next; + } + system("pause"); +} + +// Ñ°ÕÒ»õÎïÔÚ²Ö¿âÖеÄλÖà +int find(nodeList* p, char* name) { + nodeList* temp = p->next; + for (int i = 1; temp != NULL; i++) { + if (!strcmp(name, temp->name)) + return i; + temp = temp->next; + } + return 0; +} + +// È¡³ö»õÎï +nodeList* remove(nodeList* p, char* name, int num, int del) { + nodeList* temp = p; + for (int i = 1; i < del; i++) { + temp = temp->next; + } + if (num == temp->next->num) { + nodeList* c = temp->next; + temp->next = temp->next->next; + printf("ÒÑÈ¡³ö»õÎï....... \n"); + free(c); + } + else if (num < temp->next->num) { + temp->next->num -= num; + printf("ÒÑÈ¡³ö»õÎï....... \n"); + } + else { + printf("error,ûÓÐÄÇô¶à»õÎï......\n"); + } + return p; +} + +// дÈëÎļþ +void fileWrite(nodeList* p) { + FILE* fp = NULL; + nodeList* temp = p->next; + + system("cls"); + if ((fp = fopen("test.txt", "w")) == NULL) { + printf("File failed to open......\n"); + system("pause"); + return; + } + else { + printf("´ò¿ªÎļþ³É¹¦......\n"); + } + while (temp != NULL) { + fprintf(fp, "%s %d\n", temp->name, temp->num); //ÖðÐÐдÈëÎļþ + temp = temp->next; + } + fclose(fp); + printf("±£´æ³É¹¦......\n"); + system("pause"); +} + +//¶ÁÈ¡Îļþ +nodeList* fileRead() { + nodeList* p = (nodeList*)malloc(sizeof(nodeList)); + nodeList* temp = p; + system("cls"); + FILE* fp; + if ((fp = fopen("test.txt", "r")) == NULL) { + printf("File failed to open......\n"); + system("pause"); + return 0; + } + else { + printf("´ò¿ªÎļþ³É¹¦\n"); + } + + nodeList* q[1000]; + int i = 0; + q[i] = (nodeList*)malloc(sizeof(nodeList)); + while (fscanf(fp, "%s %d", q[i]->name, &q[i]->num) != EOF) { //ÖðÐжÁÈ¡Îļþ + temp->next = q[i]; + temp = temp->next; + temp->next = NULL; + i++; + q[i] = (nodeList*)malloc(sizeof(nodeList)); + } + for (int x = 0; x <= i; x++) + free(q[i]); + fclose(fp); + printf("¶ÁÈ¡³É¹¦\n"); + printf("¼´½«´òÓ¡²Ö¿â......\n"); + Sleep(1500); + showList(p); + return p; +} diff --git "a/\345\256\236\351\252\214/assist.cpp" "b/\345\256\236\351\252\214/assist.cpp" new file mode 100644 index 00000000..8ec70743 --- /dev/null +++ "b/\345\256\236\351\252\214/assist.cpp" @@ -0,0 +1,84 @@ +#include "assist.h" + +int gBoard[M_SIZE][M_SIZE]; //ÆåÅÌ + +/******************************************* +Function: is_win + +Description: ´Óµ±Ç°ÏÂÆåλÖÃÅжÏÏÂÆå·½ÊÇ·ñÒѾ­Ó®ÁË + +Input: x->int Æå×Ó×ø±ê + y->int Æå×Ó×ø±ê + chessColor->int µ±Ç°ÏÂÆå·½Æå×ÓµÄÑÕÉ« + +Return: TRUEÓ®ÁË£»FALSEûӮ +********************************************/ +int is_win(int x, int y, int chessColor) { + int i, j, n1 = 0, n2 = 0; + for (i = x, j = y + 1; j < M_SIZE; j++) { //ÍùÓÒÀÛ¼Ó£¬¼ÆËãÓÒ±ßÆå×Ó¸öÊý + if (gBoard[i][j] == chessColor) + n1++; + else + break; + } + for (i = x, j = y; j >= 0; j--) { //Íù×óÀÛ¼Ó£¬¼ÆËã×ó±ßÆå×Ó¸öÊý + if (gBoard[i][j] == chessColor) + n2++; + else + break; + } + if (n1 + n2 >= 5) + return TRUE; + n1 = 0; + n2 = 0; + for (i = x, j = y; i >= 0; i--) { //ÏòÉϼìË÷ + if (gBoard[i][j] == chessColor) + n1++; + else + break; + } + for (i = x + 1, j = y; i < M_SIZE; i++) { //ÏòϼìË÷ + if (gBoard[i][j] == chessColor) + n2++; + else + break; + } + if (n1 + n2 >= 5) + return TRUE; + n1 = 0; + n2 = 0; + for (i = x - 1, j = y + 1; i >= 0 && j < M_SIZE; i--, j++) { //ÏòÓÒÉϼìË÷ + if (gBoard[i][j] == chessColor) + n1++; + else + break; + } + for (i = x, j = y; i < M_SIZE && j >= 0; i++, j--) { //Ïò×óϼìË÷ + if (gBoard[i][j] == chessColor) + n2++; + else + break; + } + if (n1 + n2 >= 5) + return TRUE; + n1 = 0; + n2 = 0; + for (i = x, j = y; i >= 0 && j >= 0; i--, j--) {//Ïò×óÉϼìË÷ + if (gBoard[i][j] == chessColor) + n1++; + else + break; + } + for (i = x + 1, j = y + 1; i < M_SIZE && j < M_SIZE; i++, j++) {//ÏòÓÒϼìË÷ + if (gBoard[i][j] == chessColor) + n2++; + else + break; + } + if (n1 + n2 >= 5) + return TRUE; + return FALSE; +} + + + diff --git "a/\345\256\236\351\252\214/assist.h" "b/\345\256\236\351\252\214/assist.h" new file mode 100644 index 00000000..5b5a42d2 --- /dev/null +++ "b/\345\256\236\351\252\214/assist.h" @@ -0,0 +1,45 @@ +#ifndef _ASSIST_H +#define _ASSIST_H + +#include +#include +#include +#include +#include + +#define M_SIZE 15 //ÆåÅÌ´óСΪ 15 * 15 + +//Æå×Ó״̬ÑÕÉ« +#define SPACE 0 //¿Õ +#define White 1 //°××Ó +#define Black 2 //ºÚ×Ó +#define AIChessColor 1 +#define humChessColor 2 + +extern int gBoard[M_SIZE][M_SIZE]; + +//·ÖÊý¹æ¶¨ +#define WIN5 100000 //5Á¬ +#define ALIVE4 10000 //»îËÄ +#define ALIVE3 1000 //»îÈý +#define DIE4 1000 //ËÀËÄ +#define ALIVE2 100 //»î2 +#define DIE3 100 //ËÀ3 +#define DIE2 10 //ËÀ2 +#define ALIVE1 10 //»î1 + +//Æå×Ó +typedef struct chess_s { + int x = 0; + int y = 0; +}chess_t; + +//¿Õ×ÓµÄÐòÁÐ +typedef struct chess_queue_s { + chess_t chess[M_SIZE * M_SIZE]; + int len = 0; +}chess_queue; + +int is_win(int x, int y, int chessColor); + +#endif \ No newline at end of file diff --git "a/\345\256\236\351\252\214/dPlayers.cpp" "b/\345\256\236\351\252\214/dPlayers.cpp" new file mode 100644 index 00000000..4879ab78 --- /dev/null +++ "b/\345\256\236\351\252\214/dPlayers.cpp" @@ -0,0 +1,58 @@ +#include "dPlayers.h" + +/******************************************* +Function: d_Players + +Description: ÈËÈ˶ÔÕ½Ö÷º¯Êý£¬ºËÐŦÄܾÍÊǶԵ±Ç°ÆåÊÖµÄÊó±êʼþ×ö³öÏìÓ¦£¬Ã¿Ò»´ÎÏÂÆåºó¶¼¼ì²âÊÇ·ñÎå×ÓÁ¬Öé¡£ + Ps£ºÓÉÓÚÕýʽ±ÈÈüÖÐ×ÜÊǺÚÆåÏÈÏ£¬Òò´Ë³õʼ»¯ÆåÊÖΪºÚ·½£¬Ö®ºóÒÀ´Îת»» + +Input: NULL + +Return: ʤÀû·½Æå×ÓÑÕÉ« +********************************************/ +int dPlayers() { + int x, y, i, j, is_find = 0; //is_findÓÃÓÚ¼ì²âÊÇ·ñÕÒµ½ÁËÊó±êµã»÷ʱ¶ÔÓ¦ÆåÅÌÉϵÄλÖà + int player = Black; //ºÚ×ÓÏÈÏÂÆå + MOUSEMSG mouse; + chess_t chess; + RECT r = { 480,250,580,300 }; + LOGFONT f; + gettextstyle(&f); // »ñÈ¡µ±Ç°×ÖÌåÉèÖà + f.lfHeight = 20; // ÉèÖÃ×ÖÌå¸ß¶ÈΪ20 + settextstyle(&f); // ÉèÖÃ×ÖÌåÑùʽ + while (true) { + if (player == Black) + drawtext(_T("ºÚ·½ÏÂÆå"), &r, DT_CENTER); //Ìáʾµ±Ç°»ØºÏÊǺÚÉ«Æå×ÓÏÂÆå + else + drawtext(_T("°×·½ÏÂÆå"), &r, DT_CENTER); //Ìáʾµ±Ç°»ØºÏÊÇ°×É«Æå×ÓÏÂÆå + mouse = GetMouseMsg(); //»ñÈ¡Ò»¸öÊó±êÏûÏ¢ + if (mouse.uMsg == WM_LBUTTONDOWN) { //ÅжÏÊó±ê×ó¼üÊÇ·ñ°´Ï + for (i = 0; i < 15 && is_find != 1; i++) { + for (j = 0; j < 15 && is_find != 1; j++) { + if (abs(mouse.x - (112 + i * 25)) < 12 && abs(mouse.y - (112 + j * 25)) < 12) {//Ñ°ÕÒ¾àÀëÊó±êµã»÷×î½üµÄµã + x = 112 + i * 25; //¶ÔÓ¦µãµÄºá×ø±ê + y = 112 + j * 25; //¶ÔÓ¦µãµÄ×Ý×ø±ê + is_find = 1; + } + } + } + if (is_find == 1) { + i--; //ÓÉÓÚÌø³öforÑ­»»µÄʱºò++ÁË£¬Òò´ËÐèÒª-1µÖÏûÕâ¸öÎó²î + j--; + if(gBoard[j][i]==SPACE) { + if (player == Black) + setfillcolor(RGB(0, 0, 0)); //ÉèÖÃÌî³äÑÕɫΪºÚÉ« + else + setfillcolor(RGB(255, 255, 255)); //ÉèÖÃÌî³äÑÕɫΪ°×É« + solidcircle(x, y, 10); //»æÖÆÆå×Ó + gBoard[j][i] = player; //ÆåÅ̼Ǽµ±Ç°Íæ¼ÒËùϵÄÆå×Ó + clearrectangle(480, 250, 580, 300); + if (is_win(j, i, player)) //ÅжÏÍæ¼ÒÊÇ·ñÒѾ­Ê¤Àû + return player; + player = player % 2 + 1; //»»ÊÖ + } + } + is_find = 0; + } + } +} \ No newline at end of file diff --git "a/\345\256\236\351\252\214/dPlayers.h" "b/\345\256\236\351\252\214/dPlayers.h" new file mode 100644 index 00000000..21fa6ce6 --- /dev/null +++ "b/\345\256\236\351\252\214/dPlayers.h" @@ -0,0 +1,9 @@ +#ifndef _DPLAYERS_H +#define _DPLAYERS_H + +#include "assist.h" +#include "initmap.h" + +int dPlayers(); //ÈËÈ˶ÔÕ½ + +#endif diff --git "a/\345\256\236\351\252\214/hum_VS_AI.cpp" "b/\345\256\236\351\252\214/hum_VS_AI.cpp" new file mode 100644 index 00000000..8f3b4541 --- /dev/null +++ "b/\345\256\236\351\252\214/hum_VS_AI.cpp" @@ -0,0 +1,595 @@ +#include "hum_VS_AI.h" + +/************************************************* +Function: hum_VS_AI + +Description: ÈË»ú¶ÔÕ½¸¨Öúº¯Êý£¬Ö÷ÒªÓÃÓÚÏìÓ¦Íæ¼ÒµÄÊó±êʼþ£¬Í¬Ê±ÔÚÿһ´ÎÍæ¼ÒÏÂÆåºó£¬µ÷ÓÃAI_turnº¯ÊýÈÃAIÏÂÆå¡£ + +Input: null + +Return: ·µ»ØÓÎϷʤÀû·½ +********************************************/ +int hum_VS_AI() { + int x, y, i, j, is_find = 0; //is_findÓÃÓÚ¼ì²âÊÇ·ñÕÒµ½ÁËÊó±êµã»÷ʱ¶ÔÓ¦ÆåÅÌÉϵÄλÖà + int is_first = 1; + MOUSEMSG mouse; + chess_t chess; + RECT r = { 480,250,580,300 }; + LOGFONT f; + gettextstyle(&f); // »ñÈ¡µ±Ç°×ÖÌåÉèÖà + f.lfHeight = 20; // ÉèÖÃ×ÖÌå¸ß¶ÈΪ 20 + settextstyle(&f); // ÉèÖÃ×ÖÌåÑùʽ + setfillcolor(RGB(228, 121, 21)); //ÉèÖð´Å¥ÑÕÉ« + RECT R = { 480, 150, 580, 200 }; + fillrectangle(480, 150, 580, 200); + drawtext(_T("Íæ¼ÒÏÈÊÖ"), &R, DT_CENTER | DT_VCENTER | DT_SINGLELINE); + while (true) { + drawtext(_T("ÄãµÄ»ØºÏ"), &r, DT_CENTER); // ¶ÔÓ¦ÇøÓò¾ÓÖдòÓ¡ÎÄ×Ö + mouse = GetMouseMsg(); //»ñÈ¡Ò»¸öÊó±êÏûÏ¢ + if (mouse.uMsg == WM_LBUTTONDOWN) { //ÅжÏÊó±ê×ó¼üÊÇ·ñ°´Ï + for (i = 0; i < 15 && is_find != 1; i++) { + for (j = 0; j < 15 && is_find != 1; j++) { + if (abs(mouse.x - (112 + i * 25)) < 12 && abs(mouse.y - (112 + j * 25)) < 12) {//Ñ°ÕÒ¾àÀëÊó±êµã»÷×î½üµÄµã + x = 112 + i * 25; + y = 112 + j * 25; + is_find = 1; + } + } + } + if (is_find == 0 && is_first == 1) { + if (abs(mouse.x - 530) < 50 && abs(mouse.y - 175) < 25) { + RECT R = { 480, 150, 580, 200 }; + fillrectangle(480, 150, 580, 200); + drawtext(_T("AIÏÈÊÖ"), &R, DT_CENTER | DT_VCENTER | DT_SINGLELINE); + chess.x = 7, chess.y = 7; + gBoard[chess.x][chess.y] = AIChessColor; + i = 112 + 25 * 7; + j = 112 + 25 * 7; + setfillcolor(RGB(255, 255, 255)); + solidcircle(i, j, 10); //»æÖÆ°×É«Æå×Ó£¨´ú±íAI£© + setfillcolor(RGB(255, 0, 0)); + solidcircle(i, j, 2); //»æÖÆÏÂÆåʾÒâµã + is_first = 0; + } + } + if (is_find == 1) { + i--; //ÓÉÓÚÌø³öforÑ­»»µÄʱºò++ÁË£¬Òò´ËÐèÒª-1µÖÏûÕâ¸öÎó²î + j--; + if(gBoard[j][i]==SPACE) { //ÆåÅÌÉ϶ÔӦλÖÃûÓÐÆå×Ó + if (!is_first) { //Ïû³ý±ê¼Çµã + setfillcolor(RGB(255, 255, 255)); + solidcircle(25 * chess.y + 112, 25 * chess.x + 112, 10); + } + setfillcolor(RGB(0, 0, 0)); + solidcircle(x, y, 10); //»æÖƺÚÉ«Æå×Ó£¨´ú±íÍæ¼Ò£© + gBoard[j][i] = humChessColor; + HRGN del = CreateRectRgn(480, 250, 580, 300); //ɾ³ýÎÄ×ÖÇøÓòµÄÎÄ×Ö + setcliprgn(del); + DeleteObject(del); + clearcliprgn(); + setcliprgn(NULL); + if (is_win(j, i, humChessColor)) { //ÅжÏÍæ¼ÒÊÇ·ñÒѾ­»ñʤ + return humChessColor; + } + chess = AI_turn();//AIÏÂÆå + if (is_win(chess.x, chess.y, AIChessColor)) { //ÅжÏAIÊÇ·ñÒѾ­»ñʤ + return AIChessColor; + } + is_first = 0; + } + is_find = 0; + } + } + } +} + +/************************************************* +Function:AI_turn + +Description: ÂÖµ½AIÏÂÆ壬µ÷ÓÃchess_AI_Negmax_Alphabetaº¯Êý»ñÈ¡AIÏÂÆåµÄ¾ö²ß×ø±ê + ÓÃÓÚ»æÖÆÌáʾÐÅÏ¢ÒÔ¼°Æå×Ó¡¢Ê¾Òâµã + +Input: null + +Return: ·µ»Øÿһ´ÎAI¾ö²ßÏÂÆåµÄλÖÃÐÅÏ¢ +********************************************/ +chess_t AI_turn() { + RECT r = { 480,250,580,300 }; //´òÓ¡ÇøÓò + drawtext(_T("ÂÖµ½AIÏÂÆå"), &r, DT_CENTER); // ¶ÔÓ¦ÇøÓò¾ÓÖдòÓ¡ÎÄ×Ö + chess_t chess; + chess_AI_Negmax_Alphabeta(&chess); //»ñÈ¡AI¾ö²ßÏÂÆåµÄλÖà + int i = 112 + 25 * chess.y, j = 112 + 25 * chess.x; + setfillcolor(RGB(255, 255, 255)); + solidcircle(i, j, 10); //»æÖÆ°×É«Æå×Ó£¨´ú±íAI£© + setfillcolor(RGB(255, 0, 0)); + solidcircle(i, j, 2); //»æÖÆÏÂÆåʾÒâµã + gBoard[chess.x][chess.y] = AIChessColor; // ÆåÅ̼ǼÏÂÆåÐÅÏ¢ + HRGN del = CreateRectRgn(480, 250, 580, 300); //ɾ³ýÎÄ×ÖÇøÓòÎÄ×Ö + setcliprgn(del); + DeleteObject(del); + clearcliprgn(); + setcliprgn(NULL); + return chess; +} + +/************************************************* +Function: chess_AI_Negmax_Alphabeta + +Description: ÂÖµ½AIÏÂÆ壬ÔÚÿһ¸ö¿ÉÒÔÏÂÆåµÄλÖ㨾­³õ²½É¸Ñ¡£©Öоö²ß³ö·ÖÊý×î¸ßµÄλÖà + +Input: ËäÈ»ÊǺ¯ÊýµÄÊäÈ룬µ«Ö÷ÒªÓÃÓڼǼ¾ö²ß³öÀ´µÄ×î¼Ñ×ø±ê£¬×÷Óøü½Ó½üÓÚ·µ»ØÖµ¡£ + +Return: NULL +********************************************/ +void chess_AI_Negmax_Alphabeta(chess_t* chess) { + int i, k = 0; + int x, y, tmp, depth = 4; //ÕâÀï¿ÉÒÔ¸ü¸ÄËÑË÷µÄÉî¶È + int best = -WIN5; //ÕÒµ½µÄ×î¸ß·Ö,³õʼֵ¸³Ò»¸ö×îСֵ + chess_queue option_queue; //´ýÑ¡µÄ¿Õ×Ó¶ÓÁÐ + chess_queue sure_queue; //×îºÏÊʵÄÏÂ×ÓλÖà + option_queue = generate_point(AIChessColor); //Ñ°ÕÒ¿ÉÒÔÏÂ×ӵĵط½ + + for (i = 1; i <= option_queue.len; i++) { //Ò»¸öÒ»¸ö×ÓÂÖ×ÅÊÔÒ»±é + x = option_queue.chess[i].x; + y = option_queue.chess[i].y; + gBoard[x][y] = AIChessColor; //³¢ÊÔÏÂÒ»¸ö×Ó + tmp = negaMax_alphabeta(depth, option_queue.chess[i], -WIN5, WIN5, humChessColor); //»ñµÃ´ËλÖõķÖÊý + if (tmp == best) { //ÕÒµ½Ò»¸öºÍµ±Ç°×î¸ß·ÖÒ»ÑùµÄ·ÖÊý£¬±£´æÏÂÀ´ + sure_queue.chess[k].x = option_queue.chess[i].x; + sure_queue.chess[k].y = option_queue.chess[i].y; + sure_queue.len = k + 1; + k++; + } + else if (tmp > best) { //ÕÒµ½Ò»¸ö±Èµ±Ç°×î¸ß·Ö¸ü¸ßµÄ·ÖÊý£¬Ë¢ÐÂ×î¸ßÖµ + best = tmp; + k = 0; + sure_queue.len = 1; + sure_queue.chess[k].x = option_queue.chess[i].x; + sure_queue.chess[k].y = option_queue.chess[i].y; + k++; + } + gBoard[x][y] = SPACE; //³·ÏúÑ¡Ôñ + } + if (best == -WIN5) { //ÓпÉÄÜÈÎÒâλÖö¼Òª±È×îµÍ·ÖÒªµÍ£¬¼´µ±AI±ØÊäʱ£¬sure_queue¿ÉÄÜûÓмǼ£¬´ËʱËæ»úÑ¡ÔñÒ»¸öµãÏÂÆå + k = (int)(rand() % option_queue.len); + chess->x = option_queue.chess[k].x; + chess->y = option_queue.chess[k].y; + } + else { + k = (int)(rand() % sure_queue.len); //Èç¹ûÓжà¸ö×î¸ß·ÖÊý£¬Ëæ»úÑ¡ÔñÒ»¸ö + chess->x = sure_queue.chess[k].x; + chess->y = sure_queue.chess[k].y; + } +} + +/************************************************* +Function: negaMax_alphabeta + +Description: ºËÐĺ¯Êý£¬¸º¼«´óÖµËÑË÷£¬ÓÃÉî¶ÈÓÅÏȵÄ˼ÏëËÑË÷¸ø¶¨Éî¶Èϵ±Ç°Î»ÖõÄÔ¤¹À·ÖÊý£¬½áºÏalpha_beta¼ôÖ¦ + +Input: int depth : ËÑË÷Éî¶È + chess_t chess : ÉÏÒ»²½³¢ÊÔÂä×ÓµÄÆå×Ó + int alpha : ÓÃÓÚ¼ôÖ¦ + int beta : ÓÃÓÚ¼ôÖ¦ + int player:±¾ÂÖ¶ÔÓ¦Íæ¼Ò + +Return: ·µ»ØËÑË÷µÃµ½µÄÔ¤¹À·ÖÊý +********************************************/ +int negaMax_alphabeta(int depth, chess_t chess, int alpha, int beta, int player) { + int res = evaluate_board(); //µ±Ç°ÆåÅÌ·ÖÊý£¨AI-player£© + int x, y, tempScore; + if ((depth <= 0) || (is_win(chess.x, chess.y, player % 2 + 1))) //Èç¹ûÒѾ­´ïµ½Ä¿±êÉî¶È£¬»òÉÏÒ»²½¶Ô·½ÒѾ­Ê¤Àû + return res; + chess_queue queue; + queue = generate_point(player); //¼Ç¼µ±Ç°¿ÉÒÔÏÂ×ÓµÄλÖà + + for (int i = 1; i <= queue.len; i++) { //ÕâÀï¾ÍÊÇDFSµÄ¹ý³Ì + x = queue.chess[i].x; + y = queue.chess[i].y; + gBoard[x][y] = player; //×ö³öÑ¡Ôñ + tempScore = -negaMax_alphabeta(depth - 1, queue.chess[i], -beta, -alpha, player % 2 + 1); + gBoard[x][y] = SPACE; //³·ÏúÑ¡Ôñ + if (tempScore > alpha) + alpha = tempScore; + if (alpha >= beta) + break; + } + return alpha; +} + +/************************************************* +Function: evaluate_board + +Description: ½«ÆåÅÌÉϵÄλÖ÷ֽâΪÓÉÐС¢ÁС¢Ð±Ïß×é³ÉµÄһάÊý×é. + ÀûÓÃcount_scoreº¯Êý±éÀúһάÊý×飬·Ö±ð½áËãÍæ¼ÒºÍAIµÄ·ÖÊý¡£ + +Input: null + +Return: µ±Ç°Æå¾ÖAI×Ü·ÖÓëÍæ¼Ò×ֵܷIJîÖµ +********************************************/ +int evaluate_board() { + int AIScore = 0, humScore = 0; + int i, j, x, y, n[M_SIZE]; + memset(n, 0, sizeof(n)); + //¼ÆËãËùÓкáÅŵķÖÊýÖ®ºÍ + for (i = 0; i < M_SIZE; i++) { + for (j = 0; j < M_SIZE; j++) + n[j] = gBoard[i][j]; + AIScore += count_score(n, AIChessColor); + humScore += count_score(n, humChessColor); + memset(n, 0, sizeof(n)); + } + //¼ÆËãËùÓÐ×ÝÅŵķÖÊýÖ®ºÍ + for (j = 0; j < M_SIZE; j++) { + for (i = 0; i < M_SIZE; i++) + n[i] = gBoard[i][j]; + AIScore += count_score(n, AIChessColor); + humScore += count_score(n, humChessColor); + memset(n, 0, sizeof(n)); + } + //¼ÆËãËùÓÐÏ°ëÕýбÏß(\)µÄ·ÖÊýÖ®ºÍ + for (i = 0; i < M_SIZE; i++) { + for (x = i, y = 0; x < M_SIZE && y < M_SIZE; x++, y++) + n[y] = gBoard[x][y]; + AIScore += count_score(n, AIChessColor); + humScore += count_score(n, humChessColor); + memset(n, 0, sizeof(n)); + } + //¼ÆËãËùÓÐÉÏ°ëÕýбÏߵķÖÊýÖ®ºÍ + for (j = 1; j < M_SIZE; j++) { + for (x = 0, y = j; y < M_SIZE && x < M_SIZE; x++, y++) + n[x] = gBoard[x][y]; + AIScore += count_score(n, AIChessColor); + humScore += count_score(n, humChessColor); + memset(n, 0, sizeof(n)); + } + //¼ÆËãËùÓÐÉϰ뷴бÏß(/)µÄ·ÖÊýÖ®ºÍ + for (i = 0; i < M_SIZE; i++) { + for (y = i, x = 0; y >= 0 && x < M_SIZE; y--, x++) + n[x] = gBoard[x][y]; + AIScore += count_score(n, AIChessColor); + humScore += count_score(n, humChessColor); + memset(n, 0, sizeof(n)); + } + //¼ÆËãËùÓÐÏ°뷴бÏߵķÖÊýÖ®ºÍ + for (j = 1; j < M_SIZE; j++) { + for (y = j, x = M_SIZE - 1; y < M_SIZE && x >= 0; y++, x--) + n[y - j] = gBoard[x][y]; + AIScore += count_score(n, AIChessColor); + humScore += count_score(n, humChessColor); + memset(n, 0, sizeof(n)); + } + return AIScore - humScore; //·µ»Ø AI-ÈË µÄ·ÖÊý +} + +/******************************************* +Function: generate_point + +Description: Ñ°ÕÒµ±Ç°Æå¾Ö¿ÉÒÔÏÂÆåÇÒ¶ÔÆåÊÖÓÐÀûµÄλÖᣠ+ ÒªÇóÊÇλÖÃΪ¿ÕÇÒÒ»²½Ö®ÄÚÓÐÁÚ¾Ó(ÒªÇóÓÐÁÚ¾ÓÊÇΪÁËʹÏÂÆåµÄµØ·½¸ü½Ó½üÏÖʵ) + Ps£ºÎªÁ˱ãÓÚalpha-beta¼ôÖ¦£¬Ê¹ÓÃÁËÆô·¢Ê½ËÑË÷Ëã·¨£¬ÈôΪɱÆåÔòÖ±½Ó·µ»Ø£¬·ñÔò½øÐдóÖµÄÅÅÐò ¡£ + +Input: int player : µ±Ç°»ØºÏµÄÆåÊÖ + +Return: ·µ»Øµ±Ç°Æå¾Ö¿ÉÒÔÂä×ÓµÄλÖýṹÌ壨½á¹¹ÌåÖаüº¬Ò»¸öÊý×飬¼Ç¼¸÷µãλÖÃÐÅÏ¢£© +********************************************/ +chess_queue generate_point(int player) { + int i, j = 0; + int length; + chess_queue rivalFive; //¶Ô·½³É5 + chess_queue ComFour; //±¾·½»î4 + chess_queue rivalFour; //¶Ô·½»îËÄ + chess_queue ComDoubleThree; //±¾·½Ë«»îÈý + chess_queue rivalDoubleThree; //¶Ô·½Ë«»îÈý + chess_queue ComThree; //±¾·½»îÈý + chess_queue rivalThree; //¶Ô·½»îÈý + chess_queue ComTwo; //±¾·½»î¶þ + chess_queue rivalTwo; //¶Ô·½»î¶þ + chess_queue others; //ÆäÓà + chess_queue queue; //ÓÃÓÚº¯Êý·µ»Ø + + for (i = 0; i < M_SIZE; i++) { + for (j = 0; j < M_SIZE; j++) { + chess_t p; + p.x = i, p.y = j; + if ((gBoard[i][j] == SPACE) && hasNeighbors(i, j)) {//ÓÐÁھӵĿÕ×Ó,×öΪ¿ÉÏÂ×ӵĶÓÁÐ + int ComScore = evaluatePoint(player, p); //±¾·½·ÖÊý + int rivalScore = evaluatePoint(player % 2 + 1, p); //¶Ô·½·ÖÊý + + if (ComScore >= WIN5) { //±¾·½³É5 + queue.len++; + queue.chess[queue.len] = p; + return queue; //±ØɱÆ壬ֱ½Ó·µ»Ø + } + else if (rivalScore >= WIN5) { //¶Ô·½³É5 + rivalFive.len++; + rivalFive.chess[rivalFive.len] = p; //¶Ô·½µÄ±ØɱÆ壬±ØÐë×èµ² + } + else if (ComScore >= ALIVE4) { //±¾·½»îËÄ + ComFour.len++; + ComFour.chess[ComFour.len] = p; + } + else if (rivalScore >= ALIVE4) { //¶Ô·½»îËÄ + rivalFour.len++; + rivalFour.chess[rivalFour.len] = p; + } + else if (ComScore >= 2 * ALIVE3) { //±¾·½Ë«»îÈý + ComDoubleThree.len++; + ComDoubleThree.chess[ComDoubleThree.len] = p; + } + else if (rivalScore >= 2 * ALIVE3) { //¶Ô·½»îÈý + rivalDoubleThree.len++; + rivalDoubleThree.chess[rivalDoubleThree.len] = p; + } + else if (ComScore >= ALIVE3) { //±¾·½»îÈý + ComThree.len++; + ComThree.chess[ComThree.len] = p; + } + else if (rivalScore >= ALIVE3) { //¶Ô·½»îÈý + rivalThree.len++; + rivalThree.chess[rivalThree.len] = p; + } + else if (ComScore >= ALIVE2) { //±¾·½»î¶þ + ComTwo.len++; + ComTwo.chess[ComTwo.len] = p; + } + else if (rivalScore >= ALIVE2) { //¶Ô·½»î¶þ + rivalTwo.len++; + rivalTwo.chess[rivalTwo.len] = p; + } + else { //ÆäÓà + others.len++; + others.chess[others.len] = p; + } + } + } + } + if (rivalFive.len > 0) { + queue = rivalFive; + return queue; + }else if (ComFour.len > 0 || rivalFour.len > 0) { + queue = ComFour; + length = queue.len; + for (int x = 1; x <= rivalFour.len; x++) { + queue.chess[length + x] = rivalFour.chess[x]; + queue.len++; + } + return queue; + }else if (ComDoubleThree.len > 0 || rivalDoubleThree.len > 0) { + queue = ComDoubleThree; + length = queue.len; + for (int x = 1; x <= rivalDoubleThree.len; x++) { + queue.chess[length + x] = rivalDoubleThree.chess[x]; + queue.len++; + } + return queue; + } + //ÒÔÉÏΪɱÆ壬ֱ½Ó·µ»Ø + //ÒÔÏ·DZØɱ£¬Òò´Ë½øÐÐÒ»¸ö´óÖµÄÅÅÐò£¬°´Ë³Ðò´æ·Å½øqueue + queue = ComThree; + length = queue.len; + for (int x = 1; x <= rivalThree.len; x++) { + queue.chess[length + x] = rivalThree.chess[x]; + queue.len++; + } + length = queue.len; + for (int x = 1; x <= ComTwo.len; x++) { + queue.chess[length + x] = ComTwo.chess[x]; + queue.len++; + } + length = queue.len; + for (int x = 1; x <= rivalTwo.len; x++) { + queue.chess[length + x] = rivalTwo.chess[x]; + queue.len++; + } + length = queue.len; + for (int x = 1; x <= others.len; x++) { + queue.chess[length + x] = others.chess[x]; + queue.len++; + } + return queue; +} + +/******************************************* +Function: hasNeighbors + +Description: Åжϵ±Ç°Î»Öà (x,y) s²½Ö®ÄÚÊÇ·ñÓÐ×Ó(²»¹ÜÊǶԷ½µÄ×Ó»¹ÊǼº·½µÄ×Ó£© + +Input: x->int ºá×ø±ê + y->int ×Ý×ø±ê + +Return: TRUE->ÓÐ×Ó £»FALSE->ÎÞ×Ó +********************************************/ +int hasNeighbors(int x, int y) { + int s = 1; + int i, j; + for (i = (x - s > 0 ? x - s : 0); i <= x + s && i < M_SIZE; i++) + for (j = (y - s > 0 ? y - s : 0); j <= y + s && j < M_SIZE; j++) + if ((i != x || j != y) && gBoard[i][j] != SPACE) + return TRUE; + return FALSE; +} + + +/******************************************* +Function: evaluatePoint + +Description: ÆÀ¹Àijһµã¶ÔÓ¦µÄ·ÖÊý¡£×¢£º´Ëº¯ÊýÖ÷ÒªÓÃÓÚÆô·¢Ê½ËÑË÷Ëã·¨£¬ÅжϸõãµÄÀàÐÍ£¨Èç»îÈý¡¢³åËĵȣ© + +Input: int chessColor //Æå×ÓÑÕÉ« + chess_t chess //Æå×Ó×ø±ê + +Return: ÆÀ¹ÀµÃµ½µÄ·ÖÊý +********************************************/ +int evaluatePoint(int chessColor, chess_t chess) { + int x = chess.x, y = chess.y; + int i, j, number = 0, empty = 2, score = 0; + gBoard[x][y] = chessColor; + for (i = x, j = y + 1; j < M_SIZE; j++) { //ÍùÓÒÀÛ¼Ó£¬¼ÆËãÓÒ±ßÆå×Ó¸öÊý + if (gBoard[i][j] == chessColor) + number++; + else { + if (gBoard[i][j] != SPACE) //¶Ô·½Æå×Ó + empty--; + break; + } + } + for (i = x, j = y; j >= 0; j--) { //Íù×óÀÛ¼Ó£¬¼ÆËã×ó±ßÆå×Ó¸öÊý + if (gBoard[i][j] == chessColor) + number++; + else { + if (gBoard[i][j] != SPACE) + empty--; + break; + } + } + score += score_table(number, empty); + + number = 0, empty = 2; + for (i = x, j = y; i >= 0; i--) { //ÏòÉϼìË÷ + if (gBoard[i][j] == chessColor) + number++; + else { + if (gBoard[i][j] != SPACE) + empty--; + break; + } + } + for (i = x + 1, j = y; i < M_SIZE; i++) { //ÏòϼìË÷ + if (gBoard[i][j] == chessColor) + number++; + else { + if (gBoard[i][j] != SPACE) + empty--; + break; + } + } + score += score_table(number, empty); + + number = 0, empty = 2; + for (i = x - 1, j = y + 1; i >= 0 && j < M_SIZE; i--, j++) { //ÏòÓÒÉϼìË÷ + if (gBoard[i][j] == chessColor) + number++; + else { + if (gBoard[i][j] != SPACE) + empty--; + break; + } + } + for (i = x, j = y; i < M_SIZE && j >= 0; i++, j--) { //Ïò×óϼìË÷ + if (gBoard[i][j] == chessColor) + number++; + else { + if (gBoard[i][j] != SPACE) + empty--; + break; + } + } + score += score_table(number, empty); + + number = 0, empty = 2; + for (i = x, j = y; i >= 0 && j >= 0; i--, j--) {//Ïò×óÉϼìË÷ + if (gBoard[i][j] == chessColor) + number++; + else { + if (gBoard[i][j] != SPACE) + empty--; + break; + } + } + for (i = x + 1, j = y + 1; i < M_SIZE && j < M_SIZE; i++, j++) {//ÏòÓÒϼìË÷ + if (gBoard[i][j] == chessColor) + number++; + else { + if (gBoard[i][j] != SPACE) + empty--; + break; + } + } + score += score_table(number, empty); + gBoard[x][y] = SPACE; + return score; +} + + +/************************************************* +Function: count_score + +Description: ¼ÆËãÕâÒ»ÐС¢ÁС¢Ð±ÏßµÄÁ¬×ÓÊý¡¢¿ÕλÊý£¬Ã¿ÕÒµ½Ò»´®Á¬×Ӿ͸ù¾Ýµ÷ÓÃscore_tableº¯Êý¼ÆËã·ÖÊý£¬×îºóÇó³ö·ÖÊý×ܺͣ¬´ú±í¸ÃÐС¢ÁС¢Ð±ÏߵķÖÊý¡£ + +Input: int n[] : ´ú±íÿһÐС¢ÁС¢Ð±ÏßµÄһάÊý×é + int chessColor : ±¾»ØºÏÆåÊÖµÄÆå×ÓÑÕÉ« + +Return: ÕâÒ»×飨һάÊý×飩µÄ·ÖÊý×ÜºÍ +********************************************/ +int count_score(int n[], int chessColor) { + int i = 1; //n[0]ÔÚÏÂÃæ×÷Ϊ³õʼµãµ¥¶À¼ÆËã + int empty = 0; //¿ÕµÄλ×Ó + int number = 0; //Á¬×ӵĸöÊý + int scoretmp = 0; //¼ÆËã·ÖÊý×ÜºÍ + + if (n[0] == SPACE) //λÖÃΪ¿Õ + empty++; + else if (n[0] == chessColor) //±¾·½Æå×Ó + number++; + + while (i < M_SIZE) { + if (n[i] == chessColor) //±¾·½Æå×Ó + number++; + else if (n[i] == SPACE) { //λÖÃΪ¿Õ£¬·ÖÁ½ÖÖÇé¿öÀ´ÌÖÂÛ + if (number == 0) //ÉÏÒ»¸öλÖÃҲΪ¿Õ + empty = 1; + else { //ÉÏÒ»¸öλÖÃΪ±¾·½Æå×Ó + scoretmp += score_table(number, empty + 1); + empty = 1; + number = 0; + } + } + else {//¶Ô·½Æå×Ó + scoretmp += score_table(number, empty); + empty = 0; + number = 0; + } + i++; + } + scoretmp += score_table(number, empty); + return scoretmp; //·µ»Ø±¾×é·ÖÊý×ÜºÍ +} + +/************************************************* +Function: score_table + +Description: ¸ù¾ÝÁ¬×Ó¸öÊýºÍÁ½¶ËµÄ¿Õ×Ó¸öÊý¼ÆËã·ÖÊý + +Input: number->Á¬×ӵĸöÊý + empty->¿Õ×ӵĸöÊý + +Return: ·ÖÊý +********************************************/ +int score_table(char number, char empty) { + if (number >= 5) { + return WIN5; + } + else if (number == 4) { + if (empty == 2) + return ALIVE4; + else if (empty == 1) + return DIE4; + } + else if (number == 3) { + if (empty == 2) + return ALIVE3; + else if (empty == 1) + return DIE3; + } + else if (number == 2) { + if (empty == 2) + return ALIVE2; + else if (empty == 1) + return DIE2; + } + else if (number == 1 && empty == 2) + return ALIVE1; + else + return 0; +} \ No newline at end of file diff --git "a/\345\256\236\351\252\214/hum_VS_AI.h" "b/\345\256\236\351\252\214/hum_VS_AI.h" new file mode 100644 index 00000000..ece23d06 --- /dev/null +++ "b/\345\256\236\351\252\214/hum_VS_AI.h" @@ -0,0 +1,18 @@ +#ifndef _HUM_VS_AI_H +#define _HUM_VS_AI_H + +#include "initmap.h" +#include "assist.h" + +int hum_VS_AI(); +int hasNeighbors(int x, int y); +int negaMax_alphabeta(int depth, chess_t chess, int alpha, int beta, int player); +void chess_AI_Negmax_Alphabeta(chess_t* chess); +int evaluatePoint(int chessColor, chess_t chess); +int count_score(int n[], int chessColor); +int score_table(char number, char empty); +int evaluate_board(); +chess_queue generate_point(int player); +chess_t AI_turn(); + +#endif diff --git "a/\345\256\236\351\252\214/initmap.cpp" "b/\345\256\236\351\252\214/initmap.cpp" new file mode 100644 index 00000000..da419570 --- /dev/null +++ "b/\345\256\236\351\252\214/initmap.cpp" @@ -0,0 +1,81 @@ +#include "initmap.h" + +/************************************************* +Function: initmap + +Description: »ùÓÚEasyXͼÐο⣬ÓÃÓÚ³õʼ»¯»æÖÆÆåÅÌ£¬°üÀ¨ÆåÅÌ¡¢×ø±êÖá¡¢ÌìÔªµã + +Input: null + +Return: null +********************************************/ +void initmap() { + char letter[] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O' }; + TCHAR s[5];//ÓÃÓÚ´òÓ¡ÆåÅ̱ßÑصÄÊý×Ö + LOGFONT f; + gettextstyle(&f); // »ñÈ¡µ±Ç°×ÖÌåÉèÖà + f.lfHeight = 20; // ÉèÖÃ×ÖÌå¸ß¶ÈΪ20 + settextstyle(&f); // ÉèÖÃ×ÖÌåÑùʽ + cleardevice(); // Ó¦Óñ³¾°É« + setlinestyle(PS_SOLID, 1); //»­ÊµÏß ¿í¶È2 + for (int i = 0; i < 13; i++) { + line(112, 137 + i * 25, 462, 137 + i * 25); //»­ÆåÅÌÉϵĺáÏß + line(137 + i * 25, 112, 137 + i * 25, 462); //»­ÆåÅÌÉϵÄÊúÏß + } + for (int i = 0; i < 15; i++) { + RECT r = { 87,100 + i * 25,112,125 + 25 * i }; //´òÓ¡Êý×ÖµÄÇøÓò + RECT m = { 99.5 + i * 25,462,124 + i * 25,487 }; //´òÓ¡×ÖĸµÄÇøÓò + _stprintf(s, _T("%d"), i + 1); //½«Êý×Öת»»³É×Ö·û´® + drawtext(s, &r, DT_CENTER); // ¶ÔÓ¦ÇøÓò¾ÓÖдòÓ¡Êý×Ö + drawtext(letter[i], &m, DT_CENTER); //¶ÔÓ¦ÇøÓò¾ÓÖдòÓ¡×Öĸ + } + setlinestyle(PS_SOLID, 2); //»­ÊµÏß ¿í¶È2 + setfillcolor(RGB(0, 0, 0)); //ÉèÖÃÌî³äÑÕɫΪºÚÉ« + line(112, 112, 112, 462); //ÆåÅÌ×ó²à±ß½ç + line(462, 112, 462, 462); //ÆåÅÌÓÒ²à±ß½ç + line(112, 112, 462, 112); //ÆåÅÌÉÏ·½±ß½ç + line(112, 462, 462, 462); //ÆåÅÌÏ·½±ß½ç + solidcircle(187, 187, 2); //»æÖÆÌìÔªµã + solidcircle(187, 387, 2); + solidcircle(287, 287, 2); + solidcircle(387, 187, 2); + solidcircle(387, 387, 2); +} + +/************************************************* +Function: drawButton + +Description: »ùÓÚEasyXͼÐο⣬ÓÃÓÚ»æÖÆÓÎÏ·±êÌâÒÔ¼°3¸ö°´Å¥ + +Input: NULL + +Return: NULL +********************************************/ +void drawButton() { + int nR1[] = { 225, 175, 375, 225 }; + int nR2[] = { 225, 300, 375, 350 }; + int nR3[] = { 225, 425, 375, 475 }; + RECT R1 = { nR1[0], nR1[1], nR1[2], nR1[3] }; //¾ØÐÎÖ¸ÕëR1 + RECT R2 = { nR2[0], nR2[1], nR2[2], nR2[3] }; //¾ØÐÎÖ¸ÕëR2 + RECT R3 = { nR3[0], nR3[1], nR3[2], nR3[3] }; //¾ØÐÎÖ¸ÕëR3 + setfillcolor(RGB(228, 121, 21)); //ÉèÖð´Å¥ÑÕÉ« + setlinecolor(RGB(0, 0, 0)); //ÉèÖÃÏßÌõÑÕÉ« + setlinestyle(PS_SOLID, 2); //ÉèÖÃÏßÌõÊôÐÔΪʵÏß ¿í¶È2 + fillrectangle(nR1[0], nR1[1], nR1[2], nR1[3]); //»æÖÆ°´Å¥ + fillrectangle(nR2[0], nR2[1], nR2[2], nR2[3]); + fillrectangle(nR3[0], nR3[1], nR3[2], nR3[3]); + LOGFONT f; + gettextstyle(&f); // »ñÈ¡µ±Ç°×ÖÌåÉèÖà + f.lfHeight = 30; // ÉèÖÃ×ÖÌå¸ß¶ÈΪ 30 + _tcscpy_s(f.lfFaceName, _T("¿¬Ìå")); // ÉèÖÃ×ÖÌåΪ¡°¿¬Ì塱 + f.lfQuality = ANTIALIASED_QUALITY; // ÉèÖÃÊä³öЧ¹ûΪ¿¹¾â³Ý + settextstyle(&f); // ÉèÖÃ×ÖÌåÑùʽ + setbkmode(TRANSPARENT); // È¥µôÎÄ×Ö±³¾° + settextcolor(BLACK); // ÉèÖÃÎÄ×ÖÑÕɫΪºÚÉ« + drawtext(_T("ÈË»ú¶ÔÕ½"), &R1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//ÔÚ¾ØÐÎÇøÓòR1ÄÚ´òÓ¡ÎÄ×Ö£¬Ë®Æ½¾ÓÖУ¬´¹Ö±¾ÓÖУ¬µ¥ÐÐÏÔʾ + drawtext(_T("ÈËÈ˶ÔÕ½"), &R2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//ÔÚ¾ØÐÎÇøÓòR2ÄÚ´òÓ¡ÎÄ×Ö£¬Ë®Æ½¾ÓÖУ¬´¹Ö±¾ÓÖУ¬µ¥ÐÐÏÔʾ + drawtext(_T("Í˳öÓÎÏ·"), &R3, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//ÔÚ¾ØÐÎÇøÓòR3ÄÚ´òÓ¡ÎÄ×Ö£¬Ë®Æ½¾ÓÖУ¬´¹Ö±¾ÓÖУ¬µ¥ÐÐÏÔʾ + f.lfHeight = 45; //ÉèÖÃ×ÖÌå¸ß¶ÈΪ45 + settextstyle(&f); //ÉèÖÃ×ÖÌåÑùʽ + outtextxy(150, 75, _T("ÌìÌìÎå×ÓÆåV1.0")); //´òÓ¡±êÌâ +} \ No newline at end of file diff --git "a/\345\256\236\351\252\214/initmap.h" "b/\345\256\236\351\252\214/initmap.h" new file mode 100644 index 00000000..3a214710 --- /dev/null +++ "b/\345\256\236\351\252\214/initmap.h" @@ -0,0 +1,10 @@ +#ifndef _INITMAP_H_ +#define _INITMAP_H_ + +#include +#include "assist.h" + +void drawButton(); +void initmap(); + +#endif \ No newline at end of file diff --git "a/\345\256\236\351\252\214/menu.cpp" "b/\345\256\236\351\252\214/menu.cpp" new file mode 100644 index 00000000..6ee14fa1 --- /dev/null +++ "b/\345\256\236\351\252\214/menu.cpp" @@ -0,0 +1,69 @@ +#include "menu.h" +/************************************************* +Function: Menu + +Description: Ö÷²Ëµ¥£¬µ÷ÓÃdrawButtonº¯Êý»æÖƲ˵¥ºóÓÃÓÚ¼ì²âÊó±êʼþ²¢¸øÓèÏìÓ¦ + +Input: NULL + +Return: NULL +********************************************/ +void Menu() { + setbkcolor(RGB(216, 150, 84)); //ÉèÖñ³¾°É« + cleardevice(); //Ó¦Óñ³¾°É« + drawButton(); //»æÖÆ°´Å¥×é¼þÒÔ¼°±êÌâµÈ + MOUSEMSG mouse; //Êó±êÖ¸Õ룬ÓÃÓÚ¼ì²âÊó±ê¶¯×÷£¬°´Å¥ÏìÓ¦ + int button = 0, winner; + while (true) { + mouse = GetMouseMsg(); //»ñÈ¡Ò»ÌõÊó±êÏûÏ¢ + if (mouse.uMsg == WM_LBUTTONDOWN) { //Êó±ê×ó¼üµã»÷ + for (int i = 0; i < 3; i++) { //Åжϰ´µÄÊÇÄÄÒ»¸ö°´Å¥ + if (abs(mouse.x - 300) < 50 && abs(mouse.y - (200 + 125 * i) < 25)) { + button = i + 1; + break; + } + } + switch (button) { + case 1: //ÈË»ú¶ÔÕ½ + initmap(); //³õʼ»¯µØͼ + winner = hum_VS_AI(); //hum_VS_AI ·µ»ØÓÎϷʤÀû·½ + if (winner == AIChessColor) { //AI»ñµÃʤÀû + RECT r = { 480,300,580,350 }; + drawtext(_T("°Ü±±!"), &r, DT_CENTER); + } + else if (winner == humChessColor) { //Íæ¼Ò»ñµÃʤÀû + RECT r = { 480,300,580,350 }; + drawtext(_T("ÄãÓ®ÁË!"), &r, DT_CENTER); + } + for (int i = 0; i < M_SIZE; i++) { + for (int j = 0; j < M_SIZE; j++) + gBoard[i][j] = SPACE; //ÓÎÏ·½áÊø£¬ÖØÖÃÆåÅÌ + } + _getch(); + return; + break; + case 2: //ÈËÈ˶ÔÕ½ + initmap();//³õʼ»¯µØͼ + winner = dPlayers(); //dPlayers·µ»ØÓÎϷʤÀû·½ + if (winner == Black) { //ºÚ·½»ñʤ + RECT r = { 480,300,580,350 }; + drawtext(_T("ºÚ·½Ê¤Àû!"), &r, DT_CENTER); + } + else if (winner == White) { + RECT r = { 480,300,580,350 }; //°×·½»ñʤ + drawtext(_T("°×·½Ê¤Àû!"), &r, DT_CENTER); + } + for (int i = 0; i < M_SIZE; i++) { + for (int j = 0; j < M_SIZE; j++) + gBoard[i][j] = SPACE; //ÓÎÏ·½áÊø£¬ÖØÖÃÆåÅÌ + } + _getch(); + return; + break; + case 3: //ÓÎÏ·½áÊø + closegraph(); + exit(0); + } + } + } +} \ No newline at end of file diff --git "a/\345\256\236\351\252\214/menu.h" "b/\345\256\236\351\252\214/menu.h" new file mode 100644 index 00000000..b41f09e3 --- /dev/null +++ "b/\345\256\236\351\252\214/menu.h" @@ -0,0 +1,11 @@ +#ifndef _MENU_H +#define _MENU_H + +#include "initmap.h" +#include "assist.h" +#include "hum_VS_AI.h" +#include "dPlayers.h" + +void Menu(); + +#endif \ No newline at end of file diff --git "a/\345\256\236\351\252\214/resource.h" "b/\345\256\236\351\252\214/resource.h" new file mode 100644 index 00000000..bb84da6f --- /dev/null +++ "b/\345\256\236\351\252\214/resource.h" @@ -0,0 +1,14 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by go-bang.rc + +// жÔÏóµÄÏÂÒ»×éĬÈÏÖµ +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git "a/\345\256\236\351\252\214/resource1.h" "b/\345\256\236\351\252\214/resource1.h" new file mode 100644 index 00000000..bb84da6f --- /dev/null +++ "b/\345\256\236\351\252\214/resource1.h" @@ -0,0 +1,14 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by go-bang.rc + +// жÔÏóµÄÏÂÒ»×éĬÈÏÖµ +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git "a/\345\256\236\351\252\214/\346\272\220.cpp" "b/\345\256\236\351\252\214/\346\272\220.cpp" new file mode 100644 index 00000000..15b0f99d --- /dev/null +++ "b/\345\256\236\351\252\214/\346\272\220.cpp" @@ -0,0 +1,7 @@ +#include "menu.h" + +void main() { + initgraph(600, 600); // ³õʼ»¯´°¿Ú + while (true) + Menu(); +} \ No newline at end of file