五子棋程序的一种实现

本文提供一种五子棋程序的c++代码实现。
代码目录如下:
- main.cpp
- Player.cpp
- Player.h
- ChessBoard.cpp
- ChessBoard.h

以下是main.cpp

//main.cpp
#include"ChessBoard.h"
#include"Player.h"

int main()
{
    ChessBoard *board = ChessBoard::getinstance();
    Player playA("A", '*');//玩家A的棋子形状是'*'
    playA.attachToBoard(board);
    Player playB("B", '#');//玩家B的棋子形状是'#'
    playB.attachToBoard(board);
    board->show();
    while (1)
    {
        playA.setChess();//玩家A放下一个棋子
        if (playA.isWin())
        {
            cout << "Winer!"; break;
        }
        board->show();

        playB.setChess();//玩家B放下一个棋子
        if (playB.isWin())
        {
            cout << "Winer!"; break;
        }
        board->show();
    }
    return 1;
}

以下是Player.cpp

//Player.cpp
#include"Player.h"

bool Player::isInChessBoard(int x, int y)
{
    if (x1 && x>0 && y1 && y>0)
        return true;
    else
        return false;
}
/*下面是核心代码:如何判断五子是否连成线。
这里采用的是以玩家此刻放下的棋子为中心,从四种方向逐个判断是否在此方向上连成了线。
*/
bool Player::isLine(int x, int y)
{
    int re = 1; 
    int dis_x = x, dis_y = y;
    int i = 0;
    int flag[4][4] = { {-1,0,1,0}, 
                       {0,-1,0,1},
                       {-1,-1,1,1},
                       {-1, 1, 1, -1} };
    for (i = 0; i < 4; i++){
        while (isInChessBoard(x + flag[i][0], y + flag[i][1]) && m_ptBoard->m_cSquare[x + flag[i][0]][y + flag[i][1]] == m_chessType){
            x += flag[i][0];
            y += flag[i][1];
            re++;
        }
        x = dis_x;
        y = dis_y;
        while (isInChessBoard(x + flag[i][2], y + flag[i][3]) && m_ptBoard->m_cSquare[x + flag[i][2]][y + flag[i][3]] == m_chessType){
            x += flag[i][2];
            y += flag[i][3];
            re++;
        }
        x = dis_x;
        y = dis_y;
        if (re >= 5){
            return true;
        }
        else{
            re = 1;
        }
    }
    return false;   
}

void Player::setChess()
{
    cout << "请输入玩家" << m_name << "的x坐标和y坐标:" << endl;
    cin >> m_x >> m_y;
    while (cin.fail() || m_ptBoard->m_cSquare[m_x][m_y] != ' ')//输入不是int型变量或者此位置上已有棋子
    {
        cout << "输入有误,请再次输入玩家" << m_name << "的x坐标和y坐标:" << endl;
        cin.clear();    //清除fail状态  
        cin.sync();   //清除缓冲区
        cin >> m_x >> m_y;
    }
    if (isInChessBoard(m_x, m_y))
        m_ptBoard->m_cSquare[m_x][m_y] = m_chessType;
}

bool Player::isWin()
{
    return isLine(m_x, m_y) ? true : false;
}

以下是Player.h

//Player.h
#pragma once
//宏定义四种检测五子是否连成线的方向
#define HORIZON         0
#define VERTICAL            1
#define LEFTBOTTOMTORIGHTTOP    2
#define LEFTTOPTORIGHTBOTTOM    3

#include"ChessBoard.h"
#include
using namespace std;
#include

class Player
{
private:
    string m_name;
    char m_chessType;
    int m_x;
    int m_y;
    ChessBoard* m_ptBoard;
public:
    Player(string name, char chessType) :m_name(name), m_chessType(chessType), m_ptBoard(NULL){}
    void attachToBoard(ChessBoard* ptBoard){ m_ptBoard = ptBoard; }
    bool isInChessBoard(int x, int y);
    bool isLine(int x, int y);
    bool isWin();
    void setChess();
};

以下是ChessBoard.cpp

//ChessBoard.cpp
#include"ChessBoard.h"
void ChessBoard::show()
{
    system("cls");
    for (int i = 0; ifor (int j = 0; jcout << m_cSquare[i][j] << ' ';//这里的“ <<' ' ”很重要,这样才能使屏幕上ROW*COL输出为方形
        cout << endl;
    }
}

以下是 ChessBoard.h

//ChessBoard.h
#pragma once
#define ROW 15
#define COL 15
#include
using namespace std;

class ChessBoard//棋盘类
{
public:
    char m_cSquare[ROW][COL];
public: 
    static ChessBoard* getinstance(){
        static ChessBoard instance;
        return &instance;
    }
    void show();
private:
    ChessBoard()
    {
        for (int i = 1; i1; i++)
            for (int j = 1; j1; j++)
                m_cSquare[i][j] = ' ';
        for (int j = 0; j0][j] = m_cSquare[ROW - 1][j] = '-';
        for (int i = 1; i0] = m_cSquare[i][COL - 1] = '|';
    }
    ChessBoard(const ChessBoard &s){}
    ChessBoard& operator=(const ChessBoard &s){}
};

你可能感兴趣的:(小程序)