Program description of Battleship:
The Variables used:
I:The variables of scene displaying parts
my_scene = simpleGameEngine(‘Battleship.png’,84,84);
board_display = water_sprite * ones(10,21);
board_display(:,11) = blank_sprite;
II:The variables of setting different hardness level parts
Level = input(‘Set difficulty level’);
III:The variables of placing the ships for player parts
Player_Ships = zeros(10,10);
Ship_Length = [5,4,3,3,2];
Ship_Name = [“carrier”,“battleship”,“submarine”,“cruiser”,“PT boat”];
IV:The variables of placing the ships of the computer
Cpu_Ships = zeros(10,10);
Ship_Row_Co = [0,0,0,0,0];
Ship_Col_Co = [0,0,0,0,0];
Ship_Hor_Ver = [0,0,0,0,0];
Ship_Placed;
Horizontal;
Row;
Col;
V:The variables of shooting and displaying parts
Hitmiss_Display = blank_sprite * ones(10,21);
Player_Score_Count = 0;
Computer_Score_Count = 0;
PlayerShotRow = input(‘Please enter a different row coordinate from the location that is in range: ‘);
PlayerShotCol = input(‘Please enter a different column coordinate from the location that is in range: ‘);
CpuShotRow = randi([1 10]);
CpuShotCol = randi([1 10]);
Rate = randi([1 10]);
Possible_Shot_Row = randi([1 5]):randi([6 10]);
Possible_Shot_Col = randi([1 5]):randi([6 10]);
The Description of program algorithm:
For the Battleship Game, it contains the following sections:
For setting different hardness level, the variable “Level“ and an input command are used, user could decide the Hardness level he wants to play, from easy to hell.
For placing the ships of the player, we used several variables, firstly, we used a for loop to put the 5 battleships. For each iterate, there is a nested while loop that as long as the boolean variable “Ship_Placed“ is not true, program will repeat the placing process again. The variable “Horizontal” contains a logical integer 0 or 1. If it is 0, the ship will be placed horizontally. If it is 1, the ship will be placed vertically. Then we used a if statement that checks whether the ship which is currently placing overlaps with ships that already exist. “If sum(Player_Ships(row,col:(col+ship_length(ship_id)-1))) == 0” or “If sum(Player_Ships(row:(row+ship_length(ship_id)-1),col)) == 0” depends on whether the ship is horizontal or vertical, true means there is no ship units existing in the area that player want to place a ship. That is, the player can successfully place his ship in the area he wants. After placing the ship, “Player_Ships(row,col:(col+ship_length(ship_id)-1)) = Ship_ID” or “Player_Ships(row:(row+ship_length(ship_id)-1),col) = Ship_ID” also depends on whether the ship is horizontal or vertical, assigns non-zero number to the location where ships exist. “Board_Display” function is used to assign each location with images of the ship. After one ship is successfully placed, the “ship_placed” variable will be assigned with true, so that next iteration can begin until the first 5 iterations are finished(The process of placing players’ ships).
For placing the ships of the computer, we used similar algorithm of codes as the codes of placing the ships of the player. The only difference is we used “randi” function to randomly place the ships of the computer.
For the shooting parts, we used a while loop that keeps functioning as long as neither “Player_Possible_Count” nor “Computer_Possible_Count” equals to 17, the one who reaches the total score 17 as well as shots down all ships of the opponent will win, otherwise the game keeps going. Then comes to the part of shooting ships, firstly, we used two variables “PlayerShotRow” and “PlayerShotCol” to let player decide where he wants to shoot, in order to make sure player does not shoot out of the range, we used two while loop “while PlayerShotRow < 1 || PlayerShotRow > 10” and “while PlayerShotCol < 1 || PlayerShotCol > 10”. Moreover, the while loop “while Cpu_Ships(PlayerShotRow, PlayerShotCol) == -1” stops the player from shooting a same location. An if statement “if cpu_ships(playerShotRow, playerShotCol) > 0” checks whether the player shoots the ships of opponent. If player successfully shoots opponent’s ship, the player will get 1 point by adding 1 to the variable “Player_Possible_Count”, marking the location with a red dot(Possible enemy ships). If the location is empty, mark the location with a white dot. No matter the player hits the ships of opponent or misses, the location will be assigned with a value -1, which indicates that the location is already been attacked once. Then is the turn of the computer. Just as the placing process, the computer’s shooting algorithm is the same as the player’s shooting algorithm. The only difference is the variables “CpuShotRow” and “CpuShotCol” are not the inputs from the player but the “randi” function. This is the way we upgraded the IQ of the computer, making it smarter as well as increasing the hardness of the Battleship Game. The variable “Expected_Hardness” is a random integer from 1 to 10. We used an if statement “If Expected_Hardness <= Level”, which provides a possibility that changes as the player decides a different hardness level using the variable “Level” in an input command. If the statement is true(=1), inside the loop we create two variables “Possible_Shot_Row” and “Possible_Shot_Col”, and an algorithm that assigns the row and column coordinate that must contain a part of the player’s ship into those two variables, then let “CpuShotRow” and “CpuShotCol” equal to those two variables, increasing the hardness of the Battleship Game.
For stating who wins the game in the last, we used a if command that “If Player_Score_Count == 17” indicates the player wins and an elseif command “Elseif Computer_Score_Count == 17” indicates the player losses.