Untitled

 avatar
unknown
c_cpp
2 months ago
4.1 kB
2
Indexable
void Player::Move(Grid* pGrid, Command moveCommands[])
{
	int x, y;
	const int maxCommands = 5;
	Input* pIn = pGrid->GetInput();
	Output* pOut = pGrid->GetOutput();
	for (int i = 0; i < maxCommands; i++)
	{
		if (moveCommands[i] == NO_COMMAND)
		{
			break;
		}
		CellPosition currentPos = pCell->GetCellPosition();
		CellPosition newPos = currentPos;
		switch (moveCommands[i])
		{
		case MOVE_FORWARD_ONE_STEP:
			switch (currDirection) {
			case UP:
				newPos.SetVCell(newPos.VCell() - 1); // Move up
				break;
			case DOWN:
				newPos.SetVCell(newPos.VCell() + 1); // Move down
				break;
			case LEFT:
				newPos.SetHCell(newPos.HCell() - 1); // Move left
				break;
			case RIGHT:
				newPos.SetHCell(newPos.HCell() + 1); // Move right
				break;
			}
			break;
		case MOVE_FORWARD_TWO_STEPS:
			switch (currDirection) {
			case UP:
				newPos.SetVCell(newPos.VCell() - 2); // Move up 2 steps
				break;
			case DOWN:
				newPos.SetVCell(newPos.VCell() + 2); // Move down 2 steps
				break;
			case LEFT:
				newPos.SetHCell(newPos.HCell() - 2); // Move left 2 steps
				break;
			case RIGHT:
				newPos.SetHCell(newPos.HCell() + 2); // Move right 2 steps
				break;
			}
			break;
		case MOVE_FORWARD_THREE_STEPS:
			switch (currDirection) {
			case UP:
				newPos.SetVCell(currentPos.VCell() - 3);
				break;
			case DOWN:
				newPos.SetVCell(currentPos.VCell() + 3);
				break;
			case LEFT:
				newPos.SetHCell(currentPos.HCell() - 3);
				break;
			case RIGHT:
				newPos.SetHCell(currentPos.HCell() + 3);
				break;
			}
			break;

		case MOVE_BACKWARD_ONE_STEP:
			switch (currDirection) {
			case UP:
				newPos.SetVCell(currentPos.VCell() + 1);
				break;
			case DOWN:
				newPos.SetVCell(currentPos.VCell() - 1);
				break;
			case LEFT:
				newPos.SetHCell(currentPos.HCell() + 1);
				break;
			case RIGHT:
				newPos.SetHCell(currentPos.HCell() - 1);
				break;
			}
			break;

		case MOVE_BACKWARD_TWO_STEPS:
			switch (currDirection) {
			case UP:
				newPos.SetVCell(currentPos.VCell() + 2);
				break;
			case DOWN:
				newPos.SetVCell(currentPos.VCell() - 2);
				break;
			case LEFT:
				newPos.SetHCell(currentPos.HCell() + 2);
				break;
			case RIGHT:
				newPos.SetHCell(currentPos.HCell() - 2);
				break;
			}
			break;

		case MOVE_BACKWARD_THREE_STEPS:
			switch (currDirection) {
			case UP:
				newPos.SetVCell(currentPos.VCell() + 3);
				break;
			case DOWN:
				newPos.SetVCell(currentPos.VCell() - 3);
				break;
			case LEFT:
				newPos.SetHCell(currentPos.HCell() + 3);
				break;
			case RIGHT:
				newPos.SetHCell(currentPos.HCell() - 3);
				break;
			}
			break;
		case ROTATE_CLOCKWISE:
			RotateClockwise(pGrid);
			continue;
		case ROTATE_COUNTERCLOCKWISE:
			RotateCounterClockwise(pGrid);
			continue;
		default:
			continue;
		}
		if (!newPos.IsValidCell())
		{
			pOut->PrintMessage("Invalid Move.");
			pIn->GetPointClicked(x, y);
			pOut->ClearStatusBar();
			return;
		}
		pGrid->UpdatePlayerCell(this, newPos);

		if (i < maxCommands - 1 && moveCommands[i + 1] != NO_COMMAND)
		{
			pOut->PrintMessage("Click anywhere to execute the next command.");
			pIn->GetPointClicked(x, y);
			pOut->ClearStatusBar();
		}
	}
	Cell* finalCell = pGrid->GetCell(pCell->GetCellPosition());
	GameObject* obj = finalCell->GetGameObject();
	if (obj != nullptr)
	{
		obj->Apply(pGrid, this);
	}
	///TODO: Implement this function using the guidelines mentioned below

	// - If a player has 5 (could have less) saved moveCommands, the robot will execute the first saved command,
	//		then wait for a mouse click (display a message "Click anywhere to execute the next command").
	//		After executing all the 5 saved commands, the game object effect at the final destination cell will
	//		be applied.
	// 
	// - Use the CellPosition class to help you calculate the destination cell using the current cell
	// - Use the Grid class to update pCell
	// - Don't forget to apply game objects at the final destination cell and check for game ending

}
Leave a Comment