本文实例讲述了基于C语言实现五子棋游戏的方法,代码备有比较完整的注释,可以帮助读者更好的加以理解。
五子棋游戏代码如下:
#include#include #include #include #include #define ESC 0x11b #define UP 0x4800 #define DOWN 0x5000 #define LEFT 0x4b00 #define RIGHT 0x4d00 #define BLANK 0x3920 #define PLAYER1 1 #define PLAYER2 2 #define COMPUTER 2 #define LENGTH 15 #define SEARCH_DEEP 2 #define STEP 1 intx1 = 240, y1 = 240, oldx = 240, oldy = 240; intkey_mode; intkey_net; intstep_sum = 0; intchessman[LENGTH][LENGTH]; intdepth = 2; inta = 0, b = 0; intflag_run; intwin_flag = 0; typedef struct five_chess *point; struct five_chess { intx; inty; intlayer; double value; double score; intchess[LENGTH][LENGTH]; intrecord[LENGTH][LENGTH]; } A; intstack_deep0 = 0; point stack_c[10]; point close[600]; void push(point s0) { if (stack_deep0 < 10) stack_c[stack_deep0++] = s0; } point top() { if (stack_deep0 > 0) return stack_c[stack_deep0 - 1]; } void pop() { if (stack_deep0 > 0) stack_deep0--; } int is_empty() { if (stack_deep0 != 0) return 1; else return 0; } void five(); void show(); intwin_or_not(int x0, int y0, int who, int chessman[LENGTH][LENGTH], int a); void set_chessman(); void print_result(); double score_row(int i, int j, int chessman[LENGTH][LENGTH]); double score_col(int i, int j, int chessman[LENGTH][LENGTH]); double score_diag_45(int i, int j, int chessman[LENGTH][LENGTH]); double score_diag_135(int i, int j, int chessman[LENGTH][LENGTH]); double total_score(int who_running, int chessman[LENGTH][LENGTH]); double score(int chessman[LENGTH][LENGTH]); introwdt(int i, int j, int chessman[LENGTH][LENGTH]); intcoldt(int i, int j, int chessman[LENGTH][LENGTH]); intdiadt(int i, int j, int chessman[LENGTH][LENGTH]); intvdiadt(int i, int j, int chessman[LENGTH][LENGTH]); intcan_expand(int i, int j, int chessman[LENGTH][LENGTH]); void copy(point s1, point s0); int POW(int s, int t) { intsum = s, i; if (t <= 0) return 1; for (i = 0; i < t; i++) sum *= sum; return sum; } point expand(point s0) { intflag; inti, j; point new_chess = (point) malloc(sizeof(struct five_chess)); for (i = 0; i < LENGTH; i++) for (j = 0; j < LENGTH; j++) new_chess->chess[i][j] = s0->chess[i][j]; for (i = 0; i < LENGTH; i++) for (j = 0; j < LENGTH; j++) new_chess->record[i][j] = s0->chess[i][j]; if (s0->layer % 2 == 0) flag = COMPUTER; else flag = PLAYER1; for (i = 0; i < LENGTH; i++) for (j = 0; j < LENGTH; j++) { if (s0->record[i][j]) continue; if (can_expand(i, j, s0->chess) == 0) continue; s0->record[i][j] = flag; new_chess->chess[i][j] = flag; new_chess->layer = s0->layer + 1; new_chess->x = i; new_chess->y = j; new_chess->record[i][j] = flag; return new_chess; } new_chess->layer = -1; return new_chess; } void computer() { inti, j, k, num = 0; intbreak_now = 0; intbreak_then = 0; intgo_on = 0; point s0 = NULL, s1 = NULL, s2 = NULL, max_chess = NULL; point temps = NULL, s01; stack_deep0 = 0; s0 = malloc(sizeof(struct five_chess)); for (i = 0; i < 600; i++) close[i] = NULL; close[num++] = s0; for (i = 0; i < LENGTH; i++) for (j = 0; j < LENGTH; j++) { s0->chess[i][j] = chessman[i][j]; s0->record[i][j] = chessman[i][j]; } s0->layer = 0; s0->value = -3000000; s0->score = -3000000; push(s0); while (is_empty() != 0) { s01 = top(); s1 = expand(s01); close[num++] = s1; if (s1->layer == -1) { pop(); continue; } go_on = win_or_not((s1->x + 1) * 30, (s1->y + 1) * 30, 2, s1->chess, 1); if (go_on == 2) { a = (s1->x + 1) * 30; b = (s1->y + 1) * 30; break_then = 1; break; } go_on = win_or_not((s1->x + 1) * 30, (s1->y + 1) * 30, 1, s1->chess, 1); if (go_on == 1) { a = (s1->x + 1) * 30; b = (s1->y + 1) * 30; break_then = 1; break; } s1->value = 30000; push(s1); while (1) { s1 = top(); s2 = expand(s1); if (s2->layer == -1) { pop(); if (s1->value > top()->value) { top()->value = s1->value; max_chess = s1; } free(s2); break; } s2->score = score(s2->chess); temps = top(); if (s2->score < temps->value) temps->value = s2->score; free(s2); } } if (break_then == 0) { for (i = 0; i < LENGTH; i++) { for (j = 0; j < LENGTH; j++) if (max_chess->chess[i][j] != chessman[i][j]) { a = i * 30 + 30; b = j * 30 + 30; break_now = 1; break; } if (break_now == 1) break; } } for (i = 0; i < 600; i++) { if (close[i] == NULL) continue; free(close[i]); } } void main() { intkey; intplay_with_who = 1; printf("1.Play with humann2.Play with computernPlease choice: "); scanf("%d", &play_with_who); five(); show(); if (play_with_who == 1) { while (1) { settextstyle(0, 0, 2); if ((step_sum + 1) % 2) { setcolor(1); outtextxy(500, 180, "Player2"); setcolor(4); outtextxy(500, 180, "Player1"); } else { setcolor(1); outtextxy(500, 180, "Player1"); setcolor(10); outtextxy(500, 180, "Player2"); } if (bioskey(1)) { key = bioskey(0); switch (key) { case ESC: exit(0); case LEFT: if (x1 > 30) { x1 -= 30; show(); } break; case UP: if (y1 > 30) { y1 -= 30; show(); } break; case RIGHT: if (x1 < 450) { x1 += 30; show(); } break; case DOWN: if (y1 < 450) { y1 += 30; show(); } break; case BLANK: { if (chessman[x1 / 30][y1 / 30]) break; step_sum++; if (step_sum % 2) { setcolor(15); setfillstyle(SOLID_FILL, 15); circle(x1, y1, 10); floodfill(x1, y1, 15); chessman[x1 / 30][y1 / 30] = PLAYER1; win_flag = win_or_not(x1, y1, 1, chessman, 0); if (win_flag == 1) outtextxy(480, 240, "P1 Win"); else if (win_flag == 3) outtextxy(480, 240, "DOGFALL"); if (win_flag != 0) { while (bioskey(1) == 0); closegraph(); } } else { setcolor(12); setfillstyle(SOLID_FILL, 12); circle(x1, y1, 10); floodfill(x1, y1, 12); chessman[x1 / 30][y1 / 30] = PLAYER2; win_flag = win_or_not(x1, y1, 2, chessman, 0); if (win_flag == 2) outtextxy(480, 240, "P2 Win"); else if (win_flag == 3) outtextxy(480, 240, "DOGFALL"); if (win_flag != 0) { while (bioskey(1) == 0); closegraph(); } } } break; } } } } else { chessman[7][7] = COMPUTER; setcolor(12); setfillstyle(SOLID_FILL, 12); circle(240, 240, 10); floodfill(240, 240, 12); flag_run = 0; step_sum++; while (1) { while (1) { if (flag_run == 1) break; if (bioskey(1)) { key = bioskey(0); switch (key) { case ESC: exit(0); case LEFT: if (x1 > 30) { x1 -= 30; show(); } break; case UP: if (y1 > 30) { y1 -= 30; show(); } break; case RIGHT: if (x1 < 450) { x1 += 30; show(); } break; case DOWN: if (y1 < 450) { y1 += 30; show(); } break; case BLANK: { if (chessman[x1 / 30 - 1][y1 / 30 - 1]) break; setcolor(15); setfillstyle(SOLID_FILL, 15); circle(x1, y1, 10); floodfill(x1, y1, 15); chessman[x1 / 30 - 1][y1 / 30 - 1] = PLAYER1; flag_run = 1; step_sum++; win_flag = win_or_not(x1, y1, 1, chessman, 0); if (win_flag == 1) outtextxy(480, 240, "P1 Win"); else if (win_flag == 3) outtextxy(480, 240, "DOGFALL"); if (win_flag != 0) { while (bioskey(1) == 0); closegraph(); } } } } } computer(); setcolor(12); setfillstyle(SOLID_FILL, 12); circle(a, b, 10); floodfill(a, b, 12); chessman[a / 30 - 1][b / 30 - 1] = COMPUTER; flag_run = 0; step_sum++; win_flag = win_or_not(a, b, 2, chessman, 0); if (win_flag == 2) outtextxy(480, 240, "ComputerWin"); else if (win_flag == 3) outtextxy(480, 240, "DOGFALL"); if (win_flag != 0) { while (bioskey(1) == 0); closegraph(); } } } } void five() { inti, j; intgdriver = DETECT, gmode; registerbgidriver(EGAVGA_driver); initgraph(&gdriver, &gmode, " "); setbkcolor(1); for (i = 0; i < 30; i++) { setcolor((i >= 2) ? 9 : i); rectangle(i, i, 479 - i, 479 - i); } for (i = 1; i < 14; i++) for (j = 1; j < 14; j++) { setcolor(14); line(30 + 30 * i, 30, 30 + 30 * i, 449); line(30, 30 + 30 * i, 449, 30 + 30 * i); } for (i = 0; i < 15; i++) { setcolor(i); rectangle(i, i, 640 - i, 480 - i); line(480 - i, 15, 480 - i, 465); } setcolor(4); settextstyle(0, 0, 2); outtextxy(500, 45, "GOBANG"); setcolor(10); settextstyle(0, 0, 1); outtextxy(500, 90, "Designed By"); outtextxy(514, 118, "Ye Binbin"); outtextxy(480, 140, "from class A of CS"); } void show() { setcolor(1); if (oldx < 450) { if (oldy > 30) line(oldx + 7, oldy - 15, oldx + 15, oldy - 15); if (oldy > 30) line(oldx + 15, oldy - 15, oldx + 15, oldy - 7); if (oldy < 450) line(oldx + 15, oldy + 7, oldx + 15, oldy + 15); if (oldy < 450) line(oldx + 15, oldy + 15, oldx + 7, oldy + 15); } if (oldx > 30) { if (oldy < 450) line(oldx - 7, oldy + 15, oldx - 15, oldy + 15); if (oldy < 450) line(oldx - 15, oldy + 15, oldx - 15, oldy + 7); if (oldy > 30) line(oldx - 15, oldy - 7, oldx - 15, oldy - 15); if (oldy > 30) line(oldx - 15, oldy - 15, oldx - 7, oldy - 15); } setcolor(12); if (x1 < 450) { if (y1 > 30) line(x1 + 7, y1 - 15, x1 + 15, y1 - 15); if (y1 > 30) line(x1 + 15, y1 - 15, x1 + 15, y1 - 7); if (y1 < 450) line(x1 + 15, y1 + 7, x1 + 15, y1 + 15); if (y1 < 450) line(x1 + 15, y1 + 15, x1 + 7, y1 + 15); } if (x1 > 30) { if (y1 < 450) line(x1 - 7, y1 + 15, x1 - 15, y1 + 15); if (y1 < 450) line(x1 - 15, y1 + 15, x1 - 15, y1 + 7); if (y1 > 30) line(x1 - 15, y1 - 7, x1 - 15, y1 - 15); if (y1 > 30) line(x1 - 15, y1 - 15, x1 - 7, y1 - 15); } oldx = x1; oldy = y1; } void set_chessman() { inti, j; for (i = 0; i < 15; i++) for (j = 0; j < 15; j++) chessman[i][j] = 0; } int win_or_not(int x0, int y0, int who, int chessman[LENGTH][LENGTH], int a) { inti = x0 / 30 - 1, j = y0 / 30 - 1; intwho_run = who; intline_sum = -1; inttmp_i = i, tmp_j = j; intc; if (a == 1) { c = chessman[i][j]; chessman[i][j] = who_run; } while (1) { while (tmp_i >= 0 && line_sum != 4) { if (chessman[tmp_i--][j] == who_run) line_sum++; else break; } if (line_sum == 4) line_sum++; tmp_i = i; while (tmp_i <= 15 && line_sum != 5) { if (chessman[tmp_i++][j] == who_run) line_sum++; else break; } if (line_sum == 5) { if (a == 1) chessman[i][j] = c; return who_run; } line_sum = -1; tmp_i = i; break; } while (1) { while (tmp_j >= 0 && line_sum != 4) { if (chessman[i][tmp_j--] == who_run) line_sum++; else break; } if (line_sum == 4) line_sum++; tmp_j = j; while (tmp_j <= 15 && line_sum != 5) { if (chessman[i][tmp_j++] == who_run) line_sum++; else break; } if (line_sum == 5) { if (a == 1) chessman[i][j] = c; return who_run; } line_sum = -1; tmp_j = j; break; } while (1) { while (line_sum != 4 && tmp_i <= 15 && tmp_j >= 0) { if (chessman[tmp_i++][tmp_j--] == who_run) line_sum++; else break; } tmp_i = i; tmp_j = j; if (line_sum == 4) line_sum++; while (line_sum != 5 && tmp_i >= 0 && tmp_j <= 15) { if (chessman[tmp_i--][tmp_j++] == who_run) line_sum++; else break; } if (line_sum == 5) { if (a == 1) chessman[i][j] = c; return who_run; } tmp_i = i; tmp_j = j; line_sum = -1; break; } while (1) { while (line_sum != 4 && tmp_i >= 0 && tmp_j >= 0) { if (chessman[tmp_i--][tmp_j--] == who_run) line_sum++; else break; } tmp_i = i; tmp_j = j; if (line_sum == 4) line_sum++; while (line_sum != 5 && tmp_i <= 15 && tmp_j <= 15) { if (chessman[tmp_i++][tmp_j++] == who_run) line_sum++; else break; } if (line_sum == 5) { if (a == 1) chessman[i][j] = c; return who_run; } break; } if (step_sum == 225) { if (a == 1) chessman[i][j] = c; return 3; } if (a == 1) chessman[i][j] = c; return 0; } double score_row(int i, int j, int chessman[LENGTH][LENGTH]) { intsum_chessmen = 0; double score = 0; intmid_j; intwho_running = chessman[i][j]; if (j == LENGTH) { while (chessman[i][j] == who_running) { j--; sum_chessmen++; } if (sum_chessmen >= 5) score = 200000; else { if (chessman[i][j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } } else { while (chessman[i][j] == who_running && j != LENGTH) { j++; sum_chessmen++; } mid_j = j; j = j - sum_chessmen - 1; while (chessman[i][j] == who_running && j != -1) { j--; sum_chessmen++; } if (j >= 0 && mid_j < LENGTH) { if (chessman[i][j] == 0 && chessman[i][mid_j] == 0) score = 18000 / POW(50, 4 - sum_chessmen); else if ((chessman[i][j] != 0 && chessman[i][mid_j] == 0) || (chessman[i][j] == 0 && chessman[i][mid_j] != 0)) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } if (j < 0 && mid_j < LENGTH) { if (chessman[i][mid_j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } if (j >= 0 && mid_j >= LENGTH) { if (chessman[i][j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } if (j < 0 && mid_j >= LENGTH) score = 0; } return score; } double score_col(int i, int j, int chessman[LENGTH][LENGTH]) { intsum_chessmen = 0, mid_i; double score = 0; intwho_running = chessman[i][j]; if (i == LENGTH) { while (chessman[i][j] == who_running) { i--; sum_chessmen++; } if (sum_chessmen >= 5) score = 200000; if (chessman[i][j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } else { while (chessman[i][j] == who_running) { i++; sum_chessmen++; } mid_i = i; if (i == LENGTH || chessman[i][j] != who_running) { i = i - sum_chessmen; while (chessman[i - 1][j] == who_running) { i--; sum_chessmen++; } if (i >= 0) { if (chessman[i][j] == 0 && chessman[mid_i][j] == 0) score = 18000 / POW(50, 4 - sum_chessmen); else if ((chessman[i][j] != 0 && chessman[mid_i][j]) == 0 || (chessman[i][j] == 0 && chessman[mid_i][j] != 0)) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } if (i < 0 && mid_i < LENGTH) { if (chessman[mid_i][j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } if (i < 0 && mid_i < LENGTH) { if (chessman[mid_i][j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } if (i >= 0 && mid_i >= LENGTH) { if (chessman[i][j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } } } return score; } double score_diag_45(int i, int j, int chessman[LENGTH][LENGTH]) { intsum_chessmen = 0; double score = 0; intmid_i, mid_j; intwho_running = chessman[i][j]; if (i == LENGTH || j == LENGTH) { while (chessman[i][j] == who_running && i > 1 && j > 1) { i--; j--; sum_chessmen++; } if (sum_chessmen >= 5) score = 200000; else { if (chessman[i][j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } } else { while (chessman[i][j] == who_running && i <= LENGTH && j <= LENGTH) { i++; j++; sum_chessmen++; } mid_i = i; mid_j = j; i = i - sum_chessmen; j = j - sum_chessmen; while (chessman[i - 1][j - 1] == who_running) { i--; j--; sum_chessmen++; } if (sum_chessmen >= 5) score = 200000; if (i >= 0 && j >= 0 && mid_i < LENGTH && mid_j < LENGTH) { if (chessman[mid_i][mid_j] == 0 && chessman[i][j] == 0) score = 18000 / POW(50, 4 - sum_chessmen); else if ((chessman[mid_i][mid_j] == 0 && chessman[i][j] != 0) || (chessman[mid_i][mid_j] != 0 && chessman[i][j] == 0)) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } else { if (i >= 0 && j >= 0) { if (chessman[i][j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } else if (mid_i < LENGTH && mid_j < LENGTH) { if (chessman[mid_i][mid_j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } else score = 0; } } return score; } double score_diag_135(int i, int j, int chessman[LENGTH][LENGTH]) { intsum_chessmen = 0; double score = 0; intmid_i, mid_j; intwho_running = chessman[i][j]; while (chessman[i][j] == who_running && j != -1 && i < LENGTH) { i++; j--; sum_chessmen++; } mid_i = i; mid_j = j; j += sum_chessmen; i -= sum_chessmen; j++; i--; while (chessman[i][j] == who_running && j != LENGTH) { i--; j++; sum_chessmen++; } if (sum_chessmen >= 5) score = 200000; else { if (i >= 0 && j < LENGTH && mid_j >= 0 && mid_i < LENGTH) { if (chessman[i][j] == 0 && chessman[mid_i][mid_j] == 0) score = 18000 / POW(50, 4 - sum_chessmen); else { if ((chessman[i][j] == 0 && chessman[mid_i][mid_j] != 0) || (chessman[i][j] != 0 && chessman[mid_i][mid_j] == 0)) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } } else { if (i >= 0 && j < LENGTH) { if (chessman[i][j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } if (mid_j >= 0 && mid_i < LENGTH) { if (chessman[mid_i][mid_j] == 0) score = 2000 / POW(10, 4 - sum_chessmen); else score = 0; } } } return score; } double total_score(int who_running, int chessman[LENGTH][LENGTH]) { inti, j; double score = 0; for (i = 0; i < LENGTH; i++) for (j = 0; j < LENGTH; j++) { if (chessman[i][j] == who_running) { score += score_row(i, j, chessman); score += score_col(i, j, chessman); score += score_diag_45(i, j, chessman); score += score_diag_135(i, j, chessman); } } return score; } double score(int chessman[LENGTH][LENGTH]) { double sum1, sum2; sum1 = total_score(COMPUTER, chessman); sum2 = total_score(PLAYER1, chessman); return sum1 - sum2; } int rowdt(int i, int j, int chessman[LENGTH][LENGTH]) { intk; intmidjl = j - STEP, midjr = j + STEP + 1; if (midjl < 0) midjl = 0; if (midjr > LENGTH) midjr = LENGTH; for (k = midjl; k < midjr; k++) if (chessman[i][k] != 0) return 1; return 0; } int coldt(int i, int j, int chessman[LENGTH][LENGTH]) { intk; intmidil = i + STEP + 1, midiu = i - STEP; if (midiu < 0) midiu = 0; if (midil > LENGTH) midil = LENGTH; for (k = midiu; k < midil; k++) if (chessman[k][j] != 0) return 1; return 0; } int diadt(int i, int j, int chessman[LENGTH][LENGTH]) { intk, midi, midj; midi = i; midj = j; for (k = 0; k < STEP; k++) { midi++; midj--; if (midj < 0 || midi >= LENGTH) break; if (chessman[midi][midj] != 0) return 1; } for (k = 0; k < STEP; k++) { i--; j++; if (i < 0 || j >= LENGTH) break; if (chessman[i][j] != 0) return 1; } return 0; } int vdiadt(int i, int j, int chessman[LENGTH][LENGTH]) { intk, midi, midj; midi = i; midj = j; for (k = 0; k < STEP; k++) { midi--; midj--; if (midi < 0 || midj < 0) break; if (chessman[midi][midj] != 0) return 1; } for (k = 0; k < STEP; k++) { i++; j++; if (j >= LENGTH || i >= LENGTH) break; if (chessman[i][j] != 0) return 1; } return 0; } int can_expand(int i, int j, int chessman[LENGTH][LENGTH]) { if (rowdt(i, j, chessman)) return 1; if (coldt(i, j, chessman)) return 1; if (diadt(i, j, chessman)) return 1; if (vdiadt(i, j, chessman)) return 1; return 0; }



