Thread: Battleship

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    7

    Battleship

    can any one help me. i am suppose to write a battleship game as for my class and since i never played the game befor i have no clue on what i'm doing or what to do.
    here is what i have so far. i don't get the cding that i wrote so if any one has any suggestions please do so. thanks alot.

    Code:
    #include <iostream> 
    #include <iomanip> 
    #include <fstream> 
    #include <string> 
    #include <cstdlib> 
    #include <ctime> 
    
    using namespace std; 
    
    const int size_x = 10; 
    const int size_y = 10; 
    
    int field[size_x][size_y]; 
    
    void board(int location [size_x][size_y], char direction); 
    
    void main(string Carriers, string battleship, string cruiser,string submarine, string destroyer); 
    void check(string Carriers, string battleship, string cruiser,string submarine, string destroyer); 
    
    
    void location(int& size_x, int& size_y); 
    int main() 
    { 
    srand((unsigned) time(NULL)); 
    cout << fixed << showpoint << setprecision(5); 
    for ( int i = 0; i < size_x; ++i ) 
    { 
    cout << size_y <<" " << rand() % 10 << endl; 
    } 
    return 0; 
    } 
    //location function 
    void location(int& size_x, int& size_y) 
    { 
    
    } 
    
    void board(int location [size_x][size_y], char direction); 
    { 
    int Continue(10); 
    int place[x][y]; 
    
    while (Continue == 10) 
    { 
    cout << "where do you want to place your piece" ; 
    cin >> place; 
    cout << endl; 
    
    int print_x,print_y; 
    
    y1 = position.substr(0,1); 
    x1 = position.substr(1,1); 
    
    if (x1 == "1") 
    print_x = 1; 
    if (x1 == "2") 
    print_x = 2; 
    if (x1 == "3") 
    print_x = 3; 
    if (x1 == "4") 
    print_x = 4; 
    if (x1 == "5") 
    print_x = 5; 
    if (x1 == "6") 
    print_x = 6; 
    if (x1 == "7") 
    print_x = 7; 
    if (x1 == "8") 
    print_x = 8; 
    if (x1 == "9") 
    print_x = 9; 
    if (x1 == "10") 
    print_x = 10; 
    
    if (y1 == "A" || y1 == "a") 
    print_y = 10; 
    if (y1 == "B" || y1 == "b") 
    print_y = 9; 
    if (y1 == "C" || y1 == "c") 
    print_y = 8; 
    if (y1 == "D" || y1 == "d") 
    print_y = 7; 
    if (y1 == "E" || y1 == "e") 
    print_y = 6; 
    if (y1 == "F" || y1 == "f") 
    print_y = 5; 
    if (y1 == "G" || y1 == "g") 
    print_y = 4; 
    if (y1 == "H" || y1 == "h") 
    print_y = 3; 
    if (y1 == "I" || y1 == "i") 
    print_y = 2; 
    if (y1 == "J" || y1 == "j") 
    print_y = 1; 
    
    print_y++; 
    print_x++; 
    } 
    }

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    How can you write code for a game you've never played?

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    7
    its a project i have to do for class. if it was up to me i would of chose to do something else

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Full working demo right here: http://www.miniclip.com/battleships.htm
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Basically in Battleship, you have two players, each with a coordinate grid. They each have ships and submarines of varying length, and have to place those on their grid (so you'd have lines on your grid the length of each of those ships). Each player takes turns guessing coordinates. If they hit something, they have to be informed, and can keep guessing. If they miss, they need to be informed of that, and control turns to the other player. Once they have "launched a torpedo" (or whatever) at every block of a certain vessel, they need to be informed which vessel they have sunk. The winner is the first to sink everything of their opponent's.

    How I would set this up (not sure if this is how you've already set yours up). Set up two arrays of ints, of whatever dimensions you want the board to be (you'll need to learn how to deal with 2D arrays - search the boards - they're not that hard at all). Have the players select where to place their ships, and at the right locations in your matrix have numerical values representing the ship that is located there. Also decide on numerical values for each ship when it has been hit.

    Example:
    1 - Some ship
    2 - Some other ship
    3 - Submarine
    4 - Battle Ship
    5 - Some hit ship
    6 - Some other hit ship
    7 - Hit Submarine
    8 - Hit Battleship

    It's been a long time since I played so that's all I can remember, but that's just an example anyway. That's how I'd set up the data.

    For each turn, have a loop that makes it that persons turn as long as they keep guessing coordinates that are hits. Once they miss, it moves to the next player. Have them input coordinates, if at those coordinates in your array, there is something, consider it a hit.

    Most of the rest is very much up to how you've chosen to implement the game (i.e. whether it's the user against the computer). I'd be wary of doing a two player version, however, as players would be able to see eachother's boards.

    I hope this has helped you.

    Sean.

    * A little more detailed program logic: Once you have suggested coordinates from a player, use an if-statement to see if the data at those coordinates in your array are anything but zero (make sure you initialize your whole matrix to zero at the beginning). If it is, and it is less than 5, add 4 to make it "hit". If it is 5 or greater, they have already guessed that location.
    Last edited by sean; 12-11-2004 at 10:06 PM.

  6. #6
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    I see you posted this on cpp-home.com as well.

    Why would you split the comments to two places? If you make improvements from both forums, your code will be hard to manage. Just my opinion.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I see you posted this on cpp-home.com as well.

    Why would you split the comments to two places? If you make improvements from both forums, your code will be hard to manage. Just my opinion.
    I don't think it's such a bad idea. You're likely to get diverse comments from one forum alone. Cross-posting is against forum rules, but that refers to posting twice at Cprogramming.com. It will be a little more complicated to figure out what you need to do, yes, but I think the added benefits of having twice as many people give advice outweigh that.

  8. #8
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    That's true.

    BTW, I just played battleship. It's fun (sorry for the OT).

    Hope you get your program working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Battleship Game (Win32 EXP Needed)
    By ElWhapo in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 01-15-2005, 10:10 PM
  2. Battleship
    By luckygold6 in forum C++ Programming
    Replies: 25
    Last Post: 03-06-2003, 03:14 PM
  3. battleship
    By Leeman_s in forum Game Programming
    Replies: 10
    Last Post: 04-26-2002, 07:01 PM
  4. c++ battleship game
    By JTtheCPPgod in forum Game Programming
    Replies: 4
    Last Post: 01-05-2002, 11:12 AM
  5. Battleship
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 11-29-2001, 04:35 AM