Like Tree2Likes
  • 1 Post By oogabooga
  • 1 Post By laserlight

newbie battle ships

This is a discussion on newbie battle ships within the C++ Programming forums, part of the General Programming Boards category; Hello, im trying to make a command prompt based battle ships game, with a little AI in placing the ships. ...

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Australia NSW
    Posts
    6

    newbie battle ships

    Hello, im trying to make a command prompt based battle ships game, with a little AI in placing the ships. I am only up to drawing the game board and i am already stuck. here is my current code.
    Code:
     #include  <iostream>
    
    using namespace std;
    
    int main()
    {
        int d1;
        int d2;
        int gameboard[d1][d2];
    
        {
            cout<< "enter in columns:";
            cin>> d1;
            cin.ignore();
            cout<< "enter in rows:";
            cin>> d2;
            cin.ignore();
    
            {for (int x = 0; x < d1; x++)
            {for (int y = 0; y < d2; y++)
                {gameboard[x][y] = 0;}}}   // setting all elements in the array to 0
    
            gameboard[2][2] = 1; // changing 2 of the elements to 1 just too see what happens
            gameboard[3][3] = 1;
    
            {for (int x = 0; x < d1; x++)
            {for (int y = 0; y < d2; y++){
            cout<< gameboard[x][y] << " "; // shows values in all the arrays elements.
    
    }
                cout<< "\n";}
            }
    
        }
    cin.get();
    }
    when i compile and run this code it always seems to set my manually asigned array elements to the whole column. instead of the one element. is there any reason for this? or way around this to make it work properly? as i will be using this little bit later to make it randomize positions of the ships. thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    int d1;
    int d2;
    int gameboard[d1][d2];
    C++ doesn't look ahead to what future values you're going to assign to d1 and d2, then resize your array accordingly.

    Even then, standard C++ doesn't support variable sized arrays.

    Consider using std::vector instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Eielef's Avatar
    Join Date
    Apr 2012
    Posts
    9
    Yeah, maybe you can set a maxSize for your gameboard, for example, 25x25 or 50x50, and have them as constants at the beggining of your program.
    By the way, cool idea the battleship game. I'd like to see it when you finished it

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Australia NSW
    Posts
    6
    the custom sized array doesnt seem to have an issue. will look into std::vector though, thanks for that advice.
    if i comment lines 23, 24. and compile/run, the program runs properly, (as i test iv been putting rows as 5 and columns as 5, making a 5x5 gameboard)
    which prints out something like this:
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0

    which is what im after, but when i include lines 23, 24. instead of just changing elements [2][2] and [3][3] to 1, it changes everything in the 2nd and 3 column to 1.
    eg: instead of what im wanting it to do
    0 0 1 1 0 0 0 0 0 0
    0 0 1 1 0 0 0 1 0 0
    0 0 1 1 0 0 0 0 1 0
    0 0 1 1 0 0 0 0 0 0
    0 0 1 1 0 0 0 0 0 0


    p.s
    im only manually changing these 2 elements to test out what i can do, as im learning.

    but if anyone could explain why it is doing it, and how i could fix it, would be great.

    thanks guys.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Australia NSW
    Posts
    6
    sorry the 2nd example is suppose to 2 seperate print outs, seems to have put them together,

    iv re written them here.

    1st (what it is printing out)
    0 0 1 1 0
    0 0 1 1 0
    0 0 1 1 0
    0 0 1 1 0
    0 0 1 1 0

    2nd (what i want it to print out.)

    0 0 0 0 0
    0 0 1 0 0
    0 0 0 1 0
    0 0 0 0 0
    0 0 0 0 0

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,312
    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Australia NSW
    Posts
    6
    Code:
    #include  <iostream>
     
    using namespace std;
     
    int main()
    {
        int d1;
        int d2;
        int gameboard[d1][d2];
     
        {
            cout<< "enter in columns:";
            cin>> d1;
            cin.ignore();
            cout<< "enter in rows:";
            cin>> d2;
            cin.ignore();
     
            {for (int x = 0; x < d1; x++)
            {for (int y = 0; y < d2; y++)
                {gameboard[x][y] = 0;}}}   // setting all elements in the array to 0
     
            gameboard[2][2] = 1; // changing 2 of the elements to 1 just too see what happens
            gameboard[3][3] = 1;
     
            {for (int x = 0; x < d1; x++)
            {for (int y = 0; y < d2; y++){
            cout<< gameboard[x][y] << " "; // shows values in all the arrays elements.
     
    }
                cout<< "\n";}
            }
     
        }
    cin.get();
    }

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Interesting - it looks like your original post.
    Thanks for playing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to space your code and position your braces like a normal human. It will help people who are trying to read your code and it will help you understand your own code too. You should do it something like this. I've also moved the definition of gameboard to a more sane location (after d1 and d2 are known).
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
        int d1, d2;
    
        cout << "enter in columns:";
        cin >> d1;
        cin.ignore();
    
        cout << "enter in rows:";
        cin >> d2;
        cin.ignore();
    
        int gameboard[d1][d2];
    
        for (int x = 0; x < d1; x++) {
            for (int y = 0; y < d2; y++) {
                gameboard[x][y] = 0;   // setting all elements in the array to 0
            }
        }
    
        gameboard[2][2] = 1; // changing 2 of the elements to 1 just to see what happens
        gameboard[3][3] = 1;
    
        for (int x = 0; x < d1; x++) {
            for (int y = 0; y < d2; y++) {
                cout<< gameboard[x][y] << " "; // shows values in all the arrays elements.
            }
            cout<< "\n";
        }
    
        cin.get();
    }
    BTW, this code seems to work perfectly fine.
    titanicmango likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Australia NSW
    Posts
    6
    hmm, now that i am using a different computer, it is working fine, thanks anyway. sorry for wasting time.

  11. #11
    Registered User Eielef's Avatar
    Join Date
    Apr 2012
    Posts
    9
    First time I try to use a variable to initialize an array, but it works! I feel so newbie sometimes... XD
    BTW, @titanicmango, listen to @oogabooga, it was actually hard to read that. I started learning programming recently, so I thought that was just a different way to write code, but it's definitely more difficult to understand.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,312
    Quote Originally Posted by Eielef
    First time I try to use a variable to initialize an array, but it works!
    As Salem noted in post #2, this is the variable length array feature, which is non-standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User Eielef's Avatar
    Join Date
    Apr 2012
    Posts
    9
    I didn't know that, and I didn't realize when Salem pointed it, so thank you for saying it.

  14. #14
    Registered User
    Join Date
    May 2012
    Location
    Australia NSW
    Posts
    6
    Quote Originally Posted by laserlight View Post
    As Salem noted in post #2, this is the variable length array feature, which is non-standard.
    Should I have initialized it differently? as in Post #3 when Eielef advised possibly using some constant sizes? eg 25x25 50x50

    With the array, once it initializes with 'd1' 'd2' the array doesnt resize.
    Last edited by titanicmango; 05-02-2012 at 04:21 PM.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,312
    Quote Originally Posted by titanicmango
    Should I have initialized it differently? as in Post #3 when Eielef advised possibly using some constant sizes? eg 25x25 50x50
    If you use constant sizes, then that means that you have decided on some maximum size of the array, and you work within those constraints. This is fine, if your maximum is not too large. If you use std::vector, then you would size dynamically according to your exact needs.

    Quote Originally Posted by titanicmango
    With the array, once it initializes with 'd1' 'd2' the array doesnt resize.
    Yes, the variable length array feature as borrowed from C99 gives arrays that are not resizable once created.
    titanicmango likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Battle ships help
    By eisiminger in forum C Programming
    Replies: 2
    Last Post: 01-25-2010, 01:44 PM
  2. ai for enemy ships in asteroids
    By ichijoji in forum Game Programming
    Replies: 7
    Last Post: 04-09-2003, 04:09 PM
  3. RPG battle system
    By napkin111 in forum Game Programming
    Replies: 14
    Last Post: 03-18-2003, 11:28 AM
  4. Problem with std lists and my ships
    By Vorok in forum Game Programming
    Replies: 1
    Last Post: 01-15-2003, 06:44 PM
  5. The battle.
    By Sentaku senshi in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 03-25-2002, 04:28 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21