Battleship.pas is the main form of the batttleship game. It contains both plyers boards and most game controls.

 

 

//////////////////////////////////////////////////////////////////
// BoardMouseDown - Handles all occurances of clicks on the game
// board.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.BoardMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

//////////////////////////////////////////////////////////////////
// CannonFired - Fires on a given map at position Col, Row
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.CannonFired(WhichMap, Col, Row: Integer);

//////////////////////////////////////////////////////////////////
// CheckBoard - Checks for sunk boats and for victory.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.CheckBoard(WhichMap:Integer);

//////////////////////////////////////////////////////////////////
// CheckSink - Checks to see if a boat is sunk
//////////////////////////////////////////////////////////////////

function TFrmBattleShip.CheckSink(WhichMap:Integer;WhichShip:TShip):Boolean;

//////////////////////////////////////////////////////////////////
// DeclareVictory - Declares vitory for a given player
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.DeclareVictory(WhichPlayer : Integer);

//////////////////////////////////////////////////////////////////
// PlaceBoat - Places CurrentShipPlaced(GLOBAL VAR) on a given
// board at position Col, Row
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.PlaceBoat(WhichMap, Col, Row: Integer);

//////////////////////////////////////////////////////////////////
// PlaceInProgress - Places a new ship. Triggers place InProgress
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.PlaceInProgress(WhichMap, Col, Row, Size: Integer);

//////////////////////////////////////////////////////////////////
// SetPlacements - Checks and sets valid placement cells.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.SetPlacements(WhichMap, Col, Row, Size: Integer);

//////////////////////////////////////////////////////////////////
// ClearPlacements - Replaces all occurances of csPlacement in
// WhichMap to csDefault.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.ClearPlacements(WhichMap : Integer);

//////////////////////////////////////////////////////////////////
// PaintGame - Paints the 2 game boards.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.PaintGame();

//////////////////////////////////////////////////////////////////
// ErasePaint - Clears any paint on the canvas of a given board.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.ErasePaint(WhichMap : Integer);

//////////////////////////////////////////////////////////////////
// PaintCellStates - Paints the hits, misses and placment bitmaps.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.PaintCellStates(WhichMap : Integer);

//////////////////////////////////////////////////////////////////
// PaintShips - Finds all valid start points and paints the ship
// located at that position.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.PaintShips(WhichMap : Integer);

//////////////////////////////////////////////////////////////////
// PaintShip - Paints a ship at a given position to a given map.
// Called by PaintShips, which paints ALL ships.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.PaintShip(WhichMap,Col,Row : Integer);

//////////////////////////////////////////////////////////////////
// GetBearing - Gets the bearing from a start and end position.
// Returns: bNorth,bEast,bSouth or bWest.
//////////////////////////////////////////////////////////////////

function TFrmBattleShip.GetBearing(Stx,Sty,Endx,Endy : Integer):TBearing;

//////////////////////////////////////////////////////////////////
// SetEndPt - Stores the end point and calls place boat.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.SetEndPt(WhichMap, Col, Row: Integer; Ship:TShip);

//////////////////////////////////////////////////////////////////
// BoatPlaced - Places boat in specified board.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.BoatPlaced(WhichMap,StartPtX,StartPtY,EndPtX,EndPtY: Integer; Ship:TShip);

//////////////////////////////////////////////////////////////////
// PlaceVertical - Helper function for BoatPlaced. Places a boat
// vertically in a specified board.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.PlaceVertical(WhichMap, A, B, C: Integer; Ship:TShip);

//////////////////////////////////////////////////////////////////
// PlaceHorizontal - Helper function for BoatPlaced. Places a
// boat horizontally in a specified board.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.PlaceHorizontal(WhichMap, A, B, C: Integer; Ship:TShip);

//////////////////////////////////////////////////////////////////
// SuccessPlace - Wraps up after a successful place.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.SuccessPlace(Ship:TShip);

//////////////////////////////////////////////////////////////////
// CheckHorizontal - Checks for boats horizontally. Sets a global
// flag if boat is found.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.CheckHorizontal(WhichMap, Row, Col1, Col2: Integer);

//////////////////////////////////////////////////////////////////
// CheckVertical - Checks for boats vertically. Sets a global
// flag if boat is found.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.CheckVertical(WhichMap, Col, Row1, Row2: Integer);

//////////////////////////////////////////////////////////////////
// ColorCell - Colors a specific cell of a specific board a
// specified color.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.ColorCell(WhichMap, Col, Row: Integer;
color: TColor);

//////////////////////////////////////////////////////////////////
// Menu Options - Code and other shit for the top menu.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.Quit1Click(Sender: TObject);

//////////////////////////////////////////////////////////////////
// btnPlaceShip - Event called when place ship button is pressed.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.btnPlaceShipClick(Sender: TObject);

//////////////////////////////////////////////////////////////////
// FormCreate - Event ran at startup.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.FormCreate(Sender: TObject);

//////////////////////////////////////////////////////////////////
// SizeSelect - Returns the size of the CurrentShipPlaced (GLOBAL)
//////////////////////////////////////////////////////////////////

function TFrmBattleShip.SizeSelect(): Integer;

//////////////////////////////////////////////////////////////////
// btnDonePlacingClick
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.btnDonePlacingClick(Sender: TObject);

//////////////////////////////////////////////////////////////////
// CheckStatus - Check to see if all players ships are placed.
//////////////////////////////////////////////////////////////////

function TFrmBattleShip.CheckStatus(): Boolean;

//////////////////////////////////////////////////////////////////
// ShowButtons - Enables all the ship placement buttons.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.ShowButtons();

//////////////////////////////////////////////////////////////////
// btnPlayClick - OnClickEvent for play button.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.btnPlayClick(Sender: TObject);

//////////////////////////////////////////////////////////////////
// EndTurn - End CurrentPlayers turn and move on to next player
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.EndTurn();

//////////////////////////////////////////////////////////////////
// CurrentMouseToCell - MouseToCell for CurrentPlayer's grid.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.CurrentMouseToCell(X,Y:Integer; var ACol,ARow : Integer);

//////////////////////////////////////////////////////////////////
// OpposingMouseToCell - MouseToCell for OpposingPlayer's grid.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.OpposingMouseToCell(X,Y:Integer; var ACol,ARow : Integer);

//////////////////////////////////////////////////////////////////
// BoardSelectCell - Gets rid of selection...
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.BoardSelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);

//////////////////////////////////////////////////////////////////
// GetShipName - Returns a srting for a given ship type
//////////////////////////////////////////////////////////////////

function TFrmBattleShip.GetShipName(ShipType : TShip) : String;

//////////////////////////////////////////////////////////////////
// FormPaint - Repaints game when entering from another window.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.FormPaint(Sender: TObject);

//////////////////////////////////////////////////////////////////
// StartNewGame - Starts a new game different ways, depending on
// GameMode (GLOBAL). Also prompt for player names
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.StartNewGame();

//////////////////////////////////////////////////////////////////
// StartSinglePlayerGame - Init single player game.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.StartSinglePlayerGame();

//////////////////////////////////////////////////////////////////
// StartMultiPlayerGame - Init multi player game.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.StartMultiPlayerGame();

//////////////////////////////////////////////////////////////////
// StartInternetGame - Init internet game.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.StartInternetGame();

//////////////////////////////////////////////////////////////////
// ResetGame - Resets all game variables.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.ResetGame();

//////////////////////////////////////////////////////////////////
// HideGame - Hides the game.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.HideGame();

//////////////////////////////////////////////////////////////////
// ShowGame - Shows the game.
/////////////////////////////////////////////////////////////////
/
procedure TFrmBattleShip.ShowGame();

//////////////////////////////////////////////////////////////////
// btnRandomPlaceClick - Place ships randomly.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.btnRandomPlaceClick(Sender: TObject);

//////////////////////////////////////////////////////////////////
// RandomPlace - Clear and place ships randomly on a given board
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.RandomPlace(WhichMap:Integer);

//////////////////////////////////////////////////////////////////
// DoComputerTurn - Computer takes a turn.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.DoComputerTurn();

//////////////////////////////////////////////////////////////////
// GetShotCoord - Gets a coord from board 1 for the computer to
// shoot at. Help function for DoComputerTurn()
//////////////////////////////////////////////////////////////////

function TFrmBattleShip.GetShotCoord() : TPoint;

//////////////////////////////////////////////////////////////////
// SaveGame1Click - Save a battleship game
//
///////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.SaveGame1Click(Sender: TObject);

//////////////////////////////////////////////////////////////////
// LoadGame1Click - Load a battleship game onto the screen
//
///////////////////////////////////////////////////////////////////

//load game should be enabled only when no boats have been placed//
procedure TFrmBattleShip.LoadGame1Click(Sender: TObject);

//////////////////////////////////////////////////////////////////
// LoadGame- Loads a saved game
///////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.LoadGame(SaveGame : TGame);

//////////////////////////////////////////////////////////////////
// LoadCellState - Converts the saved CellState
// into a TCellState.
///////////////////////////////////////////////////////////////////

function TFrmBattleShip.LoadCellState(val : byte): TCellState;

//////////////////////////////////////////////////////////////////
// SaveCellState - Converts the TCellState into a
// a byte for the saved game.
///////////////////////////////////////////////////////////////////

function TFrmBattleShip.SaveCellState(val :TCellState): Byte ;

//////////////////////////////////////////////////////////////////
// LoadShipType - Converts the saved ShipState byte
// into a TShipState
///////////////////////////////////////////////////////////////////

function TFrmBattleShip.LoadShipType(val : byte) : TShip;

//////////////////////////////////////////////////////////////////
// SaveShipType - Converts the TShipType into a
// a byte for the saved game.
///////////////////////////////////////////////////////////////////

function TFrmBattleShip.SaveShipType(val : TShip) : Byte;

//////////////////////////////////////////////////////////////////
// DeclareLoss - Which player losses the game.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.DeclareLoss(WhichPlayer:Integer);

//////////////////////////////////////////////////////////////////
// DecodeData - Decodes data received from
// opponent.
// ================================
// =MSG=MessageText=
// =MOV=1=X=Y=
// =PLC=1=A=SX=SY=EX=EY=
// =OTH=CMD= R->Ready N->Name
//
//
// sACarrier: ShipStr := '1';
// sCarrier: ShipStr := '2';
// sBattleship: ShipStr := '3';
// sSubmarine: ShipStr := '4';
// sDestroyer: ShipStr := '5';
/////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.DecodeData(Data:OLEVariant);

//////////////////////////////////////////////////////////////////
// SendData - Sends a DataString to other player
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.SendData(const DataString: String);

//////////////////////////////////////////////////////////////////
// TCPServerConnectionRequest - Server accepts client.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.TCPServerConnectionRequest(Sender: TObject;
requestID: Integer);

//////////////////////////////////////////////////////////////////
// TCPClientConnect - When client connects to server.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.TCPClientConnect(Sender: TObject);

//////////////////////////////////////////////////////////////////
// ErrorEvents - Handle winsock errors
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.TCPServerError(Sender: TObject; Number: Smallint;
var Description: WideString; Scode: Integer; const Source,
HelpFile: WideString; HelpContext: Integer; var CancelDisplay: WordBool);
procedure TFrmBattleShip.TCPClientError(Sender: TObject; Number: Smallint;
var Description: WideString; Scode: Integer; const Source,
HelpFile: WideString; HelpContext: Integer; var CancelDisplay: WordBool);

//////////////////////////////////////////////////////////////////
// DisconnectError - Thrown when players get disconnected.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.DisconnectError();

//////////////////////////////////////////////////////////////////
// CannotConnectError - Thrown when players get disconnected.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.CannotConnectError();

//////////////////////////////////////////////////////////////////
// OnDataArrival - DecodeData on arrival
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.TCPClientDataArrival(Sender: TObject;
bytesTotal: Integer);

//////////////////////////////////////////////////////////////////
// SendMessage - Sends a message to the other player and posts
// message to chat board.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.SendMessage(const Msg:String);

//////////////////////////////////////////////////////////////////
// PostMessage - Posts message to chat board
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.PostMessage(const Msg:String);

//////////////////////////////////////////////////////////////////
// DecodeDataPlace - Decodes the PLC command
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.DecodePlace(DataString:String);

//////////////////////////////////////////////////////////////////
// DecodeOther - Decodes the OTH command
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.DecodeOther(DataString:String);

//////////////////////////////////////////////////////////////////
// DecodeMessage - Decodes the MSG command
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.DecodeMessage(DataString:String);

//////////////////////////////////////////////////////////////////
// EncodeMove - Encodes and sends to other player
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.EncodeMove(WhosTurn,X,Y:Integer);

//////////////////////////////////////////////////////////////////
// InternetBoardMouseDown - Handles all occurances of clicks on
// the game board in internet mode.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.InternetBoardMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

//////////////////////////////////////////////////////////////////
// InternetbtnDonePlacingClick - Done placing
// ships; for internet game.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.InternetbtnDonePlacingClick(Sender: TObject);

//////////////////////////////////////////////////////////////////
// MediaPlayerNotify - Replay the background
// music when finished.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.MediaPlayerNotify(Sender: TObject);

//////////////////////////////////////////////////////////////////
// FormClose - Cleans up code at close.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.FormClose(Sender: TObject;
var Action: TCloseAction);

//////////////////////////////////////////////////////////////////
// FillScoreArray - Load high scores
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.fillscorearray();

//////////////////////////////////////////////////////////////////
// WriteHighScores - Save the high scores, called
// on exit.
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.writehighScores();

//////////////////////////////////////////////////////////////////
// SearchScores - Check to see if player makes
// makes the cut...
//////////////////////////////////////////////////////////////////

procedure TFrmBattleship.searchScores(score : integer;WhichPlayer : Integer);

//////////////////////////////////////////////////////////////////
// TopScores1Click - Show top scores
//////////////////////////////////////////////////////////////////

procedure TFrmBattleShip.TopScores1Click(Sender: TObject);