// 重新排列 void CLogic::Realign(BYTE chess[CHESSCOUNT_H][CHESSCOUNT_W]) { CArrayTemplate<CPoint> pos; CArrayTemplate<BYTE> data; int temp,temp2; for(int i=0; i<CHESSCOUNT_H; i++) for(int j=0; j<CHESSCOUNT_W; j++) { if(chess[i][j] != ERROR_ALL) { pos.InsertAt(pos.GetCount(),CPoint(i,j)); data.InsertAt(data.GetCount(),chess[i][j]); } chess[i][j] = ERROR_ALL; } srand((unsigned int)time(NULL)); for(; pos.GetCount()>0;) { temp = rand()%((int)pos.GetCount()); temp2 = rand()%((int)data.GetCount()); chess[pos.ElementAt(temp).x][pos.ElementAt(temp).y] = data[temp2]; pos.RemoveAt(temp); data.RemoveAt(temp2); } }
//游戏开始 bool __cdecl CTableFrameSink::OnEventGameStart() { m_bIsGameEnd=false; m_pITableFrame->SetGameStatus(GS_PLAYING); srand((UINT)time(NULL)); int nRandNum=rand()%19; CMD_S_GameStart start; //ZeroMemory(&start,sizeof(start)); //memset(&start,ERROR_ALL,sizeof(start)); memset(start.bChess, ERROR_ALL, sizeof(start.bChess)); //初始化棋盘 int tempvalue,randnum; CArrayTemplate<CPoint> Map; for(int i=0;i<CHESSCOUNT_H;i++) { for(int j=0;j<CHESSCOUNT_W;j++) { if(LLK_MAP[nRandNum][i][j]) { Map.Add(CPoint(i,j)); } } } if(Map.GetCount()%2 == 1) { TRACE("地图棋子为奇数"); return false; } //随地图 for(; Map.GetCount()!=0;) { //随机一个棋子类型 if(rand()%10) { tempvalue = RandNumber(0,MAX_CHESS_VALUE); } else { tempvalue =RandNumber(PROP_MIN_VALUE,PROP_MIN_VALUE+PROP_MAX_CNT); } //找出第一个位置 randnum = RandNumber(0,(int)Map.GetCount()); start.bChess[Map.ElementAt(randnum).x][Map.ElementAt(randnum).y] = tempvalue; Map.RemoveAt(randnum); //找出第二个位置 randnum = RandNumber(0,(int)Map.GetCount()); start.bChess[Map.ElementAt(randnum).x][Map.ElementAt(randnum).y] = tempvalue; Map.RemoveAt(randnum); } IServerUserItem * pIServerUserItem; for (WORD i=0;i<GAME_PLAYER;i++) { pIServerUserItem=m_pITableFrame->GetServerUserItem(i); if (pIServerUserItem!=NULL) { m_bPlayStatus [i] = GS_PLAYING; memcpy(m_bChess[i],start.bChess,sizeof(m_bChess[i])); } else m_bPlayStatus [i] = GS_FREE; } memcpy(start.bPlayState, m_bPlayStatus, sizeof(m_bPlayStatus)); //发送地图数据 for(int i=0;i<GAME_PLAYER;i++) { memcpy(start.bPropCnt,m_bPropCnt[i],sizeof(m_bPropCnt[i])); m_pITableFrame->SendTableData(i,SUB_S_GAME_START,&start,sizeof(start)); m_pITableFrame->SendLookonData(i,SUB_S_GAME_START,&start,sizeof(start)); } return true; }
//游戏消息处理 bool __cdecl CTableFrameSink::OnGameMessage(WORD wSubCmdID, const void * pDataBuffer, WORD wDataSize, IServerUserItem * pIServerUserItem) { if(m_bIsGameEnd) { return true; } switch (wSubCmdID) { case SUB_C_GUIDE: { WORD wSeat = pIServerUserItem->GetChairID(); //指南针道具 m_bPropCnt[wSeat][PROP_GUIDE]--; return true; } case SUB_C_BOMB: { WORD wSeat = pIServerUserItem->GetChairID(); //指南针道具 m_bPropCnt[wSeat][PROP_BOM]--; return true; } case SUB_C_STOPIT: { if(wDataSize != sizeof(CMD_C_StopIt)) return false; CMD_C_StopIt *pCnt = (CMD_C_StopIt *)pDataBuffer; WORD wSeat = pIServerUserItem->GetChairID(); CMD_S_StopIt Stop; Stop.nSrcUserId =(BYTE)wSeat; Stop.nDesUserId =pCnt->nDesUserId; //禁手道具 m_bPropCnt[wSeat][PROP_STOPIT]--; Stop.nPropCount = m_bPropCnt[wSeat][PROP_STOPIT]; m_pITableFrame->SendTableData(INVALID_CHAIR,SUB_S_STOPIT,&Stop,sizeof(Stop)); m_pITableFrame->SendLookonData(INVALID_CHAIR,SUB_S_STOPIT,&Stop,sizeof(Stop)); return true; } case SUB_C_CLOG: { if(wDataSize != sizeof(CMD_C_Clog)) return false; CMD_C_Clog *pCnt = (CMD_C_Clog *)pDataBuffer; WORD wSeat = pIServerUserItem->GetChairID(); CMD_S_Clog Clog; memset(&Clog,ERROR_ALL,sizeof(Clog)); Clog.nDesUserId = pCnt->nDesUserId; Clog.nSrcUserId = (BYTE)wSeat; CArrayTemplate<CPoint> Map; for(int i=0;i<CHESSCOUNT_H;i++) { for(int j=0;j<CHESSCOUNT_W;j++) { if(m_bChess[pCnt->nDesUserId][i][j] == ERROR_ALL &&i>0 &&i<CHESSCOUNT_H-1&&\ j>0 && j<CHESSCOUNT_W-1) { Map.Add(CPoint(i,j)); } } } if(Map.GetCount()) { int randMaxNum=(int)Map.GetCount() >=12?12:(int)Map.GetCount(); //取偶数 randMaxNum = randMaxNum%2?randMaxNum-1:randMaxNum; srand((unsigned int)time(NULL)); int pos=0; while(randMaxNum>0) { BYTE chessValue =RandNumber(0,MAX_CHESS_VALUE); int randNum = rand()%(int)Map.GetCount(); Clog.nChessPosX[pos] =(BYTE)Map.GetAt(randNum).y; Clog.nChessPosY[pos] =(BYTE)Map.GetAt(randNum).x; Clog.nChessFace[pos]=chessValue; pos++; Map.RemoveAt(randNum); randMaxNum--; randNum = rand()%(int)Map.GetCount(); Clog.nChessPosX[pos] =(BYTE)Map.GetAt(randNum).y; Clog.nChessPosY[pos] =(BYTE)Map.GetAt(randNum).x; Clog.nChessFace[pos]=chessValue; pos++; Map.RemoveAt(randNum); randMaxNum--; } } //障碍道具 m_bPropCnt[wSeat][PROP_CLOG]--; Clog.nPropCount = m_bPropCnt[wSeat][PROP_CLOG]; m_pITableFrame->SendTableData(INVALID_CHAIR,SUB_S_CLOG,&Clog,sizeof(Clog)); m_pITableFrame->SendLookonData(INVALID_CHAIR,SUB_S_CLOG,&Clog,sizeof(Clog)); return true; } case SUB_C_TIMER: { if(wDataSize != sizeof(CMD_C_Timer)) return false; CMD_C_Timer *pCnt = (CMD_C_Timer *)pDataBuffer; WORD wSeat = pIServerUserItem->GetChairID(); m_bTimerCnt[wSeat]=pCnt->nTime; CString str; str.Format("--%d--\n",m_bTimerCnt[wSeat]); TRACE(str); if(m_bTimerCnt[wSeat] ==0) { return SubGameLost(wSeat); } return true; } case SUB_C_LINK: { if(wDataSize != sizeof(CMD_C_Link)) return false; CMD_C_Link *pPos = (CMD_C_Link *)pDataBuffer; WORD wSeat = pIServerUserItem->GetChairID(); CArrayTemplate<CPoint> path; CPoint p1,p2; if(!m_GameLogic.IsLink(m_bChess[wSeat],CPoint(pPos->nFirst_X,pPos->nFirst_Y),CPoint(pPos->nEnd_X,pPos->nEnd_Y),\ &path,&p1,&p2)) { return false; } else { if(pPos->bLinkKind ==LK_SERIES_LINK) { m_bSeriesLinkCnt[wSeat] ++; } BYTE bValue =m_bChess[wSeat][pPos->nFirst_Y][pPos->nFirst_X]; m_bChess[wSeat][pPos->nFirst_Y][pPos->nFirst_X]=ERROR_ALL; m_bChess[wSeat][pPos->nEnd_Y][pPos->nEnd_X]=ERROR_ALL; CMD_S_Link LinkPos; ZeroMemory(&LinkPos,sizeof(LinkPos)); LinkPos.nEnd_X =pPos->nEnd_X; LinkPos.nEnd_Y =pPos->nEnd_Y; LinkPos.nFirst_X =pPos->nFirst_X; LinkPos.nFirst_Y =pPos->nFirst_Y; LinkPos.nCurrentPlayer = (BYTE)wSeat; if(bValue >=PROP_MIN_VALUE) { m_bPropCnt[wSeat][bValue-PROP_MIN_VALUE] ++; LinkPos.nPropCount =m_bPropCnt[wSeat][bValue-PROP_MIN_VALUE]; LinkPos.nPropType = bValue-PROP_MIN_VALUE; } else LinkPos.nPropType = ERROR_ALL; m_pITableFrame->SendTableData(INVALID_CHAIR,SUB_S_LINK,&LinkPos,sizeof(LinkPos)); m_pITableFrame->SendLookonData(INVALID_CHAIR,SUB_S_LINK,&LinkPos,sizeof(LinkPos)); //检验游戏是否结束 bool bIsGameEnd=true; for(int i=0;i<CHESSCOUNT_H;i++) { for(int j=0;j<CHESSCOUNT_W;j++) { if(m_bChess[wSeat][i][j] != ERROR_ALL) { i=CHESSCOUNT_H; bIsGameEnd=false; break; } } } if(bIsGameEnd) { GameEnd(wSeat,GER_NORMAL); } return true; } } case SUB_C_REALIGN: { if(wDataSize != sizeof(CMD_C_Realign)) return false; CMD_C_Realign *pcRealign = (CMD_C_Realign *)pDataBuffer; WORD wSeat = pIServerUserItem->GetChairID(); memcpy(m_bChess[wSeat],pcRealign->nChess,sizeof(pcRealign->nChess)); CMD_S_Realign sRealign; sRealign.nCurrentPlayer = (BYTE)wSeat; memcpy(sRealign.nChess,pcRealign->nChess,sizeof(pcRealign->nChess)); //4为障碍道具 m_bPropCnt[wSeat][PROP_RESET]--; sRealign.nPropCount = m_bPropCnt[wSeat][PROP_RESET]; m_pITableFrame->SendTableData(INVALID_CHAIR,SUB_S_REALIGN,&sRealign,sizeof(sRealign)); m_pITableFrame->SendLookonData(INVALID_CHAIR,SUB_S_REALIGN,&sRealign,sizeof(sRealign)); return true; } default: { return false; } } return false; }
// 寻找符合条件的最短路径 void CLogic::ShortPath(BYTE chess[CHESSCOUNT_H][CHESSCOUNT_W],CArrayTemplate<CPoint> * r, CPoint one , CPoint two, CPoint* ipoint) { CArrayTemplate<CPoint> t; CPoint hotpoint; bool blog = true; if( one.x !=two.x) { for(int i= one.x,j=one.y; i!=two.x; one.x<two.x?i++:i--) { if(i!=one.x && chess[j][i]!=ERROR_ALL) { blog = false; break; } else { t.InsertAt(t.GetCount(),CPoint(i,j)); } } } if(one.y != two.y) { hotpoint.x = two.x; hotpoint.y = one.y; for(int i=two.x,j = one.y;j!=two.y; one.y<two.y ? j++:j--) { if(chess[j][i]!=ERROR_ALL) { blog = false; break; } else { t.InsertAt(t.GetCount(),CPoint(i,j)); } } } if(blog) { for(int i=0; i<t.GetCount(); ++i) { r->InsertAt(r->GetCount(),CPoint(t.ElementAt(i).x,t.ElementAt(i).y)); if(ipoint!=NULL) { ipoint->x = hotpoint.x; ipoint->y = hotpoint.y; } } return; } else { t.RemoveAll(); hotpoint.x = ERROR_ALL; hotpoint.y = ERROR_ALL; blog = true; } if(one.y != two.y) { for(int i=one.x,j = one.y;j!= two.y; one.y<two.y ? j++:j--) { if(j!= one.y&&chess[j][i]!=ERROR_ALL) { t.RemoveAll(); blog = false; break; } else { t.InsertAt(t.GetCount(),CPoint(i,j)); } } } if(one.x != two.x) { hotpoint.x = one.x; hotpoint.y = two.y; for(int i= one.x,j=two.y; i!=two.x; one.x<two.x?i++:i--) { if(chess[j][i]!=ERROR_ALL) { t.RemoveAll(); blog = false; break; } else { t.InsertAt(t.GetCount(),CPoint(i,j)); } } } if(blog) { for(int i=0; i<t.GetCount(); ++i) { r->InsertAt(r->GetCount(),CPoint(t.ElementAt(i).x,t.ElementAt(i).y)); if(ipoint!=NULL) { ipoint->x = hotpoint.x; ipoint->y = hotpoint.y; } } return; } return; }