Task Background Story: The Pacific Ocean is a powder keg ripe for explosion with
ID: 3625750 • Letter: T
Question
TaskBackground Story: The Pacific Ocean is a powder keg ripe for explosion with the Imperial Red Fleet (IRF) making trouble by terrorizing the shipping lanes between us and our interests in the Far East. The Pacific Blue Fleet (PBF) needs a capable commander to lead them in this time of peril. You have been selected as the new commander of the United States Pacific Blue Fleet. Reconnaissance units have located the enemy fleet. You job is to engage the enemy fleet and annihilate them completely. The IRF fleet is well equipped and possesses ships that rival your own. They would like nothing more than to send your fleet to the bottom of the ocean. This battle would be decided by the cunning and tactics of the commander.
Mode: The game should have three player modes: player vs. player or player vs. computer or computer vs. computer.
Goal: Strategically place your ships throughout your part of the ocean to keep them away from the enemy. Try and sink the enemy by hitting their ships before they hit you!
Skill Level: When the game is in player vs. computer mode, there should be 3 skills levels. The skill levels are as follows: Beginner: The computer randomly guesses selects coordinates. No intelligence involved. Intermediate: The computer will make an educated guess passed on previous hit. For instance, if computer achieved hit, it should check next available coordinate along x coordinate. Expert: The computer will make an educated guess passed on previous hit. For instance, if computer achieved hit, it should check next available coordinate along x axis and/or y axis.
Ocean Grids: The Pacific Ocean is broken up into two ocean grids: one grid for each fleet. An ocean grid consists of a 10 x 10 grid. Each position on the grid will be represented by a two positive integers that constitutes a XY coordinate.
Ships: There are a total of 5 ships for both the PBF and the IRF respectively. Each fleet has the same complement of ships. The description of the ships is listed below including the number of hits needed to sink a ship: Aircraft Carrier 5 hits Battleship – 4 hits Cruiser – 3 Hits Destroyer 3 hits Submarine – 2 Hits
Deployment: Each ship will be strategically placed in their part of the ocean. Ships must be placed horizontally or vertically. It can not be diagonal. Basically, for each ship placement, either the x or y coordinate must be the same for every coordinate of that particular ship. For example, a cruiser could be: (8,1) (8,2) (8,3) or a battleship could be: (5,3) (6,3) (7,3) (8,3)
How to Begin: When the game begins, the program should prompt the PBF player to enter the coordinates of every ship in the PBF fleet one at a time. Once all of your ships have been entered, the IRF player will enter the coordinates for all of the ships of their fleet.
Game Rules: The PBF player will fire first by entering a XY coordinate. If the entered coordinate scores a hit (i.e. the entered coordinate corresponds to a position on the ocean grid occupied by an enemy ship) the program should indicate that an enemy ship was hit. The PDF player would be allowed to enter in another coordinate. Once the PBF player one misses (selecting a coordinate that is not occupied by an enemy ship), player 2 would be asked to enter a coordinate. Once a player sinks a ship, the program should indicate that ship was sunk and specify which ship was sunk. Note: The program should not indicate the type of ship when it is first hit. It should only be specified once the ship is sunk. The first player that sinks all of the enemy ships will win the game.
Error Checking: The game should have some basic error checking functionality built-in. The following functionality should be included: The game should check to make sure that multiple ships are not placed on the same coordinates. When deploying a ship, the game should insure that the players place acceptable coordinates for each ship. For example, if a ship has a 1st coordinate of (2,1), it should not accept (3,4) as a second coordinate because they are not continuous. The game should make sure that when a player fires on a ship, they don’t select a coordinate outside of the grid. The game should make sure no ship is placed on a coordinate outside of the grid. The game should not allow a player to fire at the same coordinate more than once during a game.
Bonus Points: For those individuals interested in an additional challenge, here are some ideas for enhancements: The game requires that the program prints a response indicating when a ship is hit. Modify the program so that it randomly responds with different messages when a ship is hit. For example: “Great shot” “Hit her again!” “Go for the kill!” If you have other ideas for bonus points, they may be submitted for consideration
Detailed explanation: NO
Specific requirements:
This is an easy game program using C. I am seeking help because I have to take three exams in this week. Please write the code that can be worked on Microsoft Visual C++ 2008 Express Edition or 2010.
Explanation / Answer
1. I would suggest using enum: enum gameElements { sea, jetski, patrolboat, submarine, battleship, alreadyhit }; would accomplish the same thing in a more standard way. I am guessing you're letting gameElements also function as the lengths of the elements themselves, but sea and alreadyhit have length 1 not 0 and 5, respectively. So instead, write a separate array to encode lengths: int elementLengths[] = { 1, 1, 3, 4, 5, 1 }; and let gameElement index into this array. (This is not clean nor safe, but it'll get you going. A better bet is to define a structure that describes everything about each element, such as its id, its length, and its name as a string, but that's for later.) 2. Start structuring your program into separate functions. Do this habitually, not as an afterthought. Writing a whole game in a single function will not fly. 3. Where's your definition for gameElements? Assuming there is one and it has been initialized to zero, your while loop is an infinite one because its exit condition always evaluates to true. 4. I think I understand what you mean here: "* need help here, how do i do the check than place the ship?*/" As I said with #2, you need to break your program into functions. Writing it linearly makes things seem harder than they should. Create a new enumeration for the directions instead of using integers 0 and 1: enum directions { horizontal, vertical }; (You can still use a random number 0 or 1, because that's the actual integer values of these two enumerated values.) directions dir = (directions)(rand() % 2); // type-cast Write a function bool CanPlaceShip( int x, int y, directions dir, gameElements element ) that loops horizontally or vertically to see that 1. the element doesn't get placed off the grid, and 2. all cells of the element placed at that point are currently equal to sea. This is just a predicate: it doesn't change the state of the game. it just returns true or false. And then write a function void PlaceShip( int x, int y, directions dir, gameElements element ). This function changes the state of the grid. (If you wanted to, you could place these functions together, but for now keep them separate.) Note that these functions need access to grid. You can do one of three things: 1. Define grid locally to main() and pass them into the functions as new first parameter. 2. Define grid globally before main(). 3. Use C++ and define a class that encapsulates grid and all the functions. 3. is the RIGHT way to do it. But for now, I'd recommend doing 2. Also instead of hard-coding 10 all over the place, define next to main: const int gridWidth = 10; const int gridHeight = 10; Finally, get in the habit of placing assertions. The more religious you are using assertions the better programmer you will become.. e.g at the top of CanPlaceShip, make sure x and y are in range with assert( x >= 0 && xRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.