19CChessGame::CChessGame()
65CChessGame::~CChessGame()
83int CChessGame::QiZiBianMa(
int *i,
int *j, CPiece::ENUM_QiZi *qz, strCODE *pCode, ENUM_BianMa bianma)
95 *qz =
static_cast<CPiece::ENUM_QiZi
>(pCode->step[2]);
116 const char* pDescript, time_t tm)
119 QiZiBianMa(&i, &j, &qz, &step.code);
121 step.szDescript = pDescript;
126 if (m_nIndex + 1 < m_ChessGame.size())
128 m_ChessGame.resize(m_nIndex + 1);
131 m_ChessGame.push_back(step);
150 m_ChessGame.erase(--m_ChessGame.end());
169 QiZiBianMa(&i, &j, &qz, &m_ChessGame[m_nIndex].code, JieMa);
187 if (m_ChessGame.size() <= m_nIndex + 1)
190 QiZiBianMa(&i, &j, &qz, &m_ChessGame[++m_nIndex].code, JieMa);
197 if (!szFile)
return -1;
201 strncpy_s(head.head.szAppName, MAX_STRING_BUFFER, APPNAME, MAX_STRING_BUFFER);
202 strncpy_s(head.head.szAuthor, MAX_STRING_BUFFER, AUTHOR, MAX_STRING_BUFFER);
204 strncpy_s(head.head.szAppName, APPNAME, MAX_STRING_BUFFER);
205 strncpy_s(head.head.szAuthor, AUTHOR, MAX_STRING_BUFFER);
208 strncpy(head.head.szAppName, APPNAME, MAX_STRING_BUFFER);
209 strncpy(head.head.szAuthor, AUTHOR, MAX_STRING_BUFFER);
212 head.head.dwVersion = 2;
213 head.iBuShu = htons(m_ChessGame.size());
215 head.timeStart = htonl(m_tmStart);
216 head.timeEnd = htonl(m_tmEnd);
219 strncpy_s(head.szRedName, MAX_STRING_BUFFER, m_szRedName.c_str(), MAX_STRING_BUFFER);
220 strncpy_s(head.szBlackName, MAX_STRING_BUFFER, m_szBlackName.c_str(), MAX_STRING_BUFFER);
222 strncpy_s(head.szRedName, m_szRedName.c_str(), MAX_STRING_BUFFER);
223 strncpy_s(head.szBlackName, m_szBlackName.c_str(), MAX_STRING_BUFFER);
226 strncpy(head.szRedName, m_szRedName.c_str(), MAX_STRING_BUFFER);
227 strncpy(head.szBlackName, m_szBlackName.c_str(), MAX_STRING_BUFFER);
230 std::ofstream out(szFile);
233 out.write((
char*)&head,
sizeof(
strFile));
236 short nTag = htons(m_Tags.size());
237 out.write((
char*)&nTag,
sizeof (
short));
238 for(
const auto& item: m_Tags)
240 std::string key = item.first;
241 std::string val = item.second;
242 WriteStringToFile(out, key);
243 WriteStringToFile(out, val);
248 nLen = m_StartGame.size();
249 out.write(&nLen,
sizeof (
char));
252 std::vector<strStartGame>::iterator it;
253 for(it = m_StartGame.begin(); it != m_StartGame.end(); it++)
256 QiZiBianMa(&it->i, &it->j, &it->qz, &code);
257 out.write(code.step,
sizeof(strCODE));
262 std::vector<strStep>::iterator it;
263 for (it = m_ChessGame.begin(); it != m_ChessGame.end(); it++)
265 out.write(it->code.step,
sizeof(strCODE));
266 long t = htonl(it->tm);
267 out.write((
char*)&t,
sizeof(
long));
268 WriteStringToFile(out, it->szDescript);
274int CChessGame::LoadChessGame(
const char* szFile)
277 if (!szFile)
return -1;
280 memset(&head, 0,
sizeof(strFile));
282 std::ifstream in(szFile);
286 in.read((
char*)&head,
sizeof(strFile));
287 m_szRedName = head.szRedName;
288 m_szBlackName = head.szBlackName;
289 m_tmStart = ntohl(head.timeStart);
290 m_tmEnd = ntohl(head.timeEnd);
293 if (strcmp(head.head.szAppName, APPNAME))
298 if (strcmp(head.head.szAuthor, AUTHOR))
303 if (head.head.dwVersion != 2)
311 in.read((
char*)&nTags,
sizeof (
short));
312 nTags = ntohs(nTags);
313 if(nTags) m_Tags.clear();
316 std::string key, val;
317 ReadStringFromFile(in, key);
318 ReadStringFromFile(in, val);
325 in.read(&nLen,
sizeof (
char));
330 in.read(code.step,
sizeof(strCODE));
332 QiZiBianMa(&g.i, &g.j, &g.qz, &code, JieMa);
333 m_StartGame.push_back(g);
339 m_nIndex = ntohs(head.iBuShu);
343 in.read((
char*)&step.code,
sizeof(strCODE));
345 in.read((
char*)&tm,
sizeof(
long));
347 ReadStringFromFile(in, step.szDescript);
348 m_ChessGame.push_back(step);
357int CChessGame::SaveChessGamePgn(
const char *pFileName, _SavePgnFormat f)
360 if (!pFileName)
return -1;
363 std::shared_ptr<CChessSteps> Steps;
366 if(!m_szRedName.empty())
367 pgn.SetRed(m_szRedName.c_str());
368 if(!m_szBlackName.empty())
369 pgn.SetBlack(m_szBlackName.c_str());
371 CPiece::ENUM_QiZi board[9][10];
372 if(m_StartGame.empty())
379 nRet = fen.FenFromBoard(szFen, board);
381 pgn.SetFen(szFen.c_str());
383 for(
const auto& item: m_Tags)
385 std::string key = item.first;
386 std::string val = item.second;
387 pgn.SetTag(key, val);
392 pgn.SetFormat(
"ICCS");
396 pgn.SetFormat(
"WXF");
399 pgn.SetFormat(
"Chinese");
405 for(
auto& item: m_ChessGame)
408 CPiece::ENUM_QiZi qz = CPiece::NoQiZi;
409 QiZiBianMa(&i, &j, &qz, &item.code, JieMa);
410 Steps->AddStep(i, j, qz, item.szDescript);
413 std::ofstream out(pFileName);
423int CChessGame::LoadChessGamePgn(
const char *pFileName, _SavePgnFormat f)
426 if (!pFileName)
return -1;
429 std::ifstream in(pFileName);
438int CChessGame::WriteStringToFile(std::ofstream &o, std::string &s)
440 short nLen = s.size();
441 short n = htons(nLen);
442 o.write((
char*)&n,
sizeof(
short));
445 o.write(s.c_str(), nLen);
450int CChessGame::ReadStringFromFile(std::ifstream &i, std::string &s)
453 i.read((
char*)&nLen,
sizeof(
short));
457 char* pBuf =
new char[nLen + 1];
458 memset(pBuf, 0, nLen + 1);
466time_t CChessGame::GetStartTime()
471int CChessGame::SetStartTime(
const time_t& t)
477time_t CChessGame::GetEndTime()
482int CChessGame::SetEndTime(
const time_t& t)
488std::string CChessGame::GetRedName()
493int CChessGame::SetRedName(
const char* pszName)
495 m_szRedName = pszName;
499std::string CChessGame::GetBlackName()
501 return m_szBlackName;
504int CChessGame::SetBlackName(
const char* pszName)
506 m_szBlackName = pszName;
510std::string CChessGame::GetTag(
const std::string &szpTag)
512 std::map<std::string, std::string>::iterator it;
513 it = m_Tags.find(szpTag);
514 if(m_Tags.end() != it)
516 return std::string();
519int CChessGame::AddTag(
const std::string &szTag,
const std::string &szVal)
521 m_Tags[szTag] = szVal;
547 for(
int i = 0; i < 9; i++)
548 for(
int j = 0; j < 10; j++)
560 if(!((2 == i && 9 == j) || (6 == i && 9 == j)
561 || (0 == i && 7 == j) || (8 == i && 7 == j)
562 || (2 == i && 5 == j) || (6 == i && 5 == j)
568 if(i < 3 || i > 5 || j < 7)
570 if((i == 3 || 5 == i) && j == 8)
572 if(i == 4 && (j == 9 || j == 7))
577 if(i < 3 || i > 5 || j < 7)
602 if(!((2 == i && 0 == j) || (6 == i && 0 == j)
603 || (0 == i && 2 == j) || (8 == i && 2 == j)
604 || (2 == i && 4 == j) || (6 == i && 4 == j)
610 if(i < 3 || i > 5 || j > 2)
612 if((i == 3 || 5 == i) && j == 1)
614 if(i == 4 && (j == 0 || j == 2))
619 if(i < 3 || i > 5 || j > 2)
626 if(nBChe > 2 || nRChe > 2
627 || nBMa > 2 || nRMa > 2
628 || nBXiang > 2 || nRXiang > 2
629 || nBShi > 2 || nBShi > 2
630 || nBShuai > 1 || nRShuai > 1
631 || nBPao > 2 || nRPao > 2
632 || nBBing > 5 || nRBing > 5)
642 for (i = 0; i < 9; i++)
643 for (j = 0; j < 10; j++)
645 board[i][j] = CPiece::NoQiZi;
648 if(m_StartGame.size())
650 std::vector<CChessGame::strStartGame>::iterator it;
651 for(it = m_StartGame.begin(); it != m_StartGame.end(); it++)
653 board[it->i][it->j] = it->qz;
659 board[0][0] = board[8][0] = CPiece::BChe;
660 board[1][0] = board[7][0] = CPiece::BMa;
661 board[2][0] = board[6][0] = CPiece::BXiang;
662 board[3][0] = board[5][0] = CPiece::BShi;
663 board[4][0] = CPiece::BShuai;
664 board[1][2] = board[7][2] = CPiece::BPao;
665 board[0][3] = board[2][3] = board[4][3] = board[6][3] = board[8][3] = CPiece::BBing;
667 board[0][9] = board[8][9] = CPiece::RChe;
668 board[1][9] = board[7][9] = CPiece::RMa;
669 board[2][9] = board[6][9] = CPiece::RXiang;
670 board[3][9] = board[5][9] = CPiece::RShi;
671 board[4][9] = CPiece::RShuai;
672 board[1][7] = board[7][7] = CPiece::RPao;
673 board[0][6] = board[2][6] = board[4][6] = board[6][6] = board[8][6] = CPiece::RBing;
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 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 CChessStepsIccs class
福斯夫-爱德华兹记号法 (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.