void FieldManagerDlg::OnBnClickedAddBtn() { InputDlg dlg; if( dlg.DoModal() != IDOK ) return; // 注意m_lastSel的更新问题 if( m_fieldListBox.GetCount() == 0 ) setLastSelIndex( 0 ); if( !isValidField( dlg.m_str ) ) { CString msg; msg.Format( _T( "非法的字段\n[%s]" ), dlg.m_str ); MessageBox( msg ); return; } if( isFieldExistInListBox( dlg.m_str ) ) { CString msg; msg.Format( _T( "字段[%s]已存在!" ), dlg.m_str ); MessageBox( msg ); return; } // 添加字段到listbox中 int index = m_fieldListBox.AddString( dlg.m_str ); if( index != LB_ERR ) { m_fieldListBox.SetCurSel( index ); // ***创建新的字段信息,并记录*** m_infoes.append( new FieldInfo() ); // 切换selection, 保存上次selection所在位置的字段信息 updateFieldInfo(); } }
static int produceUnit(int x1, int y1, int x2, int y2, int unitId) { /// Validates initialization. if (!isValidField(currentGame.mapSize, x1, y1) || !isValidField(currentGame.mapSize, x2, y2)) { return ERROR; } if (currentGame.isInitialized == false) { return ERROR; } /// Validates distance. if (distMax(x1, y1, x2, y2) != 1) { return ERROR; } /// Picks pawn from board. pawn *currentPawn = hashmapGet(currentGame.gameMap, x1, y1); /// Checks if pawn was picked. if (currentPawn == NULL) { return ERROR; } /// Checks if pawn is able to produce. if (currentPawn->lastMove >= currentGame.currentRound - 2) { return ERROR; } /// Checks if pawn belongs to current player. if (currentGame.playerTurn != getPawnAdherence(currentPawn)) { return ERROR; } if (isPeasant(currentPawn)) { pawn *targetPawn = hashmapGet(currentGame.gameMap, x2, y2); if (getPawnId(targetPawn) == EMPTY_SPACE_ID) { pawn *createdPawn; currentPawn->lastMove = currentGame.currentRound; if (unitId == PEASANT_PRODUCE_ID) { int newPawnId = currentGame.playerTurn == PLAYER_A_TURN ? PEASANT_PLAYER_A_ID : PEASANT_PLAYER_B_ID; createdPawn = newPawn(x2, y2, currentGame.currentRound - 1, newPawnId); } else if (unitId == KNIGHT_PRODUCE_ID) { int newPawnId = currentGame.playerTurn == PLAYER_A_TURN ? KNIGHT_PLAYER_A_ID : KNIGHT_PLAYER_B_ID; createdPawn = newPawn(x2, y2, currentGame.currentRound - 1, newPawnId); } else { ///< Tried to produce wrong unit. return ERROR; } hashmapPut(currentGame.gameMap, createdPawn); return GAME_OK; } else { ///< Field was not empty. return ERROR; } } else { ///< Unit that tried to produce was not peasant. return ERROR; } }
int move(int x1, int y1, int x2, int y2) { /// Validates fields. if (!isValidField(currentGame.mapSize, x1, y1) || !isValidField(currentGame.mapSize, x2, y2)) { return ERROR; } /// Validates initialization. if (currentGame.isInitialized == false) { return ERROR; } /// Validates distance between fields. if (distMax(x1, y1, x2, y2) != 1) { return ERROR; } /// Picks pawn from board. pawn *currentPawn = hashmapRemove(currentGame.gameMap, x1, y1); /// Checks if any pawn was picked. if (currentPawn == NULL) { return ERROR; } /// Checks if pawn is able to move. if (currentPawn->lastMove >= currentGame.currentRound) { free(currentPawn); return ERROR; } /// Checks if pawn belong to current player. if (currentGame.playerTurn != getPawnAdherence(currentPawn)) { free(currentPawn); return ERROR; } currentPawn->lastMove = currentGame.currentRound; pawn *targetPawn = hashmapRemove(currentGame.gameMap, x2, y2); if (getPawnAdherence(currentPawn) == getPawnAdherence(targetPawn)) { ///< Player wants to move onto his own pawn. free(currentPawn); if (targetPawn != NULL) { free(targetPawn); } return ERROR; } else { int actionResult = performAction(currentPawn, targetPawn); switch (actionResult) { /// Frees pawn that was killed. /// Puts pawn that survived. case UNIT_MOVED: currentPawn->x = (unsigned int) x2 - 1; ///< 1-based to 0-based. currentPawn->y = (unsigned int) y2 - 1; hashmapPut(currentGame.gameMap, currentPawn); break; case ATTACKER_KILLED: currentPawn->x = (unsigned int) x2 - 1; currentPawn->y = (unsigned int) y2 - 1; hashmapPut(currentGame.gameMap, currentPawn); free(targetPawn); break; case DEFENDER_KILLED: hashmapPut(currentGame.gameMap, targetPawn); free(currentPawn); break; case ATTACKER_KILLED_KING: currentPawn->x = (unsigned int) x2 - 1; currentPawn->y = (unsigned int) y2 - 1; hashmapPut(currentGame.gameMap, currentPawn); free(targetPawn); /// Player that made move is victorious. return currentGame.playerTurn == PLAYER_A_TURN ? PLAYER_A_WON : PLAYER_B_WON; case DEFENDER_KILLED_KING: free(currentPawn); hashmapPut(currentGame.gameMap, targetPawn); /// Player that made move is beaten. return currentGame.playerTurn == PLAYER_A_TURN ? PLAYER_B_WON : PLAYER_A_WON; case BOTH_UNITS_DIED: if (isKing(currentPawn)) { free(currentPawn); free(targetPawn); return DRAW; ///< Kings killed each other. } else { free(currentPawn); free(targetPawn); } break; default: return ERROR; } } return GAME_OK; }