Question: this is the code i came up with for the coursework battleship its attached here. edit this according to the example code given below the

this is the code i came up with for the coursework battleship its attached here. edit this according to the example code given below the example code is attached to follow the coding practice
the error i get is also attached here as a snippet
MACHINE battlecw
SETS
Players ={Player1, Player2};
Results ={Success, Failure}
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 : {Deploying, Ongoing, Player1Win, Player2Win}
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
ELSE
report := Failure
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
ELSE
report := "Invalid move"
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
----example code for the code writing---------------------------------------
MACHINE
NoughtsCrosses
SETS
Players ={Noughts, Crosses};
Results ={Success, Failure}
CONSTANTS
Grid, winningSubsets
PROPERTIES
Grid =(1..3)*(1..3) &
winningSubsets : POW(Grid) &
winningSubsets ={{1}*(1..3),{2}*(1..3),{3}*(1..3),
(1..3)*{1},(1..3)*{2},(1..3)*{3},
id(1..3),{1|->3,2|->2,3|->1}}
VARIABLES
noughts, crosses
INVARIANT
noughts : Grid & crosses : Grid & noughts /\ crosses ={}
INITIALISATION
noughts :={}|| crosses :={}
OPERATIONS
res -- placeNought(pp)=
PRE
pp : Grid
THEN
IF
pp : noughts \/ crosses
THEN
res := Failure
ELSE
noughts := noughts \/{ pp }||
res := Success
END
END;
res -- placeCross(pp)=
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 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!