EvilBattleship AssignmentwrittenanddevelopedbyVarickErickson,CSUEastBay,November2019 Introduction…

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 _gures from the original gameinstructions[1]. Eachplayerhastwo10_0gridasshownabove. 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.
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 _gures from the original gameinstructions[1]. Eachplayerhastwo10_0gridasshownabove. 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 Intheo_cialrules,onceyoupositionyourships,theshippositionsare_xedandcannotbemoved. However,forthisimplementation,wearegoingtobesneakyandmoveshipsaroundastheopponentmakesguesses. Theideabehindevilbattleshipistocontinuallymovetheshipsoutofharms way. Lets consider a simple 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 _gure out if there is a con_guration such that the ships can still be placed on the board. Inthiscase,thereisplentyofopenspotsavailable. Hereisonepossiblecon_guration: 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 cant use this coordinate. However, there is still enough space to _nd a possible con_guration: 1 2 3 4 5 E D C B A Asyoumaynotice,themoremissesthereare,theharderitisto_ndavalidplacementfortheships. 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 _exibility. Suppose we have the following con_guration: 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,weshowthatwedontnecessarilyhavetocommitaparticularshiptoahit. 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, aslongaswecan_ndaplacefortheshipstobeplaced,wedonthavetoactuallyassignashipto 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,theonlyplacethePatrolboat_tsistheatthecoordinates(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 cant 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 di_erent 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)and_ll2spacessouth. 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/missbasedonthecomputersguess. 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 ============= LETS 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. Ifitdoesntcompile,itwontreceiveanyextracredit. EvilBattleship.cppgamedriver Boardclassandheaderimplementation Shipclassimplementation References [1] MiltonBradley,Hasbro Battleship: https://www.hasbro.com/common/instruct/battleship.pdf

View less

Custom Paper Help
Calculate your paper price
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.