/* processMoves(void) reads movement commands, processing each command, in turn to move the corresponding car within the carpark. Each movement command is constrained to ensure the corresponding car does not collide with any other car and that the car remains within the bounds of the carpark. A movement command is performed (and the carpark updated to reflect the movement) if the corresponding car can move in the given direction. At the completion of each movement command (regardless of whether the command lead to actual movement), the current state of the carpark is printed. The function completes immediately if a movement command leads to the target car exiting. The function terminates the program if any command is invalid. */ static void processMoves(void) { char line[BUFSIZ]; char car; char direction; int amount; DIRECTION d; int i = 0; while(!hasExited()) // make sure the TARGETCAR is not at the exit location { printPrompt(); // prompt the user for a move fgets(line, sizeof line, stdin); trimLine(line); sscanf(line, "%c %c %d", &car, &direction, &amount); // assign the users move to appropiate variables if(!isValidCarparkEntry(car)) // check the car is defined in the carpark { fprintf(stderr, "Fatal Error: car %c was not found in the grid\n", car); exit(EXIT_FAILURE); } /* The following code sets the enum value from the user input */ if(direction == 'N' || direction == 'n') { d = NORTH; } else if(direction == 'S' || direction == 's') { d = SOUTH; } else if(direction == 'E' || direction == 'e') { d = EAST; } else if(direction == 'W' || direction == 'w') { d = WEST; } moveCar(car, d, amount); i = moveCar(car, d, amount); printf("Processed move: %c %c %d\n", car, direction, amount); printCarpark(); } printf("The target car is now free after %d moves!\n", i); }
void moveToNextNode(int currentNodeID ,int nextNodeID) //Decides on which node to move to { vertexNode* v = searchNode(currentNodeID); //Found the vertex node //Create a temp head edgeNode edgeNode* temp_head = v->head; orientation direction ; while(temp_head!=NULL) { if(temp_head-> id == nextNodeID){ direction = temp_head->turn; //lastTurn = direction; //lastPositionSeen = lastTurn; distanceToNextNode = temp_head->distance; break; } temp_head = temp_head->next; } moveCar(direction, distanceToNextNode); lastTurn = direction; printf("Node explored : %d \n", currentNodeID); // Rotate based on current orientation // Set the timers // Actuate Motors // Keep updating currentDistanceTravelled }
void RaceRoad::play(){ //RaceRoad myroad(Player p()); RaceRoad(/*new Player()*/); represent(); short direction; char ispressed(1); HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE); DWORD NumInputs = 0; DWORD InputsRead = 0; bool running = true; INPUT_RECORD irInput; GetNumberOfConsoleInputEvents(hInput, &NumInputs); while(running){ direction=0; ReadConsoleInput(hInput, &irInput, 1, &InputsRead); switch(irInput.Event.KeyEvent.wVirtualKeyCode){ case VK_ESCAPE: running = false; break; case VK_LEFT: case VK_NUMPAD4: direction=-1; ispressed*=-1; break; case VK_RIGHT: case VK_NUMPAD6: direction=1; ispressed*=-1; break; } if(ispressed>0)moveCar(direction); if(isHit())cout<<"HALAL"<<endl; represent(); Sleep(1000); } }
//All player controls go into this method void World::controlPlayer(float time) { moveCar(time); }