static void ack ( int signalNumber ) // Mode d'emploi : // Acknowledge the death of a children GarerParking tasks. // Update the shared state and display accordingly. { // Retrieve the pid of the terminated child int status; pid_t pid; // For each child task that has died while ( (pid = waitpid ( -1, &status, WNOHANG )) > 0 ) { if ( WIFEXITED ( status ) ) { // Retrieve the spot number (encoded into the return value) int spotNumber = WEXITSTATUS ( status ); Car car = currentValets[pid]; car.entranceTime = time ( NULL ); // Remove this pid from the list of running tasks currentValets.erase ( pid ); // Update the state of the parking lot placeCar ( spotNumber, car ); // Display the newly parked car AfficherPlace ( spotNumber, car.priority, car.licensePlate, car.entranceTime ); } } } // Fin de ack
/** readIn Reads in data from the standard in to initialize a game state using the given data. @pre -# *this is a valid instantiation of a game state. -# *this is a cleared games state to prevent data corruption, or at least this function will perform the clearing operation. @post -# *this will contain the data of a valid game state. @detail @bAlgorithm -# *this is cleared. -# The car data is read in sequentially. -# The cars are then placed in the array @code @endcode */ void GameState::readIn() { // assert pre-conditions assert( numCars > 0 ); // variables int currentCar = 0; // read in each vehicle's data for( currentCar = 0; currentCar < numCars; currentCar++ ) { // read in each piece of car data cin >> carList[ currentCar ].length >> carList[ currentCar ].orientation >> carList[ currentCar ].yPos >> carList[ currentCar ].xPos; } // place each car for( currentCar = 0; currentCar < numCars; currentCar++ ) { placeCar( currentCar, carList[currentCar].xPos, carList[currentCar].yPos ); } // no return - void }
/** move Modifies the game state to simulate the movement of a vehicle. @param whichCar Which car in the current list is to be moved. @param direction Which direction the car will attempt to move. Function should be called using the constants given in GameState.h. @return movementSuccess Indicates the success of the movement so that decisions can be made. True for a successful movement, false otherwise. @pre -# The GameState is in a valid state with legally placed cars. @post -# Cars will be in a valid state on the game board and the success of the movement is returned. @detail @bAlgorithm -# Depending on the car and direction, the next array location the car will occupy is computed. -# This location is checked to see if it is in the bounds of the array. -# The location is then checked to see if it is occupied already. -# If the location is occupied, the car is placed on the new location and the space previously occupied is cleaned up. True is returned to indicate a successful movement. -# If for any reason the move cannot be legally made, the attempt is abandoned and false is returned. @code @endcode */ bool GameState::move( const int whichCar, const bool direction ) { // variables bool moveSuccess = false; // case: moving forward if( direction == FORWARD ) { // case: car is vertical if( carList[whichCar].orientation == kVertical ) { // case: new index is valid if( (carList[whichCar].yPos - 1) >= 0 ) { // case: new index is empty if( board[carList[whichCar].yPos - 1][carList[whichCar].xPos] == kEmpty ) { // perform move (modifies car head values) placeCar( whichCar, carList[whichCar].xPos, carList[whichCar].yPos - 1 ); // clean up behind the car board[carList[whichCar].yPos + carList[whichCar].length] [carList[whichCar].xPos] = kEmpty; // update flag moveSuccess = true; } } } // case: car is horizontal else { // case: new index is valid if( (carList[whichCar].xPos + carList[whichCar].length) < kBoardSize ) { // case: new index is empty if( ( board[carList[whichCar].yPos][carList[whichCar].xPos + carList[whichCar].length] ) == kEmpty ) { // perform move (modifies car head values) placeCar( whichCar, carList[whichCar].xPos + 1, carList[whichCar].yPos ); // clean up behind the car board[carList[whichCar].yPos][carList[whichCar].xPos - 1] = kEmpty; // update flag moveSuccess = true; } } } } // case: moving backward else { // case: car is vertical if( carList[whichCar].orientation == kVertical ) { // case: new index is valid if( (carList[whichCar].yPos + carList[whichCar].length) < kBoardSize ) { // case: new index is empty if( ( board[carList[whichCar].yPos + carList[whichCar].length][carList[whichCar].xPos] ) == kEmpty ) { // perform move (modifies car head values) placeCar( whichCar, carList[whichCar].xPos, carList[whichCar].yPos + 1 ); // clean up behind the car board[carList[whichCar].yPos - 1][carList[whichCar].xPos] = kEmpty; // update flag moveSuccess = true; } } } // case: car is horizontal else { // case: new index is valid if( (carList[whichCar].xPos - 1) >= 0 ) { // case: new index is empty if( board[carList[whichCar].yPos][carList[whichCar].xPos - 1] == kEmpty ) { // perform move (modifies car head values) placeCar( whichCar, carList[whichCar].xPos - 1, carList[whichCar].yPos ); // clean up behind the car board[ carList[whichCar].yPos ] [ carList[whichCar].xPos + carList[whichCar].length ] = kEmpty; // update flag moveSuccess = true; } } } } // return the movement's success return moveSuccess; }