Question: debug this code and check whether the following works successfully or not? deployFleet operation, playerShoots operation, shipLocations operation, shipsLeft operation, shotsTaken operation, gameStatus operation MACHINE

debug this code
and check whether the following works successfully or not?
deployFleet operation, playerShoots operation, shipLocations operation, shipsLeft operation, shotsTaken operation, gameStatus operation
MACHINE Battleship
SETS
Players ={Player1, Player2};
Results ={Success, Failure};
GameStates ={Deploying, Ongoing, Player1Win, Player2Win};
Reports ={Hit, Miss, InvalidMove};
CONSTANTS
Grid, FleetSize
PROPERTIES
Grid =(1.10)*(1.10) & /*10x10 grid */
FleetSize =3/* Each player has 3 ships */
VARIABLES
player1Ships, player2Ships, /* Positions of Player1's and Player2's ships */
player1Shots, player2Shots, /* Shots taken by Player1 and Player2*/
currentPlayer, /* Whose turn it is */
gameState /* Current state of the game */
INVARIANT
player1Ships <: Grid and
player2Ships <: Grid &
card(player1Ships)<= FleetSize &
card(player2Ships)<= FleetSize &
player1Ships /\\ player2Ships ={} & /* No overlapping ships */
player1Shots <: Grid &
player2Shots <: Grid &
currentPlayer : Players &
gameState : GameStates
INITIALISATION
player1Ships :={}||
player2Ships :={}||
player1Shots :={}||
player2Shots :={}||
currentPlayer := Player1||
gameState := Deploying
OPERATIONS
/* Deploy Fleet Operation */
report <-- deployFleet(player, positions)=
PRE
player : Players &
positions <: Grid &
card(positions)= FleetSize &
((player = Player1=> player1Ships ={}) &
(player = Player2=> player2Ships ={})) &
((player = Player1=> positions /\\ player2Ships ={}) &
(player = Player2=> positions /\\ player1Ships ={}))
THEN
IF player = Player1 THEN
player1Ships := positions
ELSE
player2Ships := positions
END ||
IF player1Ships /={} & player2Ships /={} THEN
gameState := Ongoing
END ||
report := Success
END;
/* Player Shoots Operation */
report <-- playerShoots(target)=
PRE
target : Grid &
gameState = Ongoing
THEN
IF currentPlayer = Player1 THEN
IF target : player2Ships THEN
player2Ships := player2Ships -{target}||
report := Hit ||
IF card(player2Ships)=0 THEN
gameState := Player1Win
ELSE
currentPlayer := Player2
END
ELSE
report := Miss ||
currentPlayer := Player2
END ||
player1Shots := player1Shots \/{target}
ELSE
IF target : player1Ships THEN
player1Ships := player1Ships -{target}||
report := Hit ||
IF card (player1Ships)=0 THEN
gameState := Player2Win
ELSE
currentPlayer := Player1
END
ELSE
report := Miss ||
currentPlayer := Player1
END ||
player2Shots := player2Shots \/{target}
END
END;
/* Enquiry Operation: shipLocations */
shipsquares <-- shipLocations(player)=
PRE
player : Players
THEN
IF player = Player1 THEN
shipsquares := player1Ships
ELSE
shipsquares := player2Ships
END
END;
/* Enquiry Operation: shipsLeft */
shipCounts <-- shipsLeft =
BEGIN
shipCounts :={Player1|-> card(player1Ships), Player2|-> card(player2Ships)}
END;
/* Enquiry Operation: shotsTaken */
shotCount <-- shotsTaken(player)=
PRE
player : Players
THEN
IF player = Player1 THEN
shotCount := card(player1Shots)
ELSE
shotCount := card(player2Shots)
END
END;
/* Enquiry Operation: gameStatus */
report <-- gameStatus =
BEGIN
report := gameState
END;
END

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!