服务器之家:专注于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:35A_N_Huang C/C++

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

简单C++代码实现五子棋任务,供大家参考,具体内容如下

首先先展示一下运行的图片

C++代码实现五子棋小游戏

话也不多说,直接分不同代码板块来介绍程序不同功能以及是如何实现的

首先,对于一个五子棋程序,我们要思考的,在过程式的编程思想里面,如何将其功能分解为不同的函数

1.打印棋盘

由于使用的棋子每一个棋子占两个字符,所以打印时要使用两个空格

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int b=0,w=0,player=1,x,y; //b和w分别作为参数标记黑棋白棋的胜利,player用来指明每次是下黑棋还是白棋,x,y分别用来作为棋盘的横纵坐标
int chess[11][11];//初始化
void board()//每一次打印棋盘的函数
{   
    cout << "    1 2 3 4 5 6 7 8 9 10" <<endl;
    cout << "  +--------------------+" <<endl;
    for(int i=1;i<=9;i++)
    {
        cout<<" "<<i<<"|";
        input(i-1);//input函数在后文介绍
        cout<<"|"<<endl;
    }
    cout << "10|";input(9); cout <<"|" <<endl;
    cout << "  +--------------------+" <<endl;
}

考虑到字符数组本身无法同时对连续两个字符位赋值,这里采用二位数组表示下棋位置并采用一个input函数将二维数组转化为棋子

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void init()
{
    for(int i=0;i<11;i++)
    {
        for(int j=0;j<11;j++)
        chess[i][j]=0;//初始化棋盘全为0
        }
}
void input(const int n)
{
    for(int i=n,j=0;j<10;j++)
    {
        switch(chess[i][j])//利用这个switch语句来将空白的棋盘中需要打印的棋子打印上去
        {
            case(0): cout << "  ";break;
            case(1): cout << "��";break;
            case(-1): cout << "��";break;
            }
    }
}

2.下棋部分

这一部分也是最为麻烦的部分,由于每次调用检验函数检验黑棋或白棋是否胜利会带来不必要的麻烦,所以在每一次下棋之后直接在下棋的位置往各个方向检索以判断是否胜利

?
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
void play(int x,int y)
{
    if(player==1)
    {chess[x-1][y-1]=1;//表示下黑棋
     if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)//重复的判断代码,每一次复制粘贴即可
     b=5;
     else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
     b=5;
     else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
     b=5;
     else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
     b=5;
     else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
     b=5;
     else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
     b=5;
     else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
     b=5;
     else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
     b=5;
     player=2;}
    else if(player==2)
    {chess[x-1][y-1]=-1;//表示下白棋
     if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)
     w=5;
     else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
     w=5;
     else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
     w=5;
     else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
     w=5;
     else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
     w=5;
     else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
     w=5;
     else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
     w=5;
     else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
     w=5;
     player=1;}
}

同时,我们还需要一点小小的附加代码,因为你不能保证每一次棋手下棋都是在合法位置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void judge()
{
    while(1)//c++类似的使用很多,用永真的表达式,然后判断跳出条件break,这里主要用来重复判断落子是否合法
    {
        if(x<=0||x>10||y<=0||y>10)
        {
            cout <<"invalid position,input again:"<<endl;
            cin >>x>>y;
            }
        else if(chess[x-1][y-1]!=0)
        {
            cout <<"wrong place,input again:"<<endl;
            cin >>x>>y;
            }
        else if(x>0&&x<=10&&y>0&&y<=10&&chess[x-1][y-1]==0)
            break;
        }
}

3.主函数

加下来就是main函数部分了,显而易见了

?
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
int main()
{   
    init();
    board();
    while(1)
   {
        cout << "Black: ";
        cin>>x>>y;
        judge();
        play(x,y);
        system("cls");//清屏功能
        board();
        if(b==5)
        {
            system("cls");cout << "Black win";break;
            }
        else if(w==5)
        {
            system("cls");cout << "White win";break;
            }
        cout << "White: " ;
        cin >>x>>y;
        judge();
        play(x,y);
        system("cls");
        board();
        if(b==5)
        {
            system("cls");cout << "Black win";break;
            }
        else if(w==5)
        {
            system("cls");cout << "White win";break;
            }
        }
    return 0;
}

至此,就可以实现整个五子棋代码的功能了

附上完整的代码:

?
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
#include <iostream>
using namespace std;
int b=0,w=0,player=1,x,y; //b和w分别作为参数标记黑棋白棋的胜利,player用来指明每次是下黑棋还是白棋,x,y分别用来作为棋盘的横纵坐标
int chess[11][11];//初始化
void init()
{
    for(int i=0;i<11;i++)
    {
        for(int j=0;j<11;j++)
        chess[i][j]=0;//初始化棋盘全为0
        }
}
void input(const int n)
{
    for(int i=n,j=0;j<10;j++)
    {
        switch(chess[i][j])//利用这个switch语句来将空白的棋盘中需要打印的棋子打印上去
        {
            case(0): cout << "  ";break;
            case(1): cout << "��";break;
            case(-1): cout << "��";break;
            }
    }
}
void board()//每一次打印棋盘的函数
{   
    cout << "    1 2 3 4 5 6 7 8 9 10" <<endl;
    cout << "  +--------------------+" <<endl;
    for(int i=1;i<=9;i++)
    {
        cout<<" "<<i<<"|";
        input(i-1);//input函数在后文介绍
        cout<<"|"<<endl;
    }
    cout << "10|";input(9); cout <<"|" <<endl;
    cout << "  +--------------------+" <<endl;
}
void play(int x,int y)
{
    if(player==1)
    {chess[x-1][y-1]=1;//表示下黑棋
     if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)//重复的判断代码,每一次复制粘贴即可
     b=5;
     else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
     b=5;
     else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
     b=5;
     else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
     b=5;
     else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
     b=5;
     else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
     b=5;
     else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
     b=5;
     else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
     b=5;
     player=2;}
    else if(player==2)
    {chess[x-1][y-1]=-1;//表示下白棋
     if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)
     w=5;
     else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
     w=5;
     else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
     w=5;
     else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
     w=5;
     else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
     w=5;
     else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
     w=5;
     else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
     w=5;
     else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
     w=5;
     player=1;}
}
void judge()
{
    while(1)//c++类似的使用很多,用永真的表达式,然后判断跳出条件break,这里主要用来重复判断落子是否合法
    {
        if(x<=0||x>10||y<=0||y>10)
        {
            cout <<"invalid position,input again:"<<endl;
            cin >>x>>y;
            }
        else if(chess[x-1][y-1]!=0)
        {
            cout <<"wrong place,input again:"<<endl;
            cin >>x>>y;
            }
        else if(x>0&&x<=10&&y>0&&y<=10&&chess[x-1][y-1]==0)
            break;
        }
}
int main()
{   
    init();
    board();
    while(1)
   {
        cout << "Black: ";
        cin>>x>>y;
        judge();
        play(x,y);
        system("cls");//清屏功能
        board();
        if(b==5)
        {
            system("cls");cout << "Black win";break;
            }
        else if(w==5)
        {
            system("cls");cout << "White win";break;
            }
        cout << "White: " ;
        cin >>x>>y;
        judge();
        play(x,y);
        system("cls");
        board();
        if(b==5)
        {
            system("cls");cout << "Black win";break;
            }
        else if(w==5)
        {
            system("cls");cout << "White win";break;
            }
        }
    return 0;
}

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

原文链接:https://blog.csdn.net/A_N_Huang/article/details/10465940

延伸 · 阅读

精彩推荐