The game Battleship is a strategy game. The rules of the game are as follows: the game takes place in two 10×10 fields, the left-field belongs to the player, and the right-field belongs to the computer. Players and computers each have 5 battleships (17 units).
The first stage of the game is for the player to choose the difficulty level from level 1 to level 3. The difficulty level will affect the probability that the computer successfully hits the player’s battleship.
The second stage of the game is for the player to place his 5 search ships in his field. The warship can be swayed horizontally or vertically. There are only two rules on the placement of ships: 1. ships cannot exceed the 10×10 field. 2. ships cannot overlap each other. After the player places the battleship, the computer will also automatically place its own battleship randomly and also follow the above two rules. The player enters 0 for horizontal placement and 1 for vertical placement, and enter 1~10 for row and column coordinate in the command window of Matlab.
The third stage of the game is for the player and the computer to shoot each other’s ships. Players can only see the positions of their own ships, but cannot see the positions of the computer’s ships. The player starts the first round. The player enters the row coordinate and column coordinates he/she wants to shoot on the computer’s field in the command window of Matlab.. The range of both coordinates is 1-10. If the chosen location contains a computer’s ship unit, a red dot will be displayed on that location. On the contrary, if this coordinate is made by empty seawater, a white dot will be displayed. After the player shoots once, the computer will randomly shoot a unit on the player’s field. According to the difficulty of the player’s choice, the algorithm of the code changes the probability of the computer hitting the player. Similarly, if the computer hits the player’s boat, the player’s unit will be marked red, and if the computer misses, the player’s unit will be marked white.
The last stage of the game is declaring the winner. As long as one side’s ships are all shot by another side, the system will determine which side is the winner or the loser. Finally, the game results will be displayed on the command window of Matlab.