You are on page 1of 4

1.b. The remove function. 2.

class IRenderable { public: //Constructors & Destructors IRenderable(); ~IRenderable(); //Virtual methods void Render()=0; protected: ///Protected methods and variables ///... } 3.Depending on the contexts, several optional improvements could be: The most basic one is not returning a reference to a local variable (I'm also mo difying the function to return instead a variable, and profiting the parameter i s passed as copy to modify it within the function). int IncrementValue(int oldValue) { return ++oldValue; } In case we want to modify the original value, another option would be using a re ference as parameter: void IncrementValue(int &oldValue) { ++oldValue; } 4.It's been quite a while since I programmed sockets, but it'd be something like t his: SOCKET tcpSocket = socket(PF_INET, SOCK_STREAM,) ///.... int flag=1; setsockopt(tcpSocket, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(int)); 5.It will crash in the case numSpacesToMove <= 5, as pointers might not initialize d by default to 0, but rather as 0xcdcdcdcd. If we're completely sure that the r obot shouldn't move when the numSpacesToMove is <=5, we can always initialize pR obot to 0 (or NULL, if defined). In any case, we should always make sure that "robot" is valid, as assuming that different than 0 is valid is not a good option, it might have values like 0xcdcd cdcd when not initialized, 0xfeefee if it's been already deleted, and so on. 6.-

int FreeSquaresForKnight(int x, int y) { int nFree = 0; //We can precompute the possible moves, and then we'll check for each on e of the moves //if we're outside the board. const int movesX[] = {x-2,x-2,x-1,x-1,x+1,x+1,x+2,x+2}; const int movesY[] = {y-1,y+1,y-2,y+2,y-2,y+2,y-1,y+1}; for(int i=0;i<8;++i) { if(movesX[i]>=0 && movesX[i]<8 && movesY[i]>=0 && movesY[i]<8) { int idx = movesX[i]+movesY[i]*8; if(gameBoard[idx]==0) { nFree++; } } } return nFree; } 7.//0x07e0 is the mask shift for the green color in RGB565 unsigned char GetGreen(unsigned short color) { return usigned char((color & 0x07e0) >> 5); } 8.A. #define NUM_PLAYERS 10 B. typedef enum {NUM_PLAYERS=10} GameConstant; C. namespace GameConstant { static const unsigned char NUM_PLAYERS=10; } 9.void EnemyRobot::Push(int newRobotId) { EnemyRobot* pNewRobot = new EnemyRobot(newRobotId); if(m_pHead) { EnemyRobot* it = m_pHead; while(it->GetNext()) { it = it->GetNext(); } it->SetNext(pNewRobot); } else {

m_pHead = pNewRobot; } } 10.float CalcLength(const Vector& v) { return sqrt(v.x*v.x + v.y*v.y + v.z*v.z); } 11.Assuming we only want the real solutions for the equation: struct Roots { uchar nValidRoots; float root1; float root2; } Roots CalcRoots(float a,float b, float c) { Roots result; float delta = b*b-4*a*c; if(delta==0) { result.nValidRoots=1; result.root1=-b/(2*a); } else if(delta>0) { result.nValidRoots=2; float sqDelta =sqrt(delta); result.root1=(-b+sqDelta)/(2*a); result.root2=(-b-sqDelta)/(2*a); } else { result.nValidRoots=0; } return result; } 12.In case we want to check only in the position just behind UnclePappy. bool IsGoldBehindUnclePappy() { Vector2D v = GetUnclePappyPosition(); v.x-=1; return CheckGoldAtPosition(v); } Otherwise, we could store an array of the gold positions, and update its max val ue when we move UnclePuppy, so we would only check if the X value of that max is greater or equal than UnclePappy's current position. 15.The most basic way to find the shortest path would be an implementation of the D

ijkstra's algorithm, which, translating the grid to a graph, iterating within th e set of nodes and removing them once we go forward. A pseudo-code implementatio n can be found at (http://en.wikipedia.org/wiki/Dijkstra's_algorithm). Then, we can always go with the A* algorithm, the most common algorithm to find the shortest path. It is indeed a generalization of Dijkstra's algorithm.

You might also like