Evil Battleship Assignment written and developed by Varick Erickson, CSU East Bay, November 2019 Introduction Battleship is a two player game dating back to WWI. Originally this was a pencil and paper...

1 answer below »
in c++ programming language


Evil Battleship Assignment written and developed by Varick Erickson, CSU East Bay, November 2019 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 game instructions[1]. Each player has two 10×10 grid as shown above. The target grid is used to record previous shots 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 attempt was a miss. Evil Battleship Page 1 Sneaky Ship Strategy In the official rules, once you position your ships, the ship positions are fixed and cannot be moved. However, for this implementation, we are going to be sneaky and move ships around as the oppo- nent makes guesses. The idea behind evil battleship is to continually move the ships out of harms way. Let’s consider a simple 5×5 grid. Suppose the opponent gave the coordinate (A, 1) as the opening move: 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. In this case, there is plenty of open spots available. Here is one possible configuration: 1 2 3 4 5 E D C B A Evil Battleship Page 2 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 As you may notice, the more misses there are, the harder it is to find a valid placement for the ships. 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 column that contains at least 5 empty spots). In this case, we have to admit to a hit: Carrier cannot be placed if E5 a miss 1 2 3 4 5 E D C B A Forced to admit E5 a hit 1 2 3 4 5 E D C B A Evil Battleship Page 3 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 There are multiple ways we could place the ships that account for this hit. 1 2 3 4 5 E D C B A 1 2 3 4 5 E D C B A In the example above, we show that we don’t necessarily have to commit a particular ship to a hit. 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, as long as we can find a place for the ships to be placed, we don’t have to actually assign a ship to be hit. Evil Battleship Page 4 The only time we actually have to admit a ship has been sunk is if all the grid positions of a ship position has been marked as hit. Suppose we have the following: 1 2 3 4 5 E D C B A As you can see, the only place the Patrol boat fits is the at the coordinates (A, 3)-(B, 3). In this case, we admit that the Patrol ship has been sunk. From this point onward, we permanently place the Patrol ship at this position and these spots can no longer be used as potential locations for placing ships. 1 2 3 4 5 E D C B A Evil Battleship Page 5 Greedy Ship Placement Algorithm A greedy algorithm tries to make an optimal choice at every opportunity. For ship placement, we will always try to place the ships from largest to smallest. 1. Carrier 2. Battleship 3. Submarine 4. Destroyer 5. Patrol The following are the basics of the greedy ship placement algorithm: 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" Evil Battleship Page 6 Board Class The Board class implements most of the functionality of the battleship game. It is in charge of maintaining the target and ocean grids. It is also in charge of the sneaky ship strategy. Listing 1: Board Class 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 boardbool ShipSunk(Ship* v); // Returns True if ship was sunkbool ValidPlacement(Ship* s); // Returns True if placement is validint 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 shipsbool 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 }; Evil Battleship Page 7 Ship Class It is your responsibility to design and implement a Ship class. There are many different ways to represent the ship. One convenient way to represent the ship and placement is to use: • Coordinate (row, col) • Direction (East, South) • Ship Length For example, if we wanted to place the patrol ship from (A,3)-(B,3), we could represent this with the data {(0,2), South, 2}. This means start at the coordinate (0, 2) and fill 2 spaces south. 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, then you must declare the ship sunk and permanently place the ship. Evil Battleship Page 8 Sample Output 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 ______________________ Evil Battleship Page 9 ============= 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 Evil Battleship Page 10 . 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!!!! Evil Battleship Page 11 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
Answered Same DayNov 28, 2021

Answer To: Evil Battleship Assignment written and developed by Varick Erickson, CSU East Bay, November 2019...

Sonu answered on Nov 30 2021
159 Votes
BattleShip/.vs/BattleShip/v16/.suo
BattleShip/.vs/BattleShip/v16/Browse.VC.db
BattleShip/.vs/BattleShip/v16/ipch/AutoPCH/215f38d103671f9f/MAINPROGRAM.ipch
BattleShip/.vs/BattleShip/v16/ipch/AutoPCH/d5e0f863c987050f/MAINPROGRAM.ipch
BattleShip/BattleShip/BattleShip.vcxproj



Debug
Win32


Release
Win32


Debug
x64


Release
x64



16.0
{0EA80BC4-46B2-4381-B0AB-3769E8B5293B}
BattleShip
10.0



Application
true
v142
MultiByte


Application
false
v142
true
MultiByte


Application
true
v142
MultiByte


Application
false
v142
true
MultiByte






















Level3
Disabled
true
true


Console




Level3
Disabled
true
true


Console




Level3
MaxSpeed
true
true
true
true


Console
true
true




Level3
MaxSpeed
true
true
true
true


Console
true
true








BattleShip/BattleShip/BattleShip.vcxproj.filters



{4FC737F1-C7A5-4376-A066-2A32D752A2FF}
cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx


{93995380-89BD-4b04-88EB-625FBE52EBFB}
h;hh;hpp;hxx;hm;inl;inc;ipp;xsd


{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms




Source Files


BattleShip/BattleShip/BattleShip.vcxproj.user


BattleShip/BattleShip/Debug/BattleShip.Build.CppClean.log
d:\battleship\battleship\debug\vc142.pdb
d:\battleship\battleship\debug\vc142.idb
d:\battleship\battleship\debug\mainprogram.obj
d:\battleship\debug\battleship.pdb
d:\battleship\battleship\debug\battleship.tlog\cl.command.1.tlog
d:\battleship\battleship\debug\battleship.tlog\cl.read.1.tlog
d:\battleship\battleship\debug\battleship.tlog\cl.write.1.tlog
d:\battleship\battleship\debug\battleship.tlog\link-cvtres.read.1.tlog
d:\battleship\battleship\debug\battleship.tlog\link-cvtres.write.1.tlog
d:\battleship\battleship\debug\battleship.tlog\link-rc.read.1.tlog
d:\battleship\battleship\debug\battleship.tlog\link-rc.write.1.tlog
d:\battleship\battleship\debug\battleship.tlog\link.command.1.tlog
d:\battleship\battleship\debug\battleship.tlog\link.read.1.tlog
d:\battleship\battleship\debug\battleship.tlog\link.write.1.tlog
BattleShip/BattleShip/Debug/BattleShip.log
MainProgram.c
D:\BattleShip\BattleShip\MainProgram.c(108,8): warning C4013: 'system' undefined; assuming extern returning int
D:\BattleShip\BattleShip\MainProgram.c(439,6): warning C4013: 'gets' undefined; assuming extern returning int
D:\BattleShip\BattleShip\MainProgram.c(440,7): warning C4013: 'srand' undefined; assuming extern returning int
D:\BattleShip\BattleShip\MainProgram.c(446,11): warning C4013: 'rand' undefined; assuming extern returning int
BattleShip.vcxproj -> D:\BattleShip\Debug\BattleShip.exe
BattleShip/BattleShip/Debug/BattleShip.tlog/BattleShip.lastbuildstate
#TargetFrameworkVersion=v4.0:PlatformToolSet=v142:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=10.0
Debug|Win32|D:\BattleShip\|
BattleShip/BattleShip/Debug/BattleShip.tlog/CL.command.1.tlog
^D:\BATTLESHIP\BATTLESHIP\MAINPROGRAM.C
/c /ZI /JMC /nologo /W3 /WX- /diagnostics:column /sdl /Od /Oy- /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /permissive- /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"DEBUG\\" /Fd"DEBUG\VC142.PDB" /Gd /TC /analyze- /FC D:\BATTLESHIP\BATTLESHIP\MAINPROGRAM.C
BattleShip/BattleShip/Debug/BattleShip.tlog/CL.read.1.tlog
^D:\BATTLESHIP\BATTLESHIP\MAINPROGRAM.C
C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\VC\TOOLS\MSVC\14.22.27905\BIN\HOSTX86\X86\1033\CLUI.DLL
C:\WINDOWS\GLOBALIZATION\SORTING\SORTDEFAULT.NLS
C:\WINDOWS\SYSTEM32\TZRES.DLL
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\STDIO.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\CORECRT.H
C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\VC\TOOLS\MSVC\14.22.27905\INCLUDE\VCRUNTIME.H
C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\VC\TOOLS\MSVC\14.22.27905\INCLUDE\SAL.H
C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\VC\TOOLS\MSVC\14.22.27905\INCLUDE\CONCURRENCYSAL.H
C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\VC\TOOLS\MSVC\14.22.27905\INCLUDE\VADEFS.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\CORECRT_WSTDIO.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\CORECRT_STDIO_CONFIG.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\STRING.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\CORECRT_MEMORY.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\CORECRT_MEMCPY_S.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\ERRNO.H
C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\VC\TOOLS\MSVC\14.22.27905\INCLUDE\VCRUNTIME_STRING.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\CORECRT_WSTRING.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\CONIO.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\CORECRT_WCONIO.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\TIME.H
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\CORECRT_WTIME.H
BattleShip/BattleShip/Debug/BattleShip.tlog/CL.write.1.tlog
^D:\BATTLESHIP\BATTLESHIP\MAINPROGRAM.C
D:\BATTLESHIP\BATTLESHIP\DEBUG\VC142.PDB
D:\BATTLESHIP\BATTLESHIP\DEBUG\VC142.IDB
D:\BATTLESHIP\BATTLESHIP\DEBUG\MAINPROGRAM.OBJ
BattleShip/BattleShip/Debug/BattleShip.tlog/link.command.1.tlog
^D:\BATTLESHIP\BATTLESHIP\DEBUG\MAINPROGRAM.OBJ
/OUT:"D:\BATTLESHIP\DEBUG\BATTLESHIP.EXE" /INCREMENTAL /NOLOGO KERNEL32.LIB USER32.LIB GDI32.LIB WINSPOOL.LIB COMDLG32.LIB ADVAPI32.LIB SHELL32.LIB OLE32.LIB OLEAUT32.LIB UUID.LIB ODBC32.LIB ODBCCP32.LIB /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG:FASTLINK /PDB:"D:\BATTLESHIP\DEBUG\BATTLESHIP.PDB" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"D:\BATTLESHIP\DEBUG\BATTLESHIP.LIB" /MACHINE:X86 DEBUG\MAINPROGRAM.OBJ
BattleShip/BattleShip/Debug/BattleShip.tlog/link.read.1.tlog
^D:\BATTLESHIP\BATTLESHIP\DEBUG\MAINPROGRAM.OBJ
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\KERNEL32.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\USER32.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\GDI32.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\WINSPOOL.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\COMDLG32.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\ADVAPI32.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\SHELL32.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\OLE32.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\OLEAUT32.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\UUID.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\ODBC32.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UM\X86\ODBCCP32.LIB
D:\BATTLESHIP\BATTLESHIP\DEBUG\MAINPROGRAM.OBJ
C:\WINDOWS\SYSTEM32\TZRES.DLL
C:\WINDOWS\GLOBALIZATION\SORTING\SORTDEFAULT.NLS
C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\VC\TOOLS\MSVC\14.22.27905\LIB\X86\MSVCRTD.LIB
C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\VC\TOOLS\MSVC\14.22.27905\LIB\X86\OLDNAMES.LIB
C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\VC\TOOLS\MSVC\14.22.27905\LIB\X86\VCRUNTIMED.LIB
C:\PROGRAM FILES (X86)\WINDOWS KITS\10\LIB\10.0.18362.0\UCRT\X86\UCRTD.LIB
BattleShip/BattleShip/Debug/BattleShip.tlog/link.write.1.tlog
^D:\BATTLESHIP\BATTLESHIP\DEBUG\MAINPROGRAM.OBJ
D:\BATTLESHIP\DEBUG\BATTLESHIP.EXE
D:\BATTLESHIP\DEBUG\BATTLESHIP.ILK
D:\BATTLESHIP\DEBUG\BATTLESHIP.PDB
BattleShip/BattleShip/Debug/BattleShip.vcxproj.FileListAbsolute.txt
BattleShip/BattleShip/Debug/MainProgram.obj
BattleShip/BattleShip/Debug/vc142.idb
BattleShip/BattleShip/Debug/vc142.pdb
BattleShip/BattleShip/MainProgram.c
#include
#include
#include
#include
int main()
{
    int difference = 9, XCordinate, YCordinate;
    char gridUpper[10][10], inputOrientation[2], upperOrientation[5][2];
    int i, j, k = 0;
    int checkIndex = 0;
    int TEN = 10;
    int ZERO = 0;
    int    SIX = 6;
    int    FIVE = 5;
    int    THIRTY = 30;
    int upperOrientationB[4][2], upperOrientD[3][2], upperOrienCarrier[2][2];
    int win = 0, a = 0, b = 0, c = 0, d = 0, probab;
    char griduv[10][10], gridCarrier[10][10], gridCarrierVertical[10][10];
    char tempString[10];
    for (int row = ZERO; row < TEN; row++)
    {
        for...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here