C++练习实例

您所在的位置:网站首页 中国象棋游戏规则玩法 C++练习实例

C++练习实例

2024-06-06 16:50| 来源: 网络整理| 查看: 265

通过在控制台输出字符来实现一个中国象棋小游戏实际上是很简单的,也非常有趣。游戏是人人对战模式,实现后的效果如下:

代码思路很简单,就是创建好各个游戏对象的类,然后用一个管理类来实现规则就可以了。下面直接上代码:

Point类

#ifndef POINT_H #define POINT_H #include using namespace std; //坐标类 class Point { public: Point(int x = 0, int y = 0) : m_x(x), m_y(y) {}; ~Point() {}; Point& operator=(const Point &p) { m_x = p.m_x; m_y = p.m_y; return *this; } bool operator==(const Point &p)const { if (m_x == p.m_x&&m_y == p.m_y) return true; else return false; } void Set(const int x, const int y) { m_x = x; m_y = y; } void SetX(const int x) { m_x = x; } void SetY(const int y) { m_y = y; } int GetX() const { return m_x; } int GetY()const { return m_y; } private: int m_x; int m_y; }; #endif

 Chess类

#ifndef CHESS_H #define CHESS_H #include"Point.h" enum ChessCamp//棋子阵营(红绿两方) { RED,GREEN }; enum ChessType//棋子种类 { MA,SHUAI,CHE,PAO,SHI,BING,XIANG }; class Chess { public: Chess(ChessType type, ChessCamp camp, Point position) :m_type(type),m_camp(camp),m_position(position) {} ~Chess(){} Point GetPosition()const { return m_position; } void SetPosition(const Point& newposition) { m_position = newposition; } ChessType GetType()const { return m_type; } ChessCamp GetCamp()const { return m_camp; } private: ChessType m_type; ChessCamp m_camp; Point m_position; }; #endif // !CHESS_H

ChessBoard(棋盘)类

#ifndef CHESSBOARD_H #define CHESSBOARD_H #include #include"chess.h" class ChessBoard { public: bool m_isend; ChessBoard(); ~ChessBoard() {} void PrintBoard(); void ChangeChess(const Point& spos, const Point& tpos); Chess* GetChess(const Point& pos)const { return m_allchess[pos.GetX()][pos.GetY()]; } void Clear() { for (int i = 0; i < 10; i++) { for (int j = 0; j < 9; j++) { if (m_allchess[i][j] != nullptr) { delete m_allchess[i][j]; m_allchess[i][j] = nullptr; } } } } private: Chess* m_allchess[10][9]; }; #endif #include"ChessBoard.h" ChessBoard::ChessBoard() { m_isend = false; for (int i = 0; i < 10; i++) { for (int j = 0; j < 9; j++) m_allchess[i][j] = nullptr; } //四个车 m_allchess[0][0] = new Chess(CHE, GREEN, Point(0, 0)); m_allchess[0][8] = new Chess(CHE, GREEN, Point(0, 8)); m_allchess[9][0] = new Chess(CHE, RED, Point(9, 0)); m_allchess[9][8] = new Chess(CHE, RED, Point(9, 8)); //四个马 m_allchess[0][1] = new Chess(MA, GREEN, Point(0, 1)); m_allchess[0][7] = new Chess(MA, GREEN, Point(0, 7)); m_allchess[9][1] = new Chess(MA, RED, Point(9, 1)); m_allchess[9][7] = new Chess(MA, RED, Point(9, 7)); //四个炮 m_allchess[2][1] = new Chess(PAO, GREEN, Point(2, 1)); m_allchess[2][7] = new Chess(PAO, GREEN, Point(2, 7)); m_allchess[7][1] = new Chess(PAO, RED, Point(7, 1)); m_allchess[7][7] = new Chess(PAO, RED, Point(7, 7)); //两个帅 m_allchess[0][4] = new Chess(SHUAI, GREEN, Point(0, 4)); m_allchess[9][4] = new Chess(SHUAI, RED, Point(9, 4)); //象 m_allchess[0][2] = new Chess(XIANG, GREEN, Point(0, 2)); m_allchess[0][6] = new Chess(XIANG, GREEN, Point(0, 6)); m_allchess[9][2] = new Chess(XIANG, RED, Point(9, 2)); m_allchess[9][6] = new Chess(XIANG, RED, Point(9, 6)); //士 m_allchess[0][3] = new Chess(SHI, GREEN, Point(0, 3)); m_allchess[0][5] = new Chess(SHI, GREEN, Point(0, 5)); m_allchess[9][3] = new Chess(SHI, RED, Point(9, 3)); m_allchess[9][5] = new Chess(SHI, RED, Point(9, 5)); //兵 for (int i = 0; i GetType() == SHI) { int tempx = targetpos.GetX() - startpos.GetX(); int tempy = targetpos.GetY() - startpos.GetY(); if ((tempx*tempx + tempy*tempy == 2) && (targetpos.GetY()=3) && (targetpos.GetX() >= 7 || targetpos.GetX() GetType() == SHUAI) { int tempx = targetpos.GetX() - startpos.GetX(); int tempy = targetpos.GetY() - startpos.GetY(); if ((tempx*tempx + tempy*tempy == 1) && (targetpos.GetY() = 3) && (targetpos.GetX() >= 7 || targetpos.GetX() GetType() == XIANG) { int tempx = targetpos.GetX() - startpos.GetX(); int tempy = targetpos.GetY() - startpos.GetY(); //若形成田字,并且没有踩象眼 if (tempx*tempx + tempy*tempy == 8 && m_chessboard.GetChess(Point(startpos.GetX() + tempx / 2, startpos.GetY() + tempy / 2)) == nullptr) //没有将要过河 if(m_chessboard.GetChess(startpos)->GetCamp() == RED&&targetpos.GetX() >4|| m_chessboard.GetChess(startpos)->GetCamp() == GREEN&&targetpos.GetX() GetType() == BING) { int tempx = targetpos.GetX() - startpos.GetX(); int tempy = targetpos.GetY() - startpos.GetY(); if (tempx*tempx + tempy*tempy == 1)//走了一步 { if (JudgeCrossRiver((startpos)))//若过河了 return true; else //还没过河 { //若是往前走 if (m_chessboard.GetChess(startpos)->GetCamp() == RED&&tempx == -1 || m_chessboard.GetChess(startpos)->GetCamp() == GREEN&&tempx == 1) return true; } } } return false; } bool GameManager::Move(const Point& startpos, const Point& targetpos) { if (JudgeMoveLegel(startpos, targetpos) && JudgeMoveChessRule(startpos, targetpos)) { m_chessboard.ChangeChess(startpos, targetpos); return true; } else return false; } int GameManager::JudgeChessBetween(const Point& startpos, const Point& targetpos) { int num = 0; //若两点x轴相等 if (startpos.GetX() == targetpos.GetX()) { int x = startpos.GetX(); if (startpos.GetY() > targetpos.GetY()) { for (int i = targetpos.GetY() + 1; i < startpos.GetY(); i++) { if (m_chessboard.GetChess(Point(x, i)) != nullptr)num++; } } else { for (int i = startpos.GetY() + 1; i < targetpos.GetY(); i++) { if (m_chessboard.GetChess(Point(x, i)) != nullptr)num++; } } } //若两点y轴相等 else if (startpos.GetY() == targetpos.GetY()) { int y = startpos.GetY(); if (startpos.GetX() > targetpos.GetX()) { for (int i = targetpos.GetX() + 1; i < startpos.GetX(); i++) { if (m_chessboard.GetChess(Point(i, y)) != nullptr)num++; } } else { for (int i = startpos.GetX() + 1; i < targetpos.GetX(); i++) { if (m_chessboard.GetChess(Point(i, y)) != nullptr)num++; } } } return num; } bool GameManager::JudgeCrossRiver(const Point & pos) { if (m_chessboard.GetChess(pos)->GetCamp() == RED&&pos.GetX() < 5) return true; if (m_chessboard.GetChess(pos)->GetCamp() == GREEN&&pos.GetX() > 4) return true; return false; }

main函数

#include"GameManager.h" #define mymanager GameManager::Instance() int main() { cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3