C. Program Description for Developers

This program is about a game named Battleship. We made this game with three different levels of difficulty; easy, medium, and hard. Our game has three different codes for each different level of the game, thus making our AI more user compatible and more user friendly. Our game starts off with a brief description and a background story, and later displaying the rules on how to play the game. Then, the user is prompted to select the level of difficulty they want to play. After this, the user is again prompted to select the coordinates for placing their ships; carrier ship, cruiser ship, battleship, and the destroyer. The smart AI’s ships are randomly placed on the board with slight guidance in order to not overlap the ships on each other.

The game then starts with the toss of heads or tails deciding who goes first. The toss is generated by a random number between 1 (heads) and 2 (tails). The game then begins with the player who wins the toss. If the starting players misses the ship location of their enemy, then the opposition gets a turn to guess the opponent’s ship’s location, otherwise he can keep guessing. The game goes on back and forth until one player wins.

Since our game has three different difficulty levels, there are nine different functions used in the code for the AI to guess the users ship. The easy level has 4 Quadrants. There is a function that only counts the ship sprites in that quadrant (cnt_Non_Water_4Qua(); function name) on the players board. Then a function named guess_AI_4Qua() uses this function to guess in the quadrant that has the most ship sprite on the user board. If the guess is a hit then an advanced_AI_4Qua() is called into the main code because it’s the AI’s turn to guess again, else the guessing turn goes to the player. The advanced_AI_4Qua() function circles around the hit sprite to check if there is another ship in that area and guesses the next location but the difference in this function and other advanced AI function is that it only continues to guess in the ships direction on every alternate hit. The medium level has 8 Quadrants. Similarly, there are three functions that match closely to that for the easy level. The cnt_Non_WaterSprite() function now counts the ship sprites in 8 different quadrants of the 10 by 10 water sprite board. The guess_work_AI() function based on the results of cnt_Non_WaterSprite() guesses a ship location on the user board and if it’s a hit then advanced_AI() function is called in the main program, else the guessing turn goes to the player. The advanced_AI() now circles the hit sprite and guesses in the direction next to ship sprite, but the difference here is that it doesn’t guess the hit on every alternate turn. It just guesses it if it finds the ship. The hard level is similar to the previous two, but just makes the AI seem ‘smarter’ by making it even harder for the user to win. The cnt_Non_Water_16Qua() functions divides the 10 by 10 board into 16 quadrants and counts the ship sprites in each quadrant. The guess_AI_16Qua() guesses in the quadrant with most ship sprites, limiting the guessing area in each difficulty level. The advanced_AI_16Qua() works the same as the advanced_AI() of the medium level. These are the three different levels of the game. The trick for the advanced AI functions is for the user to place their ships on the edges so that while the smart AI’s guesses circles the hit sprite, the guessing goes out of bounds because there is no row/column 0 and row/column 11. To fix this, the team made an if condition that if row+1 or row-1 is 11/ or 0, or column+1 or column-1 is 11 or 0, then the smart AI will guess in the row but a different column and visa versa.

The overall process of the game is that it first displays the story and prompts the user for their choice of difficulty level. But before selecting the difficulty level the game board with 10 by 10 water sprites has been made for each player (board = water_sprite*ones(10,21);board(:,11) = blank_sprite;) with the help of the simple game engine and battleship png (my_scene = simpleGameEngine(‘Battleship.png’,84,84);) functions and files. Then the ships are placed by the user using the getMouseInput function/command and the AI’s ships are randomly placed on the board. The AI’s board is made into 4 quadrants and each ship is placed in one quadrant so that they don’t overlap. Meanwhile, the user is not limited to quadrants. After ship placement, a toss takes place that randomly guesses a number between 1 and 2. This is achieved by using randi([1,2],1,1); a random number vector for 1 and 2. Then, the winner gets the chance to guess first. A while loop is placed to check if all the ships are sunk or not (on one board; user or AI), if there are still ships remaining, the program will continue. The AI guesses with help of the most ships located in the respective quadrant, and users guess is a mouse input([r,c]=getMouseInput();). If the respective player or AI misses the ship then the other player gets the turn to guess, and the guessing goes back and forth. The small difference is that if the AI guesses the players ship then in the second turn an advanced AI function or the respective difficulty is called and then this function checks all the sprites circling the hit sprite and guesses in the direction when it finds a ship sprite. If the ship is hit, then the variable counter of the AI increase by one, and if the player guesses correctly then the variable counter of the player increases by one. This is how it is determined if all the ships are sunk. If any of the counters reaches a value of 14, then that respective player or the AI wins the game and the while loop is terminated. Finally, a Congratulations or Sorry type of message is displayed depending on which count reached 14 first.

 

List of Variables and Commands

Main program

  • my_scene: used battleship.png to create graphics for the game.
  • board= board where the user inputs his ships.
  • blank_sprite = 1(Set sprite number as variables);
  • water_sprite = 2(Set sprite number as variables);
  • left_end_sprite = 3(Set sprite number as variables);
  • horizontal_body_sprite = 4(Set sprite number as variables);
  • right_end_sprite = 5(Set sprite number as variables);
  • top_end_sprite = 6(Set sprite number as variables);
  • vertical_body_sprite = 7(Set sprite number as variables);
  • bottom_end_sprite = 8(Set sprite number as variables);
  • hit_sprite = 9(Set sprite number as variables);
  • miss_sprite = 10(Set sprite number as variables);
  • name: variable used for to user’s name.
  • level: variable used to store user’s difficulty level.
  • i: used as a variable counter to iterate throughout the for loop.
  • [r,c]:user entered co-ordinates through mouse input.
  • copy_board: a copy of the variable ‘board’ used for displaying on the screen.
  • AI_display: AI’s display for the game (where the AI’s ships are placed).
  • toss: user input of heads and tails.
  • ran_toss: vector containing either 1 or 2 randomly selected but randi() function.
  • hitmiss_display: used to display if the guess is a hit or a miss.
  • cnt_player_hits: variable used to count the correct guesses of ships made by the user.
  • cnt_AI_hits: variable used to count the correct guesses of ships made by the AI.
  • check: variable used in the while loop to check if the guess made by the user is a hit on the ship or not, if not then the check changes to one and breaks the loop and gives a turn to AI for guessing.
  • AI_guess: vector of 2 values (longitude and latitude) guessed by the AI.
  • check_AI: variable used in the while loop to check if the guess made by the AI is a hit on the ship or not, if not then the check_AI changes to one and breaks the loop and gives a turn to the user for guessing.

 

Functions

cnt_Non_Water_4Qua()

%this function counts the number of ship sprites in 4 quadrants in a 10 by 10 user board

  • C: copy of the board with the users ships on it.
  • i: count variable in outside for loop indicating rows.
  • j: count variable in the nested for loop indicating column.
  • cQ1: count variable for quadrant 1 (1-5 rows and column), counts ship spikes in this quadrant.
  • cQ2: count variable for quadrant 2 (1-5 rows and 6-10 column), counts ship spikes in this quadrant.
  • cQ3: count variable for quadrant 3 (6-10 rows and 1-5 column), counts ship spikes in this quadrant.
  • cQ4: count variable for quadrant 4 (6-10 rows and column), counts ship spikes in the quadrant.

 

cnt_not_WaterSpike()

%this function counts the number of ship sprites in 8 quadrants in a 10 by 10 user board

  • C: copy of the board with the users ships on it.
  • i: count variable in outside for loop indicating rows.
  • j: count variable in the nested for loop indicating column.
  • cQ1: count variable for quadrant 1 (1-5 rows and 1-3 column), counts ship spikes in this quadrant.
  • cQ2: count variable for quadrant 2 (1-5 rows and 4-5 column), counts ship spikes in this quadrant.
  • cQ3: count variable for quadrant 3 (1-5 rows and 6-8 column), counts ship spikes in this quadrant.
  • cQ4: count variable for quadrant 4 (1-5 rows and 9-10column), counts ship spikes in the quadrant.
  • cQ4: count variable for quadrant 5 (6-10 rows and 1-3 column), counts ship spikes in this quadrant.
  • cQ6: count variable for quadrant 6 (6-10 rows and 4-5 column), counts ship spikes in this quadrant.
  • cQ7: count variable for quadrant 7 (6-10 rows and 6-8 column), counts ship spikes in this quadrant.
  • cQ8: count variable for quadrant 8 (6-10 rows and 9-10 column), counts ship spikes in the quadrant.

 

cnt_not_Water_16Qua()

%this function counts the number of ship sprites in 16 quadrants in a 10 by 10 user board 

  • C: copy of the board with the users ships on it.
  • i: count variable in outside for loop indicating rows.
  • j: count variable in the nested for loop indicating column.
  • cQ1: count variable for quadrant 1 (1-3 rows and 1-3 column), counts ship spikes in this quadrant.
  • cQ2: count variable for quadrant 2 (1-3 rows and 4-5 column), counts ship spikes in this quadrant.
  • cQ3: count variable for quadrant 3 (1-3 rows and 6-8 column), counts ship spikes in this quadrant.
  • cQ4: count variable for quadrant 4 (1-3 rows and 9-10column), counts ship spikes in the quadrant.
  • cQ5: count variable for quadrant 5 (4-5 rows and 1-3 column), counts ship spikes in this quadrant.
  • cQ6: count variable for quadrant 6 (4-5 rows and 4-5 column), counts ship spikes in this quadrant.
  • cQ7: count variable for quadrant 7 (4-5 rows and 6-8 column), counts ship spikes in this quadrant.
  • cQ8: count variable for quadrant 8 (4-5 rows and 9-10 column), counts ship spikes in the quadrant.
  • cQ9: count variable for quadrant 9 (6-8 rows and 1-3 column), counts ship spikes in this quadrant.
  • cQ10: count variable for quadrant 10 (6-8 rows and 4-5 column), counts ship spikes in this quadrant.
  • cQ11: count variable for quadrant 11 (6-8 rows and 6-8 column), counts ship spikes in this quadrant.
  • cQ12: count variable for quadrant 12 (6-8 rows and 9-10column), counts ship spikes in the quadrant.
  • cQ13: count variable for quadrant 13 (9-10 rows and 1-3 column), counts ship spikes in this quadrant.
  • cQ14: count variable for quadrant 14 (9-10 rows and 4-5 column), counts ship spikes in this quadrant.
  • cQ15: count variable for quadrant 15 (9-10 rows and 6-8 column), counts ship spikes in this quadrant.
  • cQ16: count variable for quadrant 16 (9-10 rows and 9-10 column), counts ship spikes in the quadrant.

 

guess_AI_4Qua()

%this function randomly guesses the coordinates of the ships in the quadrant with the least number of water sprites

  • C: copy of the board with the users ships on it.
  • cQ1: value of ship spikes in quadrant 1 received from cnt_Non_Water_4Qua()
  • cQ2: value of ship spikes in quadrant 2 received from cnt_Non_Water_4Qua()
  • cQ3: value of ship spikes in quadrant 3 received from cnt_Non_Water_4Qua()
  • cQ4: value of ship spikes in quadrant 4 received from cnt_Non_Water_4Qua()
  • longQua1: randomly guessed row between 1 and 5 inclusive
  • latQua1: randomly guessed column between 1 and 5 inclusive
  • check: variable used in while loop so that if the guess is already a hit then guess the longitude and the latitude again, else longitude and latitude is stored in guess and check is changed to 1 to break the loop
  • guess: vector of values of the guessed longitude(row) and latitiude(column).
  • longQua2: randomly guessed row between 1 and 5 inclusive
  • latQua2: randomly guessed column between 6 and 10 inclusive
  • longQua3: randomly guessed row between 6 and 10 inclusive
  • latQua3: randomly guessed column between 1 and 5 inclusive
  • longQua4: randomly guessed row between 6 and 10 inclusive
  • latQua4: randomly guessed column between 6 and 10 inclusive
  • guessed: value of guess

 

guess_work_AI()

%this function randomly guesses the coordinates of the ships in the quadrant with the least number of water sprites 

  • C: copy of the board with the users ships on it.
  • cQ1: value of ship spikes in quadrant 1 received from cnt_Non_WaterSpike()
  • cQ2: value of ship spikes in quadrant 2 received from cnt_Non_WaterSpike()
  • cQ3: value of ship spikes in quadrant 3 received from cnt_Non_WaterSpike()
  • cQ4: value of ship spikes in quadrant 4 received from cnt_Non_WaterSpike()
  • cQ5: value of ship spikes in quadrant 5 received from cnt_Non_WaterSpike()
  • cQ6: value of ship spikes in quadrant 6 received from cnt_Non_WaterSpike()
  • cQ7: value of ship spikes in quadrant 7 received from cnt_Non_WaterSpike()
  • cQ8: value of ship spikes in quadrant 8 received from cnt_Non_WaterSpike()
  • longQua1: randomly guessed row between 1 and 5 inclusive
  • latQua1: randomly guessed column between 1 and 3 inclusive
  • check: variable used in while loop so that if the guess is already a hit then guess the longitude and the latitude again, else longitude and latitude is stored in guess and check is changed to 1 to break the loop
  • guess: vector of values of the guessed longitude(row) and latitiude(column).
  • longQua2: randomly guessed row between 1 and 5 inclusive
  • latQua2: randomly guessed column between 4 and 5 inclusive
  • longQua3: randomly guessed row between 1 and 5 inclusive
  • latQua3: randomly guessed column between 6 and 8 inclusive
  • longQua4: randomly guessed row between 1 and 5 inclusive
  • latQua4: randomly guessed column between 9 and 10 inclusive
  • longQua5: randomly guessed row between 6 and 10 inclusive
  • latQua5: randomly guessed column between 1 and 3 inclusive
  • longQua6: randomly guessed row between 6 and 10 inclusive
  • latQua6: randomly guessed column between 4 and 5 inclusive
  • longQua7: randomly guessed row between 6 and 10 inclusive
  • latQua7: randomly guessed column between 6 and 8 inclusive
  • longQua8: randomly guessed row between 6 and 10 inclusive
  • latQua8: randomly guessed column between 9 and 10 inclusive
  • guessed: value of guess

 

guess_AI_16Qua()

%this function randomly guesses the coordinates of the ships in the quadrant with the least number of water sprites

  • C: copy of the board with the users ships on it.
  • cQ1: value of ship spikes in quadrant 1 received from cnt_Non_Water_16Qua()
  • cQ2: value of ship spikes in quadrant 2 received from cnt_Non_Water_16Qua()
  • cQ3: value of ship spikes in quadrant 3 received from cnt_Non_Water_16Qua()
  • cQ4: value of ship spikes in quadrant 4 received from cnt_Non_Water_16Qua()
  • cQ5: value of ship spikes in quadrant 5 received from cnt_Non_Water_16Qua()
  • cQ6: value of ship spikes in quadrant 6 received from cnt_Non_Water_16Qua()
  • cQ7: value of ship spikes in quadrant 7 received from cnt_Non_Water_16Qua()
  • cQ8: value of ship spikes in quadrant 8 received from cnt_Non_Water_16Qua()
  • cQ9: value of ship spikes in quadrant 9 received from cnt_Non_Water_16Qua()
  • cQ10: value of ship spikes in quadrant 10 received from cnt_Non_Water_16Qua()
  • cQ11: value of ship spikes in quadrant 11 received from cnt_Non_Water_16Qua()
  • cQ12: value of ship spikes in quadrant 12 received from cnt_Non_Water_16Qua()
  • cQ13: value of ship spikes in quadrant 13 received from cnt_Non_Water_16Qua()
  • cQ14: value of ship spikes in quadrant 14 received from cnt_Non_Water_16Qua()
  • cQ15: value of ship spikes in quadrant 15 received from cnt_Non_Water_16Qua()
  • cQ16: value of ship spikes in quadrant 16 received from cnt_Non_Water_16Qua()
  • longQua1: randomly guessed row between 1 and 3 inclusive
  • latQua1: randomly guessed column between 1 and 3 inclusive
  • check: variable used in while loop so that if the guess is already a hit then guess the longitude and the latitude again, else longitude and latitude is stored in guess and check is changed to 1 to break the loop
  • guess: vector of values of the guessed longitude(row) and latitiude(column).
  • longQua2: randomly guessed row between 1 and 3 inclusive
  • latQua2: randomly guessed column between 4 and 5 inclusive
  • longQua3: randomly guessed row between 1 and 3 inclusive
  • latQua3: randomly guessed column between 6 and 8 inclusive
  • longQua4: randomly guessed row between 1 and 3 inclusive
  • latQua4: randomly guessed column between 9 and 10 inclusive
  • longQua5: randomly guessed row between 4 and 5 inclusive
  • latQua5: randomly guessed column between 1 and 3 inclusive
  • longQua6: randomly guessed row between 4 and 5 inclusive
  • latQua6: randomly guessed column between 4 and 5 inclusive
  • longQua7: randomly guessed row between 4 and 5 inclusive
  • latQua7: randomly guessed column between 6 and 8 inclusive
  • longQua8: randomly guessed row between 4 and 5 inclusive
  • latQua8: randomly guessed column between 9 and 10 inclusive
  • longQua9: randomly guessed row between 6 and 8 inclusive
  • latQua9: randomly guessed column between 1 and 3 inclusive
  • longQua10: randomly guessed row between 6 and 8 inclusive
  • latQua10: randomly guessed column between 4 and 5 inclusive
  • longQua11: randomly guessed row between 6 and 8 inclusive
  • latQua11: randomly guessed column between 6 and 8 inclusive
  • longQua12: randomly guessed row between 6 and 8 inclusive
  • latQua12: randomly guessed column between 9 and 10 inclusive
  • longQua13: randomly guessed row between 9 and 10 inclusive
  • latQua13: randomly guessed column between 1 and 3 inclusive
  • longQua14: randomly guessed row between 9 and 10 inclusive
  • latQua14: randomly guessed column between 4 and 5 inclusive
  • longQua15: randomly guessed row between 9 and 10 inclusive
  • latQua15: randomly guessed column between 6 and 8 inclusive
  • longQua16: randomly guessed row between 9 and 10 inclusive
  • latQua16: randomly guessed column between 9 and 10 inclusive
  • guessed: value of guess

advanced_AI_4Qua()

%this function circles the hit sprite and guesses the ship sprite if present near the hit sprite, but this is only if the count of the AI hits is even 

  • cnt_AI_hits: value of hits made by the AI
  • row: row of the board of the user (where ships are placed)
  • column: column of the board of the user (where the ships are placed)
  • guess: vector containing the value of the row and column that is guesses
  • check: variable used in while loop so that if the guess is already a hit then guess the longitude and the latitude again, else longitude and latitude is stored in guess and check is changed to 1 to break the loop
  • guess1: copy of guess

 

advanced_AI()

%this function circles the hit sprite and guesses the ship sprite if present near the hit sprite

  • row: row of the board of the user (where ships are placed)
  • column: column of the board of the user (where the ships are placed)
  • guess: vector containing the value of the row and column that is guesses
  • check: variable used in while loop so that if the guess is already a hit then guess the longitude and the latitude again, else longitude and latitude is stored in guess and check is changed to 1 to break the loop
  • guess1: copy of guess

 

advanced_AI_16Qua()

%this function circles the hit sprite and guesses the ship sprite if present near the hit sprite

  • row: row of the board of the user (where ships are placed)
  • column: column of the board of the user (where the ships are placed)
  • guess: vector containing the value of the row and column that is guesses
  • check: variable used in while loop so that if the guess is already a hit then guess the longitude and the latitude again, else longitude and latitude is stored in guess and check is changed to 1 to break the loop
  • guess1: copy of guess

 

randomizer_AI()

%randomly places AI ships in 4 different quadrants so that they don’t overlap

  • blank_sprite = 1(Set sprite number as variables);
  • water_sprite = 2(Set sprite number as variables);
  • left_end_sprite = 3(Set sprite number as variables);
  • horizontal_body_sprite = 4(Set sprite number as variables);
  • right_end_sprite = 5(Set sprite number as variables);
  • top_end_sprite = 6(Set sprite number as variables);
  • vertical_body_sprite = 7(Set sprite number as variables);
  • bottom_end_sprite = 8(Set sprite number as variables);
  • hit_sprite = 9(Set sprite number as variables);
  • miss_sprite = 10(Set sprite number as variables);
  • board: AI’s board to place its ships
  • random: a vector with values between 2 to 5 placed randomly in it
  • j: counter for the while loop
  • long: randomly selected longitude (row) for the ship sprite
  • lat: randomly selected latitude (column) for the ship sprite
  • i: counter for the for loop
  • k: counter for the for loop
  • m: counter for the for loop
  • n: counter for the for loop
  • board_AI=copy of board

 

Game Commands

simpleGameEngine()

%allows a sprite sheet to be used to create visuals as well as a general scene for the game

 

drawScene()

%displays the board of the game and can be called as many times as needed throughout the game

 

getMouseInput()

%receives input from the user’s mouse in terms of a specific row and specific column of the board