Пример #1
0
//---------------------------------------------------------------------------------
// Function:	setships()
// Title:	Set Ships 
// Description:
//		Allows user to put ships in grid
// Programmer:	Paul Bladek
// modified by:	Kyle Graham
//				Hang Nguyen
// 
// Date:	9/12/06
//
// Version:	0.5
// 
// Environment: Hardware: i3 
//              Software: OS: Windows 7; 
//              Compiles under Microsoft Visual C++ 2012
//
// Input:	location and orientation using getCoord from cin
//
// Output:	prompts to cout
//
// Calls:	printGrid()
//		safeChoice()
//		getCoord()
//		saveGrid()
//
// Called By:	main()
//
// Parameters:	players: Player[];	the array of 2 players 
//		size: char;		'S' or 'L'
//		whichPlayer: short;	the player number (0 or 1)
// 
// Returns:	void
//
// History Log: 
//				9/12/06 PB comleted v 0.5
//				1/16/15 KG & HN completed v 0.6
//				1/23/15 KG & HN completed v 0.7
//				1/29/15 KG & HN completed v 0.8
//     
//---------------------------------------------------------------------------------
void setships(Player players[], char size, short whichPlayer)
{
	char input = 'V';
	char ok = 'Y';
	char save = 'N';
	ostringstream outSStream;
	Cell location = {0, 0};

	for(short j = 1; j < SHIP_SIZE_ARRAYSIZE; j++)
	{
		system("cls");
		printGrid(cout, players[whichPlayer].m_gameGrid[0], size);
		outSStream.str("");
		outSStream << "Player " << whichPlayer + 1 << " Enter " 
			<< shipNames[j] << " orientation";
		input = safeChoice(outSStream.str(), 'V', 'H');
		players[whichPlayer].m_ships[j].m_orientation = (input == 'V') 
			? VERTICAL : HORIZONTAL;
		cout << "Player " << whichPlayer + 1 << " Enter " << shipNames[j] 
			<< " bow coordinates <row letter><col #>: ";
		players[whichPlayer].m_ships[j].m_bowLocation = getCoord(cin, size);
		location = players[whichPlayer].m_ships[j].m_bowLocation;
		// if ok
		if(!validLocation(players[whichPlayer], j, size))
		{
			cout << "invalid location. Press <enter>" ;
			cin.get();
			j--; // redo
			continue;
		}
		// our code
		for(int i = 0; i < shipSize[j]; i ++)
		{
			if (input == 'V')
			{
				players[whichPlayer].m_gameGrid[0][location.m_row + i]
					[location.m_col] = static_cast<Ship>(j);
			}
			else
			{
				players[whichPlayer].m_gameGrid[0][location.m_row]
					[location.m_col + i] = static_cast<Ship>(j);
			}
		}
		system("CLS");
		printGrid(cout, players[whichPlayer].m_gameGrid[0], size);
		ok = safeChoice("Position okay? ", 'Y', 'N');
		// choice not ok
		if (ok == 'N')
		{
			for(int i = 0; i < shipSize[j]; i ++)
			{
				if (input == 'V')
				{
					players[whichPlayer].m_gameGrid[0][location.m_row + i]
						[location.m_col] = NOSHIP;
				}
				else
				{
					players[whichPlayer].m_gameGrid[0][location.m_row]
						[location.m_col + i] = NOSHIP;
				}
			}
			system("CLS");
			printGrid(cout, players[whichPlayer].m_gameGrid[0], size);
			j--;
		}
	} // end for j
	save = safeChoice("\nSave starting grid?", 'Y', 'N');
	if(save == 'Y')
	saveGrid(players, whichPlayer, size);
}
Пример #2
0
bool CPlayer::getGrid(string fileName)
{
	string line;
	ifstream ifs;
	CShip ship = CShip::NOSHIP;
	short shipCount[SHIP_SIZE_ARRAYSIZE] = {0};
	char cell = ' ';
	//char fsize = 'S';
	char size_of_grid;
	short numberOfRows = (toupper(getGridSize()) == 'L') ? LARGEROWS : SMALLROWS;
	short numberOfCols = (toupper(getGridSize()) == 'L') ? LARGECOLS : SMALLCOLS;
	CCell location;
	ifs.open(fileName.c_str());
	if(!ifs)
	{

		cout << "could not open file " << fileName << endl
			<< " press <enter> to continue" << endl;
		cin.ignore(BUFFER_SIZE, '\n');
		return false;
	}	
	size_of_grid = ifs.get();
	if (m_gridSize != size_of_grid)
	{ 
		cout << "The input file size is not compatible with the " <<
			"grid size that you chose from the beginning.";
		cout << endl;
		ifs.close();
		return false;
	}
	for (int i = 1; i < SHIP_SIZE_ARRAYSIZE; i++)
	{
		int input_orientation;
		int input_row;
		int input_col;
		ifs >> input_orientation;
		ifs >> input_row;
		ifs >> input_col;

		location = (input_row, input_col);
		m_ships[i].setBowLocation(location);
		m_ships[i].setOrientation(input_orientation);

		for(int j = 0; j < shipSize[i]; i++)
		{
			if (m_ships[i].getOrientation() == CDirection::VERTICAL)
			{
				setCell(ZERO, location, m_ships[i].getName());
				location.increaseRow();
			}
			else
			{
				setCell(ZERO, location, m_ships[i].getName());
				location.increaseCol();
			}
		}
	}
	system("CLS");
	printGrid(cout, ZERO);
	char ok = safeChoice("Position okay? ", 'Y', 'N');
	// choice not ok
	if (ok == 'N')
	{
		for (int i = 0; i < numberOfRows; i++)
			for (int j = 0; j < numberOfCols; j++)
				m_gameGrid[ZERO][i][j] = CShip::NOSHIP;
		return false;
	}
	cin.clear();
	cin.ignore(BUFFER_SIZE, '\n');
	fflush(stdin);
	cout << "press <enter>  . . ." << endl;
	cin.get();
	system("CLS");
	ifs.close();	
	return true;
}
Пример #3
0
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();
}
Пример #4
0
void CPlayer::setGridSize(char size)
{
	cout << "Which grid size do you want to use ? ";
	m_gridSize = safeChoice(" ", 'S', 'L');
}