Public feed #890645

Anonymous

public text v1 · immutable
Raw Edit
·published 2008-02-03 22:40 UTC
rendered paste body
#include "fleet.h"
// HEADERS  -- one for each function
// include the definitions for each of the non-template functions
//    declared in fleet.h 

const char* shipNames[7] = 
	{"No Ship","Mine Sweeper","Submarine","Frigate","Battleship","Aircraft Carrier"};
const int TOTALPIECES = 17; // total pieces in all ships

//---------------------------------------------------------------------------------
// Function:	setShipInfo()
// File:		fleet.cpp
// Title:		Set ShipInfo
// Description:
//				Sets struct ShipInfo fields
// Programmer:
// 
// Date:
//
// Version:
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		None
//
// Output:		None
//
// Calls:
//
// Called By:
//
// Parameters:	ShipInfo * shipInfoPtr:	pointer to the ShipInfo to be set
//				Ship name:				enumerated name of type of ship
//				Direction orientation:	enumerated direction
//				unsigned short row:		row-coordinate in grid
//				unsigned short col:		column-coordinate in grid
// 
// Returns:		void
//
// History Log:
//   
//   
//---------------------------------------------------------------------------------
void setShipInfo(ShipInfo * shipInfoPtr, Ship name, Direction orientation,
								unsigned short row, unsigned short col)
{
	shipInfoPtr->m_name = name;
	shipInfoPtr->m_bowLocation.m_col = col;
	shipInfoPtr->m_bowLocation.m_row = row;
	shipInfoPtr->m_orientation = orientation;
	shipInfoPtr->m_piecesLeft = shipSize[name];
} 

//---------------------------------------------------------------------------------
// Function:	allocMem()
// File:		fleet.cpp
// Title:		Allocate Memory
// Description:
//				allocates memory for current grids
// Programmer:	Paul Bladek
// modified by:	
// 
// Date:		9/13/06
//
// Version:		1.01
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		None
//
// Output:		None
//
// Calls:
//
// Called By:	main()
//
// Parameters:	Player players[]:	the array of 2 players in the game
//				char size:		:	'S' or 'L' (small or large)
// 
// Returns:		void
//
// History Log:
//				12/20/05 PB completed v 1.0 
//				9/13/06  PB completed v 1.01
//
//---------------------------------------------------------------------------------
void allocMem(Player players[], char size)
{
	short row = (toupper(size)=='L')?LARGEROWS:SMALLROWS;
	short col = (toupper(size)=='L')?LARGECOLS:SMALLCOLS;

	try
	{
		for(short i = 0; i < NUMPLAYERS; ++i)
		{
			players[i].m_gameGrid[0] = new Ship*[row];
			players[i].m_gameGrid[1] = new Ship*[row];
			for(short j = 0; j < row; ++j)
			{
				players[i].m_gameGrid[0][j] = new Ship[col];
				players[i].m_gameGrid[1][j] = new Ship[col];
				
				for(short k = 0; k <= col; ++k)
				{
					players[i].m_gameGrid[0][j][k] = NOSHIP;
					players[i].m_gameGrid[1][j][k] = NOSHIP;
				} // end for k
			} // end for j
		} // end for i
	}
	catch(exception e)
	{
		cerr << "exception: " << e.what()<< endl;
		cout << "shutting down" << endl;
		cin.ignore(256,'\n');
		exit(EXIT_FAILURE);
	}
}

//---------------------------------------------------------------------------------
// Function:	deleteMem()
// File:		fleet.cpp
// Title:		Delete Memory
// Description:
//				Deletes memory for grids
// Programmer:
// 
// Date:
//
// Version:
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		None
//
// Output:		None
//
// Calls:
//
// Called By:	main()
//
// Parameters:	Player players[]:	the array of the 2 Players
//				char size:			'S' or 'L'
// 
// Returns:		void
//
// History Log:
//   
//   
//---------------------------------------------------------------------------------
void deleteMem(Player players[], char size) 
{
	short numberOfRows = (toupper(size)=='L')?LARGEROWS:SMALLROWS;
	// your code goes here ...
	// delete in reverse order of allocMem()

}

//---------------------------------------------------------------------------------
// Function:	printShip()
// File:		fleet.cpp
// Title:		Print Ship 
// Description:
//				Print grid element for the proper ship
// Programmer:
// 
// Date:
//
// Version:
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		None
//
// Output:		three characters representing one ship to sout
//
// Calls:
//
// Called By:	printGrid()
//
// Parameters:	ostream & sout:	the stream to print to
//				Ship thisShip:	the Ship to be brinted in the grid format
// 
// Returns:		void
//
// History Log:
//     
//---------------------------------------------------------------------------------
void printShip(ostream & sout, Ship thisShip)
{	
	sout << ' ' ;
	switch(thisShip)
	{
		case NOSHIP: sout << ' ';
			break;
		case MINESWEEPER: sout << 'M';
			break;
		case SUB: sout << 'S';
			break;
		case FRIGATE: sout << 'F';
			break;
		case BATTLESHIP: sout << 'B';
			break;
		case CARRIER: sout << 'C';
			break;
		case HIT: sout << 'H';
			break;
		case MISSED: sout << MISS;
			break;
		default: sout << 'X';
	}
	sout << VERT ;
}

//---------------------------------------------------------------------------------
// Function:	printGrid()
// File:		fleet.cpp
// Title:		Print Ship 
// Description:
//				Print grid element for the proper ship
// Programmer:
// 
// Date:
//
// Version:
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		None
//
// Output:		a single grid to sout
//
// Calls:		printShip()
//
// Called By:	main()
//				setShips()
//				saveGrid()
//
// Parameters:	ostream & sout:	the stream to print to
//				Ship** grid:	the 2-D array of Ships 
//				char size:			'S' or 'L'
// 
// Returns:		void
//
// History Log:
//     
//---------------------------------------------------------------------------------
void printGrid(ostream& sout, Ship** grid, char size)
{
	short numberOfRows = (toupper(size)=='L')?LARGEROWS:SMALLROWS;
	short numberOfCols = (toupper(size)=='L')?LARGECOLS:SMALLCOLS;

	for(short j = 1; j <= numberOfCols; ++j)
		sout << setw(3) << j;
	sout << endl;

	char letter = 'A';
	
	for(short i = 0; i < numberOfRows; ++i)
	{
		sout << letter;
		for (short j = 0; j < numberOfCols; ++j)
			printShip(sout, grid[i][j]);
		sout << endl;

		sout << HORIZ;
		for(short j = 1; j <= (numberOfCols - 1); ++j)
			sout << HORIZ << HORIZ << CROSS ;
		sout << HORIZ << HORIZ << CROSS << endl;
		letter++;
	}
	
	sout << endl;

	// your code goes here ...
	// use printShip for each element in the grid


} 

//---------------------------------------------------------------------------------
// Function:	initializePlayer()
// File:		fleet.cpp
// Title:		Initialize Player 
// Description:
//				sets initial values for m_ships and m_piecesLeft
// Programmer:	Paul Bladek
// 
// Date:
//
// Version:		1.0
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		None
//
// Output:		None
//
// Calls:
//
// Called By:	main()
//
// Parameters:	Player* playerPtr:	a pointer to rhe current Player
// 
// Returns:		void
//
// History Log:
//     
//---------------------------------------------------------------------------------
void initializePlayer(Player* playerPtr)
{
	for(short i = 0; i < SHIP_SIZE_ARRAYSIZE; i++)
		setShipInfo(playerPtr->m_ships+i, static_cast<Ship>(i));
	
	playerPtr->m_piecesLeft = TOTALPIECES; 
}

//---------------------------------------------------------------------------------
// Function:	setships()
// File:		fleet.cpp
// Title:		Set Ships 
// Description:
//				Allows user to put ships in grid
// Programmer:
// 
// Date:
//
// Version:
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		location and orientation using getCoord from cin
//
// Output:		prompts to cout
//
// Calls:		printGrid()
//				safeChoice()
//				getCoord()
//				saveGrid()
//
// Called By:	main()
//
// Parameters:	Player players[]:	the array of 2 players 
//				char size:			'S' or 'L'
//				short whichPlayer:	the player number  (0 or 1)
// 
// Returns:		void
//
// History Log:
//     
//---------------------------------------------------------------------------------
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 #>: ";
			location = getCoord(cin,size);


			// if ok
			if(!validLocation(players[whichPlayer], j, size))
			{
				cout << "invalid location. Press <enter>" ;
				cin.get();
				j--; // redo
				continue;
			}


	if(players[whichPlayer].m_ships[j].m_orientation == VERTICAL)
	{
		for(int k = 0; k < shipSize[j]; k++)
		{
			players[whichPlayer].m_gameGrid[0][location.m_row + k][location.m_col] = (Ship)j;
		}
	}
	else
	{
		for(int k = 0; k < shipSize[j]; k++)
		{
			players[whichPlayer].m_gameGrid[0][location.m_row][location.m_col + k] = (Ship)j;
		}
	}

			// your code goes here ...
			


		} // end for j
		save = safeChoice("\nSave starting grid?",'Y','N');
		if(save=='Y')saveGrid(players, whichPlayer, size);
}

//---------------------------------------------------------------------------------
// Function:	saveGrid()
// File:		fleet.cpp
// Title:		Save Grid 
// Description:
//				Saves the ship grid to a file
// Programmer:
// 
// Date:
//
// Version:
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		None
//
// Output:		grid to specified file
//
// Calls:		printGrid()
//
// Called By:	setships()
//
// Parameters:	Player players[]:	the array of 2 players
//				short whichPlayer:	the player number  (0 or 1) 
//				char size:			'S' or 'L'
// 
// Returns:		void
//
// History Log:
//     
//---------------------------------------------------------------------------------
void saveGrid(Player players[], short whichPlayer, char size)
{
	short numberOfRows = (toupper(size)=='L')?LARGEROWS:SMALLROWS;
	short numberOfCols = (toupper(size)=='L')?LARGECOLS:SMALLCOLS;	
	// your code goes here ...
	
}

//---------------------------------------------------------------------------------
// Function:	getGrid()
// File:		fleet.cpp
// Title:		GetGrid 
// Description:
//				Reads grid from a file
// Programmer:
// 
// Date:
//
// Version:
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		grid from specified file
//
// Output:		prompts to cout
//
// Calls:
//
// Called By:	main()
//
// Parameters:	Player players[]:	the array of 2 players
//				short whichPlayer:	the player number  (0 or 1) 
//				char size:			'S' or 'L'
//				string fileName:	the name of the file to be openned for reading
// 
// Returns: bool -- true if the file is openned and read;
//					false otherwise
//
// History Log:
//     
//---------------------------------------------------------------------------------
bool getGrid(Player players[], short whichPlayer, char size, string fileName)
{
	string line;
	ifstream ifs;
	Ship ship;
	short shipCount[6] = {0};
	char cell = ' ';
	char fsize = 'S';
	short numberOfRows = (toupper(size)=='L')?LARGEROWS:SMALLROWS;
	short numberOfCols = (toupper(size)=='L')?LARGECOLS:SMALLCOLS;

	try
	{
		ifs.open(fileName.c_str());
		if(!ifs)
		{
			cout << "could not open file " << fileName << endl
				<< " press <enter> to continue" << endl;
			cin.ignore(256,'\n');
			return false;
		}
	}
	catch(...)
	{
		cout << "could not open file " << fileName << endl
			<< " press <enter> to continue" << endl;
		cin.ignore(256,'\n');
		return false;
	}	
	// your code goes here ...
	

	return true;
}

//---------------------------------------------------------------------------------
// Function:	getCoord()
// File:		fleet.cpp
// Title:		Get Coordinates 
// Description:
//				Returns a cell with coordinates set by user
// Programmer:
// 
// Date:
//
// Version:
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		cell coordinates (in the form "A13" from sin
//
// Output:		prompts to cout
//
// Calls:
//
// Called By:	main()
//				setships()
//
// Parameters:	istream& sin:	the stream to read from
//				char size:			'S' or 'L'
// 
// Returns:		Cell location -- a cell containing the input coordinates
//
// History Log:
//     
//---------------------------------------------------------------------------------
Cell getCoord(istream& sin, char size)
{
	short numberOfRows = (toupper(size)=='L')?LARGEROWS:SMALLROWS;
	short numberOfCols = (toupper(size)=='L')?LARGECOLS:SMALLCOLS;
	char highChar = static_cast<char>(numberOfRows - 1) + 'A';
	char row  = 'A';
	short col = 0;
	Cell location = {0, 0};
	do
	{
		col = 0;
		cout << "Row must be a letter from A to " << highChar 
			<< " and column must be from 1 to "  << numberOfCols << ": ";
		while((row = toupper(sin.get())) < 'A' || row  > highChar)
		{
			sin.ignore(256, '\n');
			cout << "Row must be a letter from A to " << highChar 
				<< " and column must be from 1 to "  << numberOfCols << ": ";
		}
		sin >> col;
		if(!sin)
			sin.clear();
		sin.ignore(256, '\n');
	}
	while(col < 1 || col > numberOfCols);
	
	location.m_col = col - 1;
	location.m_row = static_cast<short>(row - 'A');
	return location;
}

//---------------------------------------------------------------------------------
// Function:	validLocation()
// File:		fleet.cpp
// Title:		Valid Location 
// Description:
//				Can the ship legitinately go there?
// Programmer:
// 
// Date:
//
// Version:
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		None
//
// Output:		None
//
// Calls:
//
// Called By:
//
// Parameters:	const Player& player:	a reference to the specific Player
//				short shipNumber:	the number of the ship (1 - 5) in the array player.m_ships[]
//				char size:			'S' or 'L'
// 
// Returns: bool -- true if the ship would not go off the edge
//						or cross another ship;
//					false otherwise
//
// History Log:
//     
//---------------------------------------------------------------------------------
bool validLocation(const Player& player, short shipNumber, char size)
{
	short numberOfRows = (toupper(size)=='L')?LARGEROWS:SMALLROWS;
	short numberOfCols = (toupper(size)=='L')?LARGECOLS:SMALLCOLS;
	// your code goes here ...




	// replace the return value
	return true;
}

//---------------------------------------------------------------------------------
// Function:	header()
// File:		fleet.cpp
// Title:		header 
// Description:
//				Prints opening graphic
// Programmer:	Paul Bladek
// 
// Date:
//
// Version:		1.0
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		None
//
// Output:		None
//
// Calls:		boxTop()
//				boxLine()
//				boxBottom()
//
// Called By:	main()
//
// Parameters:	ostream & sout:	the stream to print to
// 
// Returns:		void
//
// History Log:
//     
//---------------------------------------------------------------------------------
void header(ostream& sout)
{
	const string empty;
	const string sink("SINK THE FLEET!");
	// your name goes here!
	const string by("   Edmonds Community College Cmpsc 143");
	boxTop(sout, BOXWIDTH);
	boxLine(sout, empty, BOXWIDTH);
	boxLine(sout, sink , BOXWIDTH, 'C');
	boxLine(sout, empty, BOXWIDTH);
	boxLine(sout, by, BOXWIDTH, 'C');
	boxLine(sout, empty, BOXWIDTH);
	boxBottom(sout, BOXWIDTH);
}

//---------------------------------------------------------------------------------
// Function:	endBox()
// File:		fleet.cpp
// Title:		End Box 
// Description:
//				prints closinging graphic
// Programmer:	Paul Bladek
// 
// Date:
//
// Version:		1.0
// 
// Environment: Hardware:IBM Pentium+ 
//              Software: Windows XP or 98+ with .NET framework for execution; 
//              Compiles under Microsoft Visual C++ 2005
//
// Input:		None
//
// Output:		None
//
// Calls:		boxTop()
//				boxLine()
//				boxBottom()
//
// Called By:	main()
//
// Parameters:	short player: the number of the winner (0 or 1)
// 
// Returns:		void
//
// History Log:
//     
//---------------------------------------------------------------------------------
void endBox(short player)
{
	const string empty;
	ostringstream msg;
	msg << "Congratulations player " << player + 1 << "!";
	boxTop(cout, BOXWIDTH);
	boxLine(cout, empty, BOXWIDTH);
	boxLine(cout, msg.str() , BOXWIDTH, 'C');
	boxLine(cout, empty, BOXWIDTH);
	boxBottom(cout, BOXWIDTH);
}