本文实例为大家分享了C语言实现三子棋的具体代码,供大家参考,具体内容如下
题目:C语言实现三子棋
问题分析:首先用到数组,存储信息。棋盘的信息和棋子的信息
打印棋盘
读取棋子的位置
判断是否连子
打印棋盘
然后重复
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
#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; } |
结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_36640480/article/details/103213563