/BattleShips-Java

A Java console application challenge by Jetbrains Academy - BattleShip Game

Primary LanguageHTML

BattleShips

A Java console application challenge by Jetbrains Academy - BattleShip Game

Details

Battleship (also called Battleships or Sea Battle) is a two-player strategy game whose history traces back to the First World War. You have a 10×10 game field and five ships to arrange on that field. The ships can be placed horizontally or vertically but not diagonally across the grid spaces; the ships should not cross or touch each other. The goal is to sink all the ships of the opponent before your opponent does this to you. Both players add the ships to their fields one by one (no peeking!), and then start shelling each other until one of them succeeds. To make the game fair and prevent the players from peeping at each other's fields, after each move added the message "Press Enter and pass the move to another player".

Example

Player 1, place your ships on the game field

  1 2 3 4 5 6 7 8 9 10  
A ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
B ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
C ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
D ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
E ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
F ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
G ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
H ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
I ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
J ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  

Enter the coordinates of the Aircraft Carrier (5 cells):

F3 F7

    1 2 3 4 5 6 7 8 9 10  
  A ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
  B ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
  C ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
  D ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
  E ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
  F ~ ~ O O O O O ~ ~ ~  
  G ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
  H ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
  I ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  
  J ~ ~ ~ ~ ~ ~ ~ ~ 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