One of the reasons microcomputers progressed so fast is people are willing to accept crashes. It's faster to build something and try it, even if it means you'll have to rebuild later... If you spent too much time building and massaging one vehicle, you don't learn anything.
John Carmack,
lead programmer of Doom (and more)
In this project your job is to implement the Battleship game for two players. Use newly learned OOP practices.
You will practice the fundamentals of Object-Oriented Programming such as:
- UML diagrams,
- clean code,
- encapsulation,
- abstraction,
- polymorphism,
- inheritance,
- enums
- S and O principles from SOLID,
- packaging in Java,
- getters and setters in Java
-
Gain good understanding of the OOP principles and implement it in your code.
- Program does not use static context.
- Classes are groupped in packages.
- Classes have fields and methods that are related only to them.
- Classes have access modifiers exposing their content as little as possible.
- Classes have getters and setters for private fields only when it is necessary.
- Classes are logically structured and inherit from parents which eliminates the code duplication.
- Classes use public methods to communicate with the outside code, and private methods for eliminating code duplication and better readability.
- Polymorphism is used wherever possible, in order to make the code as universal as possible and avoid code duplication.
-
Implement the
Battleship
class that will be used as the highest level class.- Class
Battleship
has fieldsDisplay
andInput
that are used throughout the program. - Class
Battleship
has a loop in which program runs. - Class
Battleship
shows main menu and allows the user to start new game, display highscores and exit.
- Class
-
Implement class
Display
and its methods.- Class
Display
prints the game menu. - Class
Display
prints the board during manual ship placement process. - Class
Display
prints the gameplay. - Class
Display
prints the outcome of the game when it's over. - No
System.out.print()
happens outside ofDisplay
class.
- Class
-
Implement class
Input
and its methods.- Class
Input
is responsible for gathering every needed kind of input and provides seperate method for each case. - Class
Input
handles the input validation.
- Class
-
Implement class
Game
and its methods.- Class
Game
has a loop in which players make moves. - Class
Game
has logic which determines the flow of round. - Class
Game
has condition on which game ends. - Class
Game
provides different game modes which use different round flow.
- Class
-
Implement class
Player
and its methods.- Class
Player
has logic responsible for handling shot. - Class
Player
has field of typeList<Ship>
. - Class
Player
has a methodisAlive
to check if player has not lost all ships and returns true or false accordingly.
- Class
-
[OPTIONAL] Implement
ComputerPlayer
class and it's methods.- Class
ComputerPlayerEasy
takes random shots excluding already struck fields. - Class
ComputerPlayerNormal
also excludes fields around ships when taking random hits. - Class
ComputerPlayerNormal
shoots around a ship after hitting it to determine its direction. - Class
ComputerPlayerNormal
changes direction to opposite when can not go further. - Class
ComputerPlayerNormal
follows the direction until the ship is sunk. - Class
ComputerPlayerHard
usesComputerPlayerNormal
logic but shoots diagonal fields only.
- Class
-
Implement class
BoardFactory
and it's methods.- Class
BoardFactory
has arandomPlacement()
method that handles random ship placement on board. - Class
BoardFactory
has amanualPlacement()
method that handles manual ship placement on board.
- Class
-
Implement class
Board
and it's methods.- Class
Board
has fieldSquare[][] ocean
- squares the board consists of. - Class
Board
has anisPlacementOk()
method that verifies if placement of ship is possible.
- Class
-
Implement class
Ship
and it's methods.- Class
Ship
has field ofList<Square>
- all squares the ship consists of.
- Class
-
Implement enum
ShipType
and it's methods.- Enum
ShipType
represents ship types - Carrier, Cruiser, Battleship, Submarine and Destroyer. - Each
ShipType
has a unique length.
- Enum
-
Implement class
Square
and its methods.- Class
Square
has fieldsX
andY
. - Class
Square
has a fieldSquareStatus
. - Class
Square
has method that returns givenSquareStatus
's graphical representation.
- Class
-
Implement enum
SquareStatus
.- Enum
SquareStatus
represents possible square statuses (empty, ship, hit, missed). - Each
SquareStatus
has a unicode character that can be used to print it out. This unicode character is returned by aSquareStatus.GetCharacter()
method.
- Enum
None
- There is no skeleton code for this project (on purpose), just an empty file. Try to create it from scratch.
- Focus on features first, and refactor it at the end.