Question: the code i came up with for the battleship exercise is attached here. MACHINE BATTLE SETS Players = { Player 1 , Player 2 }

the code i came up with for the battleship exercise is attached here.
MACHINE BATTLE
SETS
Players ={Player1, Player2};
Results ={Success, Failure};
GameStates ={Deploying, Ongoing, Player1Win, Player2Win};
Reports ={Hit, Miss, InvalidMove}/* Declare gameState values explicitly */
CONSTANTS
Grid, FleetSize
PROPERTIES
Grid =(1..10)*(1..10) & /* A 10x10 grid */
FleetSize =3/* Each player has 3 ships */
VARIABLES
player1Ships, player2Ships, /* Positions of Player 1's and Player 2's ships */
player1Shots, player2Shots, /* Shots taken by Player 1 and Player 2*/
currentPlayer, /* Whose turn it is */
gameState /* Current state of the game */
INVARIANT
player1Ships : Grid &
player2Ships : Grid &
card(player1Ships)= FleetSize &
card(player2Ships)= FleetSize &
player1Ships /\ player2Ships ={} & /* No overlapping ships */
player1Shots : Grid &
player2Shots : Grid &
currentPlayer : Players &
gameState : GameStates /* Use the explicitly declared GameStates set */
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: 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
can u check whether the code is correct when animated in PRO B?
The Battleships game is played as follows:
1. The game starts by each player placing their 3 ships on their own grid, each player's ships in different grid squares.
2. Player 1 has the first turn.
3. Player 1 shoots at Player 2's ships, by selecting a target square on Player 2's grid. If there is a ship in the target square it is a hit and the ship is deleted from Player 2's fleet, otherwise it is a miss and there is no change to Player 2's fleet.
4. It is then Player 2's turn.
5. Player 2 shoots at Player 1's ships, by selecting a target square on Player 1's grid. Registering a hit or miss, as above.
6. The players continue to take turns until all of one player's ships have been sunk, at this point the other player is the winner.
could u check whether the following works successfully or not?
deployFleet operation, playerShoots operation, shipLocations operation, shipsLeft operation, shotsTaken operation, gameStatus operation
else plz attach the edited code where these will work successfully.
the code i came up with for the battleship

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!