C++实现五子棋小游戏

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

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

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

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;
    }

下面是完整代码:

//玩家一与电脑用‘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;
}

运行结果:

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

(0)

相关推荐

  • C++实现简易的五子棋游戏

    本文实例为大家分享了C++实现简易五子棋游戏的具体代码,供大家参考,具体内容如下 //用c++实现五子棋 #include <iostream> #include <cstdlib> #include "getch.h" using namespace std; enum Role{RED=99,BLUE}; class Gobang {     char board[15][15];     int key_x,key_y;     Role role; pu

  • C++实现简单五子棋游戏

    五子棋是世界智力运动会竞技项目之一,是一种两人对弈的纯策略型棋类游戏,是世界智力运动会竞技项目之一,通常双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成5子连线者获胜. 规则 (1)对局双方各执一色棋子. (2)空棋盘开局. (3)黑先.白后,交替下子,每次只能下一子. (4)棋子下在棋盘的空白点上,棋子下定后,不得向其它点移动,不得从棋盘上拿掉或拿起另落别处. (5)黑方的第一枚棋子可下在棋盘任意交叉点上. (6)轮流下子是双方的权利,但允许任何一方放弃下子权(即:PASS权)

  • C++实现五子棋小程序

    这是一个用C++写的五子棋的小程序,关于A若是占据了已经下了的位置处理的不好.改动 hight,与width ,与q[][] 可以将棋盘扩大. #include<iostream> #include<vector> using namespace std; class qipan { public: qipan() {} ~qipan() {}; //向上下左右,斜的方向 char left(int x, int y) {//检查是否合适 if (x >= 1 &&a

  • C++程序设计-五子棋

    前言:很多刚刚接触编程的人都不知道怎么下手编写程序,特别是学习了新的知识点,不知道有什么用,那么本文将以简单的存储结构及简单的运算,条件语句,分支语句,循环语句结合,带来一个双人对战版五子棋,这是一个简单的模型,实现了五子棋最最基本的功能,还有好多地方需要补全,如边界问题,设计问题,游戏逻辑问题,希望读者阅读后能够注意,通过自己的努力来完善它,还能扩展各种功能,如悔棋,网络对战等,有时候写程序和小生命一样,慢慢会成长,而我们作为"父母"的看到自己的小宝宝成为有用之才,过程之欣喜特别棒!

  • C++面向对象实现五子棋小游戏

    尽量将面向对象的思想融入进程序中 ChessBoard.h //ChessBoard.h #pragma once #define ROW 15 #define COL 15 #include<iostream> using namespace std; class ChessBoard//棋盘类 { public: char m_cSquare[ROW][COL]; public: ChessBoard(); void show(); }; ChessBoard.cpp //ChessBoa

  • C++实现五子棋游戏

    三子棋.五子棋之类的游戏,非常简单,对于初学者来说是一个不错的练手的小项目,以前用C语言写过三子棋游戏.最近在看C++,所以就想到在三子棋的基础上利用C++语言实现五子棋游戏. 主要功能: 有3个模式:0表示退出.1表示电脑vs玩家.2表示玩家vs玩家. 当一局完成之后选择'y'则又会进入选择模式. 源代码(VS2013编译器下写的): #include<iostream> #include<stdio.h> #include<stdlib.h> #include &l

  • C++实现简易五子棋游戏

    C++实现的简易五子棋游戏,供大家参考,具体内容如下 三个函数: void menu():                    //菜单 int fun1(char a[21][43]):   //白旗 int fun2(char a[21][43]):   //黑棋 通过二维数组来实现棋盘与棋子. 构建棋盘: 直接通过一个二维字符数组来实现棋盘,只需cout即可.比较直观,但判断条件时过于繁杂.也可以使用二维整型数组,通过不同的整数来表示不同的字符,简化判断.cout时只需根据数组元素的类型

  • 基于C++实现五子棋AI算法思想

    今天我想要分享一下我做五子棋AI的思路.因为在做这个之前,我没有接触过任何像这种类似的东西.通过这一次,我也算是有所了解,我的思路也是来自很多网络上的博客,看了很多,最终总结出了自己的这样一个. 那我的五子棋是15*15的大小(一般也就是这样的一个大小).我的AI算法要求每一次落子之后都要去计算每一个空暇的位置的"分值",简单的说,我们需要一个存放棋子的数组,表示是否存放了棋子,还要一个计算每一个空格的数组来记录"分数",这个分数是后期AI用来运算的基础,也是你AI

  • C++语言设计实现五子棋

    本文为大家分享了C++五子棋的设计思路和设计实现,供大家参考,具体内容如下 算法思路: 在结束了对C++的学习之后,准备自己编制一些简单的练习程序.目前初步设想是编制一个人机对战的简易五子棋软件. 以下为个人设计思考的过程. 首先,进行问题分析与设计.计划实现的功能为,开局选择人机或双人对战,确定之后比赛开始.比赛结束后初始化棋盘,询问是否继续比赛或退出.后续可加入复盘.悔棋等功能.整个过程中,涉及到了棋子和棋盘两种对象,同时要加上人机对弈时的AI对象,即涉及到三个对象. 棋盘类的设计. 数据存

  • C++简单五子棋的AI设计实现

    本文实例为大家分享了C++五子棋的AI设计实现代码,供大家参考,具体内容如下 设计思路:通过接口获取信息来确定颜色,通过set_chess函数来确定落点. 对每个点位给出两种颜色棋子的打分,分别存在两个15*15的数组里,数组下标代表点的位置. 确定最大值所在数组之后,遍历该数组找出所有最大值对应的位置,然后对这些位置统计另一种颜色的棋子的分数,再选取一次最大值,从而确定要落点的位置. 打分函数的设计:在四个方向分别统计然后相加.对于某一个方向的分数统计,则分为正反两个方向进行,统计的时候如果有

随机推荐