乌克兰网站后缀,o2o网站建设计划书,多网站建设,免费开设网站文章目录 概要整体架构流程技术名词解释技术细节小结 1. 概要 #x1f50a; JackQiao 对 米粒 说#xff1a;“今天咱们玩个五子棋小游戏#xff0c;电脑与你轮流在一个 nn 的网格上放置棋子#xff08;X 或 O#xff09;#xff0c;网格由你输入的正整数n决定#xff0… 文章目录 概要整体架构流程技术名词解释技术细节小结 1. 概要 JackQiao 对 米粒 说“今天咱们玩个五子棋小游戏电脑与你轮流在一个 n×n 的网格上放置棋子X 或 O网格由你输入的正整数n决定谁先连成五个相同的棋子横、竖或斜即获胜。如果棋盘被填满且无人获胜则游戏以平局结束”。 米粒想到 ✅ 让用户输入棋盘的大小。 ✅ 创建并初始化棋盘。 ✅ 开始游戏循环交替让玩家和电脑下棋。 ✅ 每次下完棋后检查是否有人赢了或者棋盘是否满了。 ✅ 如果有玩家赢了或棋盘满了则结束游戏。 2. 整体架构流程
2.1. 包含的库 #include stdio.h
#include stdlib.h
#include time.hstdio.h用于输入输出操作比如打印信息和读取用户输入。 stdlib.h包含了一些有用的函数比如随机数生成。 time.h用于获取当前时间这里是为了设置随机数种子。 2.2. 定义符号常量 #define EMPTY
#define PLAYER1 X // 黑棋
#define PLAYER2 O // 白棋EMPTY表示空格即没有棋子的地方。PLAYER1 和 PLAYER2分别代表两个玩家的棋子一个是 X另一个是 O。 2.3. 初始化棋盘 void init_board(char board[], int n)
{for (int i 0; i n * n; i){board[i] EMPTY;}
}这个函数用来初始化棋盘。它会把棋盘上的所有位置都设为空格即没有棋子。n 是棋盘的大小比如如果 n5那么就是一个 5x5 的棋盘。 2.4. 打印棋盘 void print_board(char board[], int n)
{// 打印列号printf( );for (int i 0; i n; i) {printf(%2d , i);}printf(\n);// 打印上边框printf( );for (int i 0; i n; i) {printf(---);}printf(\n);// 打印棋盘内容及行号for (int i 0; i n; i) {printf(%2d|, i);for (int j 0; j n; j) {printf( %c |, board[i * n j]);}printf(\n);// 打印行间的分隔线printf( );for (int j 0; j n; j) {printf(---);}printf(\n);}
}✅ 效果如下 : 2.4.1. 打印列号 printf( );
for (int i 0; i n; i)
{printf(%2d , i);
}
printf(\n);printf( );这行代码打印了三个空格用于对齐后续的行号。 for (int i 0; i n; i) { ... }这个循环遍历棋盘的每一列并为每一列打印一个编号。 printf(%2d , i);这里使用了格式化字符串 %2d 来确保每个数字占用至少两个字符的空间如果数字是一位数则前面会补一个空格并在其后加上两个空格以增加可读性。 printf(\n);打印完所有列号后换行以便开始打印棋盘的上边框。 2.4.2. 打印上边框 printf( );
for (int i 0; i n; i)
{printf(---);
}
printf(\n);printf( );打印两个空格和一个加号作为左边界的起点。 for (int i 0; i n; i) { ... }这个循环遍历棋盘的每一列并为每一列打印一个由三个连字符---组成的分隔线最后用一个加号结束。 printf(\n);打印完上边框后换行准备打印棋盘内容。 2.4.3. 打印棋盘内容及行号 for (int i 0; i n; i)
{printf(%2d|, i);for (int j 0; j n; j)
{printf( %c |, board[i * n j]);}printf(\n);// 打印行间的分隔线printf( );for (int j 0; j n; j)
{printf(---);}printf(\n);
}行号与棋盘内容 for (int i 0; i n; i) { ... }这个外层循环遍历棋盘的每一行。printf(%2d|, i);为当前行打印行号同样使用 %2d 确保两位宽然后打印竖线|表示该行的开始。内层循环 for (int j 0; j n; j) { ... } 遍历每一行中的每一个单元格printf( %c |, board[i * n j]);打印当前单元格的内容即棋子或空格并用竖线将其与其他单元格分隔开。 行间分隔线 每一行内容打印完毕后紧接着打印该行下方的分隔线。这个部分与打印上边框的部分几乎相同只是它位于每两行之间而不是顶部。 2.5. 检查位置是否为空 int is_valid_move(char board[], int n, int x, int y)
{if (x 0 || x n || y 0 || y n) return 0;return board[x * n y] EMPTY;
} 这个函数检查给定的位置 (x, y) 是否在棋盘范围内并且是否为空。如果是的话返回 1 表示可以下棋否则返回 0。 2.6. 放置棋子 void make_move(char board[], int n, int x, int y, char player)
{board[x * n y] player;
} 这个函数会在指定的位置 (x, y) 上放置玩家的棋子。 2.7. 检查是否有玩家获胜 int check_win(char board[], int n, int x, int y, char player)
{int directions[8][2] { {0, 1}, {1, 0}, {1, 1}, {1, -1}, {0, -1}, {-1, 0}, {-1, -1}, {-1, 1} };for (int d 0; d 8; d) {int count 1;for (int i 1; i 5; i) {int nx x directions[d][0] * i;int ny y directions[d][1] * i;if (nx 0 nx n ny 0 ny n board[nx * n ny] player) {count;}else {break;}}for (int i 1; i 5; i) {int nx x - directions[d][0] * i;int ny y - directions[d][1] * i;if (nx 0 nx n ny 0 ny n board[nx * n ny] player) {count;}else {break;}}if (count 5) return 1;}return 0;
} 2.7.1. 定义搜索方向 int directions[8][2] { {0, 1}, {1, 0}, {1, 1}, {1, -1}, {0, -1}, {-1, 0}, {-1, -1}, {-1, 1} }; directions 是一个二维数组存储了八个方向的增量值。 {0, 1} 表示向右移动横向。 {1, 0} 表示向下移动纵向。 {1, 1} 表示右下对角线。 {1, -1} 表示左下对角线。 {0, -1} 表示向左移动横向。 {-1, 0} 表示向上移动纵向。 {-1, -1} 表示左上对角线。 {-1, 1} 表示右上对角线。 2.7.2. 检查方向 for (int i 1; i 5; i){int nx x directions[d][0] * i;int ny y directions[d][1] * i;if (nx 0 nx n ny 0 ny n board[nx * n ny] player)
{count;} else
{break;}
}内层第一个循环沿着当前方向的正方向即增加方向检查最多4个位置。nx 和 ny 计算了在当前位置 (x, y) 沿着当前方向移动 i 步后的新坐标。如果新坐标在棋盘范围内并且该位置的棋子与当前玩家相同则增加计数器 count。如果遇到边界或不同棋子则停止检查该方向 2.8. 电脑玩家随机移动 void make_computer_move(char board[], int n, char player)
{int x, y;do
{x rand() % n;y rand() % n;} while (!is_valid_move(board, n, x, y));printf(电脑玩家 %c 下在 (%d, %d)\n, player, x, y);make_move(board, n, x, y, player);
}2.9. 主函数 int main()
{int n;printf(请输入棋盘大小 n: );scanf(%d, n);char* board (char*)malloc(n * n * sizeof(char));if (!board) {printf(内存分配失败\n);return 1;}srand(time(NULL)); // 初始化随机数种子init_board(board, n);char current_player PLAYER1;int x, y;while (1) {print_board(board, n);if (current_player PLAYER1) {printf(玩家 %c请输入坐标 (x y): , current_player);scanf(%d %d, x, y);if (!is_valid_move(board, n, x, y)) {printf(无效的移动请重新输入。\n);continue;}make_move(board, n, x, y, current_player);}else {make_computer_move(board, n, current_player);}if (check_win(board, n, x, y, current_player)) {print_board(board, n);printf(玩家 %c 获胜\n, current_player);free(board);return 0;}if (is_board_full(board, n)) {print_board(board, n);printf(平局\n);free(board);return 0;}current_player (current_player PLAYER1) ? PLAYER2 : PLAYER1;}free(board);return 0;
} 2.10. 程序运行如下 3. 技术名词解释 directions 是一个二维数组存储了八个方向的增量值。 directions 是一个二维数组存储了八个方向的增量值。 {0, 1} 表示向右移动横向。 {1, 0} 表示向下移动纵向。 {1, 1} 表示右下对角线。 {1, -1} 表示左下对角线。 {0, -1} 表示向左移动横向。 {-1, 0} 表示向上移动纵向。 {-1, -1} 表示左上对角线。 {-1, 1} 表示右上对角线。 4. 技术细节 检查相应方
for (int i 1; i 5; i){int nx x - directions[d][0] * i;int ny y - directions[d][1] * i;if (nx 0 nx n ny 0 ny n board[nx * n ny] player){count;} else
{break;}
}✅ 整体代码 #include stdio.h
#include stdlib.h
#include time.h#define EMPTY
#define PLAYER1 X // 黑棋
#define PLAYER2 O // 白棋// 初始化棋盘
void init_board(char board[], int n)
{for (int i 0; i n * n; i){board[i] EMPTY;}
}// 打印棋盘
void print_board(char board[], int n)
{// 打印列号printf( );for (int i 0; i n; i) {printf(%2d , i);}printf(\n);// 打印上边框printf( );for (int i 0; i n; i) {printf(---);}printf(\n);// 打印棋盘内容及行号for (int i 0; i n; i) {printf(%2d|, i);for (int j 0; j n; j) {printf( %c |, board[i * n j]);}printf(\n);// 打印行间的分隔线printf( );for (int j 0; j n; j) {printf(---);}printf(\n);}
}// 检查位置是否为空
int is_valid_move(char board[], int n, int x, int y)
{if (x 0 || x n || y 0 || y n) return 0;return board[x * n y] EMPTY;
}// 放置棋子
void make_move(char board[], int n, int x, int y, char player)
{board[x * n y] player;
}// 检查是否有玩家获胜
int check_win(char board[], int n, int x, int y, char player)
{int directions[8][2] { {0, 1}, {1, 0}, {1, 1}, {1, -1}, {0, -1}, {-1, 0}, {-1, -1}, {-1, 1} };for (int d 0; d 8; d) {int count 1;for (int i 1; i 5; i) {int nx x directions[d][0] * i;int ny y directions[d][1] * i;if (nx 0 nx n ny 0 ny n board[nx * n ny] player) {count;}else {break;}}for (int i 1; i 5; i) {int nx x - directions[d][0] * i;int ny y - directions[d][1] * i;if (nx 0 nx n ny 0 ny n board[nx * n ny] player) {count;}else {break;}}if (count 5) return 1;}return 0;
}// 检查棋盘是否已满
int is_board_full(char board[], int n)
{for (int i 0; i n * n; i) {if (board[i] EMPTY) return 0;}return 1;
}// 电脑玩家随机移动
void make_computer_move(char board[], int n, char player)
{int x, y;do {x rand() % n;y rand() % n;} while (!is_valid_move(board, n, x, y));printf(电脑玩家 %c 下在 (%d, %d)\n, player, x, y);make_move(board, n, x, y, player);
}int main()
{int n;printf(请输入棋盘大小 n: );scanf(%d, n);char* board (char*)malloc(n * n * sizeof(char));if (!board) {printf(内存分配失败\n);return 1;}srand(time(NULL)); // 初始化随机数种子init_board(board, n);char current_player PLAYER1;int x, y;while (1) {print_board(board, n);if (current_player PLAYER1) {printf(玩家 %c请输入坐标 (x y): , current_player);scanf(%d %d, x, y);if (!is_valid_move(board, n, x, y)) {printf(无效的移动请重新输入。\n);continue;}make_move(board, n, x, y, current_player);}else {make_computer_move(board, n, current_player);}if (check_win(board, n, x, y, current_player)) {print_board(board, n);printf(玩家 %c 获胜\n, current_player);free(board);return 0;}if (is_board_full(board, n)) {print_board(board, n);printf(平局\n);free(board);return 0;}current_player (current_player PLAYER1) ? PLAYER2 : PLAYER1;}free(board);return 0;
} 5. 小结
✅ 针对该小游戏米粒做出以下知识点总结 数据结构使用一维数组 char board[] 来表示二维棋盘通过索引计算 [x * n y] 来访问特定位置。 算法逻辑通过遍历八个方向来检查每次落子后是否形成五子连珠使用方向增量数组 directions[8][2] 来简化不同方向上的搜索。 用户交互程序包含输入输出功能如读取用户输入坐标、打印当前棋盘状态并处理非法输入和游戏结束条件胜利或平局。
文章转载自: http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.trlhc.cn.gov.cn.trlhc.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.glnxd.cn.gov.cn.glnxd.cn http://www.morning.clpdm.cn.gov.cn.clpdm.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.hwcln.cn.gov.cn.hwcln.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.qpljg.cn.gov.cn.qpljg.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.rtsd.cn.gov.cn.rtsd.cn http://www.morning.mmtjk.cn.gov.cn.mmtjk.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.tbrnl.cn.gov.cn.tbrnl.cn http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.mnlk.cn.gov.cn.mnlk.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.xwlhc.cn.gov.cn.xwlhc.cn http://www.morning.hympq.cn.gov.cn.hympq.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.jytrb.cn.gov.cn.jytrb.cn http://www.morning.nqlkb.cn.gov.cn.nqlkb.cn http://www.morning.hnhgb.cn.gov.cn.hnhgb.cn http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.dtrzw.cn.gov.cn.dtrzw.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.sqqds.cn.gov.cn.sqqds.cn http://www.morning.kpcdc.cn.gov.cn.kpcdc.cn http://www.morning.qkrqt.cn.gov.cn.qkrqt.cn http://www.morning.wfzlt.cn.gov.cn.wfzlt.cn http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.wphzr.cn.gov.cn.wphzr.cn http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.tfpbm.cn.gov.cn.tfpbm.cn http://www.morning.rqfzp.cn.gov.cn.rqfzp.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.ptdzm.cn.gov.cn.ptdzm.cn http://www.morning.pfnwt.cn.gov.cn.pfnwt.cn http://www.morning.wwwghs.com.gov.cn.wwwghs.com http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.xcjwm.cn.gov.cn.xcjwm.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.qbccg.cn.gov.cn.qbccg.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.ztqyj.cn.gov.cn.ztqyj.cn http://www.morning.lywcd.cn.gov.cn.lywcd.cn http://www.morning.gqhgl.cn.gov.cn.gqhgl.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.rkqqf.cn.gov.cn.rkqqf.cn http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.ktcrr.cn.gov.cn.ktcrr.cn http://www.morning.rckdq.cn.gov.cn.rckdq.cn