三子棋小游戏
Posted cuckoo-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了三子棋小游戏相关的知识,希望对你有一定的参考价值。
1 #define _CRT_SECURE_NO_WARNINGS
2 #include<stdio.h>
3 #include<stdlib.h>
4 #include<time.h>
5
6 #define ROW 3
7 #define COL 3
8
9 char g_broad[ROW][COL];
10
11 void Init(char broad[ROW][COL])
12 {
13 for (int row = 0;row < ROW;++row)
14 {
15 for (int col = 0;col < COL;++col)
16 {
17 broad[row][col] = ‘ ‘;
18 }
19 }
20 }
21
22 void Print(char broad[ROW][COL])
23 {
24 for(int row=0;row<ROW;++row)
25 {
26 printf("| %c | %c | %c |
",broad[row][0],broad[row][1],broad[row][2]);
27 if (row != ROW - 1)
28 {
29 printf(" ---|---|---
");
30 }
31 }
32 }
33
34 void Playermove(char broad[ROW][COL])
35 {
36 printf("玩家下棋!
");
37 while(1)
38 {
39 int row = 0;
40 int col = 0;
41 printf("请输入一组坐标:
");
42 scanf("%d %d", &row, &col);
43 if (row < 0 || row >= ROW || col < 0 || col >= COL)
44 {
45 printf("您输入的坐标非法!请重新输入:
");
46 continue;
47 }
48 if (broad[row][col] != ‘ ‘)
49 {
50 printf("该位置已经有棋子,请重新输入:
");
51 continue;
52 }
53 broad[row][col] = ‘x‘;
54 break;
55 }
56 }
57
58 void Computermove(char broad[ROW][COL])
59 {
60 printf("电脑下棋!
");
61 while (1)
62 {
63 int row = rand() % ROW;
64 int col = rand() % COL;
65 if (broad[row][col] != ‘ ‘)
66 {
67 //位置被占用,重新生成
68 continue;
69 }
70 broad[row][col] = ‘o‘;
71 break;
72 }
73
74 }
75
76 int Isfull(char broad[ROW][COL])
77 {
78 for (int row = 0;row < ROW;++row)
79 {
80 for (int col = 0;col < COL;++col)
81 {
82 if (broad[row][col] == ‘ ‘)
83 {
84 return 0;
85 }
86 }
87 }
88 return 1;
89 }
90
91 char Checkgame(char broad[ROW][COL])
92 {
93 //先检查所有的行
94 for (int row = 0;row < ROW;++row)
95 {
96 if (broad[row][0] == broad[row][1]
97 && broad[row][0] == broad[row][2]
98 && broad[row][0] != ‘ ‘)
99 {
100 return broad[row][0];
101 }
102 }
103 //检查所有列
104 for (int col = 0;col < COL;++col)
105 {
106 if (broad[0][col] == broad[1][col]
107 && broad[0][col] == broad[2][col]
108 && broad != ‘ ‘)
109 {
110 return broad[0][col];
111 }
112 }
113 //检查对角线
114 if (broad[0][0] == broad[1][1]
115 && broad[0][0] == broad[2][2]
116 && broad[0][0] != ‘ ‘)
117 {
118 return broad[0][0];
119 }
120 if (broad[2][0] == broad[1][1]
121 && broad[2][0] == broad[0][2]
122 && broad[2][0] != ‘ ‘)
123 {
124 return broad[2][0];
125 }
126 //判定是否和棋
127 if (Isfull(broad))
128 {
129 return ‘q‘;
130 }
131 return ‘ ‘;
132 }
133
134 void Game()
135 {
136 //1.先对棋盘进行初始化
137 Init(g_broad);
138 char winner = ‘ ‘;
139 while (1)
140 {
141 //2.打印棋盘(此时是一个空的棋盘)
142 Print(g_broad);
143 //3.玩家下棋,检查游戏是否结束
144 Playermove(g_broad);
145 //winner==x 玩家胜利;o电脑胜利;q和棋 ;‘ ‘胜负未分
146 winner = Checkgame(g_broad);
147 if (winner != ‘ ‘)
148 {
149 break;
150 }
151 //4.电脑下棋,检查游戏是否结束
152 Computermove(g_broad);
153 winner = Checkgame(g_broad);
154 if (winner != ‘ ‘)
155 {
156 break;
157 }
158 //循环第2步。
159
160 }
161 //在游戏结束之前打印地图
162 Print(g_broad);
163 if (winner == ‘x‘)
164 {
165 printf("玩家胜利!
");
166 }
167 else if (winner == ‘o‘)
168 {
169 printf("电脑胜利!
");
170 }
171 else if (winner == ‘q‘)
172 {
173 printf("和棋!
");
174 }
175 else
176 {
177 printf("程序出错!
");
178 }
179
180 }
181
182 int Menu()
183 {
184 printf("==============
");
185 printf("1.开始游戏
");
186 printf("0.结束游戏
");
187 printf("==============
");
188 printf("请输入您的选择:
");
189 int choice = 0;
190 scanf("%d",&choice);
191 return choice;
192 }
193
194 int main()
195 {
196 //这里设置随机种子
197 srand((unsigned int)time(0));
198 while (1)
199 {
200 int choice = Menu();
201 if (choice == 1)
202 {
203 Game();
204 }
205 else
206 {
207 printf("Goodbye!
");
208 break;
209 }
210 }
211 return 0;
212 }
以上是关于三子棋小游戏的主要内容,如果未能解决你的问题,请参考以下文章