双人对战
总体流程
0.初始化
1.展示棋盘
2.玩家1落子
3.展示棋盘
4.判断是否胜利,若胜利,跳到9
5.玩家2落子
6.展示棋盘
7.判断是否胜利,若胜利,跳到9
8.若无人胜利,回到1
9.若已有人胜利,则回到0或结束。
初始化棋盘
void init_board(char arr[3][3]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { arr[i][j] = ' '; } } }
输出棋盘
void display_board(char arr[3][3]) { system("cls"); for (int i = 0; i < 3; i++) { printf(" %c|%c |%c\n", arr[i][0], arr[i][1], arr[i][2]); if (i < 2) printf("--|--|--"); printf("\n"); } }
落子
void player_chess(char arr[3][3]) { int x,y; while (1) { printf("请输入你所要下棋的坐标:"); scanf("%d%d", &x, &y); x--; y--; if (arr[x][y] == ' ' && x < 3 && y < 3) { arr[x][y] = 'X'; break; } else { printf("这个位置已经被占或在格子之外,请重新下棋。\n"); continue; } } }
判断棋盘是否已满
int check_full(char arr[3][3]) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (arr[i][j] == ' ') return 0; return 1; }
判断胜利方
char check_win(char arr[3][3]) { int i = 0; int ret = 0; for (i = 0; i < 3; i++) if ((arr[i][0] == arr[i][1]) && (arr[i][2] == arr[i][1])) return arr[i][0]; for (i = 0; i < 3; i++) if ((arr[0][i] == arr[1][i]) && (arr[2][i] == arr[1][i])) return arr[0][i]; if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2])) return arr[0][0]; if ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0])) return arr[2][0]; ret = check_full(arr); if (ret == 1) return 'q'; return ' '; }
主函数
char arr[3][3] = { 0 };char ret=0; init_board(arr); do { display_board(arr); player_chess(arr); display_board(arr); if (check_win(arr) != ' ') { ret = check_win(arr); break; } player2_chess(arr); display_board(arr); ret = check_win(arr); } while (ret == ' '); display_board(arr); if (ret == 'X') printf("玩家1赢的游戏\n"); else if (ret == 'O') printf("玩家2赢的游戏\n"); else if (ret == 'q') printf("游戏平局\n");
人机对战
电脑整体逻辑
1.电脑找三连
2.电脑防止人三连
3.电脑按优先级落子。中心点优先级>角>边
电脑找三连
int countx,count0,pos0=-1; for (int i = 0; i < 3; i++) { countx = 0; count0 = 0; pos0 = -1; for (int j = 0; j < COL; j++) { if (arr[i][j] == 'O') countx++; if (arr[i][j] == ' ') { count0++; pos0 = j; } } if (countx == 2 && count0 == 1) { arr[i][pos0] = 'O'; return; } } countx = 0; count0 = 0; pos0 = -1; for (int i = 0; i < 3; i++) { countx = 0; count0 = 0; pos0 = -1; for (int j = 0; j < 3; j++) { if (arr[j][i] == 'O') countx++; if (arr[j][i] == ' ') { count0++; pos0 = j; } } if (countx == 2 && count0 == 1) { arr[pos0][i] = 'O'; return; } } countx = 0; count0 = 0; pos0 = -1; for (int i = 0; i < 3; i++) { if (arr[i][2 - i] == 'O') countx++; if (arr[i][2 - i] == ' ') { count0++; pos0 = i; } } if (countx == 2 && count0 == 1) { arr[pos0][2 - pos0] = 'O'; return; }
电脑防止人三连
int countx,count0,pos0=-1; for (int i = 0; i < 3; i++) { countx = 0; count0 = 0; pos0 = -1; for (int j = 0; j < COL; j++) { if (arr[i][j] == 'X') countx++; if (arr[i][j] == ' ') { count0++; pos0 = j; } } if (countx == 2 && count0 == 1) { arr[i][pos0] = 'O'; return; } } countx = 0; count0 = 0; pos0 = -1; for (int i = 0; i < 3; i++) { countx = 0; count0 = 0; pos0 = -1; for (int j = 0; j < 3; j++) { if (arr[j][i] == 'X') countx++; if (arr[j][i] == ' ') { count0++; pos0 = j; } } if (countx == 2 && count0 == 1) { arr[pos0][i] = 'O'; return; } } countx = 0; count0 = 0; pos0 = -1; for (int i = 0; i < 3; i++) { if (arr[i][2 - i] == 'X') countx++; if (arr[i][2 - i] == ' ') { count0++; pos0 = i; } } if (countx == 2 && count0 == 1) { arr[pos0][2 - pos0] = 'O'; return; }
按优先级落子
if (arr[1][1] == ' ') { arr[1][1] = 'O'; return; } if (arr[0][2] == ' ') { arr[0][2] = 'O'; return; } if (arr[2][0] == ' ') { arr[2][0] = 'O'; return; } if (arr[0][0] == ' ') { arr[0][0] = 'O'; return; } if (arr[2][2] == ' ') { arr[2][2] = 'O'; return; } if (arr[0][1] == ' ') { arr[0][1] = 'O'; return; } if (arr[1][0] == ' ') { arr[1][0] = 'O'; return; } if (arr[1][2] == ' ') { arr[1][2] = 'O'; return; } if (arr[2][1] == ' ') { arr[2][1] = 'O'; return; }
完整代码
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3 #define COL 3 int check_full(char arr[ROW][COL]); void init_board(char arr[ROW][COL]) { for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { arr[i][j] = ' '; } } } void display_board(char arr[ROW][COL]) { system("cls"); for (int i = 0; i < ROW; i++) { printf(" %c|%c |%c\n", arr[i][0], arr[i][1], arr[i][2]); if (i < 2) printf("--|--|--"); printf("\n"); } } void player_chess(char arr[ROW][COL]) { int x = 0; int y = 0; while (1) { printf("请输入你所要下棋的坐标:"); scanf("%d%d", &x, &y); x--; y--; if (arr[x][y] == ' ' && x < ROW && y < ROW) { arr[x][y] = 'X'; break; } else { printf("这个位置已经被占或在格子之外,请重新下棋。\n"); continue; } } } void player2_chess(char arr[ROW][COL]) { int x = 0; int y = 0; while (1) { printf("请输入你所要下棋的坐标:,\n先输入纵轴坐标后横轴坐标\n取值范围1~3"); scanf("%d%d", &x, &y); x--; y--; if (arr[x][y] == ' ' && x < ROW && y < ROW) { arr[x][y] = 'O'; break; } else { printf("这个位置已经被占或在格子之外,请重新下棋。\n"); continue; } } } void computer_chess(char arr[ROW][COL]) { int countx,count0,pos0=-1; for (int i = 0; i < 3; i++) { countx = 0; count0 = 0; pos0 = -1; for (int j = 0; j < COL; j++) { if (arr[i][j] == 'O') countx++; if (arr[i][j] == ' ') { count0++; pos0 = j; } } if (countx == 2 && count0 == 1) { arr[i][pos0] = 'O'; return; } } for (int i = 0; i < 3; i++) { countx = 0; count0 = 0; pos0 = -1; for (int j = 0; j < 3; j++) { if (arr[j][i] == 'O') countx++; if (arr[j][i] == ' ') { count0++; pos0 = j; } } if (countx == 2 && count0 == 1) { arr[pos0][i] = 'O'; return; } } countx = 0; count0 = 0; pos0 = -1; for (int i = 0; i < 3; i++) { if (arr[i][i] == 'O') countx++; if (arr[i][i] == ' ') { count0++; pos0 = i; } } countx = 0; count0 = 0; pos0 = -1; for (int i = 0; i < 3; i++) { if (arr[i][2 - i] == 'O') countx++; if (arr[i][2 - i] == ' ') { count0++; pos0 = i; } } if (countx == 2 && count0 == 1) { arr[pos0][2 - pos0] = 'O'; return; } for (int i = 0; i < 3; i++) { countx = 0; count0 = 0; pos0 = -1; for (int j = 0; j < COL; j++) { if (arr[i][j] == 'X') countx++; if (arr[i][j] == ' ') { count0++; pos0 = j; } } if (countx == 2 && count0 == 1) { arr[i][pos0] = 'O'; return; } } for (int i = 0; i < 3; i++) { countx = 0; count0 = 0; pos0 = -1; for (int j = 0; j < 3; j++) { if (arr[j][i] == 'X') countx++; if (arr[j][i] == ' ') { count0++; pos0 = j; } } if (countx == 2&& count0 == 1) { arr[pos0][i] = 'O'; return; } } countx = 0; count0 = 0; pos0 = -1; for (int i = 0; i < 3; i++) { if (arr[i][i] == 'X') countx++; if (arr[i][i] == ' ') { count0++; pos0 = i; } } if (countx == 2 && count0 == 1) { arr[pos0][pos0] = 'O'; return; } countx = 0;count0 = 0; pos0 = -1; for (int i = 0; i < 3; i++) { if (arr[i][2-i] == 'X') countx++; if (arr[i][2-i] == ' ') { count0++; pos0 = i; } } if (countx == 2 && count0 == 1) { arr[pos0][2-pos0] = 'O'; return; } if (arr[1][1] == ' ') { arr[1][1] = 'O'; return; } if (arr[0][2] == ' ') { arr[0][2] = 'O'; return; } if (arr[2][0] == ' ') { arr[2][0] = 'O'; return; } if (arr[0][0] == ' ') { arr[0][0] = 'O'; return; } if (arr[2][2] == ' ') { arr[2][2] = 'O'; return; } if (arr[0][1] == ' ') { arr[0][1] = 'O'; return; } if (arr[1][0] == ' ') { arr[1][0] = 'O'; return; } if (arr[1][2] == ' ') { arr[1][2] = 'O'; return; } if (arr[2][1] == ' ') { arr[2][1] = 'O'; return; } } int check_full(char arr[ROW][COL]) { int i = 0; int j = 0; for (i = 0; i < ROW; i++) for (j = 0; j < COL; j++) if (arr[i][j] == ' ') return 0; return 1; } char check_win(char arr[ROW][COL]) { int i = 0; int ret = 0; for (i = 0; i < ROW; i++) if ((arr[i][0] == arr[i][1]) && (arr[i][2] == arr[i][1])) return arr[i][0]; for (i = 0; i < COL; i++) if ((arr[0][i] == arr[1][i]) && (arr[2][i] == arr[1][i])) return arr[0][i]; if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2])) return arr[0][0]; if ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0])) return arr[2][0]; ret = check_full(arr); if (ret == 1) return 'q'; return ' '; } int main() { char arr[ROW][COL] = { 0 }; int input = 1; while (input) { printf("请选择是否开始游戏:\n"); printf("1:玩家先手,电脑后手\n2:玩家后手,电脑先手\n3:双人对战,\n0:结束游戏"); scanf("%d", &input); switch (input) { case 3: { char ret = 0; init_board(arr); do { display_board(arr); player_chess(arr); display_board(arr); if (check_win(arr) != ' ') { ret = check_win(arr); break; } player2_chess(arr); ret = check_win(arr); } while (ret == ' '); display_board(arr); if (ret == 'X') printf("玩家1赢的游戏\n"); else if (ret == 'O') printf("玩家2赢的游戏\n"); else if (ret == 'q') printf("游戏平局\n"); } break; case 2: { char ret = 0; init_board(arr); do { display_board(arr); computer_chess(arr); display_board(arr); if (check_win(arr) != ' ') { ret = check_win(arr); break; } player_chess(arr); ret = check_win(arr); } while (ret == ' '); display_board(arr); if (ret == 'X') printf("玩家赢的游戏\n"); else if (ret == 'O') printf("电脑赢的游戏\n"); else if (ret == 'q') printf("游戏平局\n"); } break; case 1: { char ret = 0; init_board(arr); do { display_board(arr); player_chess(arr); display_board(arr); if (check_win(arr) != ' ') { ret = check_win(arr); break; } computer_chess(arr); ret = check_win(arr); } while (ret == ' '); display_board(arr); if (ret == 'X') printf("玩家赢的游戏\n"); else if (ret == 'O') printf("电脑赢的游戏\n"); else if (ret == 'q') printf("游戏平局\n"); } break; case 0:break; break; default:; } } system("pause"); return 0; }