Question: this is the code i came up with edit this according to the example code given below. MACHINE Battleships SETS Players = { Player 1
this is the code i came up with edit this according to the example code given below.
MACHINE
Battleships
SETS
Players Player Player
Status Ongoing PlayerWin, PlayerWin, Deploying
CONSTANTS
Grid
FleetSize
PROPERTIES
Grid & A x grid
FleetSize Each player has ships
VARIABLES
playerShips, playerShips Positions of Player s and Player s ships
playerShots, playerShots Shots taken by Player and Player
currentPlayer Whose turn it is
gameState Current state of the game
INVARIANT
playerShips : Grid &
playerShips : Grid &
cardplayerShips FleetSize &
cardplayerShips FleetSize &
playerShips playerShips & No overlapping ships
playerShots : Grid &
playerShots : Grid &
currentPlayer : Players &
gameState : Status
INITIALISATION
playerShips :
playerShips :
playerShots :
playerShots :
currentPlayer : Player
gameState : Deploying
OPERATIONS
report deployFleetplayer positions
PRE
player : Players &
positions : Grid &
cardpositions FleetSize &
player Player playerShips &
player Player playerShips &
player Player positions playerShips &
player Player positions playerShips
THEN
IF player Player THEN
playerShips : positions
ELSE
playerShips : positions
END;
IF playerShips & playerShips THEN
gameState : Ongoing
END;
report : "Success"
ELSE
report : "Invalid positions"
END;
report playerShootstarget
PRE
target : Grid &
gameState Ongoing
THEN
IF currentPlayer Player THEN
IF target : playerShips THEN
playerShips : playerShips target;
report : "Hit";
IF cardplayerShips THEN
gameState : PlayerWin
ELSE
currentPlayer : Player
END
ELSE
report : "Miss";
currentPlayer : Player
END;
playerShots : playerShots target
ELSE
IF target : playerShips THEN
playerShips : playerShips target;
report : "Hit";
IF cardplayerShips THEN
gameState : PlayerWin
ELSE
currentPlayer : Player
END
ELSE
report : "Miss";
currentPlayer : Player
END;
playerShots : playerShots target
END
ELSE
report : "Invalid move"
END;
shipsquares shipLocationsplayer
PRE
player : Players
THEN
IF player Player THEN
shipsquares : playerShips
ELSE
shipsquares : playerShips
END
END;
shipCounts shipsLeft
BEGIN
shipCounts :Player cardplayerShips Player cardplayerShips
END;
shotCount shotsTakenplayer
PRE
player : Players
THEN
IF player Player THEN
shotCount : cardplayerShots
ELSE
shotCount : cardplayerShots
END
END;
report gameStatus
BEGIN
report : gameState
END;
END
example code for GRID and the code writing
MACHINE
NoughtsCrosses
SETS
Players Noughts Crosses;
Results Success Failure
CONSTANTS
Grid, winningSubsets
PROPERTIES
Grid &
winningSubsets : POWGrid &
winningSubsets
id
VARIABLES
noughts, crosses
INVARIANT
noughts : Grid & crosses : Grid & noughts crosses
INITIALISATION
noughts : crosses :
OPERATIONS
res placeNoughtpp
PRE
pp : Grid
THEN
IF
pp : noughts crosses
THEN
res : Failure
ELSE
noughts : noughts pp
res : Success
END
END;
res placeCrosspp
PRE
pp : Grid
THEN
IF
pp : noughts crosses
THEN
res : Failure
ELSE
crosses : crosses pp
res : Success
END
END
END
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
