C. Program Description for Developers

Buckeyeship uses a 10 x 21 matrix that holds integer values. Simple Game Engine’s drawScene() function will be used to reflect the values of the 10 x 21 matrix in a GUI.

The overall game structure works using nested while loops and nested if statements. User-defined functions are structured in a recursive fashion that allows the entire program to loop indefinitely, allowing for unlimited replayability.

This game will have a semi-intelligent CPU that works in a three-phase algorithm: questing, hunting, and killing.

The program uses 4 files: Buckeyeship.m,  Setup.m, simpleGameEngine.m, and sprite_sheet.png.

 

Buckeyeship.m

The base game script, contains all primary logic of the gameplay loop. Uses 2 user-defined functions that are called recursively (meaning one function calls the other at the end of its own function, allowing for the program to work indefinitely.)

 

function TitleScreen()

Creates a figure window displaying the title screen of the game. Also sets a ‘KeyPressFcn’ which detects when the player presses the ‘space’ key. Once the function detects that the player pressed ‘space’, the figure window closes and the StartGame() function is called.

 

function StartGame()

The main meat and potatoes of Buckeyeship.m. Structured initially setting up the game board along with its GUI by defining ship_positions using function Setup(player) and then using drawScene() from simpleGameEngine.m for the GUI. Once ships are generated, more variables are defined outside the core while loop such as HP for the player and CPU, as well as targeting variables for the CPU.

The main while loop keeps running as long as the Player and CPU have an HP value above 0. Inside the loop itself contains a set of nested if/elseif/else statements for the Player’s turn, which check the players input to make sure that a proper location to hit was picked, along with further logic of the hit itself (hit == true, false).

The second half of the core while loop is a nested while loop that contains a nested if/elseif/else statements used for CPU targeting logic. The if/elseif/else targeting logic uses three variables: reference_target, current_target, and direction.

  • reference_target – reserved for when CPU finds a hit, sort of treated like a homing beacon to base hits off of until a ship is sunk
  • current_target – An immediate reference the CPU uses in conjunction with direction to properly target follow-up shots.
  • direction – a 1 to 4 value that signifies what direction the current_target variable is working off of. 1 = north, 2 = south, 3 = east, 4 = west.

Once the loop realizes that CPU or Player total HP has reached 0, the loop finishes. The script then goes to the final if statement which checks what team had 0 HP, to which the title of the GUI reflects. The title displaying victory or defeat stays there for 6 seconds, and then finally function TitleScreen() is called at the end of function StartGame() to allow an endless recursive nature for Buckeyeship.m

VARIABLES DEFINED

  • my_scene – set to a function in simpleGameEngine.m, basically sets code up to use drawScene() function using sprite_sheet.png
  • blank_sprite – first element of sprite_sheet.png, a complete black box used as a divider for the two 10 x 10 grids.
  • water_sprite – second element, self explanatory; its water.
  • left_ship_sprite – third element, shows horizontal left side of ship
  • horiz_ship_sprite – middle of a horizontal ship
  • right_ship_sprite – right side of horizontal ship
  • top_ship_sprite – top of vertical ship
  • vert_ship_sprite – middle of vertical ship
  • bot_ship_sprite – bottom of vertical ship
  • hit_sprite – red marker signifying a hit space
  • miss_sprite – white marker signifying a missed space
  • ship_positions – a 10 x 21 matrix, not to be confused with board_display. Holds the actual values of objects that board_display reflects on with drawScene()
  • board_display  – a 10 x 21 matrix used as a base layer for drawScene(), reflects the values of ship_positions to accurately show sprites from sprite_sheet.png
  • hitboard_display – a 10 x 21 matrix used as top layer of drawScene(), initially starts as blank spaces but becomes populated with hit/miss sprites as game goes on.
  • board_display – primary matrix used for GUI made by drawScene()
  • hitboard_display = secondary matrix used as top layer for GUI made by drawScene()
  • player_total_hp – total ship spaces of player
  • player_ship_hp – a 1×5 array containing the HP values of each ship for Player. Indexes of this array are used in if statements to check individual ship HP.
  • CPU_total_hp – same as player_total_hp but for CPU
  • CPU_ship_hp – same as player_ship_hp but for CPU

 

COMMANDS

drawScene() – a simpleGameEngine.m function that creates or updates the main GUI for the game.

 

Setup.m

A function file called by Buckeyeship.m, it returns 10 x 21 matrices to be used in the gameplay loop.

 

simpleGameEngine.m

An essential file that allows for GUI display of the 10 x 21 matrix using sprite_sheet.png. This file also has functions for player mouse/keyboard input.