服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现五子棋小游戏

C++实现五子棋小游戏

2022-11-22 12:31流氓虎 C/C++

这篇文章主要为大家详细介绍了C++实现五子棋小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现五子棋小游戏的具体代码,供大家参考,具体内容如下

思路:先用用system(“color 70”)改变控制台的背景色为灰白色,前景色为黑色,然后用“■”打印棋盘,然后用SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a)改变输出颜色分别为白色和黑色,用字符“●”打印黑棋和白棋。
整个棋盘用一个char类型的二维数组保存,空的地方用‘ ’标识,玩家一下棋的地方用‘x’标识,玩家二下棋的地方用‘o’标识(电脑算作玩家一)。
核心代码模块在于胜负判断,如何在char类型的二维数组中找到五星连珠?

主要思路:每落一子判断一次,从这个子向前数四个向后数四个,在这九个子中从头向后查找,看是否有五个子完全相同,若有这胜利

?
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
int judgewiner1(char a, coordinate temp)//判断横排
    {
        coordinate begin, end;
        begin.x = end.x = temp.x;
        if (temp.y <= 5)begin.y = 1;
        else begin.y = temp.y - 4;
        if (temp.y >= N-5)end.y = N-1;
        else end.y = temp.y + 4;
        for (int i = begin.x, j = begin.y;  j <= end.y - 4;  ++j)
        {
            if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i][j + 1] && chessboard[i][j + 1] == chessboard[i][j + 2] && chessboard[i][j + 2] == chessboard[i][j + 3] && chessboard[i][j + 3]== chessboard[i][j + 4])
                return 1;
        }
        return 0;
    }
    int judgewiner2(char a, coordinate temp)//判断竖排
    {
        coordinate begin, end;
        begin.y = end.y = temp.y;
        if (temp.x <= 5)begin.x = 1;
        else begin.x = temp.x - 4;
        if (temp.x >=  N-5)end.x = N-1;
        else end.x = temp.x + 4;
        for (int i = begin.x,j = begin.y; i <= end.x - 4; ++i)
        {
            if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i + 1][j] && chessboard[i + 1][j] == chessboard[i + 2][j] && chessboard[i + 2][j] == chessboard[i + 3][j] && chessboard[i + 3][j]== chessboard[i + 4][j])
                return 1;
        }
        return 0;
    }
    int judgewiner3(char a, coordinate temp)//判断主对角线
    {
        coordinate begin, end;
        if (temp.x <= 5)begin.x = 1;
        else begin.x = temp.x - 4;
        if (temp.y <= 5)begin.y = 1;
        else begin.y = temp.y - 4;
        if (temp.x >=  N-5)end.x = N-1;
        else end.x = temp.x + 4;
        if (temp.y >=  N-5)end.y = N-1;
        else end.y = temp.y + 4;
        for (int i = begin.x,j = begin.y; i <= end.x-4&&j <= end.y-4; ++i, ++j)
        {
            if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i + 1][j + 1] && chessboard[i + 1][j + 1] == chessboard[i + 2][j + 2] && chessboard[i + 2][j + 2] == chessboard[i + 3][j + 3] && chessboard[i + 3][j + 3]==chessboard[i + 4][j + 4])
                return 1;
        }
        return 0;
    }
    int judgewiner4(char a, coordinate temp)//判断负对角线
    {
        coordinate begin, end;
        if (temp.x<=5)begin.x = 1;
        else begin.x = temp.x - 4;
        if (temp.y >=  N-5)begin.y = N-1;
        else begin.y = temp.y + 4;
        if (temp.x >=  N-5)end.x = N-1;
        else end.x = temp.x + 4;
        if (temp.y <= 5)end.y = 1;
        else end.y = temp.y - 4;
        for (int i = begin.x,j = begin.y; i <= end.x - 4 && j >= end.y - 4; ++i, --j)
        {
            if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i + 1][j - 1] && chessboard[i + 1][j - 1] == chessboard[i + 2][j - 2] && chessboard[i + 2][j - 2] == chessboard[i + 3][j - 3] && chessboard[i + 3][j - 3]==chessboard[i + 4][j - 4])
                return 1;
        }
        return 0;
    }
    int judgeheqi()//判断和棋
    {
        for(int i=1;i<N;++i)
            for (int j = 1; j < N; ++j)
                if (chessboard[i][j] == ' ')return 1;
        return -1;
    }

下面是完整代码:

?
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//玩家一与电脑用‘x'标识,玩家二用'o'标识
#include<iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
using namespace::std;
 
#define N 20
char chessboardflag = ' ';  //棋盘标志
void color(int a)//改变颜色的函数
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
class coordinate            //坐标类
{
public:
    int x;
    int y;
};
class fivechess
{
public:
    void initchessboard()//初始化棋盘
    {
        for (int i = 0; i < N; ++i)
            for (int j = 0; j < N; ++j)
                chessboard[i][j] = chessboardflag;
        printchessboard();
    }
    void printchessboard()//打印棋盘
    {
        system("cls");
        system("color 70");
        cout << "  1 2 3 4 5 6 7 8 9 10111213141516171819"<<endl;
        for (int i = 1; i < N; ++i) {
            for (int j = 0; j < N; ++j)
            {
                if (j == 0) {
                    if (i <= 9)cout << i << " ";
                    else cout << i;
                }
                else
                {
                    if (chessboard[i][j] == 'x')
                    {
                        color(0x70);
                        cout << "●";
                    }
                    else if (chessboard[i][j] == 'o')
                    {
                        color(0x7f);
                        cout << "●";
                    }
                    else
                    {
                        color(0x74);
                        cout << "■";
                    }
                }
            }
            cout << endl;
            color(0x70);
        }
    }
    coordinate playchess1()  //玩家一下棋
    {
        cout << "请玩家一输入坐标:" << endl;
        int x1, y1;
        while (cin >> x1 >> y1)
        {
            if (x1 > N-1 || y1 > N-1 || x1 < 0 || y1 < 0)
            {
                cout << "输入超界,请从新输入" << endl;
                continue;
            }
            if (chessboard[x1][y1] == ' ') {
                chessboard[x1][y1] = 'x';
                break;
            }
            else
            {
                cout << "输入错误,请重新输入";
            }
        }
        coordinate temp;
        temp.x = x1;
        temp.y = y1;
        return temp;
    }
    coordinate playchess2()  //玩家二下棋
    {
        cout << "请玩家2输入坐标:" << endl;
        int x2, y2;
        while (cin >> x2 >> y2)
        {
            if (x2 > N-1 || y2 > N-1 || x2 < 0 || y2 < 0)
            {
                cout << "输入超界,请从新输入" << endl;
                continue;
            }
            if (chessboard[x2][y2] == ' ') {
                chessboard[x2][y2] = 'o';
                break;
            }
            else
            {
                cout << "输入错误,请重新输入";
            }
        }
        coordinate temp;
        temp.x = x2;
        temp.y = y2;
        return temp;
    }
    coordinate computerplayer()//电脑下棋
    {
        coordinate temp;
        srand((unsigned)time(NULL));
        int x1 = 0, y1 = 0;
        while ((x1 = (rand() % (N-1)) + 1) && (y1 = (rand() % (N-1)) + 1))
        {
            if (chessboard[x1][y1] == ' ') {
                chessboard[x1][y1] = 'x';
                break;
            }
            else continue;
        }
        temp.x = x1; temp.y = y1;
        return temp;
    }
    int judgewiner1(char a, coordinate temp)//判断横排
    {
        coordinate begin, end;
        begin.x = end.x = temp.x;
        if (temp.y <= 5)begin.y = 1;
        else begin.y = temp.y - 4;
        if (temp.y >= N-5)end.y = N-1;
        else end.y = temp.y + 4;
        for (int i = begin.x, j = begin.y; j <= end.y - 4; ++j)
        {
            if (chessboard[i][j] == a 
                && chessboard[i][j] == chessboard[i][j + 1] 
                && chessboard[i][j + 1] == chessboard[i][j + 2]
                && chessboard[i][j + 2] == chessboard[i][j + 3] 
                && chessboard[i][j + 3] == chessboard[i][j + 4])
                return 1;
        }
        return 0;
    }
    int judgewiner2(char a, coordinate temp)//判断竖排
    {
        coordinate begin, end;
        begin.y = end.y = temp.y;
        if (temp.x <= 5)begin.x = 1;
        else begin.x = temp.x - 4;
        if (temp.x >= N-5)end.x =N-1;
        else end.x = temp.x + 4;
        for (int i = begin.x, j = begin.y; i <= end.x - 4; ++i)
        {
            if (chessboard[i][j] == a
                && chessboard[i][j] == chessboard[i + 1][j] 
                && chessboard[i + 1][j] == chessboard[i + 2][j] 
                && chessboard[i + 2][j] == chessboard[i + 3][j] 
                && chessboard[i + 3][j] == chessboard[i + 4][j])
                return 1;
        }
        return 0;
    }
    int judgewiner3(char a, coordinate temp)//判断主对角线
    {
        coordinate begin, end;
        if (temp.x <= 5)begin.x = 1;
        else begin.x = temp.x - 4;
        if (temp.y <= 5)begin.y = 1;
        else begin.y = temp.y - 4;
        if (temp.x >= N-5)end.x = N-1;
        else end.x = temp.x + 4;
        if (temp.y >= N-5)end.y = N-1;
        else end.y = temp.y + 4;
        for (int i = begin.x, j = begin.y; i <= end.x - 4 && j <= end.y - 4; ++i, ++j)
        {
            if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i + 1][j + 1] 
                && chessboard[i + 1][j + 1] == chessboard[i + 2][j + 2] 
                && chessboard[i + 2][j + 2] == chessboard[i + 3][j + 3] 
                && chessboard[i + 3][j + 3] == chessboard[i + 4][j + 4])
                return 1;
        }
        return 0;
    }
    int judgewiner4(char a, coordinate temp)//判断负对角线
    {
        coordinate begin, end;
        if (temp.x <= 5)begin.x = 1;
        else begin.x = temp.x - 4;
        if (temp.y >= N-5)begin.y = N-1;
        else begin.y = temp.y + 4;
        if (temp.x >= N-5)end.x = N-1;
        else end.x = temp.x + 4;
        if (temp.y <= 5)end.y = 1;
        else end.y = temp.y - 4;
        for (int i = begin.x, j = begin.y; i <= end.x - 4 && j >= end.y - 4; ++i, --j)
        {
            if (chessboard[i][j] == a 
                && chessboard[i][j] == chessboard[i + 1][j - 1] 
                && chessboard[i + 1][j - 1] == chessboard[i + 2][j - 2]
                && chessboard[i + 2][j - 2] == chessboard[i + 3][j - 3] 
                && chessboard[i + 3][j - 3] == chessboard[i + 4][j - 4])
                return 1;
        }
        return 0;
    }
    int judgeheqi()//判断和棋
    {
        for (int i = 1; i < N; ++i)
            for (int j = 1; j < N; ++j)
                if (chessboard[i][j] == ' ')return 1;
        return -1;
    }
    void play()
    {
        initchessboard();//初始化棋盘
        int t;
        cout << "请选择模式,人机模式输入1,人人模式输入2。" << endl;
        cin >> t;
        while (t == 1)
        {
            int m = judgeheqi();
            if (m == -1)
            {
                cout << "和棋!!!" << endl;
                break;
            }
            coordinate temp1 = computerplayer();//电脑下棋
            printchessboard();//打印棋盘
            if (judgewiner1('x', temp1) || judgewiner2('x', temp1) || judgewiner3('x', temp1) || judgewiner4('x', temp1))
            {
                cout << "电脑胜!!!";
                break;
            }
            coordinate temp2 = playchess2();//玩家2下棋
            printchessboard();//打印棋盘
            if (judgewiner1('o', temp2) || judgewiner2('o', temp2) || judgewiner3('o', temp2) || judgewiner4('o', temp2))
            {
                cout << "玩家2胜!!!";
                break;
            }
        }
        while (t == 2)
        {
            int m = judgeheqi();
            if (m == -1)
            {
                cout << "和棋!!!" << endl;
                break;
            }
            coordinate temp1 = playchess1();//电脑下棋
            printchessboard();//打印棋盘
            if (judgewiner1('x', temp1) || judgewiner2('x', temp1) || judgewiner3('x', temp1) || judgewiner4('x', temp1))
            {
                cout << "玩家1胜!!!";
                break;
            }
            coordinate temp2 = playchess2();//玩家2下棋
            printchessboard();//打印棋盘
            if (judgewiner1('o', temp2) || judgewiner2('o', temp2) || judgewiner3('o', temp2) || judgewiner4('o', temp2))
            {
                cout << "玩家2胜!!!";
                break;
            }
        }
    }
private:
    char chessboard[N][N];  //棋盘
};
int main()
{
    fivechess one;
    one.play();
    return 0;
}

运行结果:

C++实现五子棋小游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/liumanghu/article/details/89338727

延伸 · 阅读

精彩推荐
  • C/C++c语言链表操作示例分享

    c语言链表操作示例分享

    这篇文章主要介绍了c语言链表操作示例,代码中有注释,需要的朋友可以参考下...

    C语言程序设计9632021-01-17
  • C/C++C++类的定义与实现

    C++类的定义与实现

    这篇文章主要介绍了C++类的定义与实现,违章围绕C++类的定义的相关资料展开全文内容,具有一定的参考价值,需要的小伙伴可以参考一下...

    梁唐6372022-08-05
  • C/C++解析C++中不能重载为友元函数的四个运算符

    解析C++中不能重载为友元函数的四个运算符

    以下是对C++中不能重载为友元函数的四个运算符进行了详细的分析介绍,需要的朋友可以过来参考下...

    C++教程网2952020-12-21
  • C/C++C++中声明类的class与声明结构体的struct关键字详解

    C++中声明类的class与声明结构体的struct关键字详解

    这篇文章主要介绍了C++中声明类的class与声明结构体的struct关键字,默认情况下结构的所有成员均是公有的,而类的所有成员是私有的,需要的朋友可以参考下...

    C++教程网6212021-03-23
  • C/C++VC下实现fopen支持中文的方法

    VC下实现fopen支持中文的方法

    这篇文章主要介绍了VC下实现fopen支持中文的方法,需要的朋友可以参考下...

    C语言程序设计10522021-01-21
  • C/C++详解C语言中const关键字的用法

    详解C语言中const关键字的用法

    这篇文章主要对C语言中const关键字的用法进行了详细的分析介绍,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    xingjiarong6482021-03-05
  • C/C++Linux网络编程之socket文件传输示例

    Linux网络编程之socket文件传输示例

    这篇文章主要介绍了Linux网络编程之socket文件传输示例,对于基于Linux平台的C程序员来说有一定的借鉴价值,需要的朋友可以参考下...

    C语言程序设计7612021-01-28
  • C/C++C语言 结构体(Struct)详解及示例代码

    C语言 结构体(Struct)详解及示例代码

    本文主要介绍C语言 结构体的知识,学习C语言肯定需要学习结构体,这里详细说明了结构体并附示例代码,供大家参考学习,有需要的小伙伴可以参考下...

    C语言中文网4802021-04-14