EvilBattleship AssignmentwrittenanddevelopedbyVarickErickson,CSUEastBay,November2019 Introduction Battleship is a two player game dating back to WWI. Originally this was a pencil and paper game, but...

EvilBattleship

AssignmentwrittenanddevelopedbyVarickErickson,CSUEastBay,November2019


Introduction


Battleship is a two player game dating back to WWI. Originally this was a pencil and paper game, but Milton Bradley released as a board game in 1967. The following are figures from the original gameinstructions[1].


Eachplayerhastwo10×10gridasshownabove. Thetargetgridisusedtorecordpreviousshots made against the other. The ocean grid is used to secretly position ships and record how many hits each ship has received. Ships are placed on the grid at right angles. Ships cannot be placed diagonally. At each turn, a player chooses a location to hit. If the guessed coordinate contains a ship, then opponent must announce that a ship has been hit. Otherwise, the opponent states the attemptwasamiss.


EvilBattleship Page1


SneakyShipStrategy


Intheofficialrules,onceyoupositionyourships,theshippositionsarefixedandcannotbemoved. However,forthisimplementation,wearegoingtobesneakyandmoveshipsaroundastheopponentmakesguesses. Theideabehindevilbattleshipistocontinuallymovetheshipsoutofharms way. Let’s consider a simple 5×5 grid. Suppose the opponent gave the coordinate (A, 1) as the openingmove:


1 2 3 4 5


E


D


C


B


A


Our goal is to figure out if there is a configuration such that the ships can still be placed on the board. Inthiscase,thereisplentyofopenspotsavailable. Hereisonepossibleconfiguration:


1 2 3 4 5


E


D


C


B


A


EvilBattleship Page2


Suppose the opponent chooses (B,2) next. Note that since (A,1) has been used, our potential ship placement can’t use this coordinate. However, there is still enough space to find a possible configuration:


1 2 3 4 5


E


D


C


B


A


Asyoumaynotice,themoremissesthereare,theharderitistofindavalidplacementfortheships. Suppose the opponent has guessed (A, 1), (B, 2), (C, 3), and (D, 4). Now suppose the opponent now guesses (E, 5). Notice there is no place where the carrier can be placed (there is no row or columnthatcontainsatleast5emptyspots). Inthiscase,wehavetoadmittoahit:


Carriercannotbe placedifE5amiss 1 2 3 4 5


E


D


C


B


A


Forcedtoadmit E5ahit 1 2 3 4 5


E


D


C


B


A


EvilBattleship Page3


However, admitting to a hit still allows for quite a bit of flexibility. Suppose we have the following configuration:


1 2 3 4 5


E


D


C


B


A


Therearemultiplewayswecouldplacetheshipsthataccountforthishit.


1 2 3 4 5


E


D


C


B


A


1 2 3 4 5


E


D


C


B


A


Intheexampleabove,weshowthatwedon’tnecessarilyhavetocommitaparticularshiptoahit. The left board shows the Carrier receiving the hit. The right board shows the Battleship receiving the hit. In fact, we can treat any "hit" location as a free space where a ship can be placed. Again, aslongaswecanfindaplacefortheshipstobeplaced,wedon’thavetoactuallyassignashipto behit.


EvilBattleship Page4


The only time we actually have to admit a ship has been sunk is if all the grid positions of a ship positionhasbeenmarkedashit. Supposewehavethefollowing:


1 2 3 4 5


E


D


C


B


A


Asyoucansee,theonlyplacethePatrolboatfitsistheatthecoordinates(A,3)-(B,3). Inthiscase, we admit that the Patrol ship has been sunk. From this point onward, we permanently place the Patrolshipatthispositionandthesespotscannolongerbeusedaspotentiallocationsforplacing ships.


1 2 3 4 5


E


D


C


B


A


EvilBattleship Page5


GreedyShipPlacementAlgorithm


A greedy algorithm tries to make an optimal choice at every opportunity. For ship placement, we willalwaystrytoplacetheshipsfromlargesttosmallest.


1. Carrier


2. Battleship


3. Submarine


4. Destroyer


5. Patrol


Thefollowingarethebasicsofthegreedyshipplacementalgorithm: Temporarily mark the current guess location as a "Miss" For each ship in descending order (place biggest ship first) { For each row and column { Try to place the ship at the current row and column (Try both orientations... see next sections for details) } If you can’t find a (row,column) for the ship, mark the last opponent guess coordinate as a "Hit" } If you were able to find a spot for all ships, then you officially declare the guess location a "Miss"


EvilBattleship Page6


BoardClass


The Board class implements most of the functionality of the battleship game. It is in charge of maintainingthetargetandoceangrids. Itisalsoinchargeofthesneakyshipstrategy.


Listing1: BoardClass const int SIZE = 10; // Grid size for the game class Board { public: Board(); void FireAttempt(int row, int col); // Attempts to fire on (row, col) void AddShip(Ship* ship); // Add Ship to the game // (add to ships vector) void PrintHitMiss(); // Prints the hits and misses void PrintShips(); // Prints the ship locations void PlaceShip(Ship* v); // Places ship v on board bool ShipSunk(Ship* v); // Returns True if ship was sunk bool ValidPlacement(Ship* s); // Returns True if placement is valid int ShipsLeft(); // Returns the ships left void SetEvil(bool e); // Sets the board to be evil or not void SetDebug(bool d); // Prints out debugging information // for ship positions private: char board[SIZE][SIZE]; // Stores the ship placements char missHits[SIZE][SIZE]; // Stores the miss and hits vector* ships; // Stores all ships bool debug = true; // Is the debug mode on? bool evil = true; // Is the board using evil strategy int shipsLeft; // How many ships left void PlaceAllShips(int row, int col); // Try to place all ships given the // guess (row, col) void PlaceSunkShips(); // Places sunk ships on board void ResetPlayBoard(); // Resets the board to be missHits void PrintBoard(char grid[SIZE][SIZE]); // Prints a board };


EvilBattleship Page7


ShipClass


It is your responsibility to design and implement a Ship class. There are many different ways to representtheship. Oneconvenientwaytorepresenttheshipandplacementistouse:


• Coordinate(row,col)


• Direction(East,South)


• ShipLength


Forexample,ifwewantedtoplacethepatrolshipfrom(A,3)-(B,3),wecouldrepresentthiswiththe data{(0,2),South,2}. Thismeansstartatthecoordinate(0,2)andfill2spacessouth.


1 2 3 4 5


E


D


C


B


A


Ships cannot overlap any coordinate marked as a "miss". As previously mentioned, "hit" locations are valid placement locations. However, if every position of the ship is on top of a "hit" location, thenyoumustdeclaretheshipsunkandpermanentlyplacetheship.


EvilBattleship Page8


Gameplay


Thesampleoutputprovidesanexampleofthegameplay. Thegameshoulddothefollowing:


Initializationbeforegameplay • Askfordebugoption


• AskforEviloption


• Askuserstoplaceallships


Gameplayloop While(noonehaswon)


• Printouttheplayerboardandtheplayerhit/missboard


• Asktheplayertoenteracoordinate


• The computer announces hit/miss. If the mode is evil, this will only announce a hit only if absolutelynecessary.


• The computer will randomly select a coordinate. It should not pick a coordinate that the computerhasalreadyguessed.


• Announceahit/missbasedonthecomputer’sguess.


EvilBattleship Page9


SampleOutput


Debug (y/n): n Evil (y/n): y Place Carrier Coordinate (row col): a 1 Direction (S/E): e ______________________ 1 2 3 4 5 6 7 8 9 10 a C C C C C b c d e f g h i j ______________________ Place Battleship Coordinate (row col): z 1 z is not a valid row Coordinate (row col): b 11 11 is not a valid col Coordinate (row col): b 1 Direction (S/E): s ______________________ 1 2 3 4 5 6 7 8 9 10 a C C C C C b B c B d B e B f g h i j ______________________ . . . etc. . . . ______________________ 1 2 3 4 5 6 7 8 9 10 a C C C C C b B c B d B e B f S S S g D D D h P P i j ______________________


EvilBattleship Page10


============= LET’S PLAY!!! ============= ______________________ 1 2 3 4 5 6 7 8 9 10 a C C C C C b B c B d B e B f S S S g D D D h P P i j ______________________ ______________________ 1 2 3 4 5 6 7 8 9 10 a b c d e f g h i j ______________________ Enter Coord: a 1 MISS!!! Computer Guess: b 1 HIT!!! ______________________ 1 2 3 4 5 6 7 8 9 10 a C C C C C b H c B d B e B f S S S g D D D h P P i j ______________________ ______________________ 1 2 3 4 5 6 7 8 9 10 a M b M c d e f g h i j _______________________ Enter Coord: b 2 . . . etc


EvilBattleship Page11


. etc . . ______________________ 1 2 3 4 5 6 7 8 9 10 a M M M M M M M M M M b M H H H H M M c M M M d M M e M M M M M M M M M M f M M g M M M M h M M M M M i M M M M j M M M M ______________________ Enter Coord: b 3 YOU SUNK MY Carrier!!! Computer Guess: b 1 MISS!!! ______________________ 1 2 3 4 5 6 7 8 9 10 a C C H C C b H c B d B e B f S H S g H D D h P P i j ______________________ ______________________ 1 2 3 4 5 6 7 8 9 10 a M M M M M M M M M M b M C C C C C M M c M M M d M M M e M M M M M M M M M M f M M g M M M M h M M M i M M M j M M M ______________________ Enter Coord: . . . Computer Guess: h 2 YOU SUNK MY Patrol Boat! ______________________ 1 2 3 4 5 6 7 8 9 10 a H H H H H b H c H d H e H f H H H g H H H h H H i j ______________________ YOU LOST!!!!


EvilBattleship Page12


DEBUG ON SAMPLE OUTPUT ______________________ 1 2 3 4 5 6 7 8 9 10 a b c d e f g h i j ______________________ Coord: a 1 Placing Carrier... ______________________ 1 2 3 4 5 6 7 8 9 10 a M C C C C C b c d e f g h i j ______________________ Placing Battleship... ______________________ 1 2 3 4 5 6 7 8 9 10 a M C C C C C B B B B b c d e f g h i j ______________________ Placing Destroyer... ______________________ 1 2 3 4 5 6 7 8 9 10 a M C C C C C B B B B b D D D c d e f g h i j ______________________ Placing Submarine... ______________________ 1 2 3 4 5 6 7 8 9 10 a M C C C C C B B B B b D D D S S S c d e


EvilBattleship Page13


f g h i j ______________________ Placing Patrol Boat... ______________________ 1 2 3 4 5 6 7 8 9 10 a M C C C C C B B B B b D D D S S S P P c d e f g h i j _____________________ All ships placed MISS!!!


Deliverables


Thegamemustcompiletogetcredit. Ifitdoesn’tcompile,itwon’treceiveanyextracredit.


• EvilBattleship.cppgamedriver


• Boardclassandheaderimplementation


• Shipclassimplementation


References


[1] MiltonBradley,Hasbro Battleship: https://www.hasbro.com/common/instruct/battleship.pdf


May 18, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here