19CChessGame::CChessGame()
65CChessGame::~CChessGame()
110 const char* pDescript, time_t tm)
191 if (!szFile)
return -1;
195 strncpy_s(head.head.szAppName, MAX_STRING_BUFFER, APPNAME, MAX_STRING_BUFFER);
196 strncpy_s(head.head.szAuthor, MAX_STRING_BUFFER, AUTHOR, MAX_STRING_BUFFER);
198 strncpy_s(head.head.szAppName, APPNAME, MAX_STRING_BUFFER);
199 strncpy_s(head.head.szAuthor, AUTHOR, MAX_STRING_BUFFER);
202 strncpy(head.head.szAppName, APPNAME, MAX_STRING_BUFFER);
203 strncpy(head.head.szAuthor, AUTHOR, MAX_STRING_BUFFER);
206 head.head.dwVersion = 2;
213 strncpy_s(head.szRedName, MAX_STRING_BUFFER,
m_szRedName.c_str(), MAX_STRING_BUFFER);
214 strncpy_s(head.szBlackName, MAX_STRING_BUFFER,
m_szBlackName.c_str(), MAX_STRING_BUFFER);
216 strncpy_s(head.szRedName,
m_szRedName.c_str(), MAX_STRING_BUFFER);
217 strncpy_s(head.szBlackName,
m_szBlackName.c_str(), MAX_STRING_BUFFER);
220 strncpy(head.szRedName,
m_szRedName.c_str(), MAX_STRING_BUFFER);
221 strncpy(head.szBlackName,
m_szBlackName.c_str(), MAX_STRING_BUFFER);
224 std::ofstream out(szFile);
227 out.write((
char*)&head,
sizeof(
strFile));
230 short nTag = htons(
m_Tags.size());
231 out.write((
char*)&nTag,
sizeof (
short));
232 for(
const auto& item:
m_Tags)
234 std::string key = item.first;
235 std::string val = item.second;
236 WriteStringToFile(out, key);
237 WriteStringToFile(out, val);
242 nLen = m_StartGame.size();
243 out.write(&nLen,
sizeof (
char));
246 std::vector<strStartGame>::iterator it;
247 for(it = m_StartGame.begin(); it != m_StartGame.end(); it++)
251 out.write(code.step,
sizeof(
strCODE));
256 std::vector<strStep>::iterator it;
259 out.write(it->code.step,
sizeof(
strCODE));
260 long t = htonl(it->tm);
261 out.write((
char*)&t,
sizeof(
long));
262 WriteStringToFile(out, it->szDescript);
268int CChessGame::LoadChessGame(
const char* szFile)
271 if (!szFile)
return -1;
274 memset(&head, 0,
sizeof(strFile));
276 std::ifstream in(szFile);
280 in.read((
char*)&head,
sizeof(strFile));
287 if (strcmp(head.head.szAppName, APPNAME))
292 if (strcmp(head.head.szAuthor, AUTHOR))
297 if (head.head.dwVersion != 2)
305 in.read((
char*)&nTags,
sizeof (
short));
306 nTags = ntohs(nTags);
310 std::string key, val;
311 ReadStringFromFile(in, key);
312 ReadStringFromFile(in, val);
319 in.read(&nLen,
sizeof (
char));
324 in.read(code.step,
sizeof(strCODE));
327 m_StartGame.push_back(g);
333 m_nIndex = ntohs(head.iBuShu);
337 in.read((
char*)&step.code,
sizeof(strCODE));
339 in.read((
char*)&tm,
sizeof(
long));
341 ReadStringFromFile(in, step.szDescript);
351int CChessGame::SaveChessGamePgn(
const char *pFileName, _SavePgnFormat f)
354 if (!pFileName)
return -1;
357 std::shared_ptr<CChessSteps> Steps;
366 if(m_StartGame.empty())
373 nRet = fen.FenFromBoard(szFen, board);
375 pgn.SetFen(szFen.c_str());
377 for(
const auto& item:
m_Tags)
379 std::string key = item.first;
380 std::string val = item.second;
381 pgn.SetTag(key, val);
386 pgn.SetFormat(
"ICCS");
390 pgn.SetFormat(
"WXF");
393 pgn.SetFormat(
"Chinese");
404 Steps->AddStep(i, j, qz, item.szDescript);
407 std::ofstream out(pFileName);
417int CChessGame::LoadChessGamePgn(
const char *pFileName, _SavePgnFormat f)
420 if (!pFileName)
return -1;
423 std::ifstream in(pFileName);
432int CChessGame::WriteStringToFile(std::ofstream &o, std::string &s)
434 short nLen = s.size();
435 short n = htons(nLen);
436 o.write((
char*)&n,
sizeof(
short));
439 o.write(s.c_str(), nLen);
444int CChessGame::ReadStringFromFile(std::ifstream &i, std::string &s)
447 i.read((
char*)&nLen,
sizeof(
short));
451 char* pBuf =
new char[nLen + 1];
452 memset(pBuf, 0, nLen + 1);
460time_t CChessGame::GetStartTime()
465int CChessGame::SetStartTime(
const time_t& t)
471time_t CChessGame::GetEndTime()
476int CChessGame::SetEndTime(
const time_t& t)
482std::string CChessGame::GetRedName()
487int CChessGame::SetRedName(
const char* pszName)
493std::string CChessGame::GetBlackName()
498int CChessGame::SetBlackName(
const char* pszName)
504std::string CChessGame::GetTag(
const std::string &szpTag)
506 std::map<std::string, std::string>::iterator it;
510 return std::string();
513int CChessGame::AddTag(
const std::string &szTag,
const std::string &szVal)
541 for(
int i = 0; i < 9; i++) {
542 for(
int j = 0; j < 10; j++) {
553 if(!((2 == i && 9 == j) || (6 == i && 9 == j)
554 || (0 == i && 7 == j) || (8 == i && 7 == j)
555 || (2 == i && 5 == j) || (6 == i && 5 == j)
561 if((i < 3) || (i > 5) || (j < 7))
563 if((i == 3 || 5 == i) && j == 8)
565 if(i == 4 && (j == 9 || j == 7))
570 if((i < 3) || (i > 5) || (j < 7))
595 if(!((2 == i && 0 == j) || (6 == i && 0 == j)
596 || (0 == i && 2 == j) || (8 == i && 2 == j)
597 || (2 == i && 4 == j) || (6 == i && 4 == j)
603 if((i < 3) || (i > 5) || (j > 2))
605 if((i == 3 || 5 == i) && j == 1)
607 if(i == 4 && (j == 0 || j == 2))
612 if((i < 3) || (i > 5) || (j > 2))
621 if(nBChe > 2 || nRChe > 2
622 || nBMa > 2 || nRMa > 2
623 || nBXiang > 2 || nRXiang > 2
624 || nBShi > 2 || nBShi > 2
625 || nBShuai > 1 || nRShuai > 1
626 || nBPao > 2 || nRPao > 2
627 || nBBing > 5 || nRBing > 5)
637 for (i = 0; i < 9; i++)
638 for (j = 0; j < 10; j++)
640 board[i][j] = CPiece::NoQiZi;
643 if(m_StartGame.size())
645 std::vector<CChessGame::strStartGame>::iterator it;
646 for(it = m_StartGame.begin(); it != m_StartGame.end(); it++)
648 board[it->i][it->j] = it->qz;
654 board[0][0] = board[8][0] = CPiece::BChe;
655 board[1][0] = board[7][0] = CPiece::BMa;
656 board[2][0] = board[6][0] = CPiece::BXiang;
657 board[3][0] = board[5][0] = CPiece::BShi;
658 board[4][0] = CPiece::BShuai;
659 board[1][2] = board[7][2] = CPiece::BPao;
660 board[0][3] = board[2][3] = board[4][3] = board[6][3] = board[8][3] = CPiece::BBing;
662 board[0][9] = board[8][9] = CPiece::RChe;
663 board[1][9] = board[7][9] = CPiece::RMa;
664 board[2][9] = board[6][9] = CPiece::RXiang;
665 board[3][9] = board[5][9] = CPiece::RShi;
666 board[4][9] = CPiece::RShuai;
667 board[1][7] = board[7][7] = CPiece::RPao;
668 board[0][6] = board[2][6] = board[4][6] = board[6][6] = board[8][6] = CPiece::RBing;
std::string m_szBlackName
红方名
std::map< std::string, std::string > m_Tags
黑方名
std::string m_szRedName
结束时间
int GetPreviouStep(int &i, int &j, CPiece::ENUM_QiZi &qz)
函数名:GetPreviouStep 功 能:上步棋 参 数:无 返回值:走棋步数 作 者:康 林 版 本:1.0.0.1 日 期:2004-10-5 时 间:10:19:51
int SaveChessGame(const char *pFileName)
Saves the chess game
int QiZiBianMa(int *i, int *j, CPiece::ENUM_QiZi *qz, strCODE *pCode, ENUM_BianMa bianma=BianMa)
棋子编解码
std::vector< strStep > m_ChessGame
复盘标志
int SaveStep(int i, int j, CPiece::ENUM_QiZi qz, const char *pDescript=nullptr, time_t tm=time(nullptr))
保存一步
int GetNextStep(int &i, int &j, CPiece::ENUM_QiZi &qz)
Gets the next step
int GetStartGameBoard(CPiece::ENUM_QiZi board[][10])
得到开局棋盘布局
static int CheckGame(const CPiece::ENUM_QiZi board[][10])
棋盘开局 检测布局是否合法, 使用标准棋盘布局,红下黑上
The CChessStepsChinese class
ICCS是中国象棋互联网服务器(Internet Chinese Chess Server)的缩写。
福斯夫-爱德华兹记号法 (Forsyth-Edwards Notation)
Class to hold all information for the Portable Game Notation (PGN) of a single game.
std::string toString() const
Gets the game as PGN string.
enum CPiece::_ENUM_QiZi ENUM_QiZi
棋子枚举值 四个位表示棋子,最左1位表示颜色
std::string szDescript
走棋时间