C语言实现三子棋
时间:2021-07-26 07:53:26|栏目:C代码|点击: 次
本文实例为大家分享了C语言实现三子棋的具体代码,供大家参考,具体内容如下
题目:C语言实现三子棋
问题分析:首先用到数组,存储信息。棋盘的信息和棋子的信息
打印棋盘
读取棋子的位置
判断是否连子
打印棋盘
然后重复
代码如下:
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //数组沙盘 char Global_Gobang[10][10]; int Global_line, Global_pow; int Global_player; int Global_Count=0; //初始化棋盘 void Initi_Gobang(){ for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ Global_Gobang[i][j] = '+'; } } } //画出棋盘 void Draw_Gobang(){ for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ printf("%c ", Global_Gobang[i][j]); } printf("\n"); } } //在位置上下棋 void Play_Gobang(int player, int line, int row){ // int flag= Check_Gobang(); while (player == 0 || player == 1) { if (player == 0) { if (Global_Gobang[line - 1][row - 1] != 'o' && Global_Gobang[line - 1][row - 1] != '@'){ Global_Gobang[line - 1][row - 1] = 'o'; break; } else { printf("这个位置已经有棋子,请重新输入"); scanf("%d%d", &Global_line, &Global_pow); Play_Gobang(Global_player, Global_line, Global_pow); break; } } else if (player == 1) { if (Global_Gobang[line - 1][row - 1] != 'o' && Global_Gobang[line - 1][row - 1] != '@'){ Global_Gobang[line - 1][row - 1] = '@'; break; } else { printf("这个位置已经有棋子,请重新输入"); scanf("%d%d", &Global_line, &Global_pow); Play_Gobang(Global_player, Global_line, Global_pow); break; } } } } //输入棋子的位置 void Input_Gobang(){ if (Global_player == 1){ printf("请1号玩家输入棋子的位置"); } else if (Global_player == 0) { printf("请2号玩家输入棋子的位置"); } scanf("%d %d", &Global_line, &Global_pow); system("cls");//刷新界面 } int Check_Gobang(){ //i j 为行列的表示,flag为判断棋盘是否满了的标志,flag=0,代表满了 int i, j, flag = 0; //判断行是否成立 for (i = 0; i < 10; i++) for (j = 0; j < 8; j++) if (Global_Gobang[i][j]!='+' && Global_Gobang[i][j] == Global_Gobang[i][j + 1] && Global_Gobang[i][j] == Global_Gobang[i][j + 2]) return 1; //判断列是否成立 for (i = 0; i < 10; i++) for (j = 0; j < 8; j++) if (Global_Gobang[i][j] != '+' &&Global_Gobang[j][i] == Global_Gobang[j + 1][i] && Global_Gobang[i][j] == Global_Gobang[j + 2][i]) return 1; //判断主对角线是否成立1 for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) if (Global_Gobang[i][j] != '+' &&Global_Gobang[j][i] == Global_Gobang[j + 1][i + 1] && Global_Gobang[i][j] == Global_Gobang[j + 2][i + 2]) return 1; //判断副对角线是否成立2 for (i = 2; i < 10; i++) for (j = 2; j < 10; j++) if (Global_Gobang[i][j] != '+' &&Global_Gobang[j][i] == Global_Gobang[j - 1][i - 1] && Global_Gobang[i][j] == Global_Gobang[j - 2][i - 2]) return 1; for (i = 0; i < 10; i++){ for (j = 0; j < 10; j++){ if (Global_Gobang[i][j] == '+') flag = 1; } } if (flag == 0) return 0; } // int Playing_Gobang(){ Global_Count++; if (Global_Count % 2 == 1) Global_player = 1; else Global_player = 0; if (Check_Gobang() == 1){ if (Global_player == 1){ printf("玩家2号赢得胜利,游戏结束\n"); return 1; } else{ printf("玩家1号赢得胜利,游戏结束\n"); return 1; } } else if (Check_Gobang() == 0) { printf("棋盘已满,游戏结束\n"); } Input_Gobang(); Play_Gobang(Global_player, Global_line, Global_pow); Draw_Gobang(); } int main(){ Initi_Gobang(); Draw_Gobang(); while (Global_Count<=100) if (Playing_Gobang() == 0 || Playing_Gobang() == 1) break; system("pause"); return 0; }
结果如下:
更多有趣的经典小游戏实现专题,分享给大家: