/* getValue(l) returns the character representing the car segment occupying location l in the carpark. The function terminates the program iff the location is not valid (i.e. outside the bounds of the carpark). */ static char getValue(LOCATION l) { if(!isValidLocation(l)) // check the location is valid { printf("The location l is not a valid carpark location\n"); exit(EXIT_FAILURE); } return carpark.grid[l.row][l.col]; }
/* setValue(l, value) sets location l in the carpark to value. The function terminates the program iff the location is not valid (i.e. outside the bounds of the carpark). Additionally, unless the function is used to "free-up" the location (set the location to EMPTY), the function also terminates the program iff the location is already occupied by another car. */ static void setValue(LOCATION l, char value) { if(!isValidLocation(l)) // check the location is valid and return error if not { printf("The location l is outside the bounds of the carpark\n"); exit(EXIT_FAILURE); } if(!isEmpty(l) && value != EMPTY) // check the location is empty or the value is empty returning false if both are negative { printf("The location: %d %d is not valid\n", l.row, l.col); exit(EXIT_FAILURE); } carpark.grid[l.row][l.col] = value; }
void CPlayer::setships() { char input = 'V'; char ok = 'Y'; char save = 'N'; ostringstream outSStream; CCell location; for(short j = 1; j < SHIP_SIZE_ARRAYSIZE; j++) { system("cls"); printGrid(cout, ZERO); outSStream.str(""); outSStream << "Player " << m_whichPlayer + ONE << " Enter " << shipNames[j] << " orientation"; input = safeChoice(outSStream.str(), 'V', 'H'); if (input == 'V') m_ships[j].setOrientation(ONE); else m_ships[j].setOrientation(ZERO); cout << "Player " << m_whichPlayer + ONE << " Enter " << shipNames[j] << " bow coordinates <row letter><col #>: "; location.getCoord(cin, m_gridSize); m_ships[j].setBowLocation(location); // if ok if(!isValidLocation(j)) { cout << "invalid location. Press <enter>" ; cin.ignore(BUFFER_SIZE, '\n'); cin.get(); j--; // redo continue; } // our code location = m_ships[j].getCell(); for(int i = 0; i < shipSize[j]; i++) { if (input == 'V') { setCell(ZERO, location, m_ships[j].getName()); location.increaseRow(); } else { setCell(ZERO, location, m_ships[j].getName()); location.increaseCol(); } } system("CLS"); printGrid(cout, ZERO); ok = safeChoice("Position okay? ", 'Y', 'N'); // choice not ok if (ok == 'N') { location = m_ships[j].getCell(); for(int i = 0; i < shipSize[j]; i++) { if (input == 'V') { setCell(ZERO, location, CShip::NOSHIP); location.increaseRow(); } else { setCell(ZERO, location, CShip::NOSHIP); location.increaseCol(); } } j--; } } // end for j save = safeChoice("\nSave starting grid?", 'Y', 'N'); if(save == 'Y') saveGrid(); }