This repository holds the Java and C++ version of a Battleship game, created during the Object Oriented Programming course, completed during my first year of studies in the Computer Engineering and Informatics Department at the University of Patras.
In the Java folder, you can find the Java version of the game, which is as such:
- Tile: This class represents each cell. It has an "enum" structure that determines the values that a cell can take ("Sea", "Ship", "Hit", "Miss"). The class also has the auxiliary methods:
- Tile (which is the constructor): The constructor takes as arguments two integers that are the coordinates of the cell and its formula. It then assigns them to the variables of the class.
- setType: It is a "setter" method that takes a formula as an argument and assigns it to the cell.
- getType: It is a "getter" method that returns the cell type.
- draw: This method takes as an argument a "boolean" variable which if it has the value "false" prints in the type of each cell (showing where the ships are) otherwise it simply prints the sea, the failures and the inaccuracies.
- Ship: An abstract class from which the other types of ships will inherit. Represents the general type of each ship. From this class inherit the classes: "Carrier", "Battleship", "Cruiser", "Submarine" and "Destroyer" and has the following abstract methods:
- placeShip: The method that is responsible for placing ships. It takes as arguments a two-position table which is the starting point of the ship, the orientation of the ship and the table on which it will place the ships.
- check_boundaries: This method checks if the ship's placement goes out of bounds. It takes as arguments a two-position table which is the starting point of the ship and the orientation of the ship.
- check_placement: Checks if there is already another ship where we are going to place a ship. As arguments he takes, a two-position table which is the starting point of the ship, the orientation of the ship and the table on which he will place the ships.
- check_orientation: Checks if there is at least one empty cell between another ship. It takes as arguments a two-position table which is the starting point of the ship, the orientation of the ship and the table on which it will place the ships.
- my_check_boundaries: It does the same job as check_boundaries, but returns "boolean" so that the ship details can be searched again.
- getType: It is a "getter" method that returns the cell type.
- my_check_placement: It does the same job as check_placement, but returns "boolean" so that the ship details can be searched again.
- my_check_orientation: It does the same job as check_orientation, but returns "boolean" so that the ship details can be searched again.
- Carrier: One of the classes that inherits from Ship. It has an integer variable "size = 5" which indicates the size of the ship. Finally, Ship implements the abstract methods.
- Battleship: One of the classes that inherits from Ship. It has an integer variable "size = 4" which indicates the size of the ship. Finally, Ship implements the abstract methods.
- Cruiser: One of the classes that inherits from Ship. It has an integer variable "size = 3" which indicates the size of the ship. Finally, Ship implements the abstract methods.
- Submarine: One of the classes that inherits from Ship. It has an integer variable "size = 3" which indicates the size of the ship. Finally, Ship implements the abstract methods.
- Destroyer: One of the classes that inherits from Ship. It has an integer variable "size = 2" which indicates the size of the ship. Finally, Ship implements the abstract methods.
- OversizeException: A class that inherits from the Exception class and has the following methods:
- OversizeException (which is also the constructor): Assigns a message to the variable of the "message" class.
- getMessage: A getter method that returns the message of the class.
- OverlapTilesException: A class that inherits from the Exception class and has the following methods:
- OverlapTilesException (which is also the constructor): Assigns a message to the variable of the "message" class.
- getMessage: A getter method that returns the message of the class.
- AdjacentTilesException: A class that inherits from the Exception class and has the following methods:
- AdjacentTilesException (which is also the constructor): Assigns a message to the variable of the "message" class.
- getMessage: A getter method that returns the message of the class.
- Board: The class that represents the 7x7 size table. There are two constants "row" and "col" which are the number of rows and columns. Finally, it has these methods:
- Board (which is also the constructor): Takes as an argument a two-dimensional "Tile" table which it initializes using the creator of the "Tile" class.
- drawboards: Takes as arguments two two-dimensional tables of type "Tile" and two objects of the class "Player". It then prints the two tables using the "Tile" method of the "Tile" class.
- getAdjacentTiles: Takes as arguments a cell and the table and returns its neighbors.
- placeAllShips: Argues a two-dimensional “Tile” type table and places the ships randomly by calling the “placeShip” method of each ship separately.
- allShipsSunk: Takes as an argument a two-dimensional "Tile" table and returns "boolean" and checks if all ships have sunk by returning "true" or "false".
- Player: A class that represents each player. It has the following methods:
- Player (which is also the constructor): Takes as arguments the alphanumeric which is the name of the player and a "boolean" which shows whether or not the table of the specific player is hidden.
- placeAllShips: Gets no arguments and calls the "placeAllShips" of the "Board" class and then returns a "Board".
- placeShip: A method that takes as an argument an object of the abstract class "Ship" and the starting point of the ship and calls the placeShip of the abstract class.
- fire: This method implements the shot made by the players. Initially increases by one the variables of the class "shots" and "tries". Then, using the "getType" and "setType" methods of the "Tile" class, he performs the shot.
- getStats: Prints each player's statistics.
- getTries: Returns the efforts of each player.
- getHidden: Returns the boolean "hidden" variable of the class.
- setHidden: Takes as an argument a Boolean variable and assigns it to the variable of class "hidden".
- Game: Displays the main class which also contains the "main" method. It's essentially the class that starts the game. Contains the following methods:
- getInput: A method that returns the starting point of ships or the position of the shot as an integer two-position table. Takes as an argument an integer two-position table and uses the "Scanner" class to get values from the user.
- getRandInput: Returns an integer table of two positions and uses the "Random" class to give values from 0 to 6 as the starting point of the ship.
- getOrientation: Returns a character that determines the orientation of the ship and takes a character as an argument. Uses the "Scanner" class to get values from the user.
- randomPlace: A method that returns and takes as a variable a boolean variable. Asks the user if he wants the ships to be placed randomly or to place them himself. Uses the "Scanner" class to get a response from the user.
- main: The main method. Creates the objects of the game. Then it calls "randomPlace" and if it is "true" it calls the "placeAllShips" of the Board class. If the answer is "false", using the "switch" structure places the ships one by one, taking values from the user using the "getInput" and "getOrientation" methods and calling the "placeShip" of each ship. Then he randomly places the ships on the computer and the game begins, where using a "do-while" shots are fired at the points given by the player and randomly from the computer. The game ends when the shooting limit is exceeded (which is twenty) or if all of a player's ships have sunk. Finally, it displays an appropriate message, as the case may be, and prints the two tables.
In the C++ folder you can find the corresponding version of the game, written in the C++ programming language, which is as such:
- Tile: This class represents each cell. It has an "enum" structure that determines the values that a cell can take ("Sea", "Ship", "Hit", "Miss"). The class also has the following methods:
- Tile: The default constructor who takes no argument and simply creates a blank object.
- Tile (which is our constructor): This creator takes as arguments two integers that are the coordinates of the cell and its formula. It then assigns them to the variables of the class.
- setType: It is a "setter" method that takes a formula as an argument and assigns it to the cell.
- getType: It is a "getter" method that returns the cell type.
- draw: This method takes as an argument a "boolean" variable which if it has the value "false" prints in the type of each cell (showing where the ships are) otherwise it simply prints the sea, the failures and the inaccuracies.
- OverlapTilesException: A class that inherits from the Exception class and has the following methods:
- OverlapTilesException (which is also the constructor): Assigns a message to the variable of the "message" class.
- ~OverlapTilesException (which is also a destructor): Destroys the object as soon as it goes out of range. We also added the "virtual" keyword in front of the destroyer to activate the "late-binding" mechanism.
- what: A method, which despite its name is a getter method, which returns the message of the class.
- AdjacentTilesException: A class that inherits from the Exception class and has the following methods:
- AdjacentTilesException (which is also the constructor): Assigns a message to the variable of the "message" class.
- ~AdjacentTilesException (which is also a destructor): Destroys the object as soon as it goes out of range. We also added the "virtual" keyword in front of the destroyer to activate the "late-binding" mechanism.
- what: A method, which despite its name is a getter method, which returns the message of the class.
- Ship: An abstract class from which the other types of ships will inherit. Represents the general type of each ship. From this class inherit the classes: "Battleship", "Cruiser" and "Destroyer" and has the following abstract methods:
- placeShip: The method that is responsible for placing ships. It takes as arguments a two-position table which is the starting point of the ship, the orientation of the ship and the table on which it will place the ships.
- check_boundaries: This method checks if the ship's placement goes out of bounds. It takes as arguments a two-position table which is the starting point of the ship and the orientation of the ship.
- check_placement: Checks if there is already another ship where we are going to place a ship. As arguments he takes, a two-position table which is the starting point of the ship, the orientation of the ship and the table on which he will place the ships.
- check_orientation: Checks if there is at least one empty cell between another ship. It takes as arguments a two-position table which is the starting point of the ship, the orientation of the ship and the table on which it will place the ships.
- my_check_boundaries: It does the same job as check_boundaries, but returns "boolean" so that the ship details can be searched again.
- my_check_placement: It does the same job as check_placement, but returns "boolean" so that the ship details can be searched again.
- my_check_orientation: It does the same job as check_orientation, but returns "boolean" so that the ship details can be searched again.
- Battleship: One of the classes that inherits from Ship. It has an integer variable "size = 4" which indicates the size of the ship. Finally, Ship implements the abstract methods.
- Cruiser: One of the classes that inherits from Ship. It has an integer variable "size = 3" which indicates the size of the ship. Finally, Ship implements the abstract methods.
- Destroyer: One of the classes that inherits from Ship. It has an integer variable "size = 2" which indicates the size of the ship. Finally, Ship implements the abstract methods.
- Board: The class that represents the 5x5 size table and has these methods:
- Board (which is also the constructor): Takes as an argument a two-dimensional "Tile" table which it initializes using the creator of the "Tile" class.
- drawboards: Takes as an argument a two-dimensional "Tile" table which it initializes using the creator of the "Tile" class.
- getAdjacentTiles: Takes as arguments a cell and the table and returns its neighbors.
- placeAllShips: Argues a two-dimensional “Tile” type table and places the ships randomly by calling the “placeShip” method of each ship separately.
- allShipsSunk: Takes as an argument a two-dimensional "Tile" table and returns "boolean" and checks if all ships have sunk by returning "true" or "false".
- setHidden: Takes as an argument a Boolean variable and assigns it to the variable of class "hidden".
- Player: A class that represents each player. It has the following methods:
- Player (which is also the constructor): Takes as arguments the alphanumeric which is the name of the player. It also initializes the other variables classes to 0.
- fire: This method implements the shot made by the player. Initially increases by one the variables of the class "shots" and "tries". Then, using the "getType" and "setType" methods of the "Tile" class, he performs the shot.
- getStats: Prints on player statistics.
- getTries: Returns the player's tries.
- Game: A class that has only one method:
- getInput: A method that returns the player's shooting point. It takes as an argument a table of 2 positions which it fills with user inputs and then returns it as a pointer (since a pointer is a one-dimensional array).
- Main method: The main method which creates the objects of the game, randomly places the ships of the computer and then asks the player to make his shots until the ships of the computer sink or until he reaches the limit of the shots which is 10. Finally , prints the computer table, its statistics and a corresponding message if the player lost or won.
