Skip to content
uconn code

uconn codeBattleship

Battleship

CSE 124 · 2000 · C++

My earliest surviving program, from data structures lab 2 in 2000: choose how many ships of length three, four, and five to hide, then sink them by calling shots by row letter and column number.

Random numbers came from a small course helper (MyRand) written by the instructor, which isn't shown.

Source code

5 files, 556 lines, exactly as written apart from line endings.

battleship.cpp · 84 lines
// Stephen Bassoli
// CSE 124 Section 1
// Lab 2

#include <iostream.h>
#include <ctype.h>
#include "Board.h"

int main()
{

	int C;												//	Column Number
	char R, again;										//	R is row letter, again plays game again
	int Row;											//	Row NUMBER
	int Score = 0;										//	Game score
	int Three, Four, Five;								//	Types of ships
	bool Sentinel = false;								//	Determines termination of loop
	bool RepeatGame = false;							//	Determines if the game is repeated
	Board GameBoard;									//	The game board
	do
	{	

		cout << "How many ships with a length of three would you like? ";
		cin >> Three;
		cout << "How many ships with a length of four would you like? ";
		cin >> Four;
		cout << "How many ships with a length of five would you like? ";
		cin >> Five;
		GameBoard.Place(Three, Four, Five);
		do
		{	
			GameBoard.PrintBoard();
			do
			{
				cout << "Your Score: " << Score << "\tShips Left: " << GameBoard.ReturnShipsLeft() << endl;
				cout << "Enter the row letter followed by the column number: ";
				cin >> R >> C;
				Row = tolower(R);
			} while(!((R >= 'a')&&(R <= 'l')&&(C >= 1)&&(C <= 12)));
			Row -= 97;
			C -= 1;
			if (GameBoard.Mark(Row,C))
			{
				if (GameBoard.IsHit(Row,C))
				{
					cout << "YOU HIT A SHIP!\n";
					if (GameBoard.IsSink())
					{	
						if (GameBoard.IsWin())
						{
							cout << "YOU HAVE WON! Your final score: " << Score << endl;
							Sentinel = true;
						}
					}
					
				}
				else
					Score++;
			}
			else 
				cout << endl << "There is already a mark there\n";
		}while (!Sentinel);
		
		do
		{
			cout << "Would you like to play again? (y/n) ";
			cin >> again;
			cin.ignore(256,'\n');
		} while (!(again == 'y')&&!(again == 'n'));
		if (again == 'y')
		{	
			GameBoard.Clear();
			Sentinel = false;
			RepeatGame = true;
			Board GameBoard;
		}
		else if (again == 'n')
			RepeatGame = false;
	} while (RepeatGame);
		
		
		
return 0;
}

battleship.cpp as plain text

board.h · 95 lines

board.h as plain text

board.cpp · 253 lines

board.cpp as plain text

ship.h · 67 lines

ship.h as plain text

ship.cpp · 57 lines

ship.cpp as plain text