C++ Battleship Game in PSPAD
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: C++ Battleship Game in PSPAD

  1. #1
    Join Date
    Mar 2013
    Posts
    4

    C++ Battleship Game in PSPAD

    Alright, so I need to pass this class and I have no idea how to do this program... It's been either hit or miss for me in this class and I likely need to do well on this assignment in order to pass class... we need to create a battleship program with the following parameters:

    You must use a two dimensional array (10 rows and 10 columns)
     You need to use at least 1 function
     You need to use at least one procedure
     You must not allow the user to enter a row or column number outside of the 1-10 range
     If a user attempts to place a shot into a location they have already shot at (whether it was a miss
    or a hit) allow them to re-enter the shot as many times as needed until they get it right.
     Ships cannot overlap (occupy any of the same spaces) for an individual player
     Ships can be arranged horizontally or vertically but not diagonally
     Ships cannot extend outside of the grid, take care to check bounds
     Be sure you track shots (misses and hits an present the results to the players)
     You must be able to detect a winner if all ships have been sunk for a player.
     Place all 5 ships for each player
     Computer player places ships and takes shots.
     Notify the player(s) which ship was sunk when it gets sunk.

    Any information would be greatly appreciated... I'll basically have a week and a half to do this after spring break so wish me luck! Thanks again for any help I receive!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,422

    Re: C++ Battleship Game in PSPAD

    Quote Originally Posted by rycurt11 View Post
    Any information would be greatly appreciated...
    Please be more specific.

    What "information" are you seeking? How to write a function? How to declare an array?
    I have no idea how to do this program
    You do this program by taking each piece, coding it, test it, and go on to the next piece.
    You must not allow the user to enter a row or column number outside of the 1-10 range
    So, can you write the code to do this? If you can't then there is absolutely no way you're going to know how to code the rest of the assignment.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    Re: C++ Battleship Game in PSPAD

    You need to use at least one procedure
    c++ doesn't have 'procedures' - it just has functions!

    The first task is to design the program. What classes are needed, what methods are needed for those classes, what is the logic for placing the ships on the grid, how do you know when a ship is sunk or been partly hit etc etc. None of this is coding - its pure program design done before coding starts. Only once the program design is completed does coding start from the design. If the design is correct then the program will work (baring pure coding issues).

    We can't design or write the progam for you. That would be considered cheating (see http://forums.codeguru.com/showthrea...ork-assignment) as this is an assignment for which you will be marked. However, we will do all we can to assist you with c++ problems you encounter. If you post code that you're written we'll comment if asked and suggest better ways of doing things where possible.

    You don't say what is your current level of knowledge of c++, but if you are struggling then I suggest you invest in a good c++ book. There are many available - try to have a look at some to see which suites your style of learning. You might also like to look at this http://www.learncpp.com/
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Mar 2013
    Posts
    4

    Re: C++ Battleship Game in PSPAD

    I guess what I'm looking for more particularly is how to write a function? I know how to declare an array, but I'm confused as to what functions I need to use for the program?

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,273

    Re: C++ Battleship Game in PSPAD

    Quote Originally Posted by rycurt11 View Post
    I guess what I'm looking for more particularly is how to write a function? I know how to declare an array, but I'm confused as to what functions I need to use for the program?
    http://www.cplusplus.com/doc/tutorial/functions/
    http://msdn.microsoft.com/en-us/magazine/jj553512.aspx
    http://www.tutorialspoint.com/cplusp..._functions.htm
    http://www.dummies.com/how-to/conten...ions-in-c.html
    ...
    https://www.google.com/search?q=c%2B...hrome&ie=UTF-8
    Victor Nijegorodov

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    Re: C++ Battleship Game in PSPAD

    I'm confused as to what functions I need to use for the program?
    Deciding upon what functions are needed is part of the program design stage, not part of the coding stage. As said in post #3, first you need to design the program and only when you are happy that the design solves the problem do you move on to the coding phase. Only once you start to code the progam from the design do you then actually need to know how to write a function. A significant part of learning how to program is learning how to design a program before coding the design in the appropriate language.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,761

    Re: C++ Battleship Game in PSPAD

    Basic interactive programs like this have a typical loop at the core.

    1 Get Input
    2 Process the input (calculation)
    3 Display output

    Since your program has 2 distinct phases (board setup and actual shooting/play phase), you probably want 2 of those loops one after the other.

    You probably need a way to stop the program, this is usually done either in step 1 or is is part of step 2. Either can work, which one if better depends on how you approach things.
    This is one of those "design desicions".

    The same is true for input validation, that can be either an inhherent part of step 1, or it can be a part of step 2. If you do it in 1, you need a loop in your step 1 that gives error messages on invalid input and doesn't proceed to step 2 until the input is valid. If you do it in 2, you may or may not want to display the current state via step 3.
    You can also do input validation in both 1 and 2. 1 then does basic input validation that does not need to know the current state of the game board (is the input numeric, is it within range). Step 2 then does additional input validation (you shot at a place that already was shot, or in the placement phase, you placed something that doesn't qualify for the rules), and step 2 does more elaborate testing .

    --

    What you do is think about what your program needs to do, and then you break it down in functional blocks (as above, input, calculations, checking placements, output) Those functional blocks will tend to be programmed as functions. And you break each of those blocks into smaller and smaller chunks until what's left is the individual lines of code you'll need to program.

    --

    Plan ahead, and expect to be spending a few hours on this. Depending on your level of expertise at C++ (and from your OP it seems that isn't a lot), expect this to take 4-6 hours to get basic functionality, and then a few hours more for testing/debugging and overall cleanup.

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,761

    Re: C++ Battleship Game in PSPAD

    Basic interactive programs like this have a typical loop at the core.

    1 Get Input
    2 Process the input (calculation)
    3 Display output

    Since your program has 2 distinct phases (board setup and actual shooting/play phase), you probably want 2 of those loops one after the other.

    You probably need a way to stop the program, this is usually done either in step 1 or is is part of step 2. Either can work, which one if better depends on how you approach things.
    This is one of those "design desicions".

    The same is true for input validation, that can be either an inhherent part of step 1, or it can be a part of step 2. If you do it in 1, you need a loop in your step 1 that gives error messages on invalid input and doesn't proceed to step 2 until the input is valid. If you do it in 2, you may or may not want to display the current state via step 3.
    You can also do input validation in both 1 and 2. 1 then does basic input validation that does not need to know the current state of the game board (is the input numeric, is it within range). Step 2 then does additional input validation (you shot at a place that already was shot, or in the placement phase, you placed something that doesn't qualify for the rules), and step 2 does more elaborate testing .

    --

    What you do is think about what your program needs to do, and then you break it down in functional blocks (as above, input, calculations, checking placements, output) Those functional blocks will tend to be programmed as functions. And you break each of those blocks into smaller and smaller chunks until what's left is the individual lines of code you'll need to program.

    --

    Plan ahead, and expect to be spending a few hours on this. Depending on your level of expertise at C++ (and from your OP it seems that isn't a lot), expect this to take 4-6 hours to get basic functionality, and then a few hours more for testing/debugging and overall cleanup.

  9. #9
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Lightbulb Re: C++ Battleship Game in PSPAD

    Quote Originally Posted by 2kaud View Post
    c++ doesn't have 'procedures' - it just has functions!
    I am sure the term "procedure" is being used loosely. It just means a function that does not return a value, that is, the return type is void. Of course, a class method is also a function.

    Only once the program design is completed does coding start from the design.
    Although much of the design needs to be done up front, it does not all need to be done before coding begins. More often than not, part of the design changes as coding progresses.

    You don't say what is your current level of knowledge of c++
    Given that the assignment explicitly states that functions/procedures must be used, this is obviously an introductory course to programming.

    Quote Originally Posted by rycurt11 View Post
    we need to create a battleship program with the following parameters
    The game board, implemented as a 10 by 10 array, will be the heart of this game. Actually, you will need two game boards, one for the human player and one for the computer player. By creating a class for the game board, you can use it twice without having to duplicate the code. For each cell in the game board, you will need to keep track of whether or not the cell is occupied by a ship, and if so, which ship. You will also need to keep track of whether or not the cell has been shot. I suggest creating a structure to hold this information for each cell. Your game board would then be by a 10 by 10 array of this structure. You may want to create an enumerated type for the different ships. You may also want an array to store the length of each ship type.

    Functions are used to break a program up into smaller pieces which are then easier to manage and test. If you need to use the same block of code multiple times in your program, then putting that code in its own function would be an excellent way to use a function. You will probably have multiple functions by the time you are finished. Validating user input, firing a shot, checking whether a ship has been sunk, and checking whether all ships have been sunk could be possible functions/methods. Code that simply prints output would be a good candidate for a "procedure".

    Good Luck. This assignment looks fun.

    It's been either hit or miss for me in this class
    Kind of like Battleship, huh?
    Last edited by Coder Dave; March 19th, 2013 at 12:23 AM.

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    Re: C++ Battleship Game in PSPAD

    I am sure the term "procedure" is being used loosely.
    It probably is, but that's no excuse for students being taught the wrong terms in context of c++. Functions that return void in c++ are functions that return void, not procedures. Pascal et al have procedures, c++ does not.

    Although much of the design needs to be done up front, it does not all need to be done before coding begins. More often than not, part of the design changes as coding progresses.
    If the design changes as coding progresses then the design was wrong. The design is done first. Only once you are happy with the design does coding start. Think of it as a two person project. The first person produces the design and the second person then codes purely from the given design. When I was doing my Computer Science degree many years ago, we had to produce a program design first that was marked and only when the design was passed did we then code from the design. Also, we had assignments where we had to produce a program design and then we were given someone's else design from which to code. Too often today it seems that program design is being overlooked in courses that just teach a specific language.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center