如何用C语言来实现三子棋(井字棋),供大家参考,具体内容如下
大致思路如下
1.先打印出菜单供用户进行游戏还是退出游戏
2.编写打印棋盘的函数
3.编写用户和电脑下棋的函数
4.最后对玩家或用户下棋后进行判断输赢
贴张胜利的图片
实现打印菜单的函数
1
2
3
4
5
6
7
|
void menu() { printf ( "******************************************\n" ); printf ( "************* 1. play *************\n" ); printf ( "************* 2.exit *************\n" ); printf ( "******************************************\n" ); } |
主函数的实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
int main() { int input; srand ((unsigned int ) time (NULL)); do { menu(); printf ( "请选择>" ); scanf ( "%d" , &input); switch (input) { case 0: printf ( "退出\n" ); break ; case 1: game(); break ; default : printf ( "输入错误," ); break ; } } while (input); return 0; } |
根据用户的选择是玩游戏还是退出,玩完一局后也可以在玩一局
game函数的实现
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
|
void game() { char board[ROW][COL]; //初始化棋盘 InitBoard(board, ROW, COL); //打印棋盘 PrinBoard(board, ROW, COL); char flag = 0; //下棋 while (1) { PlayMove(board, ROW, COL); PrinBoard(board, ROW, COL); //判断当前状态 flag = IsWin(board, ROW, COL); if (flag != 'c' ) break ; //电脑下棋 CompMove(board, ROW, COL); PrinBoard(board, ROW, COL); //判断当前状态 flag = IsWin(board, ROW, COL); if (flag != 'c' ) break ; } if (flag == '*' ) printf ( "恭喜玩家获胜\n" ); else if (flag == '#' ) printf ( "恭喜电脑获胜\n" ); else printf ( "平局啦\n" ); } |
在每次玩家或电脑下完棋后都应该进行判断,判断当前棋盘的状态.
打印棋盘
首先应先打印棋盘,而且在每次下完棋后也应在打印一下棋盘
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
|
void PrinBoard( char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for ( int j = 0; j < col; j++) { printf ( " %c " , board[i][j]); if (j < col - 1) printf ( "|" ); } printf ( "\n" ); if (i < row - 1) { for (j = 0; j < col; j++) { printf ( "---" ); if (j < col - 1) printf ( "|" ); } } printf ( "\n" ); } } |
判断函数
IsWin函数应该有四种状态分别为:玩家赢(*)、电脑赢(#)、平局(d)和继续©。
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
|
char IsWin( char board[ROW][COL], int row, int col) { int i = 0; //判断行 for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col - 1; j++) { if (board[i][j] == ' ' || board[i][j] != board[i][j + 1]) break ; } if (j == col - 1) return board[i][j - 1]; } //判断列 for (i = 0; i < col; i++) { int j = 0; for (j = 0; j < row - 1; j++) { if (board[j][i] == ' ' || board[j][i] != board[j + 1][i]) break ; } if (j == row - 1) return board[j][i]; } //右斜 for (i = 0; i < row - 1; i++) { if (board[i][i] == ' ' || board[i][i] != board[i + 1][i + 1]) break ; } if (i == row - 1) return board[i][i]; int x = 1, y = col - 2; while (x < row&&y >= 0) { if (board[x][y] == ' ' || board[x][y] != board[x - 1][y + 1]) break ; x++; y--; } if (x == row && y < 0) return board[x - 1][y + 1]; //判断是否平局 for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] == ' ' ) return 'c' ; } } //平局 return 'd' ; } |
下棋函数
电脑下棋时通过时间戳来生成随机数,并判断当前位置是否合法。每次下完棋后应该对棋盘进行判断,通过调用函数IsWin根据返回值,判断是继续还是跳出。
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
|
void PlayMove( char board[ROW][COL], int row, int col) { printf ( "请玩家下棋>" ); while (1) { int x, y; printf ( "输入您要下的位置" ); scanf ( "%d %d" , &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { //判断当前位置是否有棋子 if (board[x - 1][y - 1] == ' ' ) { board[x - 1][y - 1] = '*' ; break ; } else printf ( "此位置重复\n" ); } else printf ( "此坐标不合法,超出棋盘大小\n" ); } } void CompMove( char board[ROW][COL], int row, int col) { printf ( "电脑下棋>\n" ); printf ( "\n" ); while (1) { int x = rand () % row; int y = rand () % col; if (board[x][y] == ' ' ) { board[x][y] = '#' ; break ; } } } |
到这各个函数的实现已完成。
完整代码如下
text.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
|
#include"game.h" void menu() { printf ( "******************************************\n" ); printf ( "************* 1. play *************\n" ); printf ( "************* 2.exit *************\n" ); printf ( "******************************************\n" ); } void game() { char board[ROW][COL]; //初始化棋盘 InitBoard(board, ROW, COL); //打印棋盘 PrinBoard(board, ROW, COL); char flag = 0; //下棋 while (1) { PlayMove(board, ROW, COL); PrinBoard(board, ROW, COL); //判断当前状态 flag = IsWin(board, ROW, COL); if (flag != 'c' ) break ; //电脑下棋 CompMove(board, ROW, COL); PrinBoard(board, ROW, COL); //判断当前状态 flag = IsWin(board, ROW, COL); if (flag != 'c' ) break ; } if (flag == '*' ) printf ( "恭喜玩家获胜\n" ); else if (flag == '#' ) printf ( "恭喜电脑获胜\n" ); else printf ( "平局啦\n" ); } int main() { int input; srand ((unsigned int ) time (NULL)); do { menu(); printf ( "请选择>" ); scanf ( "%d" , &input); switch (input) { case 0: printf ( "退出\n" ); break ; case 1: game(); break ; default : printf ( "输入错误," ); break ; } } while (input); return 0; } |
game.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
142
143
144
|
#include"game.h" void InitBoard( char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] = ' ' ; } } } void PrinBoard( char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for ( int j = 0; j < col; j++) { printf ( " %c " , board[i][j]); if (j < col - 1) printf ( "|" ); } printf ( "\n" ); if (i < row - 1) { for (j = 0; j < col; j++) { printf ( "---" ); if (j < col - 1) printf ( "|" ); } } printf ( "\n" ); } } void PlayMove( char board[ROW][COL], int row, int col) { printf ( "请玩家下棋>\n" ); while (1) { int x, y; printf ( "输入您要下的位置" ); scanf ( "%d %d" , &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { //判断当前位置是否有棋子 if (board[x - 1][y - 1] == ' ' ) { board[x - 1][y - 1] = '*' ; break ; } else printf ( "此位置重复\n" ); } else printf ( "此坐标不合法,超出棋盘大小\n" ); } } void CompMove( char board[ROW][COL], int row, int col) { printf ( "电脑下棋>\n" ); printf ( "\n" ); while (1) { int x = rand () % row; int y = rand () % col; if (board[x][y] == ' ' ) { board[x][y] = '#' ; break ; } } } char IsWin( char board[ROW][COL], int row, int col) { int i = 0; //判断行 for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col - 1; j++) { if (board[i][j] == ' ' || board[i][j] != board[i][j + 1]) break ; } if (j == col - 1) return board[i][j - 1]; } //判断列 for (i = 0; i < col; i++) { int j = 0; for (j = 0; j < row - 1; j++) { if (board[j][i] == ' ' || board[j][i] != board[j + 1][i]) break ; } if (j == row - 1) return board[j][i]; } //右斜 for (i = 0; i < row - 1; i++) { if (board[i][i] == ' ' || board[i][i] != board[i + 1][i + 1]) break ; } if (i == row - 1) return board[i][i]; int x = 1, y = col - 2; while (x < row&&y >= 0) { if (board[x][y] == ' ' || board[x][y] != board[x - 1][y + 1]) break ; x++; y--; } if (x == row && y < 0) return board[x - 1][y + 1]; //判断是否平局 for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] == ' ' ) return 'c' ; } } //平局 return 'd' ; } |
game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#pragma once #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<time.h> #include<stdlib.h> enum { ROW = 3, COL = 3 }; void InitBoard( char board[ROW][COL], int row, int col); void PrinBoard( char board[ROW][COL], int row, int col); void PlayMove( char board[ROW][COL], int row, int col); void CompMove( char board[ROW][COL], int row, int col); char IsWin( char board[ROW][COL], int row, int col); |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_46383092/article/details/119150131