C. Program Description for Developers


Aspects of the code:

The code is comprised of 6 main sections:

  • Initializing of the scene and variables – This section creates the board for the game to be played on, as well as all the necessary variables.
  • Placing the ships – This next section of code allows the user to place all 5 of their ships anywhere on the board. The code ensure that the ships remain on the board, that they don’t overlap, that their location is stored, and that all 5 of them are placed. (The function Setup creates the placement of the computer’s ships).
  • Player’s turn – In this section, the row and column of the player’s mouse click is stored. The code checks whether that space is on the board, occupied by water, another hit, miss, or ship square. Depending on those conditions, nothing will happen (invalid space clicked), a hit will be displayed, or a miss.
  • Computer’s turn – This is referred to as the AI. In this section, the computer starts by using a random number generator to shoot at a random row/column on the player’s board. If the shot is a miss, the computer continues in the same manner on its next turn. If the shot is a hit, the computer will shoot at the space above the hit (unless it is a wall or already occupied by another hit or miss – then the algorithm will take over **slightly more complex to explain but prevents the game from breaking). If that space above is a miss, the computer will try the space to the right of the hit, then below, then to the left. However, if any of those previously mentioned shots are a hit, the computer will instead continue shooting in the line until a miss occurs (or a wall). At that point, the computer will shoot in that same line, but in the opposite direction until a miss (similar to how humans play battleship). At that point, the computer goes back to randomly shooting, and the process resets. (** important note: this process has been explained in a simplified manner. Additional code is in place make the process more efficient by using information already on the board such as walls, previous hits, and previous misses.)
  • Ending the game – This code counts how many hits are on both sides of the board. Each side has a total of 17 ship squares, so once the number of hits on either side of the board is equal to 17, the game ends and a winner is determined.
  • **Text displays** – This section is not at the end of the code, but throughout the entire script. String variables, display functions, and other commands are in place to provide on-screen instructions throughout the game, as well as real-time feedback on ships being destroyed and hits.

Variables Used:

Sprite-related Variables:

  • BattleSprites – Stores the sprites used
  • blank_sprite = 1 (Black square – located as the 1st sprite within “BattleSprites”)
  • water_sprite = 2 (Water square – located as the 2nd sprite within “BattleSprites”)
  • left_ship_sprite = 3 (left-end of a horizontal ship square – located as the 3rd sprite within “BattleSprites”)
  • horiz_ship_sprite = 4 (Middle-section of horizontal ship square – located as the 4th sprite within “BattleSprites”)
  • right_ship_sprite = 5 (Right-end of a horizontal ship square – located as the 5th sprite within “BattleSprites”)
  • top_ship_sprite = 6 (Top-end of a vertical ship square – located as the 6th sprite within “BattleSprites”)
  • vert_ship_sprite = 7 (Middle-section of vertical ship square – located as the 7th sprite within “BattleSprites”)
  • bot_ship_sprite = 8 (Bottom-end of vertical ship square – located as the 8th sprite within “BattleSprites”)
  • hit_sprite = 9 (“hit” square – located as the 9th sprite within “BattleSprites”)
  • miss_sprite = 10 (“miss” square – located as the 10th sprite within “BattleSprites”)
  • ButtonH_UN = 11 (Horizontal button “unpressed” – 11th sprite in “BattleSprites”)
  • ButtonH_P = 12 (Horizontal Button “pressed” – 12th sprite in “BattleSprites)
  • ButtonV_UN = 13 (Vertical Button “unpressed” – 13th sprite in “BattleSprites)
  • ButtonV_P = 14 (Vertical Button “pressed” – 14th sprite in “BattleSprites)
  • Boards1 – stores the board being displayed (both sides plus the blank middle divider and bottom with buttons)

 

Text-Related Variables

  • x – stores displayed text as a string (so it can be deleted)
  • Prompt – stores displayed text as a string
  • Prompt_11 – stores the print-out of “Prompt” as a variable (so it can be deleted)
  • Prompt1 – stores text to be displayed as a string
  • Prompt2 – stores text to be displayed as a string
  • Prompt_1 – stores the displayed text from “Prompt1” as a variable (so it can be deleted)
  • Prompt_2 – stores the displayed text from “Prompt2” as a variable (so it can be deleted)
  • rec1 – stores the rectangle that is displayed behind the text Prompt_gay1 (so it can be deleted)
  • rec2 – stores the rectangle that is displayed behind the text Prompt_gay2 (so it can be deleted)
  • scoreP – stores text as a string (initial score of 0/17 for the player)
  • Score_P – stores the print-out of “score” as a variable (so it can be deleted)
  • scoreC – stores text as a string (initial score of 0/17 for the computer)
  • Score_C – stores the print-out of “scoreC” as a variable (so it can be deleted)
  • ADisp – Stores text to be displayed as a string (Carrier Sunk!)
  • BDisp – Stores text to be displayed as a string (Battleship Sunk!)
  • NDisp – Stores text to be displayed as a string (Cruiser Sunk!)
  • DDisp – Stores text to be displayed as a string (Submarine Sunk!)
  • EDisp – Stores text to be displayed as a string (Destroyer Sunk!)
  • AADisp – Stores text to be displayed as a string (Carrier Sunk!)
  • BBDisp – Stores text to be displayed as a string (Battleship Sunk!)
  • NNDisp – Stores text to be displayed as a string (Cruiser Sunk!)
  • DDDisp – Stores text to be displayed as a string (Submarine Sunk!)
  • EEDisp – Stores text to be displayed as a string (Destroyer Sunk!)
  • A_Disp – Stores the print-out of “ADisp” as a variable (so it can be deleted)
  • B_Disp – Stores the print-out of “BDisp” as a variable (so it can be deleted)
  • N_Disp – Stores the print-out of “NDisp” as a variable (so it can be deleted)
  • D_Disp – Stores the print-out of “DDisp” as a variable (so it can be deleted)
  • E_Disp – Stores the print-out of “EDisp” as a variable (so it can be deleted)
  • AA_Disp – Stores the print-out of “AADisp” as a variable (so it can be deleted)
  • BB_Disp – Stores the print-out of “BBDisp” as a variable (so it can be deleted)
  • NN_Disp – Stores the print-out of “NNDisp” as a variable (so it can be deleted)
  • DD_Disp – Stores the print-out of “DDDisp” as a variable (so it can be deleted)
  • EE_Disp – Stores the print-out of “EEDisp” as a variable (so it can be deleted)
  • WinC – Stores text to be displayed as a string (You Lose!)
  • Win_C – Stores the print-out of “WinC” as a variable (so it can be deleted)
  • WinP – Stores text to be displayed as a string (You Win!)
  • Win_P – Stores the print-out of “WinP” as a variable (so it can be deleted)

 

Other Variables Used:

  • User_Board – matrix that stores the location of the different user boats, hits, misses, and water
  • Computer_Board – matrix that stores the location of the different computer boats, hits, misses, and water
  • restart_button – stores information of the restart button (also calls the “restart_game” function)
  • Vert – determines if the ships placed are vertical or horizontal (determined by the buttons in the bottom left of the screen)
  • Boat_counter – counts how many boats the player has placed
  • row – keeps track of the row the player has clicked
  • col – keeps track of the column the player has clicked
  • button – stores a value based on if the mouse is clicked (right vs left click)
  • turnCounter – keeps track of whose turn it is
  • k5 = -2 (counting variable used for the AI in “shit = #5”)
  • k6 = 2 (counting variable used for the AI in “shit = #6”)
  • k7 = 2 (counting variable used for the AI in “shit = #7”)
  • k8 = -2 (counting variable used for the AI in “shit = #8”)
  • k9 = 1 (counting variable used for the AI in “shit = #9”)
  • k10 = -1 (counting variable used for the AI in “shit = #10”)
  • k11 = -1 (counting variable used for the AI in “shit = #11”)
  • k12 = 1 (counting variable used for the AI in “shit = #12”)
  • lock1 – (“lock” used to prevent text #1 from being displayed more than once)
  • lock2 – (“lock” used to prevent text #2 from being displayed more than once)
  • lock3 – (“lock” used to prevent text #3 from being displayed more than once)
  • lock4 – (“lock” used to prevent text #4 from being displayed more than once)
  • lock5 – (“lock” used to prevent text #5 from being displayed more than once)
  • lock6 – (“lock” used to prevent text #6 from being displayed more than once)
  • lock7 – (“lock” used to prevent text #7 from being displayed more than once)
  • lock8 – (“lock” used to prevent text #8 from being displayed more than once)
  • lock9 – (“lock” used to prevent text #9 from being displayed more than once)
  • lock10 – (“lock” used to prevent text #10 from being displayed more than once)
  • Win – remains equal to 0 until a game winning condition is met (used to end the game)
  • shit – used to differentiate between the 12 possible types of moves the AI can make
  • R – stores the random number generated for the row the AI shoots at
  • C – stores the random number generated for the column the AI shoots at
  • Hitcount_P – Counts and stores the number of hits on the player’s board
  • Hitcount_C – Counts and stores the number of hits on the computer’s board
  • A – stores the number of user carrier spaces still on the board
  • B – stores the number of user battleship spaces still on the board
  • N – stores the number of user cruiser spaces still on the board
  • D – stores the number of user submarine spaces still on the board
  • E – stores the number of user destroyer spaces still on the board
  • AA – stores the number of computer carrier spaces still on the board
  • BB – stores the number of computer battleship spaces still on the board
  • NN – stores the number of computer cruiser spaces still on the board
  • DD – stores the number of computer submarine spaces still on the board
  • EE – stores the number of computer destroyer spaces still on the board

Game Commands Used:

Restart button – Used to restart the game (closes the figure and then runs the script). This uses the only function created by team B in the game (name: restart_game). The function can be seen at the end of the code. The function works by closing the current figure and then running the script file Battleship1.m when called. The button is creating using the uicontrol MATLAB function.

Horizontal button – Allows ships to be placed horizontally. (switches between a “button presses” and “unpressed” sprite when clicked). The button functions by simply switching the “vert” variable between 0 and 1 when clicked. (0 = horizontal, 1 = vertical).

Vertical button – Allows ships to be placed vertically. (same description as above).

Mouse click – Outputs the location of the user’s mouse clicks (as well as a value for which button was clicked “right/left/center”). Uses the function ginput to operate (that function is defined in the “simpleGameEngine” script file.